1/*
2 * Copyright (c) 2015, 2016, 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#include "precompiled.hpp"
25#include "gc/z/zList.inline.hpp"
26#include "unittest.hpp"
27
28#ifndef PRODUCT
29
30class ZTestEntry {
31 friend class ZList<ZTestEntry>;
32
33private:
34 const int _id;
35 ZListNode<ZTestEntry> _node;
36
37public:
38 ZTestEntry(int id) :
39 _id(id),
40 _node() {}
41
42 int id() const {
43 return _id;
44 }
45};
46
47class ZListTest : public ::testing::Test {
48protected:
49 static void assert_sorted(ZList<ZTestEntry>* list) {
50 // Iterate forward
51 {
52 int count = list->first()->id();
53 ZListIterator<ZTestEntry> iter(list);
54 for (ZTestEntry* entry; iter.next(&entry);) {
55 ASSERT_EQ(entry->id(), count);
56 count++;
57 }
58 }
59
60 // Iterate backward
61 {
62 int count = list->last()->id();
63 ZListReverseIterator<ZTestEntry> iter(list);
64 for (ZTestEntry* entry; iter.next(&entry);) {
65 EXPECT_EQ(entry->id(), count);
66 count--;
67 }
68 }
69 }
70};
71
72TEST_F(ZListTest, test_insert) {
73 ZList<ZTestEntry> list;
74 ZTestEntry e0(0);
75 ZTestEntry e1(1);
76 ZTestEntry e2(2);
77 ZTestEntry e3(3);
78 ZTestEntry e4(4);
79 ZTestEntry e5(5);
80
81 list.insert_first(&e2);
82 list.insert_before(&e2, &e1);
83 list.insert_after(&e2, &e3);
84 list.insert_last(&e4);
85 list.insert_first(&e0);
86 list.insert_last(&e5);
87
88 EXPECT_EQ(list.size(), 6u);
89 assert_sorted(&list);
90}
91
92TEST_F(ZListTest, test_remove) {
93 // Remove first
94 {
95 ZList<ZTestEntry> list;
96 ZTestEntry e0(0);
97 ZTestEntry e1(1);
98 ZTestEntry e2(2);
99 ZTestEntry e3(3);
100 ZTestEntry e4(4);
101 ZTestEntry e5(5);
102
103 list.insert_last(&e0);
104 list.insert_last(&e1);
105 list.insert_last(&e2);
106 list.insert_last(&e3);
107 list.insert_last(&e4);
108 list.insert_last(&e5);
109
110 EXPECT_EQ(list.size(), 6u);
111
112 for (int i = 0; i < 6; i++) {
113 ZTestEntry* e = list.remove_first();
114 EXPECT_EQ(e->id(), i);
115 }
116
117 EXPECT_EQ(list.size(), 0u);
118 }
119
120 // Remove last
121 {
122 ZList<ZTestEntry> list;
123 ZTestEntry e0(0);
124 ZTestEntry e1(1);
125 ZTestEntry e2(2);
126 ZTestEntry e3(3);
127 ZTestEntry e4(4);
128 ZTestEntry e5(5);
129
130 list.insert_last(&e0);
131 list.insert_last(&e1);
132 list.insert_last(&e2);
133 list.insert_last(&e3);
134 list.insert_last(&e4);
135 list.insert_last(&e5);
136
137 EXPECT_EQ(list.size(), 6u);
138
139 for (int i = 5; i >= 0; i--) {
140 ZTestEntry* e = list.remove_last();
141 EXPECT_EQ(e->id(), i);
142 }
143
144 EXPECT_EQ(list.size(), 0u);
145 }
146}
147
148TEST_F(ZListTest, test_transfer) {
149 // Transfer empty to empty
150 {
151 ZList<ZTestEntry> list0;
152 ZList<ZTestEntry> list1;
153
154 EXPECT_TRUE(list0.is_empty());
155 EXPECT_TRUE(list1.is_empty());
156
157 list0.transfer(&list1);
158
159 EXPECT_TRUE(list0.is_empty());
160 EXPECT_TRUE(list1.is_empty());
161 }
162
163 // Transfer non-empty to empty
164 {
165 ZList<ZTestEntry> list0;
166 ZList<ZTestEntry> list1;
167 ZTestEntry e0(0);
168 ZTestEntry e1(1);
169 ZTestEntry e2(2);
170 ZTestEntry e3(3);
171 ZTestEntry e4(4);
172 ZTestEntry e5(5);
173
174 list1.insert_last(&e0);
175 list1.insert_last(&e1);
176 list1.insert_last(&e2);
177 list1.insert_last(&e3);
178 list1.insert_last(&e4);
179 list1.insert_last(&e5);
180
181 EXPECT_EQ(list0.size(), 0u);
182 EXPECT_EQ(list1.size(), 6u);
183
184 list0.transfer(&list1);
185
186 EXPECT_EQ(list0.size(), 6u);
187 EXPECT_EQ(list1.size(), 0u);
188
189 assert_sorted(&list0);
190 }
191
192 // Transfer non-empty to non-empty
193 {
194 ZList<ZTestEntry> list0;
195 ZList<ZTestEntry> list1;
196 ZTestEntry e0(0);
197 ZTestEntry e1(1);
198 ZTestEntry e2(2);
199 ZTestEntry e3(3);
200 ZTestEntry e4(4);
201 ZTestEntry e5(5);
202
203 list0.insert_last(&e0);
204 list0.insert_last(&e1);
205 list0.insert_last(&e2);
206
207 list1.insert_last(&e3);
208 list1.insert_last(&e4);
209 list1.insert_last(&e5);
210
211 EXPECT_EQ(list0.size(), 3u);
212 EXPECT_EQ(list1.size(), 3u);
213
214 list0.transfer(&list1);
215
216 EXPECT_EQ(list0.size(), 6u);
217 EXPECT_EQ(list1.size(), 0u);
218
219 assert_sorted(&list0);
220 }
221}
222
223#endif // PRODUCT
224