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