Path: blob/master/test/hotspot/gtest/gc/z/test_zList.cpp
41152 views
/*1* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"24#include "gc/z/zList.inline.hpp"25#include "unittest.hpp"2627#ifndef PRODUCT2829class ZTestEntry {30friend class ZList<ZTestEntry>;3132private:33const int _id;34ZListNode<ZTestEntry> _node;3536public:37ZTestEntry(int id) :38_id(id),39_node() {}4041int id() const {42return _id;43}44};4546class ZListTest : public ::testing::Test {47protected:48static void assert_sorted(ZList<ZTestEntry>* list) {49// Iterate forward50{51int count = list->first()->id();52ZListIterator<ZTestEntry> iter(list);53for (ZTestEntry* entry; iter.next(&entry);) {54ASSERT_EQ(entry->id(), count);55count++;56}57}5859// Iterate backward60{61int count = list->last()->id();62ZListReverseIterator<ZTestEntry> iter(list);63for (ZTestEntry* entry; iter.next(&entry);) {64EXPECT_EQ(entry->id(), count);65count--;66}67}68}69};7071TEST_F(ZListTest, test_insert) {72ZList<ZTestEntry> list;73ZTestEntry e0(0);74ZTestEntry e1(1);75ZTestEntry e2(2);76ZTestEntry e3(3);77ZTestEntry e4(4);78ZTestEntry e5(5);7980list.insert_first(&e2);81list.insert_before(&e2, &e1);82list.insert_after(&e2, &e3);83list.insert_last(&e4);84list.insert_first(&e0);85list.insert_last(&e5);8687EXPECT_EQ(list.size(), 6u);88assert_sorted(&list);8990for (int i = 0; i < 6; i++) {91ZTestEntry* e = list.remove_first();92EXPECT_EQ(e->id(), i);93}9495EXPECT_EQ(list.size(), 0u);96}9798TEST_F(ZListTest, test_remove) {99// Remove first100{101ZList<ZTestEntry> list;102ZTestEntry e0(0);103ZTestEntry e1(1);104ZTestEntry e2(2);105ZTestEntry e3(3);106ZTestEntry e4(4);107ZTestEntry e5(5);108109list.insert_last(&e0);110list.insert_last(&e1);111list.insert_last(&e2);112list.insert_last(&e3);113list.insert_last(&e4);114list.insert_last(&e5);115116EXPECT_EQ(list.size(), 6u);117118for (int i = 0; i < 6; i++) {119ZTestEntry* e = list.remove_first();120EXPECT_EQ(e->id(), i);121}122123EXPECT_EQ(list.size(), 0u);124}125126// Remove last127{128ZList<ZTestEntry> list;129ZTestEntry e0(0);130ZTestEntry e1(1);131ZTestEntry e2(2);132ZTestEntry e3(3);133ZTestEntry e4(4);134ZTestEntry e5(5);135136list.insert_last(&e0);137list.insert_last(&e1);138list.insert_last(&e2);139list.insert_last(&e3);140list.insert_last(&e4);141list.insert_last(&e5);142143EXPECT_EQ(list.size(), 6u);144145for (int i = 5; i >= 0; i--) {146ZTestEntry* e = list.remove_last();147EXPECT_EQ(e->id(), i);148}149150EXPECT_EQ(list.size(), 0u);151}152}153154#endif // PRODUCT155156157