1 | /* $Id: tstIprtList.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - RTCList/RTCMTList.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/cpp/mtlist.h>
|
---|
32 |
|
---|
33 | #include <iprt/cpp/ministring.h>
|
---|
34 | #include <iprt/test.h>
|
---|
35 | #include <iprt/rand.h>
|
---|
36 | #include <iprt/thread.h>
|
---|
37 | #include <iprt/time.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************************************************************************
|
---|
41 | * Global Variables *
|
---|
42 | *********************************************************************************************************************************/
|
---|
43 | /** Used for the string test. */
|
---|
44 | static const char *g_apszTestStrings[] =
|
---|
45 | {
|
---|
46 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
|
---|
47 | "Vestibulum non turpis vel metus pellentesque tincidunt at id massa.",
|
---|
48 | "Cras quis erat sed nulla ullamcorper molestie.",
|
---|
49 | "Mauris ac elit turpis, id pulvinar diam.",
|
---|
50 | "Nulla quis dolor dolor, in ultrices diam.",
|
---|
51 | "Vivamus ac quam non ipsum vehicula tempor ac ac arcu.",
|
---|
52 | "Aenean posuere lacus blandit erat semper eu iaculis ante eleifend.",
|
---|
53 | "Donec quis quam a lacus interdum sollicitudin quis eu est.",
|
---|
54 | "Morbi sed nisi a arcu commodo convallis.",
|
---|
55 | "Aenean molestie condimentum velit, non mattis magna ultricies quis.",
|
---|
56 | "Nulla id velit at mauris gravida mattis.",
|
---|
57 | "Phasellus viverra velit eu urna semper in porta arcu sollicitudin.",
|
---|
58 | "Pellentesque consequat turpis et tortor hendrerit id tempor ipsum lacinia.",
|
---|
59 | "Cras iaculis nulla quis risus pulvinar eget tempor lectus placerat.",
|
---|
60 | "Nullam in nulla sed sapien euismod euismod.",
|
---|
61 | "Morbi in tortor at magna sagittis fermentum ut eu nunc.",
|
---|
62 | "Nulla vitae ante sit amet dui molestie sagittis lacinia quis tellus.",
|
---|
63 | "Proin iaculis lorem ultricies metus bibendum tincidunt.",
|
---|
64 | "Sed gravida purus id risus sollicitudin ac porta orci vestibulum.",
|
---|
65 | "Duis quis purus non ligula consectetur cursus eu interdum erat.",
|
---|
66 | "Nullam non nunc in elit volutpat tempor in nec metus.",
|
---|
67 | "Aliquam id purus eget enim luctus molestie.",
|
---|
68 | "Sed id elit nec elit luctus scelerisque.",
|
---|
69 | "Suspendisse viverra leo non ligula congue ac luctus nisl vulputate.",
|
---|
70 | "Nulla dignissim lobortis nunc, eu tempus ipsum luctus sed.",
|
---|
71 | "Integer vel lacus lacus, quis condimentum felis.",
|
---|
72 | "Nulla ut lacus ac lacus gravida ultrices id sed ipsum.",
|
---|
73 | "Etiam non purus ut augue fermentum consequat.",
|
---|
74 | "Nam sit amet eros quis nibh blandit lacinia non posuere lectus.",
|
---|
75 | "Sed sit amet ipsum et dolor sagittis facilisis.",
|
---|
76 | "Ut congue nisi lacus, vel ultrices est.",
|
---|
77 | "Donec vel erat ut justo hendrerit sodales eu eget libero.",
|
---|
78 | "Integer a ipsum ac nunc eleifend congue convallis a urna.",
|
---|
79 | "Sed vel eros eu lectus imperdiet vehicula.",
|
---|
80 | "Vivamus eget turpis sed erat dapibus varius eget eu nulla.",
|
---|
81 | "Nam id nulla non elit eleifend commodo sed ac est.",
|
---|
82 | "Integer pulvinar dolor sodales velit pulvinar et facilisis eros scelerisque.",
|
---|
83 | "Ut mattis arcu ut libero imperdiet in rhoncus augue sodales.",
|
---|
84 | "Ut luctus turpis ligula, id dapibus felis.",
|
---|
85 | "Nullam sit amet sapien eget tellus hendrerit vestibulum eget in odio.",
|
---|
86 | "Phasellus non orci vitae mi placerat semper.",
|
---|
87 | "Quisque pharetra aliquet velit, quis tempor magna porttitor nec.",
|
---|
88 | "Praesent porta neque felis, vehicula facilisis odio.",
|
---|
89 | "Maecenas ultricies ipsum eu velit laoreet faucibus.",
|
---|
90 | "Mauris et nunc leo, et euismod quam.",
|
---|
91 | "Phasellus a felis et justo fringilla lacinia.",
|
---|
92 | "Vestibulum eget augue ante, ac viverra neque.",
|
---|
93 | "Mauris pellentesque ligula quis metus elementum venenatis.",
|
---|
94 | "Curabitur eu neque tellus, non porta sapien.",
|
---|
95 | "Ut mattis metus id enim aliquam laoreet et sed tortor.",
|
---|
96 | "Aenean quis nulla vitae nulla auctor lobortis a egestas turpis.",
|
---|
97 | "Praesent vitae ante a urna porta placerat non nec eros.",
|
---|
98 | "Donec quis neque eros, placerat adipiscing turpis.",
|
---|
99 | "Cras sit amet sapien risus, quis euismod arcu.",
|
---|
100 | "Integer volutpat massa eros, ac gravida mi.",
|
---|
101 | "Nunc vitae nunc sagittis diam vulputate suscipit.",
|
---|
102 | "Suspendisse quis mauris bibendum mauris aliquet pulvinar.",
|
---|
103 | "Donec volutpat vestibulum ligula, eget interdum tortor malesuada sit amet.",
|
---|
104 | "Mauris hendrerit dui non nibh varius sit amet fringilla orci pretium.",
|
---|
105 | "Phasellus a quam tellus, auctor lacinia sapien.",
|
---|
106 | "Sed dapibus leo vitae neque faucibus id porttitor sapien ultricies.",
|
---|
107 | "Maecenas euismod elit nec tortor sagittis pretium.",
|
---|
108 | "Ut tincidunt risus at erat fermentum sit amet molestie ante lacinia.",
|
---|
109 | "Nulla non leo nec lacus sollicitudin lobortis a a nisl.",
|
---|
110 | "Nunc vulputate erat vel libero elementum a interdum turpis malesuada.",
|
---|
111 | "Morbi id libero turpis, a lobortis dolor.",
|
---|
112 | "Donec vehicula imperdiet lorem, non pretium nulla tempus ut.",
|
---|
113 | "Morbi lacinia massa id nunc tempus in blandit risus blandit.",
|
---|
114 | "Sed feugiat orci id ipsum suscipit quis fringilla enim rutrum.",
|
---|
115 | "Mauris suscipit lobortis urna, vel dictum justo iaculis ac.",
|
---|
116 | "In rhoncus lectus tristique nunc blandit gravida placerat turpis rutrum.",
|
---|
117 | "Aliquam pellentesque ornare justo, sed hendrerit metus mattis a.",
|
---|
118 | "Nam aliquet lorem congue nisl blandit posuere.",
|
---|
119 | "Sed lobortis interdum ipsum, ac cursus erat lacinia in.",
|
---|
120 | "Maecenas vel tortor vel lorem facilisis interdum.",
|
---|
121 | "Aenean porttitor massa enim, eget dignissim est.",
|
---|
122 | "Nullam id libero lacus, mattis feugiat risus.",
|
---|
123 | "Fusce et dolor at eros ornare auctor malesuada vel ipsum.",
|
---|
124 | "Donec at massa sit amet lorem pellentesque interdum at ac lacus.",
|
---|
125 | "Praesent suscipit velit at justo suscipit eu vestibulum ligula interdum.",
|
---|
126 | "Aenean id justo nulla, vitae vulputate diam.",
|
---|
127 | "Fusce pellentesque leo quis orci pulvinar at pellentesque tellus dictum.",
|
---|
128 | "Ut facilisis purus at enim varius vulputate.",
|
---|
129 | "Donec malesuada bibendum sapien, sed pretium nisi cursus quis.",
|
---|
130 | "Mauris porttitor diam ut sapien pretium egestas.",
|
---|
131 | "Vestibulum ut justo eu libero semper convallis vitae et velit.",
|
---|
132 | "Quisque eleifend dapibus ligula, eu tincidunt massa rutrum at.",
|
---|
133 | "Sed euismod diam eget enim suscipit dictum.",
|
---|
134 | "Mauris fermentum orci eu nunc venenatis in sollicitudin tellus vestibulum.",
|
---|
135 | "Vivamus faucibus consequat turpis, lobortis vehicula lectus gravida eget.",
|
---|
136 | "Curabitur eu erat eu mi interdum scelerisque.",
|
---|
137 | "Morbi consequat molestie nulla, imperdiet elementum augue sagittis vel.",
|
---|
138 | "Sed ullamcorper velit suscipit arcu egestas quis commodo est hendrerit.",
|
---|
139 | "Proin vitae velit ut enim sollicitudin ultrices.",
|
---|
140 | "Curabitur posuere euismod lacus, sed volutpat erat adipiscing sit amet.",
|
---|
141 | "Cras sit amet sem lorem, in cursus augue.",
|
---|
142 | "Sed fermentum ultricies orci, quis hendrerit risus imperdiet et.",
|
---|
143 | "Proin nec arcu interdum ipsum molestie vestibulum.",
|
---|
144 | "Nulla quis quam non sem pretium scelerisque et eu velit.",
|
---|
145 | "Donec eu tellus nisl, ac vehicula tortor."
|
---|
146 | };
|
---|
147 |
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Does a list test.
|
---|
151 | *
|
---|
152 | * @param T1 The list type.
|
---|
153 | * @param T2 The input type
|
---|
154 | * @param pcszDesc The test description.
|
---|
155 | * @param paTestData Pointer to the array with the test input data.
|
---|
156 | * @param cTestItems The size of the input data.
|
---|
157 | */
|
---|
158 | template<template <class, typename> class L, typename T1, typename T2, typename T3>
|
---|
159 | static void test1(const char *pcszDesc, T3 paTestData[], size_t cTestItems)
|
---|
160 | {
|
---|
161 | RTTestISubF("%s with size of %u (items=%u)", pcszDesc, sizeof(T1), cTestItems);
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * Construction
|
---|
165 | */
|
---|
166 |
|
---|
167 | /* Create a test list */
|
---|
168 | L<T1, T2> testList;
|
---|
169 |
|
---|
170 | const size_t defCap = L<T1, T2>::kDefaultCapacity;
|
---|
171 | RTTESTI_CHECK(testList.isEmpty());
|
---|
172 | RTTESTI_CHECK(testList.size() == 0);
|
---|
173 | RTTESTI_CHECK(testList.capacity() == defCap);
|
---|
174 |
|
---|
175 | /*
|
---|
176 | * Adding
|
---|
177 | */
|
---|
178 |
|
---|
179 | /* Add the second half of the test data */
|
---|
180 | size_t cAdded = 1;
|
---|
181 |
|
---|
182 | /* Start adding the second half of our test list */
|
---|
183 | for (size_t i = cTestItems / 2; i < cTestItems; ++i, ++cAdded)
|
---|
184 | {
|
---|
185 | testList.append(paTestData[i]);
|
---|
186 | RTTESTI_CHECK_RETV(testList.size() == cAdded);
|
---|
187 | RTTESTI_CHECK(testList.at(0) == paTestData[cTestItems / 2]);
|
---|
188 | RTTESTI_CHECK(testList[0] == paTestData[cTestItems / 2]);
|
---|
189 | RTTESTI_CHECK(testList.first() == paTestData[cTestItems / 2]);
|
---|
190 | RTTESTI_CHECK(testList.at(cAdded - 1) == paTestData[i]);
|
---|
191 | RTTESTI_CHECK(testList[cAdded - 1] == paTestData[i]);
|
---|
192 | RTTESTI_CHECK(testList.last() == paTestData[i]);
|
---|
193 | }
|
---|
194 |
|
---|
195 | /* Check that all is correctly appended. */
|
---|
196 | RTTESTI_CHECK_RETV(testList.size() == cTestItems / 2);
|
---|
197 | RTTESTI_CHECK_RETV(testList.isEmpty() == false);
|
---|
198 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
199 | RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]);
|
---|
200 |
|
---|
201 | /* Start prepending the first half of our test list. Iterate reverse to get
|
---|
202 | * the correct sorting back. */
|
---|
203 | for (size_t i = cTestItems / 2; i > 0; --i, ++cAdded)
|
---|
204 | {
|
---|
205 | testList.prepend(paTestData[i - 1]);
|
---|
206 | RTTESTI_CHECK_RETV(testList.size() == cAdded);
|
---|
207 | RTTESTI_CHECK(testList.at(0) == paTestData[i - 1]);
|
---|
208 | RTTESTI_CHECK(testList[0] == paTestData[i - 1]);
|
---|
209 | RTTESTI_CHECK(testList.first() == paTestData[i - 1]);
|
---|
210 | RTTESTI_CHECK(testList.at(cAdded - 1) == paTestData[cTestItems - 1]);
|
---|
211 | RTTESTI_CHECK(testList[cAdded - 1] == paTestData[cTestItems - 1]);
|
---|
212 | RTTESTI_CHECK(testList.last() == paTestData[cTestItems - 1]);
|
---|
213 | }
|
---|
214 |
|
---|
215 | /* Check that all is correctly prepended. */
|
---|
216 | RTTESTI_CHECK_RETV(testList.size() == cTestItems);
|
---|
217 | RTTESTI_CHECK_RETV(testList.isEmpty() == false);
|
---|
218 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
219 | RTTESTI_CHECK(testList.at(i) == paTestData[i]);
|
---|
220 |
|
---|
221 | /*
|
---|
222 | * Contains
|
---|
223 | */
|
---|
224 | L<T1, T2> testList2;
|
---|
225 |
|
---|
226 | /* Check full list. */
|
---|
227 | RTTESTI_CHECK( testList.contains(paTestData[0]));
|
---|
228 | RTTESTI_CHECK( testList.contains(paTestData[cTestItems / 2]));
|
---|
229 | RTTESTI_CHECK( testList.contains(paTestData[cTestItems - 1]));
|
---|
230 | RTTESTI_CHECK(!testList.contains(T1()));
|
---|
231 | /* Check empty list. */
|
---|
232 | RTTESTI_CHECK(!testList2.contains(paTestData[0]));
|
---|
233 | RTTESTI_CHECK(!testList2.contains(paTestData[cTestItems / 2]));
|
---|
234 | RTTESTI_CHECK(!testList2.contains(paTestData[cTestItems - 1]));
|
---|
235 | RTTESTI_CHECK(!testList2.contains(T1()));
|
---|
236 |
|
---|
237 | /*
|
---|
238 | * Copy operator
|
---|
239 | */
|
---|
240 | L<T1, T2> testList3(testList);
|
---|
241 |
|
---|
242 | /* Check that all is correctly appended. */
|
---|
243 | RTTESTI_CHECK_RETV(testList3.size() == cTestItems);
|
---|
244 | for (size_t i = 0; i < testList3.size(); ++i)
|
---|
245 | RTTESTI_CHECK(testList3.at(i) == paTestData[i]);
|
---|
246 |
|
---|
247 | /*
|
---|
248 | * "=" operator
|
---|
249 | */
|
---|
250 | L<T1, T2> testList4;
|
---|
251 | testList4 = testList;
|
---|
252 |
|
---|
253 | /* Check that all is correctly appended. */
|
---|
254 | RTTESTI_CHECK_RETV(testList4.size() == cTestItems);
|
---|
255 | for (size_t i = 0; i < testList4.size(); ++i)
|
---|
256 | RTTESTI_CHECK(testList4.at(i) == paTestData[i]);
|
---|
257 |
|
---|
258 | /*
|
---|
259 | * Append list
|
---|
260 | */
|
---|
261 | testList3.append(testList4);
|
---|
262 |
|
---|
263 | /* Check that all is correctly appended. */
|
---|
264 | RTTESTI_CHECK_RETV(testList3.size() == cTestItems * 2);
|
---|
265 | for (size_t i = 0; i < testList3.size(); ++i)
|
---|
266 | RTTESTI_CHECK(testList3.at(i) == paTestData[i % cTestItems]);
|
---|
267 |
|
---|
268 | /*
|
---|
269 | * Prepend list
|
---|
270 | */
|
---|
271 | testList3.prepend(testList4);
|
---|
272 |
|
---|
273 | /* Check that all is correctly appended. */
|
---|
274 | RTTESTI_CHECK_RETV(testList3.size() == cTestItems * 3);
|
---|
275 | for (size_t i = 0; i < testList3.size(); ++i)
|
---|
276 | RTTESTI_CHECK(testList3.at(i) == paTestData[i % cTestItems]);
|
---|
277 |
|
---|
278 | /*
|
---|
279 | * "value" method
|
---|
280 | */
|
---|
281 | for (size_t i = 0; i < testList3.size(); ++i)
|
---|
282 | RTTESTI_CHECK(testList3.value(i) == paTestData[i % cTestItems]);
|
---|
283 | for (size_t i = 0; i < testList3.size(); ++i)
|
---|
284 | RTTESTI_CHECK(testList3.value(i, T1()) == paTestData[i % cTestItems]);
|
---|
285 | RTTESTI_CHECK(testList3.value(testList3.size() + 1) == T1()); /* Invalid index */
|
---|
286 | RTTESTI_CHECK(testList3.value(testList3.size() + 1, T1()) == T1()); /* Invalid index */
|
---|
287 |
|
---|
288 | /*
|
---|
289 | * operator[] (reading)
|
---|
290 | */
|
---|
291 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
292 | RTTESTI_CHECK(testList[i] == paTestData[i]);
|
---|
293 |
|
---|
294 | /*
|
---|
295 | * operator[] (writing)
|
---|
296 | *
|
---|
297 | * Replace with inverted array.
|
---|
298 | */
|
---|
299 | for (size_t i = 0; i < cTestItems; ++i)
|
---|
300 | testList[i] = paTestData[cTestItems - i - 1];
|
---|
301 | RTTESTI_CHECK_RETV(testList.size() == cTestItems);
|
---|
302 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
303 | RTTESTI_CHECK(testList[i] == paTestData[cTestItems - i - 1]);
|
---|
304 |
|
---|
305 | /*
|
---|
306 | * Replace
|
---|
307 | *
|
---|
308 | * Replace with inverted array (Must be original array when finished).
|
---|
309 | */
|
---|
310 | for (size_t i = 0; i < cTestItems; ++i)
|
---|
311 | testList.replace(i, paTestData[i]);
|
---|
312 | RTTESTI_CHECK_RETV(testList.size() == cTestItems);
|
---|
313 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
314 | RTTESTI_CHECK(testList[i] == paTestData[i]);
|
---|
315 |
|
---|
316 | /*
|
---|
317 | * Removing
|
---|
318 | */
|
---|
319 |
|
---|
320 | /* Remove Range */
|
---|
321 | testList3.removeRange(cTestItems, cTestItems * 2);
|
---|
322 | RTTESTI_CHECK_RETV(testList3.size() == cTestItems * 2);
|
---|
323 | for (size_t i = 0; i < testList3.size(); ++i)
|
---|
324 | RTTESTI_CHECK(testList3.at(i) == paTestData[i % cTestItems]);
|
---|
325 |
|
---|
326 | /* Remove the first half (reverse) */
|
---|
327 | size_t cRemoved = 1;
|
---|
328 | for (size_t i = cTestItems / 2; i > 0; --i, ++cRemoved)
|
---|
329 | {
|
---|
330 | testList.removeAt(i - 1);
|
---|
331 | RTTESTI_CHECK_RETV(testList.size() == cTestItems - cRemoved);
|
---|
332 | }
|
---|
333 | RTTESTI_CHECK_RETV(testList.size() == cTestItems / 2);
|
---|
334 |
|
---|
335 | /* Check that all is correctly removed and only the second part of the list
|
---|
336 | * is still there. */
|
---|
337 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
338 | RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]);
|
---|
339 |
|
---|
340 | /*
|
---|
341 | * setCapacity
|
---|
342 | */
|
---|
343 | testList.setCapacity(cTestItems * 5);
|
---|
344 | RTTESTI_CHECK(testList.capacity() == cTestItems * 5);
|
---|
345 | RTTESTI_CHECK_RETV(testList.size() == cTestItems / 2);
|
---|
346 |
|
---|
347 | /* As the capacity just increased, we should still have all entries from
|
---|
348 | * the previous list. */
|
---|
349 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
350 | RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]);
|
---|
351 |
|
---|
352 | /* Decrease the capacity so it will be smaller than the count of items in
|
---|
353 | * the list. The list should be shrink automatically, but the remaining
|
---|
354 | * items should be still valid. */
|
---|
355 | testList.setCapacity(cTestItems / 4);
|
---|
356 | RTTESTI_CHECK_RETV(testList.size() == cTestItems / 4);
|
---|
357 | RTTESTI_CHECK(testList.capacity() == cTestItems / 4);
|
---|
358 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
359 | RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]);
|
---|
360 |
|
---|
361 | /* Clear all */
|
---|
362 | testList.clear();
|
---|
363 | RTTESTI_CHECK_RETV(testList.isEmpty());
|
---|
364 | RTTESTI_CHECK_RETV(testList.size() == 0);
|
---|
365 | RTTESTI_CHECK(testList.capacity() == defCap);
|
---|
366 |
|
---|
367 |
|
---|
368 | /* Copy empty lists. */
|
---|
369 | L<T1, T2> testList5(testList);
|
---|
370 | RTTESTI_CHECK_RETV(testList5.isEmpty());
|
---|
371 | RTTESTI_CHECK_RETV(testList5.size() == 0);
|
---|
372 | RTTESTI_CHECK(testList5.capacity() == 0);
|
---|
373 |
|
---|
374 | testList5.append(paTestData[0]);
|
---|
375 | testList5 = testList;
|
---|
376 | RTTESTI_CHECK_RETV(testList5.isEmpty());
|
---|
377 | RTTESTI_CHECK_RETV(testList5.size() == 0);
|
---|
378 | RTTESTI_CHECK(testList5.capacity() == 0);
|
---|
379 |
|
---|
380 | /*
|
---|
381 | * Negative testing.
|
---|
382 | */
|
---|
383 | bool fMayPanic = RTAssertMayPanic();
|
---|
384 | bool fQuiet = RTAssertAreQuiet();
|
---|
385 | RTAssertSetMayPanic(false);
|
---|
386 | RTAssertSetQuiet(true);
|
---|
387 |
|
---|
388 | L<T1, T2> testList6;
|
---|
389 | for (size_t i = 0; i < cTestItems; ++i)
|
---|
390 | testList6.insert(i, paTestData[i]);
|
---|
391 | RTTESTI_CHECK(testList6.size() == cTestItems);
|
---|
392 |
|
---|
393 | /* Insertion beyond the end of the array ends up at the end. */
|
---|
394 | size_t cBefore = testList6.size();
|
---|
395 | testList6.insert(cBefore + 3, paTestData[0]);
|
---|
396 | RTTESTI_CHECK(testList6.size() == cBefore + 1);
|
---|
397 | RTTESTI_CHECK(testList6.at(cBefore) == paTestData[0]);
|
---|
398 |
|
---|
399 | cBefore = testList6.size();
|
---|
400 | L<T1, T2> testList7(testList6);
|
---|
401 | testList6.insert(testList6.size() + 42, testList7);
|
---|
402 | RTTESTI_CHECK(testList6.size() == cBefore + testList7.size());
|
---|
403 |
|
---|
404 | /* Inserting, appending or prepending a list to itself is not supported. */
|
---|
405 | cBefore = testList6.size();
|
---|
406 | testList6.insert(3, testList6);
|
---|
407 | RTTESTI_CHECK(testList6.size() == cBefore);
|
---|
408 |
|
---|
409 | cBefore = testList6.size();
|
---|
410 | testList6.append(testList6);
|
---|
411 | RTTESTI_CHECK(testList6.size() == cBefore);
|
---|
412 |
|
---|
413 | cBefore = testList6.size();
|
---|
414 | testList6.prepend(testList6);
|
---|
415 | RTTESTI_CHECK(testList6.size() == cBefore);
|
---|
416 |
|
---|
417 | /* Replace does nothing if the index is bad. */
|
---|
418 | cBefore = testList6.size();
|
---|
419 | testList6.replace(cBefore, testList6[6]);
|
---|
420 | RTTESTI_CHECK(testList6.size() == cBefore);
|
---|
421 |
|
---|
422 | cBefore = testList6.size();
|
---|
423 | testList6.replace(cBefore + 64, testList6[6]);
|
---|
424 | RTTESTI_CHECK(testList6.size() == cBefore);
|
---|
425 |
|
---|
426 | /* Indexing beyond the array returns the last element. */
|
---|
427 | cBefore = testList6.size();
|
---|
428 | RTTESTI_CHECK(testList6[cBefore] == testList6.last());
|
---|
429 | RTTESTI_CHECK(testList6[cBefore + 42] == testList6.last());
|
---|
430 |
|
---|
431 | RTTESTI_CHECK(&testList6[cBefore] == &testList6[cBefore - 1]);
|
---|
432 | RTTESTI_CHECK(&testList6[cBefore + 42] == &testList6[cBefore - 1]);
|
---|
433 |
|
---|
434 | /* removeAt does nothing if the index is bad. */
|
---|
435 | cBefore = testList6.size();
|
---|
436 | testList6.removeAt(cBefore);
|
---|
437 | RTTESTI_CHECK(testList6.size() == cBefore);
|
---|
438 |
|
---|
439 | cBefore = testList6.size();
|
---|
440 | testList6.removeAt(cBefore + 42);
|
---|
441 | RTTESTI_CHECK(testList6.size() == cBefore);
|
---|
442 |
|
---|
443 | L<T1, T2> testListEmpty1; RTTESTI_CHECK(!testListEmpty1.size());
|
---|
444 | testListEmpty1.removeFirst();
|
---|
445 | RTTESTI_CHECK(!testListEmpty1.size());
|
---|
446 |
|
---|
447 | testListEmpty1.removeLast();
|
---|
448 | RTTESTI_CHECK(!testListEmpty1.size());
|
---|
449 |
|
---|
450 | testListEmpty1.removeAt(128);
|
---|
451 | RTTESTI_CHECK(!testListEmpty1.size());
|
---|
452 |
|
---|
453 | /* removeRange interprets indexes beyond the end as the end of array (asserted). */
|
---|
454 | testListEmpty1.removeRange(42, 128);
|
---|
455 | RTTESTI_CHECK(!testListEmpty1.size());
|
---|
456 |
|
---|
457 | cBefore = testList6.size();
|
---|
458 | testList6.removeRange(cBefore, cBefore);
|
---|
459 | RTTESTI_CHECK(testList6.size() == cBefore);
|
---|
460 |
|
---|
461 | cBefore = testList6.size();
|
---|
462 | testList6.removeRange(cBefore + 12, cBefore + 128);
|
---|
463 | RTTESTI_CHECK(testList6.size() == cBefore);
|
---|
464 |
|
---|
465 | /* If end is less or equal to the start, nothing is done. */
|
---|
466 | testListEmpty1.removeRange(128, 0);
|
---|
467 | RTTESTI_CHECK(!testListEmpty1.size());
|
---|
468 |
|
---|
469 | cBefore = testList6.size();
|
---|
470 | testList6.removeRange(cBefore, 0);
|
---|
471 | RTTESTI_CHECK(testList6.size() == cBefore);
|
---|
472 |
|
---|
473 | cBefore = testList6.size();
|
---|
474 | testList6.removeRange(0, 0);
|
---|
475 | RTTESTI_CHECK(testList6.size() == cBefore);
|
---|
476 |
|
---|
477 | cBefore = testList6.size();
|
---|
478 | testList6.removeRange(0, 0);
|
---|
479 | RTTESTI_CHECK(testList6.size() == cBefore);
|
---|
480 |
|
---|
481 | RTAssertSetQuiet(fQuiet);
|
---|
482 | RTAssertSetMayPanic(fMayPanic);
|
---|
483 | }
|
---|
484 |
|
---|
485 | /* define RTCList here to see what happens without MT support ;)
|
---|
486 | * (valgrind is the preferred tool to check). */
|
---|
487 | #define MTTESTLISTTYPE RTCMTList
|
---|
488 | #define MTTESTTYPE uint32_t
|
---|
489 | #define MTTESTITEMS 1000
|
---|
490 |
|
---|
491 | /**
|
---|
492 | * Thread for prepending items to a shared list.
|
---|
493 | *
|
---|
494 | * @param hSelf The thread handle.
|
---|
495 | * @param pvUser The provided user data.
|
---|
496 | */
|
---|
497 | static DECLCALLBACK(int) MtTest1ThreadProc(RTTHREAD hSelf, void *pvUser)
|
---|
498 | {
|
---|
499 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
500 | RT_NOREF_PV(hSelf);
|
---|
501 |
|
---|
502 | /* Prepend new items at the start of the list. */
|
---|
503 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
504 | pTestList->prepend(0x0);
|
---|
505 |
|
---|
506 | return VINF_SUCCESS;
|
---|
507 | }
|
---|
508 |
|
---|
509 | /**
|
---|
510 | * Thread for appending items to a shared list.
|
---|
511 | *
|
---|
512 | * @param hSelf The thread handle.
|
---|
513 | * @param pvUser The provided user data.
|
---|
514 | */
|
---|
515 | static DECLCALLBACK(int) MtTest2ThreadProc(RTTHREAD hSelf, void *pvUser)
|
---|
516 | {
|
---|
517 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
518 | RT_NOREF_PV(hSelf);
|
---|
519 |
|
---|
520 | /* Append new items at the end of the list. */
|
---|
521 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
522 | pTestList->append(0xFFFFFFFF);
|
---|
523 |
|
---|
524 | return VINF_SUCCESS;
|
---|
525 | }
|
---|
526 |
|
---|
527 | /**
|
---|
528 | * Thread for inserting items to a shared list.
|
---|
529 | *
|
---|
530 | * @param hSelf The thread handle.
|
---|
531 | * @param pvUser The provided user data.
|
---|
532 | */
|
---|
533 | static DECLCALLBACK(int) MtTest3ThreadProc(RTTHREAD hSelf, void *pvUser)
|
---|
534 | {
|
---|
535 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
536 | RT_NOREF_PV(hSelf);
|
---|
537 |
|
---|
538 | /* Insert new items in the middle of the list. */
|
---|
539 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
540 | pTestList->insert(pTestList->size() / 2, 0xF0F0F0F0);
|
---|
541 |
|
---|
542 | return VINF_SUCCESS;
|
---|
543 | }
|
---|
544 |
|
---|
545 | /**
|
---|
546 | * Thread for reading items from a shared list.
|
---|
547 | *
|
---|
548 | * @param hSelf The thread handle.
|
---|
549 | * @param pvUser The provided user data.
|
---|
550 | */
|
---|
551 | static DECLCALLBACK(int) MtTest4ThreadProc(RTTHREAD hSelf, void *pvUser)
|
---|
552 | {
|
---|
553 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
554 | RT_NOREF_PV(hSelf);
|
---|
555 |
|
---|
556 | MTTESTTYPE a;
|
---|
557 | /* Try to read C items from random places. */
|
---|
558 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
559 | {
|
---|
560 | /* Make sure there is at least one item in the list. */
|
---|
561 | while (pTestList->isEmpty())
|
---|
562 | RTThreadYield();
|
---|
563 | a = pTestList->at(RTRandU32Ex(0, (uint32_t)pTestList->size() - 1));
|
---|
564 | }
|
---|
565 |
|
---|
566 | return VINF_SUCCESS;
|
---|
567 | }
|
---|
568 |
|
---|
569 | /**
|
---|
570 | * Thread for replacing items in a shared list.
|
---|
571 | *
|
---|
572 | * @param hSelf The thread handle.
|
---|
573 | * @param pvUser The provided user data.
|
---|
574 | */
|
---|
575 | static DECLCALLBACK(int) MtTest5ThreadProc(RTTHREAD hSelf, void *pvUser)
|
---|
576 | {
|
---|
577 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
578 | RT_NOREF_PV(hSelf);
|
---|
579 |
|
---|
580 | /* Try to replace C items from random places. */
|
---|
581 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
582 | {
|
---|
583 | /* Make sure there is at least one item in the list. */
|
---|
584 | while (pTestList->isEmpty())
|
---|
585 | RTThreadYield();
|
---|
586 | pTestList->replace(RTRandU32Ex(0, (uint32_t)pTestList->size() - 1), 0xFF00FF00);
|
---|
587 | }
|
---|
588 |
|
---|
589 | return VINF_SUCCESS;
|
---|
590 | }
|
---|
591 |
|
---|
592 | /**
|
---|
593 | * Thread for erasing items from a shared list.
|
---|
594 | *
|
---|
595 | * @param hSelf The thread handle.
|
---|
596 | * @param pvUser The provided user data.
|
---|
597 | */
|
---|
598 | static DECLCALLBACK(int) MtTest6ThreadProc(RTTHREAD hSelf, void *pvUser)
|
---|
599 | {
|
---|
600 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
601 | RT_NOREF_PV(hSelf);
|
---|
602 |
|
---|
603 | /* Try to delete items from random places. */
|
---|
604 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
605 | {
|
---|
606 | /* Make sure there is at least one item in the list. */
|
---|
607 | while (pTestList->isEmpty())
|
---|
608 | RTThreadYield();
|
---|
609 | pTestList->removeAt(RTRandU32Ex(0, (uint32_t)pTestList->size() - 1));
|
---|
610 | }
|
---|
611 |
|
---|
612 | return VINF_SUCCESS;
|
---|
613 | }
|
---|
614 |
|
---|
615 | /**
|
---|
616 | * Does a multi-threading list test. Several list additions, reading, replacing
|
---|
617 | * and erasing are done simultaneous.
|
---|
618 | *
|
---|
619 | */
|
---|
620 | static void test2()
|
---|
621 | {
|
---|
622 | RTTestISubF("MT test with 6 threads (%u tests per thread).", MTTESTITEMS);
|
---|
623 |
|
---|
624 | MTTESTLISTTYPE<MTTESTTYPE> testList;
|
---|
625 | RTTHREAD ahThreads[6];
|
---|
626 | static PFNRTTHREAD apfnThreads[6] =
|
---|
627 | {
|
---|
628 | MtTest1ThreadProc, MtTest2ThreadProc, MtTest3ThreadProc, MtTest4ThreadProc, MtTest5ThreadProc, MtTest6ThreadProc
|
---|
629 | };
|
---|
630 |
|
---|
631 | for (unsigned i = 0; i < RT_ELEMENTS(ahThreads); i++)
|
---|
632 | {
|
---|
633 | RTTESTI_CHECK_RC_RETV(RTThreadCreateF(&ahThreads[i], apfnThreads[i], &testList, 0,
|
---|
634 | RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "mttest%u", i), VINF_SUCCESS);
|
---|
635 | }
|
---|
636 |
|
---|
637 | uint64_t tsMsDeadline = RTTimeMilliTS() + 60000;
|
---|
638 | for (unsigned i = 0; i < RT_ELEMENTS(ahThreads); i++)
|
---|
639 | {
|
---|
640 | uint64_t tsNow = RTTimeMilliTS();
|
---|
641 | uint32_t cWait = tsNow > tsMsDeadline ? 5000 : tsMsDeadline - tsNow;
|
---|
642 | RTTESTI_CHECK_RC(RTThreadWait(ahThreads[i], cWait, NULL), VINF_SUCCESS);
|
---|
643 | }
|
---|
644 |
|
---|
645 | RTTESTI_CHECK_RETV(testList.size() == MTTESTITEMS * 2);
|
---|
646 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
647 | {
|
---|
648 | uint32_t a = testList.at(i);
|
---|
649 | RTTESTI_CHECK(a == 0x0 || a == 0xFFFFFFFF || a == 0xF0F0F0F0 || a == 0xFF00FF00);
|
---|
650 | }
|
---|
651 | }
|
---|
652 |
|
---|
653 | int main()
|
---|
654 | {
|
---|
655 | /* How many integer test items should be created. */
|
---|
656 | static const size_t s_cTestCount = 1000;
|
---|
657 |
|
---|
658 | RTTEST hTest;
|
---|
659 | RTEXITCODE rcExit = RTTestInitAndCreate("tstIprtList", &hTest);
|
---|
660 | if (rcExit)
|
---|
661 | return rcExit;
|
---|
662 | RTTestBanner(hTest);
|
---|
663 |
|
---|
664 | /*
|
---|
665 | * Native types.
|
---|
666 | */
|
---|
667 | uint8_t au8TestInts[s_cTestCount];
|
---|
668 | for (size_t i = 0; i < RT_ELEMENTS(au8TestInts); ++i)
|
---|
669 | au8TestInts[i] = (uint8_t)RTRandU32Ex(1, UINT8_MAX);
|
---|
670 | test1<RTCList, uint8_t, uint8_t, uint8_t>("ST: Native type", au8TestInts, RT_ELEMENTS(au8TestInts));
|
---|
671 | test1<RTCMTList, uint8_t, uint8_t, uint8_t>("MT: Native type", au8TestInts, RT_ELEMENTS(au8TestInts));
|
---|
672 |
|
---|
673 | uint16_t au16TestInts[s_cTestCount];
|
---|
674 | for (size_t i = 0; i < RT_ELEMENTS(au16TestInts); ++i)
|
---|
675 | au16TestInts[i] = (uint16_t)RTRandU32Ex(1, UINT16_MAX);
|
---|
676 | test1<RTCList, uint16_t, uint16_t, uint16_t>("ST: Native type", au16TestInts, RT_ELEMENTS(au16TestInts));
|
---|
677 | test1<RTCMTList, uint16_t, uint16_t, uint16_t>("MT: Native type", au16TestInts, RT_ELEMENTS(au16TestInts));
|
---|
678 |
|
---|
679 | uint32_t au32TestInts[s_cTestCount];
|
---|
680 | for (size_t i = 0; i < RT_ELEMENTS(au32TestInts); ++i)
|
---|
681 | au32TestInts[i] = RTRandU32Ex(1, UINT32_MAX);
|
---|
682 | test1<RTCList, uint32_t, uint32_t, uint32_t>("ST: Native type", au32TestInts, RT_ELEMENTS(au32TestInts));
|
---|
683 | test1<RTCMTList, uint32_t, uint32_t, uint32_t>("MT: Native type", au32TestInts, RT_ELEMENTS(au32TestInts));
|
---|
684 |
|
---|
685 | /*
|
---|
686 | * Specialized type.
|
---|
687 | */
|
---|
688 | uint64_t au64TestInts[s_cTestCount];
|
---|
689 | for (size_t i = 0; i < RT_ELEMENTS(au64TestInts); ++i)
|
---|
690 | au64TestInts[i] = RTRandU64Ex(1, UINT64_MAX);
|
---|
691 | test1<RTCList, uint64_t, uint64_t, uint64_t>("ST: Specialized type", au64TestInts, RT_ELEMENTS(au64TestInts));
|
---|
692 | test1<RTCMTList, uint64_t, uint64_t, uint64_t>("MT: Specialized type", au64TestInts, RT_ELEMENTS(au64TestInts));
|
---|
693 |
|
---|
694 | /*
|
---|
695 | * Big size type (translate to internal pointer list).
|
---|
696 | */
|
---|
697 | test1<RTCList, RTCString, RTCString *, const char *>("ST: Class type", g_apszTestStrings, RT_ELEMENTS(g_apszTestStrings));
|
---|
698 | test1<RTCMTList, RTCString, RTCString *, const char *>("MT: Class type", g_apszTestStrings, RT_ELEMENTS(g_apszTestStrings));
|
---|
699 |
|
---|
700 | /*
|
---|
701 | * Multi-threading test.
|
---|
702 | */
|
---|
703 | test2();
|
---|
704 |
|
---|
705 | /*
|
---|
706 | * Summary.
|
---|
707 | */
|
---|
708 | return RTTestSummaryAndDestroy(hTest);
|
---|
709 | }
|
---|
710 |
|
---|