Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / dlls / ntdll / tests / rtl.c
blob986495ae9ab3ab6aee44176749f87bb9cf195b38
1 /* Unit test suite for Rtl* API functions
3 * Copyright 2003 Thomas Mertes
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * NOTES
20 * We use function pointers here as there is no import library for NTDLL on
21 * windows.
24 #include <stdarg.h>
25 #include <stdlib.h>
27 #include "ntstatus.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wine/test.h"
31 #include "winnt.h"
32 #include "winnls.h"
33 #include "winreg.h"
34 #include "winternl.h"
36 /* Function ptrs for ntdll calls */
37 static HMODULE hntdll = 0;
38 static SIZE_T (WINAPI *pRtlCompareMemory)(LPCVOID,LPCVOID,SIZE_T);
39 static SIZE_T (WINAPI *pRtlCompareMemoryUlong)(PULONG, SIZE_T, ULONG);
40 static VOID (WINAPI *pRtlMoveMemory)(LPVOID,LPCVOID,SIZE_T);
41 static VOID (WINAPI *pRtlFillMemory)(LPVOID,SIZE_T,BYTE);
42 static VOID (WINAPI *pRtlFillMemoryUlong)(LPVOID,SIZE_T,ULONG);
43 static VOID (WINAPI *pRtlZeroMemory)(LPVOID,SIZE_T);
44 static ULONGLONG (WINAPIV *pRtlUlonglongByteSwap)(ULONGLONG source);
45 static ULONG (WINAPI *pRtlUniform)(PULONG);
46 static ULONG (WINAPI *pRtlRandom)(PULONG);
47 static BOOLEAN (WINAPI *pRtlAreAllAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
48 static BOOLEAN (WINAPI *pRtlAreAnyAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
49 static DWORD (WINAPI *pRtlComputeCrc32)(DWORD,const BYTE*,INT);
51 #define LEN 16
52 static const char* src_src = "This is a test!"; /* 16 bytes long, incl NUL */
53 static ULONG src_aligned_block[4];
54 static ULONG dest_aligned_block[32];
55 static const char *src = (const char*)src_aligned_block;
56 static char* dest = (char*)dest_aligned_block;
58 static void InitFunctionPtrs(void)
60 hntdll = LoadLibraryA("ntdll.dll");
61 ok(hntdll != 0, "LoadLibrary failed");
62 if (hntdll) {
63 pRtlCompareMemory = (void *)GetProcAddress(hntdll, "RtlCompareMemory");
64 pRtlCompareMemoryUlong = (void *)GetProcAddress(hntdll, "RtlCompareMemoryUlong");
65 pRtlMoveMemory = (void *)GetProcAddress(hntdll, "RtlMoveMemory");
66 pRtlFillMemory = (void *)GetProcAddress(hntdll, "RtlFillMemory");
67 pRtlFillMemoryUlong = (void *)GetProcAddress(hntdll, "RtlFillMemoryUlong");
68 pRtlZeroMemory = (void *)GetProcAddress(hntdll, "RtlZeroMemory");
69 pRtlUlonglongByteSwap = (void *)GetProcAddress(hntdll, "RtlUlonglongByteSwap");
70 pRtlUniform = (void *)GetProcAddress(hntdll, "RtlUniform");
71 pRtlRandom = (void *)GetProcAddress(hntdll, "RtlRandom");
72 pRtlAreAllAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAllAccessesGranted");
73 pRtlAreAnyAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAnyAccessesGranted");
74 pRtlComputeCrc32 = (void *)GetProcAddress(hntdll, "RtlComputeCrc32");
76 strcpy((char*)src_aligned_block, src_src);
77 ok(strlen(src) == 15, "Source must be 16 bytes long!\n");
80 #define COMP(str1,str2,cmplen,len) size = pRtlCompareMemory(str1, str2, cmplen); \
81 ok(size == len, "Expected %ld, got %ld\n", size, (SIZE_T)len)
83 static void test_RtlCompareMemory(void)
85 SIZE_T size;
87 if (!pRtlCompareMemory)
88 return;
90 strcpy(dest, src);
92 COMP(src,src,0,0);
93 COMP(src,src,LEN,LEN);
94 dest[0] = 'x';
95 COMP(src,dest,LEN,0);
98 static void test_RtlCompareMemoryUlong(void)
100 ULONG a[10];
101 ULONG result;
103 a[0]= 0x0123;
104 a[1]= 0x4567;
105 a[2]= 0x89ab;
106 a[3]= 0xcdef;
107 result = pRtlCompareMemoryUlong(a, 0, 0x0123);
108 ok(result == 0, "RtlCompareMemoryUlong(%p, 0, 0x0123) returns %lu, expected 0\n", a, result);
109 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
110 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %lu, expected 0\n", a, result);
111 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
112 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %lu, expected 4\n", a, result);
113 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
114 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %lu, expected 4\n", a, result);
115 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
116 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %lu, expected 4\n", a, result);
117 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
118 ok(result == 4, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %lu, expected 4\n", a, result);
119 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
120 ok(result == 4, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %lu, expected 4\n", a, result);
121 result = pRtlCompareMemoryUlong(a, 4, 0x0127);
122 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x0127) returns %lu, expected 0\n", a, result);
123 result = pRtlCompareMemoryUlong(a, 4, 0x7123);
124 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x7123) returns %lu, expected 0\n", a, result);
125 result = pRtlCompareMemoryUlong(a, 16, 0x4567);
126 ok(result == 0, "RtlCompareMemoryUlong(%p, 16, 0x4567) returns %lu, expected 0\n", a, result);
128 a[1]= 0x0123;
129 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
130 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %lu, expected 0\n", a, result);
131 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
132 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %lu, expected 4\n", a, result);
133 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
134 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %lu, expected 4\n", a, result);
135 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
136 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %lu, expected 4\n", a, result);
137 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
138 ok(result == 8, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %lu, expected 8\n", a, result);
139 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
140 ok(result == 8, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %lu, expected 8\n", a, result);
143 #define COPY(len) memset(dest,0,sizeof(dest_aligned_block)); pRtlMoveMemory(dest, src, len)
144 #define CMP(str) ok(strcmp(dest,str) == 0, "Expected '%s', got '%s'\n", str, dest)
146 static void test_RtlMoveMemory(void)
148 if (!pRtlMoveMemory)
149 return;
151 /* Length should be in bytes and not rounded. Use strcmp to ensure we
152 * didn't write past the end (it checks for the final NUL left by memset)
154 COPY(0); CMP("");
155 COPY(1); CMP("T");
156 COPY(2); CMP("Th");
157 COPY(3); CMP("Thi");
158 COPY(4); CMP("This");
159 COPY(5); CMP("This ");
160 COPY(6); CMP("This i");
161 COPY(7); CMP("This is");
162 COPY(8); CMP("This is ");
163 COPY(9); CMP("This is a");
165 /* Overlapping */
166 strcpy(dest, src); pRtlMoveMemory(dest, dest + 1, strlen(src) - 1);
167 CMP("his is a test!!");
168 strcpy(dest, src); pRtlMoveMemory(dest + 1, dest, strlen(src));
169 CMP("TThis is a test!");
172 #define FILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemory(dest,len,'x')
174 static void test_RtlFillMemory(void)
176 if (!pRtlFillMemory)
177 return;
179 /* Length should be in bytes and not rounded. Use strcmp to ensure we
180 * didn't write past the end (the remainder of the string should match)
182 FILL(0); CMP("This is a test!");
183 FILL(1); CMP("xhis is a test!");
184 FILL(2); CMP("xxis is a test!");
185 FILL(3); CMP("xxxs is a test!");
186 FILL(4); CMP("xxxx is a test!");
187 FILL(5); CMP("xxxxxis a test!");
188 FILL(6); CMP("xxxxxxs a test!");
189 FILL(7); CMP("xxxxxxx a test!");
190 FILL(8); CMP("xxxxxxxxa test!");
191 FILL(9); CMP("xxxxxxxxx test!");
194 #define LFILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemoryUlong(dest,len,val)
196 static void test_RtlFillMemoryUlong(void)
198 ULONG val = ('x' << 24) | ('x' << 16) | ('x' << 8) | 'x';
199 if (!pRtlFillMemoryUlong)
200 return;
202 /* Length should be in bytes and not rounded. Use strcmp to ensure we
203 * didn't write past the end (the remainder of the string should match)
205 LFILL(0); CMP("This is a test!");
206 LFILL(1); CMP("This is a test!");
207 LFILL(2); CMP("This is a test!");
208 LFILL(3); CMP("This is a test!");
209 LFILL(4); CMP("xxxx is a test!");
210 LFILL(5); CMP("xxxx is a test!");
211 LFILL(6); CMP("xxxx is a test!");
212 LFILL(7); CMP("xxxx is a test!");
213 LFILL(8); CMP("xxxxxxxxa test!");
214 LFILL(9); CMP("xxxxxxxxa test!");
217 #define ZERO(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlZeroMemory(dest,len)
218 #define MCMP(str) ok(memcmp(dest,str,LEN) == 0, "Memcmp failed\n")
220 static void test_RtlZeroMemory(void)
222 if (!pRtlZeroMemory)
223 return;
225 /* Length should be in bytes and not rounded. */
226 ZERO(0); MCMP("This is a test!");
227 ZERO(1); MCMP("\0his is a test!");
228 ZERO(2); MCMP("\0\0is is a test!");
229 ZERO(3); MCMP("\0\0\0s is a test!");
230 ZERO(4); MCMP("\0\0\0\0 is a test!");
231 ZERO(5); MCMP("\0\0\0\0\0is a test!");
232 ZERO(6); MCMP("\0\0\0\0\0\0s a test!");
233 ZERO(7); MCMP("\0\0\0\0\0\0\0 a test!");
234 ZERO(8); MCMP("\0\0\0\0\0\0\0\0a test!");
235 ZERO(9); MCMP("\0\0\0\0\0\0\0\0\0 test!");
238 static void test_RtlUlonglongByteSwap(void)
240 ULONGLONG result;
242 result = pRtlUlonglongByteSwap( ((ULONGLONG)0x76543210 << 32) | 0x87654321 );
243 ok( (((ULONGLONG)0x21436587 << 32) | 0x10325476) == result,
244 "RtlUlonglongByteSwap(0x7654321087654321) returns 0x%llx, expected 0x2143658710325476",
245 result);
249 static void test_RtlUniform(void)
251 ULONGLONG num;
252 ULONG seed;
253 ULONG seed_bak;
254 ULONG expected;
255 ULONG result;
258 * According to the documentation RtlUniform is using D.H. Lehmer's 1948
259 * algorithm. This algorithm is:
261 * seed = (seed * const_1 + const_2) % const_3;
263 * According to the documentation the random number is distributed over
264 * [0..MAXLONG]. Therefore const_3 is MAXLONG + 1:
266 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
268 * Because MAXLONG is 0x7fffffff (and MAXLONG + 1 is 0x80000000) the
269 * algorithm can be expressed without division as:
271 * seed = (seed * const_1 + const_2) & MAXLONG;
273 * To find out const_2 we just call RtlUniform with seed set to 0:
275 seed = 0;
276 expected = 0x7fffffc3;
277 result = pRtlUniform(&seed);
278 ok(result == expected,
279 "RtlUniform(&seed (seed == 0)) returns %lx, expected %lx",
280 result, expected);
282 * The algorithm is now:
284 * seed = (seed * const_1 + 0x7fffffc3) & MAXLONG;
286 * To find out const_1 we can use:
288 * const_1 = RtlUniform(1) - 0x7fffffc3;
290 * If that does not work a search loop can try all possible values of
291 * const_1 and compare to the result to RtlUniform(1).
292 * This way we find out that const_1 is 0xffffffed.
294 * For seed = 1 the const_2 is 0x7fffffc4:
296 seed = 1;
297 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
298 result = pRtlUniform(&seed);
299 ok(result == expected,
300 "RtlUniform(&seed (seed == 1)) returns %lx, expected %lx",
301 result, expected);
303 * For seed = 2 the const_2 is 0x7fffffc3:
305 seed = 2;
306 expected = seed * 0xffffffed + 0x7fffffc3;
307 result = pRtlUniform(&seed);
308 ok(result == expected,
309 "RtlUniform(&seed (seed == 2)) returns %lx, expected %lx",
310 result, expected);
312 * More tests show that if seed is odd the result must be incremented by 1:
314 seed = 3;
315 expected = seed * 0xffffffed + 0x7fffffc3 + (seed & 1);
316 result = pRtlUniform(&seed);
317 ok(result == expected,
318 "RtlUniform(&seed (seed == 2)) returns %lx, expected %lx",
319 result, expected);
321 seed = 0x6bca1aa;
322 expected = seed * 0xffffffed + 0x7fffffc3;
323 result = pRtlUniform(&seed);
324 ok(result == expected,
325 "RtlUniform(&seed (seed == 0x6bca1aa)) returns %lx, expected %lx",
326 result, expected);
328 seed = 0x6bca1ab;
329 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
330 result = pRtlUniform(&seed);
331 ok(result == expected,
332 "RtlUniform(&seed (seed == 0x6bca1ab)) returns %lx, expected %lx",
333 result, expected);
335 * When seed is 0x6bca1ac there is an exception:
337 seed = 0x6bca1ac;
338 expected = seed * 0xffffffed + 0x7fffffc3 + 2;
339 result = pRtlUniform(&seed);
340 ok(result == expected,
341 "RtlUniform(&seed (seed == 0x6bca1ac)) returns %lx, expected %lx",
342 result, expected);
344 * Note that up to here const_3 is not used
345 * (the highest bit of the result is not set).
347 * Starting with 0x6bca1ad: If seed is even the result must be incremented by 1:
349 seed = 0x6bca1ad;
350 expected = (seed * 0xffffffed + 0x7fffffc3) & MAXLONG;
351 result = pRtlUniform(&seed);
352 ok(result == expected,
353 "RtlUniform(&seed (seed == 0x6bca1ad)) returns %lx, expected %lx",
354 result, expected);
356 seed = 0x6bca1ae;
357 expected = (seed * 0xffffffed + 0x7fffffc3 + 1) & MAXLONG;
358 result = pRtlUniform(&seed);
359 ok(result == expected,
360 "RtlUniform(&seed (seed == 0x6bca1ae)) returns %lx, expected %lx",
361 result, expected);
363 * There are several ranges where for odd or even seed the result must be
364 * incremented by 1. You can see this ranges in the following test.
366 * For a full test use one of the following loop heads:
368 * for (num = 0; num <= 0xffffffff; num++) {
369 * seed = num;
370 * ...
372 * seed = 0;
373 * for (num = 0; num <= 0xffffffff; num++) {
374 * ...
376 seed = 0;
377 for (num = 0; num <= 100000; num++) {
379 expected = seed * 0xffffffed + 0x7fffffc3;
380 if (seed < 0x6bca1ac) {
381 expected = expected + (seed & 1);
382 } else if (seed == 0x6bca1ac) {
383 expected = (expected + 2) & MAXLONG;
384 } else if (seed < 0xd79435c) {
385 expected = (expected + (~seed & 1)) & MAXLONG;
386 } else if (seed < 0x1435e50b) {
387 expected = expected + (seed & 1);
388 } else if (seed < 0x1af286ba) {
389 expected = (expected + (~seed & 1)) & MAXLONG;
390 } else if (seed < 0x21af2869) {
391 expected = expected + (seed & 1);
392 } else if (seed < 0x286bca18) {
393 expected = (expected + (~seed & 1)) & MAXLONG;
394 } else if (seed < 0x2f286bc7) {
395 expected = expected + (seed & 1);
396 } else if (seed < 0x35e50d77) {
397 expected = (expected + (~seed & 1)) & MAXLONG;
398 } else if (seed < 0x3ca1af26) {
399 expected = expected + (seed & 1);
400 } else if (seed < 0x435e50d5) {
401 expected = (expected + (~seed & 1)) & MAXLONG;
402 } else if (seed < 0x4a1af284) {
403 expected = expected + (seed & 1);
404 } else if (seed < 0x50d79433) {
405 expected = (expected + (~seed & 1)) & MAXLONG;
406 } else if (seed < 0x579435e2) {
407 expected = expected + (seed & 1);
408 } else if (seed < 0x5e50d792) {
409 expected = (expected + (~seed & 1)) & MAXLONG;
410 } else if (seed < 0x650d7941) {
411 expected = expected + (seed & 1);
412 } else if (seed < 0x6bca1af0) {
413 expected = (expected + (~seed & 1)) & MAXLONG;
414 } else if (seed < 0x7286bc9f) {
415 expected = expected + (seed & 1);
416 } else if (seed < 0x79435e4e) {
417 expected = (expected + (~seed & 1)) & MAXLONG;
418 } else if (seed < 0x7ffffffd) {
419 expected = expected + (seed & 1);
420 } else if (seed < 0x86bca1ac) {
421 expected = (expected + (~seed & 1)) & MAXLONG;
422 } else if (seed == 0x86bca1ac) {
423 expected = (expected + 1) & MAXLONG;
424 } else if (seed < 0x8d79435c) {
425 expected = expected + (seed & 1);
426 } else if (seed < 0x9435e50b) {
427 expected = (expected + (~seed & 1)) & MAXLONG;
428 } else if (seed < 0x9af286ba) {
429 expected = expected + (seed & 1);
430 } else if (seed < 0xa1af2869) {
431 expected = (expected + (~seed & 1)) & MAXLONG;
432 } else if (seed < 0xa86bca18) {
433 expected = expected + (seed & 1);
434 } else if (seed < 0xaf286bc7) {
435 expected = (expected + (~seed & 1)) & MAXLONG;
436 } else if (seed == 0xaf286bc7) {
437 expected = (expected + 2) & MAXLONG;
438 } else if (seed < 0xb5e50d77) {
439 expected = expected + (seed & 1);
440 } else if (seed < 0xbca1af26) {
441 expected = (expected + (~seed & 1)) & MAXLONG;
442 } else if (seed < 0xc35e50d5) {
443 expected = expected + (seed & 1);
444 } else if (seed < 0xca1af284) {
445 expected = (expected + (~seed & 1)) & MAXLONG;
446 } else if (seed < 0xd0d79433) {
447 expected = expected + (seed & 1);
448 } else if (seed < 0xd79435e2) {
449 expected = (expected + (~seed & 1)) & MAXLONG;
450 } else if (seed < 0xde50d792) {
451 expected = expected + (seed & 1);
452 } else if (seed < 0xe50d7941) {
453 expected = (expected + (~seed & 1)) & MAXLONG;
454 } else if (seed < 0xebca1af0) {
455 expected = expected + (seed & 1);
456 } else if (seed < 0xf286bc9f) {
457 expected = (expected + (~seed & 1)) & MAXLONG;
458 } else if (seed < 0xf9435e4e) {
459 expected = expected + (seed & 1);
460 } else if (seed < 0xfffffffd) {
461 expected = (expected + (~seed & 1)) & MAXLONG;
462 } else {
463 expected = expected + (seed & 1);
464 } /* if */
465 seed_bak = seed;
466 result = pRtlUniform(&seed);
467 ok(result == expected,
468 "test: %llu RtlUniform(&seed (seed == %lx)) returns %lx, expected %lx",
469 num, seed_bak, result, expected);
470 ok(seed == expected,
471 "test: %llu RtlUniform(&seed (seed == %lx)) sets seed to %lx, expected %lx",
472 num, seed_bak, seed, expected);
473 } /* for */
475 * Further investigation shows: In the different regions the highest bit
476 * is set or cleared when even or odd seeds need an increment by 1.
477 * This leads to a simplified algorithm:
479 * seed = seed * 0xffffffed + 0x7fffffc3;
480 * if (seed == 0xffffffff || seed == 0x7ffffffe) {
481 * seed = (seed + 2) & MAXLONG;
482 * } else if (seed == 0x7fffffff) {
483 * seed = 0;
484 * } else if ((seed & 0x80000000) == 0) {
485 * seed = seed + (~seed & 1);
486 * } else {
487 * seed = (seed + (seed & 1)) & MAXLONG;
490 * This is also the algorithm used for RtlUniform of wine (see dlls/ntdll/rtl.c).
492 * Now comes the funny part:
493 * It took me one weekend, to find the complicated algorithm and one day more,
494 * to find the simplified algorithm. Several weeks later I found out: The value
495 * MAXLONG (=0x7fffffff) is never returned, neighter with the native function
496 * nor with the simplified algorithm. In reality the native function and our
497 * function return a random number distributed over [0..MAXLONG-1]. Note
498 * that this is different to what native documentation states [0..MAXLONG].
499 * Expressed with D.H. Lehmer's 1948 algorithm it looks like:
501 * seed = (seed * const_1 + const_2) % MAXLONG;
503 * Further investigations show that the real algorithm is:
505 * seed = (seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
507 * This is checked with the test below:
509 seed = 0;
510 for (num = 0; num <= 100000; num++) {
511 expected = (seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
512 seed_bak = seed;
513 result = pRtlUniform(&seed);
514 ok(result == expected,
515 "test: %llu RtlUniform(&seed (seed == %lx)) returns %lx, expected %lx",
516 num, seed_bak, result, expected);
517 ok(seed == expected,
518 "test: %llu RtlUniform(&seed (seed == %lx)) sets seed to %lx, expected %lx",
519 num, seed_bak, seed, expected);
520 } /* for */
522 * More tests show that RtlUniform does not return 0x7ffffffd for seed values
523 * in the range [0..MAXLONG-1]. Additionally 2 is returned twice. This shows
524 * that there is more than one cycle of generated randon numbers ...
529 ULONG WINAPI my_RtlRandom(PULONG seed)
531 static ULONG saved_value[128] =
532 { /* 0 */ 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,
533 /* 8 */ 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,
534 /* 16 */ 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,
535 /* 24 */ 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,
536 /* 32 */ 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,
537 /* 40 */ 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,
538 /* 48 */ 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,
539 /* 56 */ 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,
540 /* 64 */ 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,
541 /* 72 */ 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,
542 /* 80 */ 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,
543 /* 88 */ 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,
544 /* 96 */ 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,
545 /* 104 */ 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,
546 /* 112 */ 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,
547 /* 120 */ 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d };
548 ULONG rand;
549 int pos;
550 ULONG result;
552 rand = (*seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
553 *seed = (rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
554 pos = *seed & 0x7f;
555 result = saved_value[pos];
556 saved_value[pos] = rand;
557 return(result);
561 static void test_RtlRandom(void)
563 ULONGLONG num;
564 ULONG seed;
565 ULONG seed_bak;
566 ULONG seed_expected;
567 ULONG result;
568 ULONG result_expected;
571 * Unlike RtlUniform, RtlRandom is not documented. We guess that for
572 * RtlRandom D.H. Lehmer's 1948 algorithm is used like stated in
573 * the documentation of the RtlUniform function. This algorithm is:
575 * seed = (seed * const_1 + const_2) % const_3;
577 * According to the RtlUniform documentation the random number is
578 * distributed over [0..MAXLONG], but in reality it is distributed
579 * over [0..MAXLONG-1]. Therefore const_3 might be MAXLONG + 1 or
580 * MAXLONG:
582 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
584 * or
586 * seed = (seed * const_1 + const_2) % MAXLONG;
588 * To find out const_2 we just call RtlRandom with seed set to 0:
590 seed = 0;
591 result_expected = 0x320a1743;
592 seed_expected =0x44b;
593 result = pRtlRandom(&seed);
594 ok(result == result_expected,
595 "pRtlRandom(&seed (seed == 0)) returns %lx, expected %lx",
596 result, result_expected);
597 ok(seed == seed_expected,
598 "pRtlRandom(&seed (seed == 0)) sets seed to %lx, expected %lx",
599 seed, seed_expected);
601 * Seed is not equal to result as with RtlUniform. To see more we
602 * call RtlRandom aggain with seed set to 0:
604 seed = 0;
605 result_expected = 0x7fffffc3;
606 seed_expected =0x44b;
607 result = pRtlRandom(&seed);
608 ok(result == result_expected,
609 "RtlRandom(&seed (seed == 0)) returns %lx, expected %lx",
610 result, result_expected);
611 ok(seed == seed_expected,
612 "RtlRandom(&seed (seed == 0)) sets seed to %lx, expected %lx",
613 seed, seed_expected);
615 * Seed is set to the same value as before but the result is different.
616 * To see more we call RtlRandom aggain with seed set to 0:
618 seed = 0;
619 result_expected = 0x7fffffc3;
620 seed_expected =0x44b;
621 result = pRtlRandom(&seed);
622 ok(result == result_expected,
623 "RtlRandom(&seed (seed == 0)) returns %lx, expected %lx",
624 result, result_expected);
625 ok(seed == seed_expected,
626 "RtlRandom(&seed (seed == 0)) sets seed to %lx, expected %lx",
627 seed, seed_expected);
629 * Seed is aggain set to the same value as before. This time we also
630 * have the same result as before. Interestingly the value of the
631 * result is 0x7fffffc3 which is the same value used in RtlUniform
632 * as const_2. If we do
634 * seed = 0;
635 * result = RtlUniform(&seed);
637 * we get the same result (0x7fffffc3) as with
639 * seed = 0;
640 * RtlRandom(&seed);
641 * seed = 0;
642 * result = RtlRandom(&seed);
644 * And there is another interesting thing. If we do
646 * seed = 0;
647 * RtlUniform(&seed);
648 * RtlUniform(&seed);
650 * seed is set to the value 0x44b which ist the same value that
652 * seed = 0;
653 * RtlRandom(&seed);
655 * assigns to seed. Putting this two findings together leads to
656 * the concluson that RtlRandom saves the value in some variable,
657 * like in the following algorithm:
659 * result = saved_value;
660 * saved_value = RtlUniform(&seed);
661 * RtlUniform(&seed);
662 * return(result);
664 * Now we do further tests with seed set to 1:
666 seed = 1;
667 result_expected = 0x7a50bbc6;
668 seed_expected =0x5a1;
669 result = pRtlRandom(&seed);
670 ok(result == result_expected,
671 "RtlRandom(&seed (seed == 1)) returns %lx, expected %lx",
672 result, result_expected);
673 ok(seed == seed_expected,
674 "RtlRandom(&seed (seed == 1)) sets seed to %lx, expected %lx",
675 seed, seed_expected);
677 * If there is just one saved_value the result now would be
678 * 0x7fffffc3. From this test we can see that there is more than
679 * one saved_value, like with this algorithm:
681 * result = saved_value[pos];
682 * saved_value[pos] = RtlUniform(&seed);
683 * RtlUniform(&seed);
684 * return(result);
686 * But how the value of pos is determined? The calls to RtlUniform
687 * create a sequence of random numbers. Every second random number
688 * is put into the saved_value array and is used in some later call
689 * of RtlRandom as result. The only reasonable source to determine
690 * pos are the random numbers generated by RtlUniform which are not
691 * put into the saved_value array. This are the values of seed
692 * between the two calls of RtlUniform as in this altorithm:
694 * rand = RtlUniform(&seed);
695 * RtlUniform(&seed);
696 * pos = position(seed);
697 * result = saved_value[pos];
698 * saved_value[pos] = rand;
699 * return(result);
701 * What remains to determine is: The size of the saved_value array,
702 * the initial values of the saved_value array and the function
703 * position(seed). This tests are not shown here.
704 * The result of this tests ist: The size of the saved_value array
705 * is 128, the initial values can be seen in the my_RtlRandom
706 * function and the position(seed) function is (seed & 0x7f).
708 * For a full test of RtlRandom use one of the following loop heads:
710 * for (num = 0; num <= 0xffffffff; num++) {
711 * seed = num;
712 * ...
714 * seed = 0;
715 * for (num = 0; num <= 0xffffffff; num++) {
716 * ...
718 seed = 0;
719 for (num = 0; num <= 100000; num++) {
720 seed_bak = seed;
721 seed_expected = seed;
722 result_expected = my_RtlRandom(&seed_expected);
723 /* The following corrections are necessary because the */
724 /* previous tests changed the saved_value array */
725 if (num == 0) {
726 result_expected = 0x7fffffc3;
727 } else if (num == 81) {
728 result_expected = 0x7fffffb1;
729 } /* if */
730 result = pRtlRandom(&seed);
731 ok(result == result_expected,
732 "test: %llu RtlUniform(&seed (seed == %lx)) returns %lx, expected %lx",
733 num, seed_bak, result, result_expected);
734 ok(seed == seed_expected,
735 "test: %llu RtlUniform(&seed (seed == %lx)) sets seed to %lx, expected %lx",
736 num, seed_bak, seed, seed_expected);
737 } /* for */
741 typedef struct {
742 ACCESS_MASK GrantedAccess;
743 ACCESS_MASK DesiredAccess;
744 BOOLEAN result;
745 } all_accesses_t;
747 static const all_accesses_t all_accesses[] = {
748 {0xFEDCBA76, 0xFEDCBA76, 1},
749 {0x00000000, 0xFEDCBA76, 0},
750 {0xFEDCBA76, 0x00000000, 1},
751 {0x00000000, 0x00000000, 1},
752 {0xFEDCBA76, 0xFEDCBA70, 1},
753 {0xFEDCBA70, 0xFEDCBA76, 0},
754 {0xFEDCBA76, 0xFEDC8A76, 1},
755 {0xFEDC8A76, 0xFEDCBA76, 0},
756 {0xFEDCBA76, 0xC8C4B242, 1},
757 {0xC8C4B242, 0xFEDCBA76, 0},
759 #define NB_ALL_ACCESSES (sizeof(all_accesses)/sizeof(*all_accesses))
762 static void test_RtlAreAllAccessesGranted(void)
764 size_t test_num;
765 BOOLEAN result;
767 for (test_num = 0; test_num < NB_ALL_ACCESSES; test_num++) {
768 result = pRtlAreAllAccessesGranted(all_accesses[test_num].GrantedAccess,
769 all_accesses[test_num].DesiredAccess);
770 ok(all_accesses[test_num].result == result,
771 "(test %d): RtlAreAllAccessesGranted(%08lx, %08lx) returns %d, expected %d",
772 test_num, all_accesses[test_num].GrantedAccess,
773 all_accesses[test_num].DesiredAccess,
774 result, all_accesses[test_num].result);
775 } /* for */
779 typedef struct {
780 ACCESS_MASK GrantedAccess;
781 ACCESS_MASK DesiredAccess;
782 BOOLEAN result;
783 } any_accesses_t;
785 static const any_accesses_t any_accesses[] = {
786 {0xFEDCBA76, 0xFEDCBA76, 1},
787 {0x00000000, 0xFEDCBA76, 0},
788 {0xFEDCBA76, 0x00000000, 0},
789 {0x00000000, 0x00000000, 0},
790 {0xFEDCBA76, 0x01234589, 0},
791 {0x00040000, 0xFEDCBA76, 1},
792 {0x00040000, 0xFED8BA76, 0},
793 {0xFEDCBA76, 0x00040000, 1},
794 {0xFED8BA76, 0x00040000, 0},
796 #define NB_ANY_ACCESSES (sizeof(any_accesses)/sizeof(*any_accesses))
799 static void test_RtlAreAnyAccessesGranted(void)
801 size_t test_num;
802 BOOLEAN result;
804 for (test_num = 0; test_num < NB_ANY_ACCESSES; test_num++) {
805 result = pRtlAreAnyAccessesGranted(any_accesses[test_num].GrantedAccess,
806 any_accesses[test_num].DesiredAccess);
807 ok(any_accesses[test_num].result == result,
808 "(test %d): RtlAreAnyAccessesGranted(%08lx, %08lx) returns %d, expected %d",
809 test_num, any_accesses[test_num].GrantedAccess,
810 any_accesses[test_num].DesiredAccess,
811 result, any_accesses[test_num].result);
812 } /* for */
815 static void test_RtlComputeCrc32()
817 DWORD crc = 0;
819 if (!pRtlComputeCrc32)
820 return;
822 crc = pRtlComputeCrc32(crc, src, LEN);
823 ok(crc == 0x40861dc2,"Expected 0x40861dc2, got %8lx\n", crc);
826 START_TEST(rtl)
828 InitFunctionPtrs();
830 test_RtlCompareMemory();
831 test_RtlCompareMemoryUlong();
832 test_RtlMoveMemory();
833 test_RtlFillMemory();
834 test_RtlFillMemoryUlong();
835 test_RtlZeroMemory();
836 if (pRtlUlonglongByteSwap) {
837 test_RtlUlonglongByteSwap();
838 } /* if */
839 test_RtlUniform();
840 test_RtlRandom();
841 test_RtlAreAllAccessesGranted();
842 test_RtlAreAnyAccessesGranted();
843 test_RtlComputeCrc32();