VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTBitOperations.cpp@ 33867

Last change on this file since 33867 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.0 KB
Line 
1/* $Id: tstRTBitOperations.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Inlined Bit Operations.
4 */
5
6/*
7 * Copyright (C) 2006-2009 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/asm.h>
31
32#include <iprt/initterm.h>
33#include <iprt/stream.h>
34#include <iprt/string.h>
35#include <iprt/test.h>
36
37
38/*
39 * Test 2 - ID allocation using a bitmap.
40 */
41
42#define NIL_TEST2_ID 0
43#define TEST2_ID_LAST ((RT_BIT_32(28) - 1) >> 8)
44
45struct TestMap2
46{
47 uint32_t idNil;
48 uint32_t idLast;
49 uint32_t idChunkPrev;
50 uint32_t bmChunkId[(TEST2_ID_LAST + 1 + 31) / 32];
51};
52
53static uint32_t test2AllocId(struct TestMap2 *p2)
54{
55 /*
56 * Scan sequentially from the last one + 1.
57 */
58 int32_t idChunk = ++p2->idChunkPrev;
59 if ( (uint32_t)idChunk < TEST2_ID_LAST
60 && idChunk > NIL_TEST2_ID)
61 {
62 idChunk = ASMBitNextClear(&p2->bmChunkId[0], TEST2_ID_LAST + 1, idChunk);
63 if (idChunk > NIL_TEST2_ID)
64 {
65 if (ASMAtomicBitTestAndSet(&p2->bmChunkId[0], idChunk))
66 {
67 RTTestFailed(NIL_RTTEST, "line %d: idChunk=%#x", __LINE__, idChunk);
68 return NIL_TEST2_ID;
69 }
70 return p2->idChunkPrev = idChunk;
71 }
72 }
73
74 /*
75 * Ok, scan from the start.
76 */
77 idChunk = ASMBitFirstClear(&p2->bmChunkId[0], TEST2_ID_LAST + 1);
78 if (idChunk <= NIL_TEST2_ID)
79 {
80 RTTestFailed(NIL_RTTEST, "line %d: idChunk=%#x", __LINE__, idChunk);
81 return NIL_TEST2_ID;
82 }
83 if (ASMAtomicBitTestAndSet(&p2->bmChunkId[0], idChunk))
84 {
85 RTTestFailed(NIL_RTTEST, "line %d: idChunk=%#x", __LINE__, idChunk);
86 return NIL_TEST2_ID;
87 }
88
89 return p2->idChunkPrev = idChunk;
90}
91
92
93static void test2(RTTEST hTest)
94{
95 struct TestMap2 *p2 = (struct TestMap2 *)RTTestGuardedAllocTail(hTest, sizeof(TestMap2));
96 p2->idNil = NIL_TEST2_ID;
97 p2->idLast = TEST2_ID_LAST;
98
99 /* Some simple tests first. */
100 RT_ZERO(p2->bmChunkId);
101 RTTEST_CHECK(hTest, ASMBitFirstSet(&p2->bmChunkId[0], TEST2_ID_LAST + 1) == -1);
102 for (uint32_t iBit = 0; iBit <= TEST2_ID_LAST; iBit++)
103 RTTEST_CHECK(hTest, !ASMBitTest(&p2->bmChunkId[0], iBit));
104
105 memset(&p2->bmChunkId[0], 0xff, sizeof(p2->bmChunkId));
106 RTTEST_CHECK(hTest, ASMBitFirstClear(&p2->bmChunkId[0], TEST2_ID_LAST + 1) == -1);
107 for (uint32_t iBit = 0; iBit <= TEST2_ID_LAST; iBit++)
108 RTTEST_CHECK(hTest, ASMBitTest(&p2->bmChunkId[0], iBit));
109
110 /* The real test. */
111 p2->idChunkPrev = 0;
112 RT_ZERO(p2->bmChunkId);
113 ASMBitSet(p2->bmChunkId, NIL_TEST2_ID);
114 uint32_t cLeft = TEST2_ID_LAST;
115 while (cLeft-- > 0)
116 test2AllocId(p2);
117
118 RTTEST_CHECK(hTest, ASMBitFirstClear(&p2->bmChunkId[0], TEST2_ID_LAST + 1) == -1);
119}
120
121
122int main()
123{
124 /*
125 * Init the runtime and stuff.
126 */
127 RTTEST hTest;
128 int rc = RTTestInitAndCreate("tstRTBitOperations", &hTest);
129 if (rc)
130 return rc;
131 RTTestBanner(hTest);
132
133 int rcRet = 0;
134 int i;
135 int j;
136 int k;
137
138 /*
139 * Tests
140 */
141 struct TestMap
142 {
143 uint32_t au32[4];
144 };
145#if 0
146 struct TestMap sTest;
147 struct TestMap *p = &sTest;
148#else
149 struct TestMap *p = (struct TestMap *)RTTestGuardedAllocTail(hTest, sizeof(*p));
150#endif
151#define DUMP() RTTestPrintf(hTest, RTTESTLVL_INFO, "au32={%08x,%08x,%08x,%08x}", p->au32[0], p->au32[1], p->au32[2], p->au32[3])
152#define CHECK(expr) do { if (!(expr)) { RTTestFailed(hTest, "line %d: %s", __LINE__, #expr); DUMP(); } CHECK_GUARD(s); } while (0)
153#define CHECK_BIT(expr, b1) do { if (!(expr)) { RTTestFailed(hTest, "line %d, b1=%d: %s", __LINE__, b1, #expr); } CHECK_GUARD(s); } while (0)
154#define CHECK_BIT2(expr, b1, b2) do { if (!(expr)) { RTTestFailed(hTest, "line %d, b1=%d b2=%d: %s", __LINE__, b1, b2, #expr); } CHECK_GUARD(s); } while (0)
155#define CHECK_BIT3(expr, b1, b2, b3) do { if (!(expr)) { RTTestFailed(hTest, "line %d, b1=%d b2=%d b3=%d: %s", __LINE__, b1, b2, b3, #expr); } CHECK_GUARD(s); } while (0)
156
157#define GUARD_MAP(p) do { } while (0)
158#define CHECK_GUARD(p) do { } while (0)
159#define MAP_CLEAR(p) do { RT_ZERO(*(p)); GUARD_MAP(p); } while (0)
160#define MAP_SET(p) do { memset(p, 0xff, sizeof(*(p))); GUARD_MAP(p); } while (0)
161
162 /* self check. */
163 MAP_CLEAR(p);
164 CHECK_GUARD(p);
165
166 /* set */
167 MAP_CLEAR(p);
168 ASMBitSet(&p->au32[0], 0);
169 ASMBitSet(&p->au32[0], 31);
170 ASMBitSet(&p->au32[0], 65);
171 CHECK(p->au32[0] == 0x80000001U);
172 CHECK(p->au32[2] == 0x00000002U);
173 CHECK(ASMBitTestAndSet(&p->au32[0], 0) && p->au32[0] == 0x80000001U);
174 CHECK(!ASMBitTestAndSet(&p->au32[0], 16) && p->au32[0] == 0x80010001U);
175 CHECK(ASMBitTestAndSet(&p->au32[0], 16) && p->au32[0] == 0x80010001U);
176 CHECK(!ASMBitTestAndSet(&p->au32[0], 80) && p->au32[2] == 0x00010002U);
177
178 MAP_CLEAR(p);
179 ASMAtomicBitSet(&p->au32[0], 0);
180 ASMAtomicBitSet(&p->au32[0], 30);
181 ASMAtomicBitSet(&p->au32[0], 64);
182 CHECK(p->au32[0] == 0x40000001U);
183 CHECK(p->au32[2] == 0x00000001U);
184 CHECK(ASMAtomicBitTestAndSet(&p->au32[0], 0) && p->au32[0] == 0x40000001U);
185 CHECK(!ASMAtomicBitTestAndSet(&p->au32[0], 16) && p->au32[0] == 0x40010001U);
186 CHECK(ASMAtomicBitTestAndSet(&p->au32[0], 16) && p->au32[0] == 0x40010001U);
187 CHECK(!ASMAtomicBitTestAndSet(&p->au32[0], 80) && p->au32[2] == 0x00010001U);
188
189 /* clear */
190 MAP_SET(p);
191 ASMBitClear(&p->au32[0], 0);
192 ASMBitClear(&p->au32[0], 31);
193 ASMBitClear(&p->au32[0], 65);
194 CHECK(p->au32[0] == ~0x80000001U);
195 CHECK(p->au32[2] == ~0x00000002U);
196 CHECK(!ASMBitTestAndClear(&p->au32[0], 0) && p->au32[0] == ~0x80000001U);
197 CHECK(ASMBitTestAndClear(&p->au32[0], 16) && p->au32[0] == ~0x80010001U);
198 CHECK(!ASMBitTestAndClear(&p->au32[0], 16) && p->au32[0] == ~0x80010001U);
199 CHECK(ASMBitTestAndClear(&p->au32[0], 80) && p->au32[2] == ~0x00010002U);
200
201 MAP_SET(p);
202 ASMAtomicBitClear(&p->au32[0], 0);
203 ASMAtomicBitClear(&p->au32[0], 30);
204 ASMAtomicBitClear(&p->au32[0], 64);
205 CHECK(p->au32[0] == ~0x40000001U);
206 CHECK(p->au32[2] == ~0x00000001U);
207 CHECK(!ASMAtomicBitTestAndClear(&p->au32[0], 0) && p->au32[0] == ~0x40000001U);
208 CHECK(ASMAtomicBitTestAndClear(&p->au32[0], 16) && p->au32[0] == ~0x40010001U);
209 CHECK(!ASMAtomicBitTestAndClear(&p->au32[0], 16) && p->au32[0] == ~0x40010001U);
210 CHECK(ASMAtomicBitTestAndClear(&p->au32[0], 80) && p->au32[2] == ~0x00010001U);
211
212 /* toggle */
213 MAP_SET(p);
214 ASMBitToggle(&p->au32[0], 0);
215 ASMBitToggle(&p->au32[0], 31);
216 ASMBitToggle(&p->au32[0], 65);
217 ASMBitToggle(&p->au32[0], 47);
218 ASMBitToggle(&p->au32[0], 47);
219 CHECK(p->au32[0] == ~0x80000001U);
220 CHECK(p->au32[2] == ~0x00000002U);
221 CHECK(!ASMBitTestAndToggle(&p->au32[0], 0) && p->au32[0] == ~0x80000000U);
222 CHECK(ASMBitTestAndToggle(&p->au32[0], 0) && p->au32[0] == ~0x80000001U);
223 CHECK(ASMBitTestAndToggle(&p->au32[0], 16) && p->au32[0] == ~0x80010001U);
224 CHECK(!ASMBitTestAndToggle(&p->au32[0], 16) && p->au32[0] == ~0x80000001U);
225 CHECK(ASMBitTestAndToggle(&p->au32[0], 80) && p->au32[2] == ~0x00010002U);
226
227 MAP_SET(p);
228 ASMAtomicBitToggle(&p->au32[0], 0);
229 ASMAtomicBitToggle(&p->au32[0], 30);
230 ASMAtomicBitToggle(&p->au32[0], 64);
231 ASMAtomicBitToggle(&p->au32[0], 47);
232 ASMAtomicBitToggle(&p->au32[0], 47);
233 CHECK(p->au32[0] == ~0x40000001U);
234 CHECK(p->au32[2] == ~0x00000001U);
235 CHECK(!ASMAtomicBitTestAndToggle(&p->au32[0], 0) && p->au32[0] == ~0x40000000U);
236 CHECK(ASMAtomicBitTestAndToggle(&p->au32[0], 0) && p->au32[0] == ~0x40000001U);
237 CHECK(ASMAtomicBitTestAndToggle(&p->au32[0], 16) && p->au32[0] == ~0x40010001U);
238 CHECK(!ASMAtomicBitTestAndToggle(&p->au32[0], 16) && p->au32[0] == ~0x40000001U);
239 CHECK(ASMAtomicBitTestAndToggle(&p->au32[0], 80) && p->au32[2] == ~0x00010001U);
240
241 /* test bit. */
242 for (i = 0; i < 128; i++)
243 {
244 MAP_SET(p);
245 CHECK_BIT(ASMBitTest(&p->au32[0], i), i);
246 ASMBitToggle(&p->au32[0], i);
247 CHECK_BIT(!ASMBitTest(&p->au32[0], i), i);
248 CHECK_BIT(!ASMBitTestAndToggle(&p->au32[0], i), i);
249 CHECK_BIT(ASMBitTest(&p->au32[0], i), i);
250 CHECK_BIT(ASMBitTestAndToggle(&p->au32[0], i), i);
251 CHECK_BIT(!ASMBitTest(&p->au32[0], i), i);
252
253 MAP_SET(p);
254 CHECK_BIT(ASMBitTest(&p->au32[0], i), i);
255 ASMAtomicBitToggle(&p->au32[0], i);
256 CHECK_BIT(!ASMBitTest(&p->au32[0], i), i);
257 CHECK_BIT(!ASMAtomicBitTestAndToggle(&p->au32[0], i), i);
258 CHECK_BIT(ASMBitTest(&p->au32[0], i), i);
259 CHECK_BIT(ASMAtomicBitTestAndToggle(&p->au32[0], i), i);
260 CHECK_BIT(!ASMBitTest(&p->au32[0], i), i);
261 }
262
263 /* bit searching */
264 MAP_SET(p);
265 CHECK(ASMBitFirstClear(&p->au32[0], sizeof(p->au32) * 8) == -1);
266 CHECK(ASMBitFirstSet(&p->au32[0], sizeof(p->au32) * 8) == 0);
267
268 ASMBitClear(&p->au32[0], 1);
269 CHECK(ASMBitFirstClear(&p->au32[0], sizeof(p->au32) * 8) == 1);
270 CHECK(ASMBitFirstSet(&p->au32[0], sizeof(p->au32) * 8) == 0);
271
272 MAP_SET(p);
273 ASMBitClear(&p->au32[0], 95);
274 CHECK(ASMBitFirstClear(&p->au32[0], sizeof(p->au32) * 8) == 95);
275 CHECK(ASMBitFirstSet(&p->au32[0], sizeof(p->au32) * 8) == 0);
276
277 MAP_SET(p);
278 ASMBitClear(&p->au32[0], 127);
279 CHECK(ASMBitFirstClear(&p->au32[0], sizeof(p->au32) * 8) == 127);
280 CHECK(ASMBitFirstSet(&p->au32[0], sizeof(p->au32) * 8) == 0);
281 CHECK(ASMBitNextSet(&p->au32[0], sizeof(p->au32) * 8, 0) == 1);
282 CHECK(ASMBitNextSet(&p->au32[0], sizeof(p->au32) * 8, 1) == 2);
283 CHECK(ASMBitNextSet(&p->au32[0], sizeof(p->au32) * 8, 2) == 3);
284
285
286 MAP_SET(p);
287 CHECK(ASMBitNextClear(&p->au32[0], sizeof(p->au32) * 8, 0) == -1);
288 ASMBitClear(&p->au32[0], 32);
289 CHECK(ASMBitNextClear(&p->au32[0], sizeof(p->au32) * 8, 32) == -1);
290 ASMBitClear(&p->au32[0], 88);
291 CHECK(ASMBitNextClear(&p->au32[0], sizeof(p->au32) * 8, 57) == 88);
292
293 MAP_SET(p);
294 ASMBitClear(&p->au32[0], 31);
295 ASMBitClear(&p->au32[0], 57);
296 ASMBitClear(&p->au32[0], 88);
297 ASMBitClear(&p->au32[0], 101);
298 ASMBitClear(&p->au32[0], 126);
299 ASMBitClear(&p->au32[0], 127);
300 CHECK(ASMBitFirstClear(&p->au32[0], sizeof(p->au32) * 8) == 31);
301 CHECK(ASMBitNextClear(&p->au32[0], sizeof(p->au32) * 8, 31) == 57);
302 CHECK(ASMBitNextClear(&p->au32[0], sizeof(p->au32) * 8, 57) == 88);
303 CHECK(ASMBitNextClear(&p->au32[0], sizeof(p->au32) * 8, 88) == 101);
304 CHECK(ASMBitNextClear(&p->au32[0], sizeof(p->au32) * 8, 101) == 126);
305 CHECK(ASMBitNextClear(&p->au32[0], sizeof(p->au32) * 8, 126) == 127);
306 CHECK(ASMBitNextClear(&p->au32[0], sizeof(p->au32) * 8, 127) == -1);
307
308 CHECK(ASMBitNextSet(&p->au32[0], sizeof(p->au32) * 8, 29) == 30);
309 CHECK(ASMBitNextSet(&p->au32[0], sizeof(p->au32) * 8, 30) == 32);
310
311 MAP_CLEAR(p);
312 for (i = 1; i < 128; i++)
313 CHECK_BIT(ASMBitNextClear(&p->au32[0], sizeof(p->au32) * 8, i - 1) == i, i);
314 for (i = 0; i < 128; i++)
315 {
316 MAP_SET(p);
317 ASMBitClear(&p->au32[0], i);
318 CHECK_BIT(ASMBitFirstClear(&p->au32[0], sizeof(p->au32) * 8) == i, i);
319 for (j = 0; j < i; j++)
320 CHECK_BIT(ASMBitNextClear(&p->au32[0], sizeof(p->au32) * 8, j) == i, i);
321 for (j = i; j < 128; j++)
322 CHECK_BIT(ASMBitNextClear(&p->au32[0], sizeof(p->au32) * 8, j) == -1, i);
323 }
324
325 /* clear range. */
326 MAP_SET(p);
327 ASMBitClearRange(&p->au32, 0, 128);
328 CHECK(!p->au32[0] && !p->au32[1] && !p->au32[2] && !p->au32[3]);
329 for (i = 0; i < 128; i++)
330 {
331 for (j = i + 1; j <= 128; j++)
332 {
333 MAP_SET(p);
334 ASMBitClearRange(&p->au32, i, j);
335 for (k = 0; k < i; k++)
336 CHECK_BIT3(ASMBitTest(&p->au32[0], k), i, j, k);
337 for (k = i; k < j; k++)
338 CHECK_BIT3(!ASMBitTest(&p->au32[0], k), i, j, k);
339 for (k = j; k < 128; k++)
340 CHECK_BIT3(ASMBitTest(&p->au32[0], k), i, j, k);
341 }
342 }
343
344 /* searching for set bits. */
345 MAP_CLEAR(p);
346 CHECK(ASMBitFirstSet(&p->au32[0], sizeof(p->au32) * 8) == -1);
347
348 ASMBitSet(&p->au32[0], 65);
349 CHECK(ASMBitFirstSet(&p->au32[0], sizeof(p->au32) * 8) == 65);
350 CHECK(ASMBitNextSet(&p->au32[0], sizeof(p->au32) * 8, 65) == -1);
351 for (i = 0; i < 65; i++)
352 CHECK(ASMBitNextSet(&p->au32[0], sizeof(p->au32) * 8, i) == 65);
353 for (i = 65; i < 128; i++)
354 CHECK(ASMBitNextSet(&p->au32[0], sizeof(p->au32) * 8, i) == -1);
355
356 ASMBitSet(&p->au32[0], 17);
357 CHECK(ASMBitFirstSet(&p->au32[0], sizeof(p->au32) * 8) == 17);
358 CHECK(ASMBitNextSet(&p->au32[0], sizeof(p->au32) * 8, 17) == 65);
359 for (i = 0; i < 16; i++)
360 CHECK(ASMBitNextSet(&p->au32[0], sizeof(p->au32) * 8, i) == 17);
361 for (i = 17; i < 65; i++)
362 CHECK(ASMBitNextSet(&p->au32[0], sizeof(p->au32) * 8, i) == 65);
363
364 MAP_SET(p);
365 for (i = 1; i < 128; i++)
366 CHECK_BIT(ASMBitNextSet(&p->au32[0], sizeof(p->au32) * 8, i - 1) == i, i);
367 for (i = 0; i < 128; i++)
368 {
369 MAP_CLEAR(p);
370 ASMBitSet(&p->au32[0], i);
371 CHECK_BIT(ASMBitFirstSet(&p->au32[0], sizeof(p->au32) * 8) == i, i);
372 for (j = 0; j < i; j++)
373 CHECK_BIT(ASMBitNextSet(&p->au32[0], sizeof(p->au32) * 8, j) == i, i);
374 for (j = i; j < 128; j++)
375 CHECK_BIT(ASMBitNextSet(&p->au32[0], sizeof(p->au32) * 8, j) == -1, i);
376 }
377
378
379 CHECK(ASMBitLastSetU32(0) == 0);
380 CHECK(ASMBitLastSetU32(1) == 1);
381 CHECK(ASMBitLastSetU32(0x80000000) == 32);
382 CHECK(ASMBitLastSetU32(0xffffffff) == 32);
383 CHECK(ASMBitLastSetU32(RT_BIT(23) | RT_BIT(11)) == 24);
384 for (i = 0; i < 32; i++)
385 CHECK(ASMBitLastSetU32(1 << i) == (unsigned)i + 1);
386
387 CHECK(ASMBitFirstSetU32(0) == 0);
388 CHECK(ASMBitFirstSetU32(1) == 1);
389 CHECK(ASMBitFirstSetU32(0x80000000) == 32);
390 CHECK(ASMBitFirstSetU32(0xffffffff) == 1);
391 CHECK(ASMBitFirstSetU32(RT_BIT(23) | RT_BIT(11)) == 12);
392 for (i = 0; i < 32; i++)
393 CHECK(ASMBitFirstSetU32(1 << i) == (unsigned)i + 1);
394
395 /*
396 * Special tests.
397 */
398 test2(hTest);
399
400 /*
401 * Summary
402 */
403 return RTTestSummaryAndDestroy(hTest);
404}
405
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette