1 
2 #include "precomp.h"
3 #include <versionhelpers.h>
4 
5 static BOOL IsBroken = FALSE;
6 
7 void
8 Test_RtlFindMostSignificantBit(void)
9 {
10     ok_int(RtlFindMostSignificantBit(0), -1);
11 
12     ok_int(RtlFindMostSignificantBit(0x0000000000000001ULL), 0);
13     ok_int(RtlFindMostSignificantBit(0x0000000000000002ULL), 1);
14     ok_int(RtlFindMostSignificantBit(0x1000000000000000ULL), 60);
15     ok_int(RtlFindMostSignificantBit(0x8000000000000000ULL), 63);
16     ok_int(RtlFindMostSignificantBit(0x8000000000000001ULL), 63);
17     ok_int(RtlFindMostSignificantBit(0xFFFFFFFFFFFFFFFFULL), 63);
18     ok_int(RtlFindMostSignificantBit(0x0000000070000000ULL), 30);
19 }
20 
21 void
22 Test_RtlFindLeastSignificantBit(void)
23 {
24     ok_int(RtlFindLeastSignificantBit(0), -1);
25 
26     ok_int(RtlFindLeastSignificantBit(0x0000000000000001ULL), 0);
27     ok_int(RtlFindLeastSignificantBit(0x0000000000000002ULL), 1);
28     ok_int(RtlFindLeastSignificantBit(0x1000000000000000ULL), 60);
29     ok_int(RtlFindLeastSignificantBit(0x8000000000000000ULL), 63);
30     ok_int(RtlFindLeastSignificantBit(0x8000000000000000ULL), 63);
31     ok_int(RtlFindLeastSignificantBit(0xFFFFFFFFFFFFFFFFULL), 0);
32     ok_int(RtlFindLeastSignificantBit(0x0000000070000000ULL), 28);
33 }
34 
35 void
36 Test_RtlInitializeBitMap(void)
37 {
38     RTL_BITMAP BitMapHeader;
39     ULONG Buffer[2];
40     BOOLEAN Exception = FALSE;
41 
42     _SEH2_TRY
43     {
44         RtlInitializeBitMap(NULL, NULL, 0);
45     }
46     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
47     {
48         Exception = TRUE;
49     }
50     _SEH2_END;
51     ok_int(Exception, 1);
52 
53     RtlInitializeBitMap(&BitMapHeader, NULL, -1);
54     ok_int(BitMapHeader.SizeOfBitMap, -1);
55     ok_ptr(BitMapHeader.Buffer, NULL);
56 
57     memset(Buffer, 0xcc, sizeof(Buffer));
58     RtlInitializeBitMap(&BitMapHeader, Buffer, 0);
59     ok_int(BitMapHeader.SizeOfBitMap, 0);
60     ok_ptr(BitMapHeader.Buffer, Buffer);
61     ok_hex(Buffer[0], 0xcccccccc);
62 
63     RtlInitializeBitMap(&BitMapHeader, Buffer, 8);
64     ok_int(BitMapHeader.SizeOfBitMap, 8);
65     ok_hex(Buffer[0], 0xcccccccc);
66 }
67 
68 void
69 Test_RtlClearAllBits(void)
70 {
71     RTL_BITMAP BitMapHeader;
72     ULONG *Buffer;
73     ULONG BufferSize = 2 * sizeof(*Buffer);
74 
75     Buffer = AllocateGuarded(BufferSize);
76     RtlInitializeBitMap(&BitMapHeader, Buffer, 19);
77     memset(Buffer, 0xcc, BufferSize);
78     RtlClearAllBits(&BitMapHeader);
79     ok_hex(Buffer[0], 0x00000000);
80     ok_hex(Buffer[1], 0xcccccccc);
81 
82     RtlInitializeBitMap(&BitMapHeader, Buffer, 0);
83     memset(Buffer, 0xcc, BufferSize);
84     RtlClearAllBits(&BitMapHeader);
85     ok_hex(Buffer[0], 0xcccccccc);
86     ok_hex(Buffer[1], 0xcccccccc);
87 
88     RtlInitializeBitMap(&BitMapHeader, Buffer, 64);
89     memset(Buffer, 0xcc, BufferSize);
90     RtlClearAllBits(&BitMapHeader);
91     ok_hex(Buffer[0], 0x00000000);
92     ok_hex(Buffer[1], 0x00000000);
93     FreeGuarded(Buffer);
94 }
95 
96 void
97 Test_RtlSetAllBits(void)
98 {
99     RTL_BITMAP BitMapHeader;
100     ULONG *Buffer;
101     ULONG BufferSize = 2 * sizeof(*Buffer);
102 
103     Buffer = AllocateGuarded(BufferSize);
104     RtlInitializeBitMap(&BitMapHeader, Buffer, 19);
105     memset(Buffer, 0xcc, BufferSize);
106     RtlSetAllBits(&BitMapHeader);
107     ok_hex(Buffer[0], 0xffffffff);
108     ok_hex(Buffer[1], 0xcccccccc);
109 
110     RtlInitializeBitMap(&BitMapHeader, Buffer, 0);
111     memset(Buffer, 0xcc, BufferSize);
112     RtlSetAllBits(&BitMapHeader);
113     ok_hex(Buffer[0], 0xcccccccc);
114     ok_hex(Buffer[1], 0xcccccccc);
115 
116     RtlInitializeBitMap(&BitMapHeader, Buffer, 64);
117     memset(Buffer, 0xcc, BufferSize);
118     RtlSetAllBits(&BitMapHeader);
119     ok_hex(Buffer[0], 0xffffffff);
120     ok_hex(Buffer[1], 0xffffffff);
121     FreeGuarded(Buffer);
122 }
123 
124 void
125 Test_RtlClearBits(void)
126 {
127     RTL_BITMAP BitMapHeader;
128     ULONG *Buffer;
129     ULONG BufferSize = 2 * sizeof(*Buffer);
130 
131     Buffer = AllocateGuarded(BufferSize);
132     RtlInitializeBitMap(&BitMapHeader, Buffer, 19);
133 
134     memset(Buffer, 0xff, BufferSize);
135     RtlClearBits(&BitMapHeader, 0, 0);
136     ok_hex(Buffer[0], 0xffffffff);
137     ok_hex(Buffer[1], 0xffffffff);
138 
139     memset(Buffer, 0xff, BufferSize);
140     RtlClearBits(&BitMapHeader, 0, 1);
141     ok_hex(Buffer[0], 0xfffffffe);
142     ok_hex(Buffer[1], 0xffffffff);
143 
144     memset(Buffer, 0xff, BufferSize);
145     RtlClearBits(&BitMapHeader, 21, 1);
146     ok_hex(Buffer[0], 0xffdfffff);
147     ok_hex(Buffer[1], 0xffffffff);
148 
149     memset(Buffer, 0xff, BufferSize);
150     RtlClearBits(&BitMapHeader, 7, 9);
151     ok_hex(Buffer[0], 0xffff007f);
152     ok_hex(Buffer[1], 0xffffffff);
153 
154     memset(Buffer, 0xff, BufferSize);
155     RtlClearBits(&BitMapHeader, 13, 22);
156     ok_hex(Buffer[0], 0x00001fff);
157     ok_hex(Buffer[1], 0xfffffff8);
158 
159     memset(Buffer, 0xff, BufferSize);
160     RtlClearBits(&BitMapHeader, 63, 1);
161     ok_hex(Buffer[0], 0xffffffff);
162     ok_hex(Buffer[1], 0x7fffffff);
163 
164     memset(Buffer, 0xcc, BufferSize);
165     RtlClearBits(&BitMapHeader, 3, 6);
166     RtlClearBits(&BitMapHeader, 11, 5);
167     RtlClearBits(&BitMapHeader, 21, 7);
168     RtlClearBits(&BitMapHeader, 37, 4);
169     ok_hex(Buffer[0], 0xc00c0404);
170     ok_hex(Buffer[1], 0xcccccc0c);
171     FreeGuarded(Buffer);
172 }
173 
174 void
175 Test_RtlSetBits(void)
176 {
177     RTL_BITMAP BitMapHeader;
178     ULONG *Buffer;
179     ULONG BufferSize = 2 * sizeof(*Buffer);
180 
181     Buffer = AllocateGuarded(BufferSize);
182     RtlInitializeBitMap(&BitMapHeader, Buffer, 19);
183 
184     memset(Buffer, 0x00, BufferSize);
185     RtlSetBits(&BitMapHeader, 0, 0);
186     ok_hex(Buffer[0], 0x00000000);
187     ok_hex(Buffer[1], 0x00000000);
188 
189     memset(Buffer, 0x00, BufferSize);
190     RtlSetBits(&BitMapHeader, 0, 1);
191     ok_hex(Buffer[0], 0x00000001);
192     ok_hex(Buffer[1], 0x00000000);
193 
194     memset(Buffer, 0x00, BufferSize);
195     RtlSetBits(&BitMapHeader, 21, 1);
196     ok_hex(Buffer[0], 0x00200000);
197     ok_hex(Buffer[1], 0x00000000);
198 
199     memset(Buffer, 0x00, BufferSize);
200     RtlSetBits(&BitMapHeader, 7, 9);
201     ok_hex(Buffer[0], 0x0000ff80);
202     ok_hex(Buffer[1], 0x00000000);
203 
204     memset(Buffer, 0x00, BufferSize);
205     RtlSetBits(&BitMapHeader, 13, 22);
206     ok_hex(Buffer[0], 0xffffe000);
207     ok_hex(Buffer[1], 0x00000007);
208 
209     memset(Buffer, 0x00, BufferSize);
210     RtlSetBits(&BitMapHeader, 63, 1);
211     ok_hex(Buffer[0], 0x00000000);
212     ok_hex(Buffer[1], 0x80000000);
213 
214     memset(Buffer, 0xcc, BufferSize);
215     RtlSetBits(&BitMapHeader, 3, 6);
216     RtlSetBits(&BitMapHeader, 11, 5);
217     RtlSetBits(&BitMapHeader, 21, 7);
218     RtlSetBits(&BitMapHeader, 37, 4);
219     ok_hex(Buffer[0], 0xcfecfdfc);
220     ok_hex(Buffer[1], 0xcccccdec);
221     FreeGuarded(Buffer);
222 }
223 
224 void
225 Test_RtlAreBitsClear(void)
226 {
227     RTL_BITMAP BitMapHeader;
228     ULONG *Buffer;
229 
230     Buffer = AllocateGuarded(2 * sizeof(*Buffer));
231     RtlInitializeBitMap(&BitMapHeader, Buffer, 19);
232     Buffer[0] = 0x00ff00ff;
233     Buffer[1] = 0xc0cfc0cf;
234 
235     ok_hex(RtlAreBitsClear(&BitMapHeader, 0, 8), FALSE);
236     ok_hex(RtlAreBitsClear(&BitMapHeader, 8, 8), TRUE);
237     ok_hex(RtlAreBitsClear(&BitMapHeader, 7, 8), FALSE);
238     ok_hex(RtlAreBitsClear(&BitMapHeader, 8, 9), FALSE);
239     ok_hex(RtlAreBitsClear(&BitMapHeader, 24, 1), FALSE);
240 
241     RtlInitializeBitMap(&BitMapHeader, Buffer, 31);
242     ok_hex(RtlAreBitsClear(&BitMapHeader, 24, 1), TRUE);
243     ok_hex(RtlAreBitsClear(&BitMapHeader, 24, 7), TRUE);
244     ok_hex(RtlAreBitsClear(&BitMapHeader, 24, 8), FALSE);
245 
246     RtlInitializeBitMap(&BitMapHeader, Buffer, 64);
247     ok_hex(RtlAreBitsClear(&BitMapHeader, 60, 4), FALSE);
248     FreeGuarded(Buffer);
249 }
250 
251 void
252 Test_RtlAreBitsSet(void)
253 {
254     RTL_BITMAP BitMapHeader;
255     ULONG *Buffer;
256 
257     Buffer = AllocateGuarded(2 * sizeof(*Buffer));
258     RtlInitializeBitMap(&BitMapHeader, Buffer, 19);
259     Buffer[0] = 0xff00ff00;
260     Buffer[1] = 0x3F303F30;
261 
262     ok_hex(RtlAreBitsSet(&BitMapHeader, 0, 8), FALSE);
263     ok_hex(RtlAreBitsSet(&BitMapHeader, 8, 8), TRUE);
264     ok_hex(RtlAreBitsSet(&BitMapHeader, 7, 8), FALSE);
265     ok_hex(RtlAreBitsSet(&BitMapHeader, 8, 9), FALSE);
266     ok_hex(RtlAreBitsSet(&BitMapHeader, 24, 1), FALSE);
267 
268     RtlInitializeBitMap(&BitMapHeader, Buffer, 31);
269     ok_hex(RtlAreBitsSet(&BitMapHeader, 24, 1), TRUE);
270     ok_hex(RtlAreBitsSet(&BitMapHeader, 24, 7), TRUE);
271     ok_hex(RtlAreBitsSet(&BitMapHeader, 24, 8), FALSE);
272 
273     RtlInitializeBitMap(&BitMapHeader, Buffer, 64);
274     ok_hex(RtlAreBitsSet(&BitMapHeader, 60, 4), FALSE);
275     FreeGuarded(Buffer);
276 }
277 
278 void
279 Test_RtlNumberOfSetBits(void)
280 {
281     RTL_BITMAP BitMapHeader;
282     ULONG *Buffer;
283 
284     Buffer = AllocateGuarded(2 * sizeof(*Buffer));
285     Buffer[0] = 0xff00ff0f;
286     Buffer[1] = 0x3F303F30;
287 
288     RtlInitializeBitMap(&BitMapHeader, Buffer, 64);
289     ok_int(RtlNumberOfSetBits(&BitMapHeader), 36);
290     ok_hex(Buffer[0], 0xff00ff0f);
291     ok_hex(Buffer[1], 0x3F303F30);
292 
293     RtlInitializeBitMap(&BitMapHeader, Buffer, 56);
294     ok_int(RtlNumberOfSetBits(&BitMapHeader), 30);
295     ok_hex(Buffer[0], 0xff00ff0f);
296     ok_hex(Buffer[1], 0x3F303F30);
297 
298     RtlInitializeBitMap(&BitMapHeader, Buffer, 31);
299     ok_int(RtlNumberOfSetBits(&BitMapHeader), 19);
300     ok_hex(Buffer[0], IsBroken ? 0x7f00ff0f : 0xff00ff0f);
301     ok_hex(Buffer[1], 0x3F303F30);
302 
303     RtlInitializeBitMap(&BitMapHeader, Buffer, 4);
304     ok_int(RtlNumberOfSetBits(&BitMapHeader), 4);
305     ok_hex(Buffer[0], IsBroken ? 0x7f00ff0f : 0xff00ff0f);
306     ok_hex(Buffer[1], 0x3F303F30);
307 
308     RtlInitializeBitMap(&BitMapHeader, Buffer, 0);
309     ok_int(RtlNumberOfSetBits(&BitMapHeader), 0);
310     ok_hex(Buffer[0], IsBroken ? 0x7f00ff0f : 0xff00ff0f);
311     ok_hex(Buffer[1], 0x3F303F30);
312 
313     FreeGuarded(Buffer);
314 }
315 
316 void
317 Test_RtlNumberOfClearBits(void)
318 {
319     RTL_BITMAP BitMapHeader;
320     ULONG *Buffer;
321 
322     Buffer = AllocateGuarded(2 * sizeof(*Buffer));
323     Buffer[0] = 0xff00fff0;
324     Buffer[1] = 0x3F303F30;
325 
326     RtlInitializeBitMap(&BitMapHeader, Buffer, 64);
327     ok_int(RtlNumberOfClearBits(&BitMapHeader), 28);
328     ok_hex(Buffer[0], 0xff00fff0);
329     ok_hex(Buffer[1], 0x3F303F30);
330 
331     RtlInitializeBitMap(&BitMapHeader, Buffer, 56);
332     ok_int(RtlNumberOfClearBits(&BitMapHeader), 26);
333     ok_hex(Buffer[0], 0xff00fff0);
334     ok_hex(Buffer[1], 0x3F303F30);
335 
336     RtlInitializeBitMap(&BitMapHeader, Buffer, 31);
337     ok_int(RtlNumberOfClearBits(&BitMapHeader), 12);
338     ok_hex(Buffer[0], IsBroken ? 0x7f00fff0 : 0xff00fff0);
339     ok_hex(Buffer[1], 0x3F303F30);
340 
341     RtlInitializeBitMap(&BitMapHeader, Buffer, 4);
342     ok_int(RtlNumberOfClearBits(&BitMapHeader), 4);
343     ok_hex(Buffer[0], IsBroken ? 0x7f00ff00 : 0xff00fff0);
344     ok_hex(Buffer[1], 0x3F303F30);
345 
346     RtlInitializeBitMap(&BitMapHeader, Buffer, 0);
347     ok_int(RtlNumberOfClearBits(&BitMapHeader), 0);
348     ok_hex(Buffer[0], IsBroken ? 0x7f00ff00 : 0xff00fff0);
349     ok_hex(Buffer[1], 0x3F303F30);
350 
351     FreeGuarded(Buffer);
352 }
353 
354 void
355 Test_RtlFindClearBits(void)
356 {
357     RTL_BITMAP BitMapHeader;
358     ULONG *Buffer;
359 
360     Buffer = AllocateGuarded(2 * sizeof(*Buffer));
361     Buffer[0] = 0x060F874D;
362     Buffer[1] = 0x3F303F30;
363 
364     RtlInitializeBitMap(&BitMapHeader, Buffer, 0);
365     ok_int(RtlFindClearBits(&BitMapHeader, 0, 0), 0);
366     ok_int(RtlFindClearBits(&BitMapHeader, 0, 3), 0);
367     ok_int(RtlFindClearBits(&BitMapHeader, 1, 0), -1);
368     ok_int(RtlFindClearBits(&BitMapHeader, 1, 1), -1);
369 
370     RtlInitializeBitMap(&BitMapHeader, Buffer, 8);
371     ok_int(RtlFindClearBits(&BitMapHeader, 0, 3), 0);
372     ok_int(RtlFindClearBits(&BitMapHeader, 1, 0), 1);
373     ok_int(RtlFindClearBits(&BitMapHeader, 1, 1), 1);
374     ok_int(RtlFindClearBits(&BitMapHeader, 1, 2), 4);
375 
376     ok_int(RtlFindClearBits(&BitMapHeader, 2, 0), 4);
377     ok_int(RtlFindClearBits(&BitMapHeader, 3, 0), -1);
378 
379     RtlInitializeBitMap(&BitMapHeader, Buffer, 32);
380     ok_int(RtlFindClearBits(&BitMapHeader, 0, 3), 0);
381     ok_int(RtlFindClearBits(&BitMapHeader, 0, 21), 16);
382     ok_int(RtlFindClearBits(&BitMapHeader, 0, 12), 8);
383     ok_int(RtlFindClearBits(&BitMapHeader, 0, 31), 24);
384     ok_int(RtlFindClearBits(&BitMapHeader, 0, 32), 0);
385     ok_int(RtlFindClearBits(&BitMapHeader, 0, 39), 0);
386     ok_int(RtlFindClearBits(&BitMapHeader, 4, 0), 11);
387     ok_int(RtlFindClearBits(&BitMapHeader, 5, 0), 20);
388     ok_int(RtlFindClearBits(&BitMapHeader, 4, 11), 11);
389     ok_int(RtlFindClearBits(&BitMapHeader, 4, 12), 20);
390     ok_int(RtlFindClearBits(&BitMapHeader, 2, 11), 11);
391     ok_int(RtlFindClearBits(&BitMapHeader, 2, 12), 12);
392     ok_int(RtlFindClearBits(&BitMapHeader, 1, 32), 1);
393     ok_int(RtlFindClearBits(&BitMapHeader, 4, 32), 11);
394     ok_int(RtlFindClearBits(&BitMapHeader, 5, 32), 20);
395 
396     RtlInitializeBitMap(&BitMapHeader, Buffer, 64);
397     ok_int(RtlFindClearBits(&BitMapHeader, 5, 64), 20);
398     ok_int(RtlFindClearBits(&BitMapHeader, 9, 28), 27);
399     ok_int(RtlFindClearBits(&BitMapHeader, 10, 0), -1);
400     Buffer[1] = 0xFF303F30;
401     ok_int(RtlFindClearBits(&BitMapHeader, 1, 56), 1);
402     FreeGuarded(Buffer);
403 }
404 
405 void
406 Test_RtlFindSetBits(void)
407 {
408     RTL_BITMAP BitMapHeader;
409     ULONG *Buffer;
410 
411     Buffer = AllocateGuarded(2 * sizeof(*Buffer));
412     Buffer[0] = 0xF9F078B2;
413     Buffer[1] = 0x3F303F30;
414 
415     RtlInitializeBitMap(&BitMapHeader, Buffer, 0);
416     ok_int(RtlFindSetBits(&BitMapHeader, 0, 0), 0);
417     ok_int(RtlFindSetBits(&BitMapHeader, 0, 3), 0);
418     ok_int(RtlFindSetBits(&BitMapHeader, 1, 0), -1);
419     ok_int(RtlFindSetBits(&BitMapHeader, 1, 1), -1);
420 
421     RtlInitializeBitMap(&BitMapHeader, Buffer, 8);
422     ok_int(RtlFindSetBits(&BitMapHeader, 0, 3), 0);
423     ok_int(RtlFindSetBits(&BitMapHeader, 1, 0), 1);
424     ok_int(RtlFindSetBits(&BitMapHeader, 1, 1), 1);
425     ok_int(RtlFindSetBits(&BitMapHeader, 1, 2), 4);
426 
427     ok_int(RtlFindSetBits(&BitMapHeader, 2, 0), 4);
428     ok_int(RtlFindSetBits(&BitMapHeader, 3, 0), -1);
429 
430     RtlInitializeBitMap(&BitMapHeader, Buffer, 32);
431     ok_int(RtlFindSetBits(&BitMapHeader, 0, 3), 0);
432     ok_int(RtlFindSetBits(&BitMapHeader, 0, 21), 16);
433     ok_int(RtlFindSetBits(&BitMapHeader, 0, 12), 8);
434     ok_int(RtlFindSetBits(&BitMapHeader, 0, 31), 24);
435     ok_int(RtlFindSetBits(&BitMapHeader, 0, 32), 0);
436     ok_int(RtlFindSetBits(&BitMapHeader, 0, 39), 0);
437     ok_int(RtlFindSetBits(&BitMapHeader, 4, 0), 11);
438     ok_int(RtlFindSetBits(&BitMapHeader, 5, 0), 20);
439     ok_int(RtlFindSetBits(&BitMapHeader, 4, 11), 11);
440     ok_int(RtlFindSetBits(&BitMapHeader, 4, 12), 20);
441     ok_int(RtlFindSetBits(&BitMapHeader, 2, 11), 11);
442     ok_int(RtlFindSetBits(&BitMapHeader, 1, 32), 1);
443     ok_int(RtlFindSetBits(&BitMapHeader, 4, 32), 11);
444     ok_int(RtlFindSetBits(&BitMapHeader, 5, 32), 20);
445 
446     RtlInitializeBitMap(&BitMapHeader, Buffer, 64);
447     ok_int(RtlFindSetBits(&BitMapHeader, 5, 64), 20);
448     ok_int(RtlFindSetBits(&BitMapHeader, 6, 57), 40);
449     ok_int(RtlFindSetBits(&BitMapHeader, 7, 0), -1);
450     ok_int(RtlFindSetBits(&BitMapHeader, 1, 62), 1);
451     FreeGuarded(Buffer);
452 }
453 
454 void
455 Test_RtlFindClearBitsAndSet(void)
456 {
457     RTL_BITMAP BitMapHeader;
458     ULONG *Buffer;
459 
460     Buffer = AllocateGuarded(2 * sizeof(*Buffer));
461     Buffer[0] = 0x060F874D;
462     Buffer[1] = 0x3F303F30;
463 
464     RtlInitializeBitMap(&BitMapHeader, Buffer, 0);
465     ok_int(RtlFindClearBitsAndSet(&BitMapHeader, 0, 0), 0);
466     ok_int(RtlFindClearBitsAndSet(&BitMapHeader, 0, 3), 0);
467     ok_int(RtlFindClearBitsAndSet(&BitMapHeader, 1, 0), -1);
468     ok_int(RtlFindClearBitsAndSet(&BitMapHeader, 1, 1), -1);
469     ok_hex(Buffer[0], 0x060F874D);
470 
471     Buffer[0] = 0x060F874D;
472     RtlInitializeBitMap(&BitMapHeader, Buffer, 8);
473     ok_int(RtlFindClearBitsAndSet(&BitMapHeader, 1, 0), 1);
474     ok_hex(Buffer[0], 0x60f874f);
475     ok_int(RtlFindClearBitsAndSet(&BitMapHeader, 1, 1), 4);
476     ok_hex(Buffer[0], 0x60f875f);
477     ok_int(RtlFindClearBitsAndSet(&BitMapHeader, 1, 2), 5);
478     ok_hex(Buffer[0], 0x60f877f);
479     ok_int(RtlFindClearBitsAndSet(&BitMapHeader, 2, 0), -1);
480     ok_hex(Buffer[0], 0x60f877f);
481 
482     Buffer[0] = 0x060F874D;
483     RtlInitializeBitMap(&BitMapHeader, Buffer, 32);
484     ok_int(RtlFindClearBitsAndSet(&BitMapHeader, 4, 0), 11);
485     ok_hex(Buffer[0], 0x60fff4d);
486     ok_int(RtlFindClearBitsAndSet(&BitMapHeader, 5, 0), 20);
487     ok_hex(Buffer[0], 0x7ffff4d);
488     ok_int(RtlFindClearBitsAndSet(&BitMapHeader, 4, 11), 27);
489     ok_hex(Buffer[0], 0x7fffff4d);
490 
491     Buffer[0] = 0x060F874D;
492     ok_int(RtlFindClearBitsAndSet(&BitMapHeader, 4, 12), 20);
493     ok_hex(Buffer[0], 0x6ff874d);
494     ok_int(RtlFindClearBitsAndSet(&BitMapHeader, 2, 11), 11);
495     ok_hex(Buffer[0], 0x6ff9f4d);
496     ok_int(RtlFindClearBitsAndSet(&BitMapHeader, 2, 12), 13);
497     ok_hex(Buffer[0], 0x6ffff4d);
498     FreeGuarded(Buffer);
499 }
500 
501 void
502 Test_RtlFindSetBitsAndClear(void)
503 {
504     RTL_BITMAP BitMapHeader;
505     ULONG *Buffer;
506 
507     Buffer = AllocateGuarded(2 * sizeof(*Buffer));
508     Buffer[0] = 0xF9F078B2;
509     Buffer[1] = 0x3F303F30;
510 
511     RtlInitializeBitMap(&BitMapHeader, Buffer, 0);
512     ok_int(RtlFindSetBitsAndClear(&BitMapHeader, 0, 0), 0);
513     ok_int(RtlFindSetBitsAndClear(&BitMapHeader, 0, 3), 0);
514     ok_int(RtlFindSetBitsAndClear(&BitMapHeader, 1, 0), -1);
515     ok_int(RtlFindSetBitsAndClear(&BitMapHeader, 1, 1), -1);
516     ok_hex(Buffer[0], 0xF9F078B2);
517 
518     Buffer[0] = 0xF9F078B2;
519     RtlInitializeBitMap(&BitMapHeader, Buffer, 8);
520     ok_int(RtlFindSetBitsAndClear(&BitMapHeader, 1, 0), 1);
521     ok_hex(Buffer[0], 0xf9f078b0);
522     ok_int(RtlFindSetBitsAndClear(&BitMapHeader, 1, 1), 4);
523     ok_hex(Buffer[0], 0xf9f078a0);
524     ok_int(RtlFindSetBitsAndClear(&BitMapHeader, 1, 2), 5);
525     ok_hex(Buffer[0], 0xf9f07880);
526     ok_int(RtlFindSetBitsAndClear(&BitMapHeader, 2, 0), -1);
527     ok_hex(Buffer[0], 0xf9f07880);
528 
529     Buffer[0] = 0xF9F078B2;
530     RtlInitializeBitMap(&BitMapHeader, Buffer, 32);
531     ok_int(RtlFindSetBitsAndClear(&BitMapHeader, 4, 0), 11);
532     ok_hex(Buffer[0], 0xf9f000b2);
533     ok_int(RtlFindSetBitsAndClear(&BitMapHeader, 5, 0), 20);
534     ok_hex(Buffer[0], 0xf80000b2);
535     ok_int(RtlFindSetBitsAndClear(&BitMapHeader, 4, 11), 27);
536     ok_hex(Buffer[0], 0x800000b2);
537 
538     Buffer[0] = 0xF9F078B2;
539     ok_int(RtlFindSetBitsAndClear(&BitMapHeader, 4, 12), 20);
540     ok_hex(Buffer[0], 0xf90078b2);
541     ok_int(RtlFindSetBitsAndClear(&BitMapHeader, 2, 11), 11);
542     ok_hex(Buffer[0], 0xf90060b2);
543     ok_int(RtlFindSetBitsAndClear(&BitMapHeader, 2, 12), 13);
544     ok_hex(Buffer[0], 0xf90000b2);
545     FreeGuarded(Buffer);
546 }
547 
548 void
549 Test_RtlFindNextForwardRunClear(void)
550 {
551     RTL_BITMAP BitMapHeader;
552     ULONG *Buffer;
553     ULONG Index;
554 
555     Buffer = AllocateGuarded(2 * sizeof(*Buffer));
556     Buffer[0] = 0xF9F078B2;
557     Buffer[1] = 0x3F303F30;
558 
559     RtlInitializeBitMap(&BitMapHeader, Buffer, 0);
560     ok_int(RtlFindNextForwardRunClear(&BitMapHeader, 0, &Index), 0);
561     ok_int(Index, 0);
562     ok_int(RtlFindNextForwardRunClear(&BitMapHeader, 1, &Index), 0);
563     ok_int(Index, 1);
564 
565     Index = -1;
566     RtlInitializeBitMap(&BitMapHeader, Buffer, 8);
567     ok_int(RtlFindNextForwardRunClear(&BitMapHeader, 0, &Index), 1);
568     ok_int(Index, 0);
569     ok_int(RtlFindNextForwardRunClear(&BitMapHeader, 1, &Index), 2);
570     ok_int(Index, 2);
571     ok_int(RtlFindNextForwardRunClear(&BitMapHeader, 7, &Index), 0);
572     ok_int(Index, 8);
573     ok_int(RtlFindNextForwardRunClear(&BitMapHeader, 17, &Index), 0);
574     ok_int(Index, 17);
575     ok_int(RtlFindNextForwardRunClear(&BitMapHeader, 39, &Index), 0);
576     ok_int(Index, 39);
577     FreeGuarded(Buffer);
578 }
579 
580 void
581 Test_RtlFindFirstRunClear(void)
582 {
583 }
584 
585 void
586 Test_RtlFindLastBackwardRunClear(void)
587 {
588 }
589 
590 void
591 Test_RtlFindClearRuns(void)
592 {
593 }
594 
595 void
596 Test_RtlFindLongestRunClear(void)
597 {
598 }
599 
600 
601 START_TEST(RtlBitmap)
602 {
603     /* Windows 2003 has broken bitmap code that modifies the buffer */
604     if (!IsWindows7OrGreater() && !IsReactOS())
605     {
606         IsBroken = TRUE;
607     }
608 
609     Test_RtlFindMostSignificantBit();
610     Test_RtlFindLeastSignificantBit();
611     Test_RtlInitializeBitMap();
612     Test_RtlClearAllBits();
613     Test_RtlSetAllBits();
614     Test_RtlClearBits();
615     Test_RtlSetBits();
616     Test_RtlAreBitsClear();
617     Test_RtlAreBitsSet();
618     Test_RtlNumberOfSetBits();
619     Test_RtlNumberOfClearBits();
620     Test_RtlFindClearBits();
621     Test_RtlFindSetBits();
622     Test_RtlFindClearBitsAndSet();
623     Test_RtlFindSetBitsAndClear();
624     Test_RtlFindNextForwardRunClear();
625     Test_RtlFindFirstRunClear();
626     Test_RtlFindLastBackwardRunClear();
627     Test_RtlFindClearRuns();
628     Test_RtlFindLongestRunClear();
629 }
630 
631