1 | /* $Id: tstIprtList.cpp 36532 2011-04-04 14:41:14Z 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 | * Copy operator
|
---|
221 | */
|
---|
222 | L<T1, T2> testList2(testList);
|
---|
223 |
|
---|
224 | /* Check that all is correctly appended. */
|
---|
225 | RTTESTI_CHECK_RETV(testList2.size() == cTestItems);
|
---|
226 | for (size_t i = 0; i < testList2.size(); ++i)
|
---|
227 | RTTESTI_CHECK(testList2.at(i) == paTestData[i]);
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * "=" operator
|
---|
231 | */
|
---|
232 | L<T1, T2> testList3;
|
---|
233 | testList3 = testList;
|
---|
234 |
|
---|
235 | /* Check that all is correctly appended. */
|
---|
236 | RTTESTI_CHECK_RETV(testList3.size() == cTestItems);
|
---|
237 | for (size_t i = 0; i < testList3.size(); ++i)
|
---|
238 | RTTESTI_CHECK(testList3.at(i) == paTestData[i]);
|
---|
239 |
|
---|
240 | /*
|
---|
241 | * Append list
|
---|
242 | */
|
---|
243 | testList2.append(testList3);
|
---|
244 |
|
---|
245 | /* Check that all is correctly appended. */
|
---|
246 | RTTESTI_CHECK_RETV(testList2.size() == cTestItems * 2);
|
---|
247 | for (size_t i = 0; i < testList2.size(); ++i)
|
---|
248 | RTTESTI_CHECK(testList2.at(i) == paTestData[i % cTestItems]);
|
---|
249 |
|
---|
250 | /*
|
---|
251 | * Prepend list
|
---|
252 | */
|
---|
253 | testList2.prepend(testList3);
|
---|
254 |
|
---|
255 | /* Check that all is correctly appended. */
|
---|
256 | RTTESTI_CHECK_RETV(testList2.size() == cTestItems * 3);
|
---|
257 | for (size_t i = 0; i < testList2.size(); ++i)
|
---|
258 | RTTESTI_CHECK(testList2.at(i) == paTestData[i % cTestItems]);
|
---|
259 |
|
---|
260 | /*
|
---|
261 | * "value" method
|
---|
262 | */
|
---|
263 | for (size_t i = 0; i < testList2.size(); ++i)
|
---|
264 | RTTESTI_CHECK(testList2.value(i) == paTestData[i % cTestItems]);
|
---|
265 | for (size_t i = 0; i < testList2.size(); ++i)
|
---|
266 | RTTESTI_CHECK(testList2.value(i, T1()) == paTestData[i % cTestItems]);
|
---|
267 | RTTESTI_CHECK(testList2.value(testList2.size() + 1) == T1()); /* Invalid index */
|
---|
268 | RTTESTI_CHECK(testList2.value(testList2.size() + 1, T1()) == T1()); /* Invalid index */
|
---|
269 |
|
---|
270 | /*
|
---|
271 | * operator[] (reading)
|
---|
272 | */
|
---|
273 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
274 | RTTESTI_CHECK(testList[i] == paTestData[i]);
|
---|
275 |
|
---|
276 | /*
|
---|
277 | * operator[] (writing)
|
---|
278 | *
|
---|
279 | * Replace with inverted array.
|
---|
280 | */
|
---|
281 | for (size_t i = 0; i < cTestItems; ++i)
|
---|
282 | testList[i] = paTestData[cTestItems - i - 1];
|
---|
283 | RTTESTI_CHECK_RETV(testList.size() == cTestItems);
|
---|
284 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
285 | RTTESTI_CHECK(testList[i] == paTestData[cTestItems - i - 1]);
|
---|
286 |
|
---|
287 | /*
|
---|
288 | * Replace
|
---|
289 | *
|
---|
290 | * Replace with inverted array (Must be original array when finished).
|
---|
291 | */
|
---|
292 | for (size_t i = 0; i < cTestItems; ++i)
|
---|
293 | testList.replace(i, paTestData[i]);
|
---|
294 | RTTESTI_CHECK_RETV(testList.size() == cTestItems);
|
---|
295 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
296 | RTTESTI_CHECK(testList[i] == paTestData[i]);
|
---|
297 |
|
---|
298 | /*
|
---|
299 | * Removing
|
---|
300 | */
|
---|
301 |
|
---|
302 | /* Remove Range */
|
---|
303 | testList2.removeRange(cTestItems, cTestItems * 2);
|
---|
304 | RTTESTI_CHECK_RETV(testList2.size() == cTestItems * 2);
|
---|
305 | for (size_t i = 0; i < testList2.size(); ++i)
|
---|
306 | RTTESTI_CHECK(testList2.at(i) == paTestData[i % cTestItems]);
|
---|
307 |
|
---|
308 | /* Remove the first half (reverse) */
|
---|
309 | size_t cRemoved = 1;
|
---|
310 | for (size_t i = cTestItems / 2; i > 0; --i, ++cRemoved)
|
---|
311 | {
|
---|
312 | testList.removeAt(i - 1);
|
---|
313 | RTTESTI_CHECK_RETV(testList.size() == cTestItems - cRemoved);
|
---|
314 | }
|
---|
315 | RTTESTI_CHECK_RETV(testList.size() == cTestItems / 2);
|
---|
316 |
|
---|
317 | /* Check that all is correctly removed and only the second part of the list
|
---|
318 | * is still there. */
|
---|
319 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
320 | RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]);
|
---|
321 |
|
---|
322 | /*
|
---|
323 | * setCapacitiy
|
---|
324 | */
|
---|
325 | testList.setCapacity(cTestItems * 5);
|
---|
326 | RTTESTI_CHECK(testList.capacity() == cTestItems * 5);
|
---|
327 | RTTESTI_CHECK_RETV(testList.size() == cTestItems / 2);
|
---|
328 |
|
---|
329 | /* As the capacity just increased, we should still have all entries from
|
---|
330 | * the previous list. */
|
---|
331 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
332 | RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]);
|
---|
333 |
|
---|
334 | /* Decrease the capacity so it will be smaller than the count of items in
|
---|
335 | * the list. The list should be shrink automatically, but the remaining
|
---|
336 | * items should be still valid. */
|
---|
337 | testList.setCapacity(cTestItems / 4);
|
---|
338 | RTTESTI_CHECK_RETV(testList.size() == cTestItems / 4);
|
---|
339 | RTTESTI_CHECK(testList.capacity() == cTestItems / 4);
|
---|
340 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
341 | RTTESTI_CHECK(testList.at(i) == paTestData[cTestItems / 2 + i]);
|
---|
342 |
|
---|
343 | /* Clear all */
|
---|
344 | testList.clear();
|
---|
345 | RTTESTI_CHECK_RETV(testList.isEmpty());
|
---|
346 | RTTESTI_CHECK_RETV(testList.size() == 0);
|
---|
347 | RTTESTI_CHECK(testList.capacity() == defCap);
|
---|
348 | }
|
---|
349 |
|
---|
350 | /* define RTCList here to see what happens without MT support ;)
|
---|
351 | * (valgrind is the preferred tool to check). */
|
---|
352 | #define MTTESTLISTTYPE RTCMTList
|
---|
353 | #define MTTESTTYPE uint32_t
|
---|
354 | #define MTTESTITEMS 1000
|
---|
355 |
|
---|
356 | /**
|
---|
357 | * Thread for prepending items to a shared list.
|
---|
358 | *
|
---|
359 | * @param hSelf The thread handle.
|
---|
360 | * @param pvUser The provided user data.
|
---|
361 | */
|
---|
362 | DECLCALLBACK(int) mttest1(RTTHREAD hSelf, void *pvUser)
|
---|
363 | {
|
---|
364 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
365 |
|
---|
366 | /* Prepend new items at the start of the list. */
|
---|
367 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
368 | pTestList->prepend(0x0);
|
---|
369 |
|
---|
370 | return VINF_SUCCESS;
|
---|
371 | }
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * Thread for appending items to a shared list.
|
---|
375 | *
|
---|
376 | * @param hSelf The thread handle.
|
---|
377 | * @param pvUser The provided user data.
|
---|
378 | */
|
---|
379 | DECLCALLBACK(int) mttest2(RTTHREAD hSelf, void *pvUser)
|
---|
380 | {
|
---|
381 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
382 |
|
---|
383 | /* Append new items at the end of the list. */
|
---|
384 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
385 | pTestList->append(0xFFFFFFFF);
|
---|
386 |
|
---|
387 | return VINF_SUCCESS;
|
---|
388 | }
|
---|
389 |
|
---|
390 | /**
|
---|
391 | * Thread for inserting items to a shared list.
|
---|
392 | *
|
---|
393 | * @param hSelf The thread handle.
|
---|
394 | * @param pvUser The provided user data.
|
---|
395 | */
|
---|
396 | DECLCALLBACK(int) mttest3(RTTHREAD hSelf, void *pvUser)
|
---|
397 | {
|
---|
398 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
399 |
|
---|
400 | /* Insert new items in the middle of the list. */
|
---|
401 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
402 | pTestList->insert(pTestList->size() / 2, 0xF0F0F0F0);
|
---|
403 |
|
---|
404 | return VINF_SUCCESS;
|
---|
405 | }
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * Thread for reading items from a shared list.
|
---|
409 | *
|
---|
410 | * @param hSelf The thread handle.
|
---|
411 | * @param pvUser The provided user data.
|
---|
412 | */
|
---|
413 | DECLCALLBACK(int) mttest4(RTTHREAD hSelf, void *pvUser)
|
---|
414 | {
|
---|
415 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
416 |
|
---|
417 | MTTESTTYPE a;
|
---|
418 | /* Try to read C items from random places. */
|
---|
419 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
420 | {
|
---|
421 | /* Make sure there is at least one item in the list. */
|
---|
422 | while (pTestList->isEmpty()) {};
|
---|
423 | a = pTestList->at(RTRandU32Ex(0, (uint32_t)pTestList->size() - 1));
|
---|
424 | }
|
---|
425 |
|
---|
426 | return VINF_SUCCESS;
|
---|
427 | }
|
---|
428 |
|
---|
429 | /**
|
---|
430 | * Thread for replacing items in a shared list.
|
---|
431 | *
|
---|
432 | * @param hSelf The thread handle.
|
---|
433 | * @param pvUser The provided user data.
|
---|
434 | */
|
---|
435 | DECLCALLBACK(int) mttest5(RTTHREAD hSelf, void *pvUser)
|
---|
436 | {
|
---|
437 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
438 |
|
---|
439 | /* Try to replace C items from random places. */
|
---|
440 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
441 | {
|
---|
442 | /* Make sure there is at least one item in the list. */
|
---|
443 | while (pTestList->isEmpty()) {};
|
---|
444 | pTestList->replace(RTRandU32Ex(0, (uint32_t)pTestList->size() - 1), 0xFF00FF00);
|
---|
445 | }
|
---|
446 |
|
---|
447 | return VINF_SUCCESS;
|
---|
448 | }
|
---|
449 |
|
---|
450 | /**
|
---|
451 | * Thread for erasing items from a shared list.
|
---|
452 | *
|
---|
453 | * @param hSelf The thread handle.
|
---|
454 | * @param pvUser The provided user data.
|
---|
455 | */
|
---|
456 | DECLCALLBACK(int) mttest6(RTTHREAD hSelf, void *pvUser)
|
---|
457 | {
|
---|
458 | MTTESTLISTTYPE<MTTESTTYPE> *pTestList = (MTTESTLISTTYPE<MTTESTTYPE> *)pvUser;
|
---|
459 |
|
---|
460 | /* Try to delete items from random places. */
|
---|
461 | for (size_t i = 0; i < MTTESTITEMS; ++i)
|
---|
462 | {
|
---|
463 | /* Make sure there is at least one item in the list. */
|
---|
464 | while (pTestList->isEmpty()) {};
|
---|
465 | pTestList->removeAt(RTRandU32Ex(0, (uint32_t)pTestList->size() - 1));
|
---|
466 | }
|
---|
467 |
|
---|
468 | return VINF_SUCCESS;
|
---|
469 | }
|
---|
470 |
|
---|
471 | /**
|
---|
472 | * Does a multi-threading list test. Several list additions, reading, replacing
|
---|
473 | * and erasing are done simultaneous.
|
---|
474 | *
|
---|
475 | */
|
---|
476 | static void test2()
|
---|
477 | {
|
---|
478 | RTTestISubF("MT test with 6 threads (%u tests per thread).", MTTESTITEMS);
|
---|
479 |
|
---|
480 | RTTHREAD hThread1, hThread2, hThread3, hThread4, hThread5, hThread6;
|
---|
481 | int rc = VINF_SUCCESS;
|
---|
482 |
|
---|
483 | MTTESTLISTTYPE<MTTESTTYPE> testList;
|
---|
484 | rc = RTThreadCreate(&hThread1, &mttest1, &testList, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "mttest1");
|
---|
485 | AssertRC(rc);
|
---|
486 | rc = RTThreadCreate(&hThread2, &mttest2, &testList, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "mttest2");
|
---|
487 | AssertRC(rc);
|
---|
488 | rc = RTThreadCreate(&hThread3, &mttest3, &testList, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "mttest3");
|
---|
489 | AssertRC(rc);
|
---|
490 | rc = RTThreadCreate(&hThread4, &mttest4, &testList, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "mttest4");
|
---|
491 | AssertRC(rc);
|
---|
492 | rc = RTThreadCreate(&hThread5, &mttest5, &testList, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "mttest5");
|
---|
493 | AssertRC(rc);
|
---|
494 | rc = RTThreadCreate(&hThread6, &mttest6, &testList, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "mttest6");
|
---|
495 | AssertRC(rc);
|
---|
496 |
|
---|
497 | rc = RTThreadWait(hThread1, RT_INDEFINITE_WAIT, 0);
|
---|
498 | AssertRC(rc);
|
---|
499 | rc = RTThreadWait(hThread2, RT_INDEFINITE_WAIT, 0);
|
---|
500 | AssertRC(rc);
|
---|
501 | rc = RTThreadWait(hThread3, RT_INDEFINITE_WAIT, 0);
|
---|
502 | AssertRC(rc);
|
---|
503 | rc = RTThreadWait(hThread4, RT_INDEFINITE_WAIT, 0);
|
---|
504 | AssertRC(rc);
|
---|
505 | rc = RTThreadWait(hThread5, RT_INDEFINITE_WAIT, 0);
|
---|
506 | AssertRC(rc);
|
---|
507 | rc = RTThreadWait(hThread6, RT_INDEFINITE_WAIT, 0);
|
---|
508 | AssertRC(rc);
|
---|
509 |
|
---|
510 | RTTESTI_CHECK_RETV(testList.size() == MTTESTITEMS * 2);
|
---|
511 | for (size_t i = 0; i < testList.size(); ++i)
|
---|
512 | {
|
---|
513 | uint32_t a = testList.at(i);
|
---|
514 | RTTESTI_CHECK(a == 0x0 || a == 0xFFFFFFFF || a == 0xF0F0F0F0 || a == 0xFF00FF00);
|
---|
515 | }
|
---|
516 | }
|
---|
517 |
|
---|
518 | int main()
|
---|
519 | {
|
---|
520 | /* How many integer test items should be created. */
|
---|
521 | static const size_t s_cTestCount = 1000;
|
---|
522 |
|
---|
523 | RTTEST hTest;
|
---|
524 | RTEXITCODE rcExit = RTTestInitAndCreate("tstIprtList", &hTest);
|
---|
525 | if (rcExit)
|
---|
526 | return rcExit;
|
---|
527 | RTTestBanner(hTest);
|
---|
528 |
|
---|
529 | /* Some host info. */
|
---|
530 | RTTestIPrintf(RTTESTLVL_ALWAYS, "sizeof(void*)=%d", sizeof(void*));
|
---|
531 |
|
---|
532 | /*
|
---|
533 | * The tests.
|
---|
534 | */
|
---|
535 |
|
---|
536 | /*
|
---|
537 | * Native types.
|
---|
538 | */
|
---|
539 | uint8_t au8TestInts[s_cTestCount];
|
---|
540 | for (size_t i = 0; i < RT_ELEMENTS(au8TestInts); ++i)
|
---|
541 | au8TestInts[i] = (uint8_t)RTRandU32Ex(0, UINT8_MAX);
|
---|
542 | test1<RTCList, uint8_t, uint8_t, uint8_t>("ST: Native type", au8TestInts, RT_ELEMENTS(au8TestInts));
|
---|
543 | test1<RTCMTList, uint8_t, uint8_t, uint8_t>("MT: Native type", au8TestInts, RT_ELEMENTS(au8TestInts));
|
---|
544 |
|
---|
545 | uint16_t au16TestInts[s_cTestCount];
|
---|
546 | for (size_t i = 0; i < RT_ELEMENTS(au16TestInts); ++i)
|
---|
547 | au16TestInts[i] = (uint16_t)RTRandU32Ex(0, UINT16_MAX);
|
---|
548 | test1<RTCList, uint16_t, uint16_t, uint16_t>("ST: Native type", au16TestInts, RT_ELEMENTS(au16TestInts));
|
---|
549 | test1<RTCMTList, uint16_t, uint16_t, uint16_t>("MT: Native type", au16TestInts, RT_ELEMENTS(au16TestInts));
|
---|
550 |
|
---|
551 | uint32_t au32TestInts[s_cTestCount];
|
---|
552 | for (size_t i = 0; i < RT_ELEMENTS(au32TestInts); ++i)
|
---|
553 | au32TestInts[i] = RTRandU32();
|
---|
554 | test1<RTCList, uint32_t, uint32_t, uint32_t>("ST: Native type", au32TestInts, RT_ELEMENTS(au32TestInts));
|
---|
555 | test1<RTCMTList, uint32_t, uint32_t, uint32_t>("MT: Native type", au32TestInts, RT_ELEMENTS(au32TestInts));
|
---|
556 |
|
---|
557 | /*
|
---|
558 | * Specialized type.
|
---|
559 | */
|
---|
560 | uint64_t au64TestInts[s_cTestCount];
|
---|
561 | for (size_t i = 0; i < RT_ELEMENTS(au64TestInts); ++i)
|
---|
562 | au64TestInts[i] = RTRandU64();
|
---|
563 | test1<RTCList, uint64_t, uint64_t, uint64_t>("ST: Specialized type", au64TestInts, RT_ELEMENTS(au64TestInts));
|
---|
564 | test1<RTCMTList, uint64_t, uint64_t, uint64_t>("MT: Specialized type", au64TestInts, RT_ELEMENTS(au64TestInts));
|
---|
565 |
|
---|
566 | /*
|
---|
567 | * Big size type (translate to internal pointer list).
|
---|
568 | */
|
---|
569 | test1<RTCList, RTCString, RTCString *, const char *>("ST: Class type", g_apszTestStrings, RT_ELEMENTS(g_apszTestStrings));
|
---|
570 | test1<RTCMTList, RTCString, RTCString *, const char *>("MT: Class type", g_apszTestStrings, RT_ELEMENTS(g_apszTestStrings));
|
---|
571 |
|
---|
572 | /*
|
---|
573 | * Multi-threading test.
|
---|
574 | */
|
---|
575 | test2();
|
---|
576 |
|
---|
577 | /*
|
---|
578 | * Summary.
|
---|
579 | */
|
---|
580 | return RTTestSummaryAndDestroy(hTest);
|
---|
581 | }
|
---|
582 |
|
---|