1 | /* $Id: tstIprtList.cpp 37861 2011-07-11 10:03:22Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - RTCList/RTCMTList.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011 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 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #include <iprt/cpp/mtlist.h>
|
---|
31 |
|
---|
32 | #include <iprt/cpp/ministring.h>
|
---|
33 | #include <iprt/test.h>
|
---|
34 | #include <iprt/rand.h>
|
---|
35 | #include <iprt/thread.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /*******************************************************************************
|
---|
39 | * Global Variables *
|
---|
40 | *******************************************************************************/
|
---|
41 | /** Used for the string test. */
|
---|
42 | static const char *g_apszTestStrings[] =
|
---|
43 | {
|
---|
44 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
|
---|
45 | "Vestibulum non turpis vel metus pellentesque tincidunt at id massa.",
|
---|
46 | "Cras quis erat sed nulla ullamcorper molestie.",
|
---|
47 | "Mauris ac elit turpis, id pulvinar diam.",
|
---|
48 | "Nulla quis dolor dolor, in ultrices diam.",
|
---|
49 | "Vivamus ac quam non ipsum vehicula tempor ac ac arcu.",
|
---|
50 | "Aenean posuere lacus blandit erat semper eu iaculis ante eleifend.",
|
---|
51 | "Donec quis quam a lacus interdum sollicitudin quis eu est.",
|
---|
52 | "Morbi sed nisi a arcu commodo convallis.",
|
---|
53 | "Aenean molestie condimentum velit, non mattis magna ultricies quis.",
|
---|
54 | "Nulla id velit at mauris gravida mattis.",
|
---|
55 | "Phasellus viverra velit eu urna semper in porta arcu sollicitudin.",
|
---|
56 | "Pellentesque consequat turpis et tortor hendrerit id tempor ipsum lacinia.",
|
---|
57 | "Cras iaculis nulla quis risus pulvinar eget tempor lectus placerat.",
|
---|
58 | "Nullam in nulla sed sapien euismod euismod.",
|
---|
59 | "Morbi in tortor at magna sagittis fermentum ut eu nunc.",
|
---|
60 | "Nulla vitae ante sit amet dui molestie sagittis lacinia quis tellus.",
|
---|
61 | "Proin iaculis lorem ultricies metus bibendum tincidunt.",
|
---|
62 | "Sed gravida purus id risus sollicitudin ac porta orci vestibulum.",
|
---|
63 | "Duis quis purus non ligula consectetur cursus eu interdum erat.",
|
---|
64 | "Nullam non nunc in elit volutpat tempor in nec metus.",
|
---|
65 | "Aliquam id purus eget enim luctus molestie.",
|
---|
66 | "Sed id elit nec elit luctus scelerisque.",
|
---|
67 | "Suspendisse viverra leo non ligula congue ac luctus nisl vulputate.",
|
---|
68 | "Nulla dignissim lobortis nunc, eu tempus ipsum luctus sed.",
|
---|
69 | "Integer vel lacus lacus, quis condimentum felis.",
|
---|
70 | "Nulla ut lacus ac lacus gravida ultrices id sed ipsum.",
|
---|
71 | "Etiam non purus ut augue fermentum consequat.",
|
---|
72 | "Nam sit amet eros quis nibh blandit lacinia non posuere lectus.",
|
---|
73 | "Sed sit amet ipsum et dolor sagittis facilisis.",
|
---|
74 | "Ut congue nisi lacus, vel ultrices est.",
|
---|
75 | "Donec vel erat ut justo hendrerit sodales eu eget libero.",
|
---|
76 | "Integer a ipsum ac nunc eleifend congue convallis a urna.",
|
---|
77 | "Sed vel eros eu lectus imperdiet vehicula.",
|
---|
78 | "Vivamus eget turpis sed erat dapibus varius eget eu nulla.",
|
---|
79 | "Nam id nulla non elit eleifend commodo sed ac est.",
|
---|
80 | "Integer pulvinar dolor sodales velit pulvinar et facilisis eros scelerisque.",
|
---|
81 | "Ut mattis arcu ut libero imperdiet in rhoncus augue sodales.",
|
---|
82 | "Ut luctus turpis ligula, id dapibus felis.",
|
---|
83 | "Nullam sit amet sapien eget tellus hendrerit vestibulum eget in odio.",
|
---|
84 | "Phasellus non orci vitae mi placerat semper.",
|
---|
85 | "Quisque pharetra aliquet velit, quis tempor magna porttitor nec.",
|
---|
86 | "Praesent porta neque felis, vehicula facilisis odio.",
|
---|
87 | "Maecenas ultricies ipsum eu velit laoreet faucibus.",
|
---|
88 | "Mauris et nunc leo, et euismod quam.",
|
---|
89 | "Phasellus a felis et justo fringilla lacinia.",
|
---|
90 | "Vestibulum eget augue ante, ac viverra neque.",
|
---|
91 | "Mauris pellentesque ligula quis metus elementum venenatis.",
|
---|
92 | "Curabitur eu neque tellus, non porta sapien.",
|
---|
93 | "Ut mattis metus id enim aliquam laoreet et sed tortor.",
|
---|
94 | "Aenean quis nulla vitae nulla auctor lobortis a egestas turpis.",
|
---|
95 | "Praesent vitae ante a urna porta placerat non nec eros.",
|
---|
96 | "Donec quis neque eros, placerat adipiscing turpis.",
|
---|
97 | "Cras sit amet sapien risus, quis euismod arcu.",
|
---|
98 | "Integer volutpat massa eros, ac gravida mi.",
|
---|
99 | "Nunc vitae nunc sagittis diam vulputate suscipit.",
|
---|
100 | "Suspendisse quis mauris bibendum mauris aliquet pulvinar.",
|
---|
101 | "Donec volutpat vestibulum ligula, eget interdum tortor malesuada sit amet.",
|
---|
102 | "Mauris hendrerit dui non nibh varius sit amet fringilla orci pretium.",
|
---|
103 | "Phasellus a quam tellus, auctor lacinia sapien.",
|
---|
104 | "Sed dapibus leo vitae neque faucibus id porttitor sapien ultricies.",
|
---|
105 | "Maecenas euismod elit nec tortor sagittis pretium.",
|
---|
106 | "Ut tincidunt risus at erat fermentum sit amet molestie ante lacinia.",
|
---|
107 | "Nulla non leo nec lacus sollicitudin lobortis a a nisl.",
|
---|
108 | "Nunc vulputate erat vel libero elementum a interdum turpis malesuada.",
|
---|
109 | "Morbi id libero turpis, a lobortis dolor.",
|
---|
110 | "Donec vehicula imperdiet lorem, non pretium nulla tempus ut.",
|
---|
111 | "Morbi lacinia massa id nunc tempus in blandit risus blandit.",
|
---|
112 | "Sed feugiat orci id ipsum suscipit quis fringilla enim rutrum.",
|
---|
113 | "Mauris suscipit lobortis urna, vel dictum justo iaculis ac.",
|
---|
114 | "In rhoncus lectus tristique nunc blandit gravida placerat turpis rutrum.",
|
---|
115 | "Aliquam pellentesque ornare justo, sed hendrerit metus mattis a.",
|
---|
116 | "Nam aliquet lorem congue nisl blandit posuere.",
|
---|
117 | "Sed lobortis interdum ipsum, ac cursus erat lacinia in.",
|
---|
118 | "Maecenas vel tortor vel lorem facilisis interdum.",
|
---|
119 | "Aenean porttitor massa enim, eget dignissim est.",
|
---|
120 | "Nullam id libero lacus, mattis feugiat risus.",
|
---|
121 | "Fusce et dolor at eros ornare auctor malesuada vel ipsum.",
|
---|
122 | "Donec at massa sit amet lorem pellentesque interdum at ac lacus.",
|
---|
123 | "Praesent suscipit velit at justo suscipit eu vestibulum ligula interdum.",
|
---|
124 | "Aenean id justo nulla, vitae vulputate diam.",
|
---|
125 | "Fusce pellentesque leo quis orci pulvinar at pellentesque tellus dictum.",
|
---|
126 | "Ut facilisis purus at enim varius vulputate.",
|
---|
127 | "Donec malesuada bibendum sapien, sed pretium nisi cursus quis.",
|
---|
128 | "Mauris porttitor diam ut sapien pretium egestas.",
|
---|
129 | "Vestibulum ut justo eu libero semper convallis vitae et velit.",
|
---|
130 | "Quisque eleifend dapibus ligula, eu tincidunt massa rutrum at.",
|
---|
131 | "Sed euismod diam eget enim suscipit dictum.",
|
---|
132 | "Mauris fermentum orci eu nunc venenatis in sollicitudin tellus vestibulum.",
|
---|
133 | "Vivamus faucibus consequat turpis, lobortis vehicula lectus gravida eget.",
|
---|
134 | "Curabitur eu erat eu mi interdum scelerisque.",
|
---|
135 | "Morbi consequat molestie nulla, imperdiet elementum augue sagittis vel.",
|
---|
136 | "Sed ullamcorper velit suscipit arcu egestas quis commodo est hendrerit.",
|
---|
137 | "Proin vitae velit ut enim sollicitudin ultrices.",
|
---|
138 | "Curabitur posuere euismod lacus, sed volutpat erat adipiscing sit amet.",
|
---|
139 | "Cras sit amet sem lorem, in cursus augue.",
|
---|
140 | "Sed fermentum ultricies orci, quis hendrerit risus imperdiet et.",
|
---|
141 | "Proin nec arcu interdum ipsum molestie vestibulum.",
|
---|
142 | "Nulla quis quam non sem pretium scelerisque et eu velit.",
|
---|
143 | "Donec eu tellus nisl, ac vehicula tortor."
|
---|
144 | };
|
---|
145 |
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Does a list test.
|
---|
149 | *
|
---|
150 | * @param T1 The list type.
|
---|
151 | * @param T2 The input type
|
---|
152 | * @param pcszDesc The test description.
|
---|
153 | * @param paTestData Pointer to the array with the test input data.
|
---|
154 | * @param cTestItems The size of the input data.
|
---|
155 | */
|
---|
156 | template<template <class, typename> class L, typename T1, typename T2, typename T3>
|
---|
157 | static void test1(const char *pcszDesc, T3 paTestData[], size_t cTestItems)
|
---|
158 | {
|
---|
159 | RTTestISubF("%s with size of %u (items=%u)", pcszDesc, sizeof(T1), cTestItems);
|
---|
160 |
|
---|
161 | /*
|
---|
162 | * Construction
|
---|
163 | */
|
---|
164 |
|
---|
165 | /* Create a test list */
|
---|
166 | L<T1, T2> testList;
|
---|
167 |
|
---|
168 | const size_t defCap = L<T1, T2>::DefaultCapacity;
|
---|
169 | RTTESTI_CHECK(testList.isEmpty());
|
---|
170 | RTTESTI_CHECK(testList.size() == 0);
|
---|
171 | RTTESTI_CHECK(testList.capacity() == defCap);
|
---|
172 |
|
---|
173 | /*
|
---|
174 | * Adding
|
---|
175 | */
|
---|
176 |
|
---|
177 | /* Add the second half of the test data */
|
---|
178 | size_t cAdded = 1;
|
---|
179 |
|
---|
180 | /* Start adding the second half of our test list */
|
---|
181 | for (size_t i = cTestItems / 2; i < cTestItems; ++i, ++cAdded)
|
---|
182 | {
|
---|
183 | testList.append(paTestData[i]);
|
---|
184 | RTTESTI_CHECK_RETV(testList.size() == cAdded);
|
---|
185 | RTTESTI_CHECK(testList.at(0) == paTestData[cTestItems / 2]);
|
---|
186 | RTTESTI_CHECK(testList[0] == paTestData[cTestItems / 2]);
|
---|
187 | RTTESTI_CHECK(testList.first() == paTestData[cTestItems / 2]);
|
---|
188 | RTTESTI_CHECK(testList.at(cAdded - 1) == paTestData[i]);
|
---|
189 | RTTESTI_CHECK(testList[cAdded - 1] == paTestData[i]);
|
---|
190 | RTTESTI_CHECK(testList.last() == paTestData[i]);
|
---|
191 | }
|
---|
192 |
|
---|
193 | /* Check that all is correctly appended. */
|
---|
194 | RTTESTI_CHECK_RETV(testList.size() == cTestItems / 2);
|
---|
195 | RTTESTI_CHECK_RETV(testList.isEmpty() == false);
|
---|
196 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
197 | RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]);
|
---|
198 |
|
---|
199 | /* Start prepending the first half of our test list. Iterate reverse to get
|
---|
200 | * the correct sorting back. */
|
---|
201 | for (size_t i = cTestItems / 2; i > 0; --i, ++cAdded)
|
---|
202 | {
|
---|
203 | testList.prepend(paTestData[i - 1]);
|
---|
204 | RTTESTI_CHECK_RETV(testList.size() == cAdded);
|
---|
205 | RTTESTI_CHECK(testList.at(0) == paTestData[i - 1]);
|
---|
206 | RTTESTI_CHECK(testList[0] == paTestData[i - 1]);
|
---|
207 | RTTESTI_CHECK(testList.first() == paTestData[i - 1]);
|
---|
208 | RTTESTI_CHECK(testList.at(cAdded - 1) == paTestData[cTestItems - 1]);
|
---|
209 | RTTESTI_CHECK(testList[cAdded - 1] == paTestData[cTestItems - 1]);
|
---|
210 | RTTESTI_CHECK(testList.last() == paTestData[cTestItems - 1]);
|
---|
211 | }
|
---|
212 |
|
---|
213 | /* Check that all is correctly prepended. */
|
---|
214 | RTTESTI_CHECK_RETV(testList.size() == cTestItems);
|
---|
215 | RTTESTI_CHECK_RETV(testList.isEmpty() == false);
|
---|
216 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
217 | RTTESTI_CHECK(testList.at(i) == paTestData[i]);
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * Contains
|
---|
221 | */
|
---|
222 | L<T1, T2> testList2;
|
---|
223 |
|
---|
224 | /* Check full list. */
|
---|
225 | RTTESTI_CHECK( testList.contains(paTestData[0]));
|
---|
226 | RTTESTI_CHECK( testList.contains(paTestData[cTestItems / 2]));
|
---|
227 | RTTESTI_CHECK( testList.contains(paTestData[cTestItems - 1]));
|
---|
228 | RTTESTI_CHECK(!testList.contains(T1()));
|
---|
229 | /* Check empty list. */
|
---|
230 | RTTESTI_CHECK(!testList2.contains(paTestData[0]));
|
---|
231 | RTTESTI_CHECK(!testList2.contains(paTestData[cTestItems / 2]));
|
---|
232 | RTTESTI_CHECK(!testList2.contains(paTestData[cTestItems - 1]));
|
---|
233 | RTTESTI_CHECK(!testList2.contains(T1()));
|
---|
234 |
|
---|
235 | /*
|
---|
236 | * Copy operator
|
---|
237 | */
|
---|
238 | L<T1, T2> testList3(testList);
|
---|
239 |
|
---|
240 | /* Check that all is correctly appended. */
|
---|
241 | RTTESTI_CHECK_RETV(testList3.size() == cTestItems);
|
---|
242 | for (size_t i = 0; i < testList3.size(); ++i)
|
---|
243 | RTTESTI_CHECK(testList3.at(i) == paTestData[i]);
|
---|
244 |
|
---|
245 | /*
|
---|
246 | * "=" operator
|
---|
247 | */
|
---|
248 | L<T1, T2> testList4;
|
---|
249 | testList4 = testList;
|
---|
250 |
|
---|
251 | /* Check that all is correctly appended. */
|
---|
252 | RTTESTI_CHECK_RETV(testList4.size() == cTestItems);
|
---|
253 | for (size_t i = 0; i < testList4.size(); ++i)
|
---|
254 | RTTESTI_CHECK(testList4.at(i) == paTestData[i]);
|
---|
255 |
|
---|
256 | /*
|
---|
257 | * Append list
|
---|
258 | */
|
---|
259 | testList3.append(testList4);
|
---|
260 |
|
---|
261 | /* Check that all is correctly appended. */
|
---|
262 | RTTESTI_CHECK_RETV(testList3.size() == cTestItems * 2);
|
---|
263 | for (size_t i = 0; i < testList3.size(); ++i)
|
---|
264 | RTTESTI_CHECK(testList3.at(i) == paTestData[i % cTestItems]);
|
---|
265 |
|
---|
266 | /*
|
---|
267 | * Prepend list
|
---|
268 | */
|
---|
269 | testList3.prepend(testList4);
|
---|
270 |
|
---|
271 | /* Check that all is correctly appended. */
|
---|
272 | RTTESTI_CHECK_RETV(testList3.size() == cTestItems * 3);
|
---|
273 | for (size_t i = 0; i < testList3.size(); ++i)
|
---|
274 | RTTESTI_CHECK(testList3.at(i) == paTestData[i % cTestItems]);
|
---|
275 |
|
---|
276 | /*
|
---|
277 | * "value" method
|
---|
278 | */
|
---|
279 | for (size_t i = 0; i < testList3.size(); ++i)
|
---|
280 | RTTESTI_CHECK(testList3.value(i) == paTestData[i % cTestItems]);
|
---|
281 | for (size_t i = 0; i < testList3.size(); ++i)
|
---|
282 | RTTESTI_CHECK(testList3.value(i, T1()) == paTestData[i % cTestItems]);
|
---|
283 | RTTESTI_CHECK(testList3.value(testList3.size() + 1) == T1()); /* Invalid index */
|
---|
284 | RTTESTI_CHECK(testList3.value(testList3.size() + 1, T1()) == T1()); /* Invalid index */
|
---|
285 |
|
---|
286 | /*
|
---|
287 | * operator[] (reading)
|
---|
288 | */
|
---|
289 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
290 | RTTESTI_CHECK(testList[i] == paTestData[i]);
|
---|
291 |
|
---|
292 | /*
|
---|
293 | * operator[] (writing)
|
---|
294 | *
|
---|
295 | * Replace with inverted array.
|
---|
296 | */
|
---|
297 | for (size_t i = 0; i < cTestItems; ++i)
|
---|
298 | testList[i] = paTestData[cTestItems - i - 1];
|
---|
299 | RTTESTI_CHECK_RETV(testList.size() == cTestItems);
|
---|
300 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
301 | RTTESTI_CHECK(testList[i] == paTestData[cTestItems - i - 1]);
|
---|
302 |
|
---|
303 | /*
|
---|
304 | * Replace
|
---|
305 | *
|
---|
306 | * Replace with inverted array (Must be original array when finished).
|
---|
307 | */
|
---|
308 | for (size_t i = 0; i < cTestItems; ++i)
|
---|
309 | testList.replace(i, paTestData[i]);
|
---|
310 | RTTESTI_CHECK_RETV(testList.size() == cTestItems);
|
---|
311 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
312 | RTTESTI_CHECK(testList[i] == paTestData[i]);
|
---|
313 |
|
---|
314 | /*
|
---|
315 | * Removing
|
---|
316 | */
|
---|
317 |
|
---|
318 | /* Remove Range */
|
---|
319 | testList3.removeRange(cTestItems, cTestItems * 2);
|
---|
320 | RTTESTI_CHECK_RETV(testList3.size() == cTestItems * 2);
|
---|
321 | for (size_t i = 0; i < testList3.size(); ++i)
|
---|
322 | RTTESTI_CHECK(testList3.at(i) == paTestData[i % cTestItems]);
|
---|
323 |
|
---|
324 | /* Remove the first half (reverse) */
|
---|
325 | size_t cRemoved = 1;
|
---|
326 | for (size_t i = cTestItems / 2; i > 0; --i, ++cRemoved)
|
---|
327 | {
|
---|
328 | testList.removeAt(i - 1);
|
---|
329 | RTTESTI_CHECK_RETV(testList.size() == cTestItems - cRemoved);
|
---|
330 | }
|
---|
331 | RTTESTI_CHECK_RETV(testList.size() == cTestItems / 2);
|
---|
332 |
|
---|
333 | /* Check that all is correctly removed and only the second part of the list
|
---|
334 | * is still there. */
|
---|
335 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
336 | RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]);
|
---|
337 |
|
---|
338 | /*
|
---|
339 | * setCapacitiy
|
---|
340 | */
|
---|
341 | testList.setCapacity(cTestItems * 5);
|
---|
342 | RTTESTI_CHECK(testList.capacity() == cTestItems * 5);
|
---|
343 | RTTESTI_CHECK_RETV(testList.size() == cTestItems / 2);
|
---|
344 |
|
---|
345 | /* As the capacity just increased, we should still have all entries from
|
---|
346 | * the previous list. */
|
---|
347 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
348 | RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]);
|
---|
349 |
|
---|
350 | /* Decrease the capacity so it will be smaller than the count of items in
|
---|
351 | * the list. The list should be shrink automatically, but the remaining
|
---|
352 | * items should be still valid. */
|
---|
353 | testList.setCapacity(cTestItems / 4);
|
---|
354 | RTTESTI_CHECK_RETV(testList.size() == cTestItems / 4);
|
---|
355 | RTTESTI_CHECK(testList.capacity() == cTestItems / 4);
|
---|
356 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
357 | RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]);
|
---|
358 |
|
---|
359 | /* Clear all */
|
---|
360 | testList.clear();
|
---|
361 | RTTESTI_CHECK_RETV(testList.isEmpty());
|
---|
362 | RTTESTI_CHECK_RETV(testList.size() == 0);
|
---|
363 | RTTESTI_CHECK(testList.capacity() == defCap);
|
---|
364 |
|
---|
365 |
|
---|
366 | /* Copy empty lists. */
|
---|
367 | L<T1, T2> testList5(testList);
|
---|
368 | RTTESTI_CHECK_RETV(testList5.isEmpty());
|
---|
369 | RTTESTI_CHECK_RETV(testList5.size() == 0);
|
---|
370 | RTTESTI_CHECK(testList5.capacity() == 0);
|
---|
371 |
|
---|
372 | testList5.append(paTestData[0]);
|
---|
373 | testList5 = testList;
|
---|
374 | RTTESTI_CHECK_RETV(testList5.isEmpty());
|
---|
375 | RTTESTI_CHECK_RETV(testList5.size() == 0);
|
---|
376 | RTTESTI_CHECK(testList5.capacity() == 0);
|
---|
377 |
|
---|
378 | }
|
---|
379 |
|
---|
380 | /* define RTCList here to see what happens without MT support ;)
|
---|
381 | * (valgrind is the preferred tool to check). */
|
---|
382 | #define MTTESTLISTTYPE RTCMTList
|
---|
383 | #define MTTESTTYPE uint32_t
|
---|
384 | #define MTTESTITEMS 1000
|
---|
385 |
|
---|
386 | /**
|
---|
387 | * Thread for prepending items to a shared list.
|
---|
388 | *
|
---|
389 | * @param hSelf The thread handle.
|
---|
390 | * @param pvUser The provided user data.
|
---|
391 | */
|
---|
392 | DECLCALLBACK(int) mttest1(RTTHREAD hSelf, void *pvUser)
|
---|
393 | {
|
---|
394 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
395 |
|
---|
396 | /* Prepend new items at the start of the list. */
|
---|
397 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
398 | pTestList->prepend(0x0);
|
---|
399 |
|
---|
400 | return VINF_SUCCESS;
|
---|
401 | }
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * Thread for appending items to a shared list.
|
---|
405 | *
|
---|
406 | * @param hSelf The thread handle.
|
---|
407 | * @param pvUser The provided user data.
|
---|
408 | */
|
---|
409 | DECLCALLBACK(int) mttest2(RTTHREAD hSelf, void *pvUser)
|
---|
410 | {
|
---|
411 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
412 |
|
---|
413 | /* Append new items at the end of the list. */
|
---|
414 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
415 | pTestList->append(0xFFFFFFFF);
|
---|
416 |
|
---|
417 | return VINF_SUCCESS;
|
---|
418 | }
|
---|
419 |
|
---|
420 | /**
|
---|
421 | * Thread for inserting items to a shared list.
|
---|
422 | *
|
---|
423 | * @param hSelf The thread handle.
|
---|
424 | * @param pvUser The provided user data.
|
---|
425 | */
|
---|
426 | DECLCALLBACK(int) mttest3(RTTHREAD hSelf, void *pvUser)
|
---|
427 | {
|
---|
428 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
429 |
|
---|
430 | /* Insert new items in the middle of the list. */
|
---|
431 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
432 | pTestList->insert(pTestList->size() / 2, 0xF0F0F0F0);
|
---|
433 |
|
---|
434 | return VINF_SUCCESS;
|
---|
435 | }
|
---|
436 |
|
---|
437 | /**
|
---|
438 | * Thread for reading items from a shared list.
|
---|
439 | *
|
---|
440 | * @param hSelf The thread handle.
|
---|
441 | * @param pvUser The provided user data.
|
---|
442 | */
|
---|
443 | DECLCALLBACK(int) mttest4(RTTHREAD hSelf, void *pvUser)
|
---|
444 | {
|
---|
445 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
446 |
|
---|
447 | MTTESTTYPE a;
|
---|
448 | /* Try to read C items from random places. */
|
---|
449 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
450 | {
|
---|
451 | /* Make sure there is at least one item in the list. */
|
---|
452 | while (pTestList->isEmpty()) {};
|
---|
453 | a = pTestList->at(RTRandU32Ex(0, (uint32_t)pTestList->size() - 1));
|
---|
454 | }
|
---|
455 |
|
---|
456 | return VINF_SUCCESS;
|
---|
457 | }
|
---|
458 |
|
---|
459 | /**
|
---|
460 | * Thread for replacing items in a shared list.
|
---|
461 | *
|
---|
462 | * @param hSelf The thread handle.
|
---|
463 | * @param pvUser The provided user data.
|
---|
464 | */
|
---|
465 | DECLCALLBACK(int) mttest5(RTTHREAD hSelf, void *pvUser)
|
---|
466 | {
|
---|
467 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
468 |
|
---|
469 | /* Try to replace C items from random places. */
|
---|
470 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
471 | {
|
---|
472 | /* Make sure there is at least one item in the list. */
|
---|
473 | while (pTestList->isEmpty()) {};
|
---|
474 | pTestList->replace(RTRandU32Ex(0, (uint32_t)pTestList->size() - 1), 0xFF00FF00);
|
---|
475 | }
|
---|
476 |
|
---|
477 | return VINF_SUCCESS;
|
---|
478 | }
|
---|
479 |
|
---|
480 | /**
|
---|
481 | * Thread for erasing items from a shared list.
|
---|
482 | *
|
---|
483 | * @param hSelf The thread handle.
|
---|
484 | * @param pvUser The provided user data.
|
---|
485 | */
|
---|
486 | DECLCALLBACK(int) mttest6(RTTHREAD hSelf, void *pvUser)
|
---|
487 | {
|
---|
488 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
489 |
|
---|
490 | /* Try to delete items from random places. */
|
---|
491 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
492 | {
|
---|
493 | /* Make sure there is at least one item in the list. */
|
---|
494 | while (pTestList->isEmpty()) {};
|
---|
495 | pTestList->removeAt(RTRandU32Ex(0, (uint32_t)pTestList->size() - 1));
|
---|
496 | }
|
---|
497 |
|
---|
498 | return VINF_SUCCESS;
|
---|
499 | }
|
---|
500 |
|
---|
501 | /**
|
---|
502 | * Does a multi-threading list test. Several list additions, reading, replacing
|
---|
503 | * and erasing are done simultaneous.
|
---|
504 | *
|
---|
505 | */
|
---|
506 | static void test2()
|
---|
507 | {
|
---|
508 | RTTestISubF("MT test with 6 threads (%u tests per thread).", MTTESTITEMS);
|
---|
509 |
|
---|
510 | RTTHREAD hThread1, hThread2, hThread3, hThread4, hThread5, hThread6;
|
---|
511 | int rc = VINF_SUCCESS;
|
---|
512 |
|
---|
513 | MTTESTLISTTYPE<MTTESTTYPE> testList;
|
---|
514 | rc = RTThreadCreate(&hThread1, &mttest1, &testList, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "mttest1");
|
---|
515 | AssertRC(rc);
|
---|
516 | rc = RTThreadCreate(&hThread2, &mttest2, &testList, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "mttest2");
|
---|
517 | AssertRC(rc);
|
---|
518 | rc = RTThreadCreate(&hThread3, &mttest3, &testList, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "mttest3");
|
---|
519 | AssertRC(rc);
|
---|
520 | rc = RTThreadCreate(&hThread4, &mttest4, &testList, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "mttest4");
|
---|
521 | AssertRC(rc);
|
---|
522 | rc = RTThreadCreate(&hThread5, &mttest5, &testList, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "mttest5");
|
---|
523 | AssertRC(rc);
|
---|
524 | rc = RTThreadCreate(&hThread6, &mttest6, &testList, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "mttest6");
|
---|
525 | AssertRC(rc);
|
---|
526 |
|
---|
527 | rc = RTThreadWait(hThread1, RT_INDEFINITE_WAIT, 0);
|
---|
528 | AssertRC(rc);
|
---|
529 | rc = RTThreadWait(hThread2, RT_INDEFINITE_WAIT, 0);
|
---|
530 | AssertRC(rc);
|
---|
531 | rc = RTThreadWait(hThread3, RT_INDEFINITE_WAIT, 0);
|
---|
532 | AssertRC(rc);
|
---|
533 | rc = RTThreadWait(hThread4, RT_INDEFINITE_WAIT, 0);
|
---|
534 | AssertRC(rc);
|
---|
535 | rc = RTThreadWait(hThread5, RT_INDEFINITE_WAIT, 0);
|
---|
536 | AssertRC(rc);
|
---|
537 | rc = RTThreadWait(hThread6, RT_INDEFINITE_WAIT, 0);
|
---|
538 | AssertRC(rc);
|
---|
539 |
|
---|
540 | RTTESTI_CHECK_RETV(testList.size() == MTTESTITEMS * 2);
|
---|
541 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
542 | {
|
---|
543 | uint32_t a = testList.at(i);
|
---|
544 | RTTESTI_CHECK(a == 0x0 || a == 0xFFFFFFFF || a == 0xF0F0F0F0 || a == 0xFF00FF00);
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 | int main()
|
---|
549 | {
|
---|
550 | /* How many integer test items should be created. */
|
---|
551 | static const size_t s_cTestCount = 1000;
|
---|
552 |
|
---|
553 | RTTEST hTest;
|
---|
554 | RTEXITCODE rcExit = RTTestInitAndCreate("tstIprtList", &hTest);
|
---|
555 | if (rcExit)
|
---|
556 | return rcExit;
|
---|
557 | RTTestBanner(hTest);
|
---|
558 |
|
---|
559 | /* Some host info. */
|
---|
560 | RTTestIPrintf(RTTESTLVL_ALWAYS, "sizeof(void*)=%d", sizeof(void*));
|
---|
561 |
|
---|
562 | /*
|
---|
563 | * The tests.
|
---|
564 | */
|
---|
565 |
|
---|
566 | /*
|
---|
567 | * Native types.
|
---|
568 | */
|
---|
569 | uint8_t au8TestInts[s_cTestCount];
|
---|
570 | for (size_t i = 0; i < RT_ELEMENTS(au8TestInts); ++i)
|
---|
571 | au8TestInts[i] = (uint8_t)RTRandU32Ex(1, UINT8_MAX);
|
---|
572 | test1<RTCList, uint8_t, uint8_t, uint8_t>("ST: Native type", au8TestInts, RT_ELEMENTS(au8TestInts));
|
---|
573 | test1<RTCMTList, uint8_t, uint8_t, uint8_t>("MT: Native type", au8TestInts, RT_ELEMENTS(au8TestInts));
|
---|
574 |
|
---|
575 | uint16_t au16TestInts[s_cTestCount];
|
---|
576 | for (size_t i = 0; i < RT_ELEMENTS(au16TestInts); ++i)
|
---|
577 | au16TestInts[i] = (uint16_t)RTRandU32Ex(1, UINT16_MAX);
|
---|
578 | test1<RTCList, uint16_t, uint16_t, uint16_t>("ST: Native type", au16TestInts, RT_ELEMENTS(au16TestInts));
|
---|
579 | test1<RTCMTList, uint16_t, uint16_t, uint16_t>("MT: Native type", au16TestInts, RT_ELEMENTS(au16TestInts));
|
---|
580 |
|
---|
581 | uint32_t au32TestInts[s_cTestCount];
|
---|
582 | for (size_t i = 0; i < RT_ELEMENTS(au32TestInts); ++i)
|
---|
583 | au32TestInts[i] = RTRandU32Ex(1, UINT32_MAX);
|
---|
584 | test1<RTCList, uint32_t, uint32_t, uint32_t>("ST: Native type", au32TestInts, RT_ELEMENTS(au32TestInts));
|
---|
585 | test1<RTCMTList, uint32_t, uint32_t, uint32_t>("MT: Native type", au32TestInts, RT_ELEMENTS(au32TestInts));
|
---|
586 |
|
---|
587 | /*
|
---|
588 | * Specialized type.
|
---|
589 | */
|
---|
590 | uint64_t au64TestInts[s_cTestCount];
|
---|
591 | for (size_t i = 0; i < RT_ELEMENTS(au64TestInts); ++i)
|
---|
592 | au64TestInts[i] = RTRandU64Ex(1, UINT64_MAX);
|
---|
593 | test1<RTCList, uint64_t, uint64_t, uint64_t>("ST: Specialized type", au64TestInts, RT_ELEMENTS(au64TestInts));
|
---|
594 | test1<RTCMTList, uint64_t, uint64_t, uint64_t>("MT: Specialized type", au64TestInts, RT_ELEMENTS(au64TestInts));
|
---|
595 |
|
---|
596 | /*
|
---|
597 | * Big size type (translate to internal pointer list).
|
---|
598 | */
|
---|
599 | test1<RTCList, RTCString, RTCString *, const char *>("ST: Class type", g_apszTestStrings, RT_ELEMENTS(g_apszTestStrings));
|
---|
600 | test1<RTCMTList, RTCString, RTCString *, const char *>("MT: Class type", g_apszTestStrings, RT_ELEMENTS(g_apszTestStrings));
|
---|
601 |
|
---|
602 | /*
|
---|
603 | * Multi-threading test.
|
---|
604 | */
|
---|
605 | test2();
|
---|
606 |
|
---|
607 | /*
|
---|
608 | * Summary.
|
---|
609 | */
|
---|
610 | return RTTestSummaryAndDestroy(hTest);
|
---|
611 | }
|
---|
612 |
|
---|