Path: blob/master/test/hotspot/gtest/utilities/test_linkedlist.cpp
41144 views
/*1* Copyright (c) 2011, 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.2122*/2324#include "precompiled.hpp"25#include "unittest.hpp"26#include "utilities/linkedlist.hpp"2728class Integer : public StackObj {29private:30int _value;31public:3233Integer(int i) : _value(i) {34}3536int value() const {37return _value;38}3940bool equals(const Integer& i) const {41return _value == i.value();42}4344static int compare(const Integer& i1, const Integer& i2) {45return i1.value() - i2.value();46}47};4849static void check_list_values(const int* expected, const LinkedList<Integer>* list) {50LinkedListNode<Integer>* head = list->head();51int index = 0;52while (head != NULL) {53ASSERT_EQ(expected[index], head->peek()->value())54<< "Unexpected value at index " << index;55head = head->next();56index++;57}58}5960const Integer one(1), two(2), three(3), four(4), five(5), six(6), notfound(404);6162// Test regular linked list63TEST(LinkedList, simple) {64LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> ll;6566ASSERT_TRUE(ll.is_empty()) << "Start with empty list";6768ll.add(six);69ASSERT_TRUE(!ll.is_empty()) << "Should not be empty";7071Integer* i = ll.find(six);72ASSERT_TRUE(i != NULL) << "Should find it";73ASSERT_EQ(six.value(), i->value()) << "Should be 6";7475i = ll.find(three);76ASSERT_TRUE(i == NULL) << "Not in the list";7778LinkedListNode<Integer>* node = ll.find_node(six);79ASSERT_TRUE(node != NULL) << "6 is in the list";8081ll.insert_after(three, node);82ll.insert_before(one, node);83int expected[3] = {1, 6, 3};84check_list_values(expected, &ll);85}8687TEST(LinkedList, generic) {88LinkedListImpl<int> il;89const int N = 100;90for (int i=0; i<N; ++i) {91il.add(i);92}93EXPECT_EQ(il.size(), (size_t)N);9495const LinkedListIterator<int> cit(il.head());96for (int i=N-1; i>=0; --i) {97const int* e = cit.next();98EXPECT_EQ(*e, i);99}100EXPECT_TRUE(cit.is_empty());101EXPECT_EQ(il.size(), (size_t)N);102EXPECT_EQ(*(il.head()->peek()), N-1);103104typedef LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> list_t;105LinkedList<Integer>* list = new(ResourceObj::C_HEAP, mtTest) list_t();106list->add(Integer(1));107list->add(Integer(2));108EXPECT_EQ(list->size(), (size_t)2);109list->~LinkedList<Integer>();110EXPECT_EQ(list->size(), (size_t)0);111112// copyable113//list_t a;114//a.add(Integer(1));115//list_t b(a);116//EXPECT_EQ(b.size(), (size_t)1);117//EXPECT_TRUE(b.head()->peek()->equals(Integer(1)));118119list_t lifo, dummy;120const Integer* e;121lifo.add(one);122lifo.add(two);123LinkedListIterator<Integer> it(lifo.head());124125EXPECT_FALSE(it.is_empty());126// pop 2127e = it.next();128EXPECT_TRUE(e->equals(two));129EXPECT_FALSE(it.is_empty());130// pop 1131e = it.next();132EXPECT_TRUE(e->equals(one));133//empty134EXPECT_TRUE(it.is_empty());135136LinkedListIterator<Integer> it2(dummy.head());137EXPECT_TRUE(it2.is_empty());138EXPECT_EQ(it2.next(), (Integer* )NULL);139}140141TEST(LinkedList, algorithm) {142LinkedListImpl<int> il;143il.add(1);144il.add(2);145il.add(3);146EXPECT_EQ(*il.find(1), 1);147EXPECT_EQ(il.find(404), (int* )NULL);148EXPECT_TRUE(il.remove(1));149EXPECT_FALSE(il.remove(404));150151LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> ll;152ll.add(one);153154EXPECT_TRUE(ll.find(one));155EXPECT_FALSE(ll.find(notfound));156157EXPECT_TRUE(ll.remove(one));158EXPECT_FALSE(ll.find(one));159EXPECT_FALSE(ll.remove(notfound));160EXPECT_FALSE(ll.find(notfound));161}162163// Test sorted linked list164TEST(SortedLinkedList, simple) {165LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> ll;166ll.add(one);167ll.add(six);168ll.add(three);169ll.add(two);170ll.add(four);171ll.add(five);172173SortedLinkedList<Integer, Integer::compare, ResourceObj::C_HEAP, mtTest> sl;174ASSERT_TRUE(sl.is_empty()) << "Start with empty list";175176size_t ll_size = ll.size();177sl.move(&ll);178size_t sl_size = sl.size();179180ASSERT_EQ(ll_size, sl_size) << "Should be the same size";181ASSERT_TRUE(ll.is_empty()) << "No more entries";182183// sorted result184int sorted_result[] = {1, 2, 3, 4, 5, 6};185check_list_values(sorted_result, &sl);186if (HasFatalFailure()) {187return;188}189190LinkedListNode<Integer>* node = sl.find_node(four);191ASSERT_TRUE(node != NULL) << "4 is in the list";192sl.remove_before(node);193sl.remove_after(node);194int remains[] = {1, 2, 4, 6};195check_list_values(remains, &sl);196}197198199