Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/utilities/test_linkedlist.cpp
41144 views
1
/*
2
* Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
23
*/
24
25
#include "precompiled.hpp"
26
#include "unittest.hpp"
27
#include "utilities/linkedlist.hpp"
28
29
class Integer : public StackObj {
30
private:
31
int _value;
32
public:
33
34
Integer(int i) : _value(i) {
35
}
36
37
int value() const {
38
return _value;
39
}
40
41
bool equals(const Integer& i) const {
42
return _value == i.value();
43
}
44
45
static int compare(const Integer& i1, const Integer& i2) {
46
return i1.value() - i2.value();
47
}
48
};
49
50
static void check_list_values(const int* expected, const LinkedList<Integer>* list) {
51
LinkedListNode<Integer>* head = list->head();
52
int index = 0;
53
while (head != NULL) {
54
ASSERT_EQ(expected[index], head->peek()->value())
55
<< "Unexpected value at index " << index;
56
head = head->next();
57
index++;
58
}
59
}
60
61
const Integer one(1), two(2), three(3), four(4), five(5), six(6), notfound(404);
62
63
// Test regular linked list
64
TEST(LinkedList, simple) {
65
LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> ll;
66
67
ASSERT_TRUE(ll.is_empty()) << "Start with empty list";
68
69
ll.add(six);
70
ASSERT_TRUE(!ll.is_empty()) << "Should not be empty";
71
72
Integer* i = ll.find(six);
73
ASSERT_TRUE(i != NULL) << "Should find it";
74
ASSERT_EQ(six.value(), i->value()) << "Should be 6";
75
76
i = ll.find(three);
77
ASSERT_TRUE(i == NULL) << "Not in the list";
78
79
LinkedListNode<Integer>* node = ll.find_node(six);
80
ASSERT_TRUE(node != NULL) << "6 is in the list";
81
82
ll.insert_after(three, node);
83
ll.insert_before(one, node);
84
int expected[3] = {1, 6, 3};
85
check_list_values(expected, &ll);
86
}
87
88
TEST(LinkedList, generic) {
89
LinkedListImpl<int> il;
90
const int N = 100;
91
for (int i=0; i<N; ++i) {
92
il.add(i);
93
}
94
EXPECT_EQ(il.size(), (size_t)N);
95
96
const LinkedListIterator<int> cit(il.head());
97
for (int i=N-1; i>=0; --i) {
98
const int* e = cit.next();
99
EXPECT_EQ(*e, i);
100
}
101
EXPECT_TRUE(cit.is_empty());
102
EXPECT_EQ(il.size(), (size_t)N);
103
EXPECT_EQ(*(il.head()->peek()), N-1);
104
105
typedef LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> list_t;
106
LinkedList<Integer>* list = new(ResourceObj::C_HEAP, mtTest) list_t();
107
list->add(Integer(1));
108
list->add(Integer(2));
109
EXPECT_EQ(list->size(), (size_t)2);
110
list->~LinkedList<Integer>();
111
EXPECT_EQ(list->size(), (size_t)0);
112
113
// copyable
114
//list_t a;
115
//a.add(Integer(1));
116
//list_t b(a);
117
//EXPECT_EQ(b.size(), (size_t)1);
118
//EXPECT_TRUE(b.head()->peek()->equals(Integer(1)));
119
120
list_t lifo, dummy;
121
const Integer* e;
122
lifo.add(one);
123
lifo.add(two);
124
LinkedListIterator<Integer> it(lifo.head());
125
126
EXPECT_FALSE(it.is_empty());
127
// pop 2
128
e = it.next();
129
EXPECT_TRUE(e->equals(two));
130
EXPECT_FALSE(it.is_empty());
131
// pop 1
132
e = it.next();
133
EXPECT_TRUE(e->equals(one));
134
//empty
135
EXPECT_TRUE(it.is_empty());
136
137
LinkedListIterator<Integer> it2(dummy.head());
138
EXPECT_TRUE(it2.is_empty());
139
EXPECT_EQ(it2.next(), (Integer* )NULL);
140
}
141
142
TEST(LinkedList, algorithm) {
143
LinkedListImpl<int> il;
144
il.add(1);
145
il.add(2);
146
il.add(3);
147
EXPECT_EQ(*il.find(1), 1);
148
EXPECT_EQ(il.find(404), (int* )NULL);
149
EXPECT_TRUE(il.remove(1));
150
EXPECT_FALSE(il.remove(404));
151
152
LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> ll;
153
ll.add(one);
154
155
EXPECT_TRUE(ll.find(one));
156
EXPECT_FALSE(ll.find(notfound));
157
158
EXPECT_TRUE(ll.remove(one));
159
EXPECT_FALSE(ll.find(one));
160
EXPECT_FALSE(ll.remove(notfound));
161
EXPECT_FALSE(ll.find(notfound));
162
}
163
164
// Test sorted linked list
165
TEST(SortedLinkedList, simple) {
166
LinkedListImpl<Integer, ResourceObj::C_HEAP, mtTest> ll;
167
ll.add(one);
168
ll.add(six);
169
ll.add(three);
170
ll.add(two);
171
ll.add(four);
172
ll.add(five);
173
174
SortedLinkedList<Integer, Integer::compare, ResourceObj::C_HEAP, mtTest> sl;
175
ASSERT_TRUE(sl.is_empty()) << "Start with empty list";
176
177
size_t ll_size = ll.size();
178
sl.move(&ll);
179
size_t sl_size = sl.size();
180
181
ASSERT_EQ(ll_size, sl_size) << "Should be the same size";
182
ASSERT_TRUE(ll.is_empty()) << "No more entries";
183
184
// sorted result
185
int sorted_result[] = {1, 2, 3, 4, 5, 6};
186
check_list_values(sorted_result, &sl);
187
if (HasFatalFailure()) {
188
return;
189
}
190
191
LinkedListNode<Integer>* node = sl.find_node(four);
192
ASSERT_TRUE(node != NULL) << "4 is in the list";
193
sl.remove_before(node);
194
sl.remove_after(node);
195
int remains[] = {1, 2, 4, 6};
196
check_list_values(remains, &sl);
197
}
198
199