Avoid annoying 'macro redefinition' warnings by defining
[wine/wine-kai.git] / dlls / ntdll / tests / rtl.c
blob8de7132e0db842ea05ac9fcf0fa7e1296c6398dd
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 <stdlib.h>
26 #include "ntdll_test.h"
27 #include "winnls.h"
29 /* Function ptrs for ntdll calls */
30 static HMODULE hntdll = 0;
31 static SIZE_T (WINAPI *pRtlCompareMemory)(LPCVOID,LPCVOID,SIZE_T);
32 static SIZE_T (WINAPI *pRtlCompareMemoryUlong)(PULONG, SIZE_T, ULONG);
33 static VOID (WINAPI *pRtlMoveMemory)(LPVOID,LPCVOID,SIZE_T);
34 static VOID (WINAPI *pRtlFillMemory)(LPVOID,SIZE_T,BYTE);
35 static VOID (WINAPI *pRtlFillMemoryUlong)(LPVOID,SIZE_T,ULONG);
36 static VOID (WINAPI *pRtlZeroMemory)(LPVOID,SIZE_T);
37 static ULONGLONG (WINAPIV *pRtlUlonglongByteSwap)(ULONGLONG source);
38 static ULONG (WINAPI *pRtlUniform)(PULONG);
39 static ULONG (WINAPI *pRtlRandom)(PULONG);
40 static BOOLEAN (WINAPI *pRtlAreAllAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
41 static BOOLEAN (WINAPI *pRtlAreAnyAccessesGranted)(ACCESS_MASK, ACCESS_MASK);
42 static DWORD (WINAPI *pRtlComputeCrc32)(DWORD,const BYTE*,INT);
44 #define LEN 16
45 static const char* src_src = "This is a test!"; /* 16 bytes long, incl NUL */
46 static ULONG src_aligned_block[4];
47 static ULONG dest_aligned_block[32];
48 static const char *src = (const char*)src_aligned_block;
49 static char* dest = (char*)dest_aligned_block;
51 static void InitFunctionPtrs(void)
53 hntdll = LoadLibraryA("ntdll.dll");
54 ok(hntdll != 0, "LoadLibrary failed\n");
55 if (hntdll) {
56 pRtlCompareMemory = (void *)GetProcAddress(hntdll, "RtlCompareMemory");
57 pRtlCompareMemoryUlong = (void *)GetProcAddress(hntdll, "RtlCompareMemoryUlong");
58 pRtlMoveMemory = (void *)GetProcAddress(hntdll, "RtlMoveMemory");
59 pRtlFillMemory = (void *)GetProcAddress(hntdll, "RtlFillMemory");
60 pRtlFillMemoryUlong = (void *)GetProcAddress(hntdll, "RtlFillMemoryUlong");
61 pRtlZeroMemory = (void *)GetProcAddress(hntdll, "RtlZeroMemory");
62 pRtlUlonglongByteSwap = (void *)GetProcAddress(hntdll, "RtlUlonglongByteSwap");
63 pRtlUniform = (void *)GetProcAddress(hntdll, "RtlUniform");
64 pRtlRandom = (void *)GetProcAddress(hntdll, "RtlRandom");
65 pRtlAreAllAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAllAccessesGranted");
66 pRtlAreAnyAccessesGranted = (void *)GetProcAddress(hntdll, "RtlAreAnyAccessesGranted");
67 pRtlComputeCrc32 = (void *)GetProcAddress(hntdll, "RtlComputeCrc32");
69 strcpy((char*)src_aligned_block, src_src);
70 ok(strlen(src) == 15, "Source must be 16 bytes long!\n");
73 #define COMP(str1,str2,cmplen,len) size = pRtlCompareMemory(str1, str2, cmplen); \
74 ok(size == len, "Expected %ld, got %ld\n", size, (SIZE_T)len)
76 static void test_RtlCompareMemory(void)
78 SIZE_T size;
80 if (!pRtlCompareMemory)
81 return;
83 strcpy(dest, src);
85 COMP(src,src,0,0);
86 COMP(src,src,LEN,LEN);
87 dest[0] = 'x';
88 COMP(src,dest,LEN,0);
91 static void test_RtlCompareMemoryUlong(void)
93 ULONG a[10];
94 ULONG result;
96 a[0]= 0x0123;
97 a[1]= 0x4567;
98 a[2]= 0x89ab;
99 a[3]= 0xcdef;
100 result = pRtlCompareMemoryUlong(a, 0, 0x0123);
101 ok(result == 0, "RtlCompareMemoryUlong(%p, 0, 0x0123) returns %lu, expected 0\n", a, result);
102 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
103 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %lu, expected 0\n", a, result);
104 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
105 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %lu, expected 4\n", a, result);
106 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
107 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %lu, expected 4\n", a, result);
108 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
109 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %lu, expected 4\n", a, result);
110 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
111 ok(result == 4, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %lu, expected 4\n", a, result);
112 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
113 ok(result == 4, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %lu, expected 4\n", a, result);
114 result = pRtlCompareMemoryUlong(a, 4, 0x0127);
115 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x0127) returns %lu, expected 0\n", a, result);
116 result = pRtlCompareMemoryUlong(a, 4, 0x7123);
117 ok(result == 0, "RtlCompareMemoryUlong(%p, 4, 0x7123) returns %lu, expected 0\n", a, result);
118 result = pRtlCompareMemoryUlong(a, 16, 0x4567);
119 ok(result == 0, "RtlCompareMemoryUlong(%p, 16, 0x4567) returns %lu, expected 0\n", a, result);
121 a[1]= 0x0123;
122 result = pRtlCompareMemoryUlong(a, 3, 0x0123);
123 ok(result == 0, "RtlCompareMemoryUlong(%p, 3, 0x0123) returns %lu, expected 0\n", a, result);
124 result = pRtlCompareMemoryUlong(a, 4, 0x0123);
125 ok(result == 4, "RtlCompareMemoryUlong(%p, 4, 0x0123) returns %lu, expected 4\n", a, result);
126 result = pRtlCompareMemoryUlong(a, 5, 0x0123);
127 ok(result == 4, "RtlCompareMemoryUlong(%p, 5, 0x0123) returns %lu, expected 4\n", a, result);
128 result = pRtlCompareMemoryUlong(a, 7, 0x0123);
129 ok(result == 4, "RtlCompareMemoryUlong(%p, 7, 0x0123) returns %lu, expected 4\n", a, result);
130 result = pRtlCompareMemoryUlong(a, 8, 0x0123);
131 ok(result == 8, "RtlCompareMemoryUlong(%p, 8, 0x0123) returns %lu, expected 8\n", a, result);
132 result = pRtlCompareMemoryUlong(a, 9, 0x0123);
133 ok(result == 8, "RtlCompareMemoryUlong(%p, 9, 0x0123) returns %lu, expected 8\n", a, result);
136 #define COPY(len) memset(dest,0,sizeof(dest_aligned_block)); pRtlMoveMemory(dest, src, len)
137 #define CMP(str) ok(strcmp(dest,str) == 0, "Expected '%s', got '%s'\n", str, dest)
139 static void test_RtlMoveMemory(void)
141 if (!pRtlMoveMemory)
142 return;
144 /* Length should be in bytes and not rounded. Use strcmp to ensure we
145 * didn't write past the end (it checks for the final NUL left by memset)
147 COPY(0); CMP("");
148 COPY(1); CMP("T");
149 COPY(2); CMP("Th");
150 COPY(3); CMP("Thi");
151 COPY(4); CMP("This");
152 COPY(5); CMP("This ");
153 COPY(6); CMP("This i");
154 COPY(7); CMP("This is");
155 COPY(8); CMP("This is ");
156 COPY(9); CMP("This is a");
158 /* Overlapping */
159 strcpy(dest, src); pRtlMoveMemory(dest, dest + 1, strlen(src) - 1);
160 CMP("his is a test!!");
161 strcpy(dest, src); pRtlMoveMemory(dest + 1, dest, strlen(src));
162 CMP("TThis is a test!");
165 #define FILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemory(dest,len,'x')
167 static void test_RtlFillMemory(void)
169 if (!pRtlFillMemory)
170 return;
172 /* Length should be in bytes and not rounded. Use strcmp to ensure we
173 * didn't write past the end (the remainder of the string should match)
175 FILL(0); CMP("This is a test!");
176 FILL(1); CMP("xhis is a test!");
177 FILL(2); CMP("xxis is a test!");
178 FILL(3); CMP("xxxs is a test!");
179 FILL(4); CMP("xxxx is a test!");
180 FILL(5); CMP("xxxxxis a test!");
181 FILL(6); CMP("xxxxxxs a test!");
182 FILL(7); CMP("xxxxxxx a test!");
183 FILL(8); CMP("xxxxxxxxa test!");
184 FILL(9); CMP("xxxxxxxxx test!");
187 #define LFILL(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlFillMemoryUlong(dest,len,val)
189 static void test_RtlFillMemoryUlong(void)
191 ULONG val = ('x' << 24) | ('x' << 16) | ('x' << 8) | 'x';
192 if (!pRtlFillMemoryUlong)
193 return;
195 /* Length should be in bytes and not rounded. Use strcmp to ensure we
196 * didn't write past the end (the remainder of the string should match)
198 LFILL(0); CMP("This is a test!");
199 LFILL(1); CMP("This is a test!");
200 LFILL(2); CMP("This is a test!");
201 LFILL(3); CMP("This is a test!");
202 LFILL(4); CMP("xxxx is a test!");
203 LFILL(5); CMP("xxxx is a test!");
204 LFILL(6); CMP("xxxx is a test!");
205 LFILL(7); CMP("xxxx is a test!");
206 LFILL(8); CMP("xxxxxxxxa test!");
207 LFILL(9); CMP("xxxxxxxxa test!");
210 #define ZERO(len) memset(dest,0,sizeof(dest_aligned_block)); strcpy(dest, src); pRtlZeroMemory(dest,len)
211 #define MCMP(str) ok(memcmp(dest,str,LEN) == 0, "Memcmp failed\n")
213 static void test_RtlZeroMemory(void)
215 if (!pRtlZeroMemory)
216 return;
218 /* Length should be in bytes and not rounded. */
219 ZERO(0); MCMP("This is a test!");
220 ZERO(1); MCMP("\0his is a test!");
221 ZERO(2); MCMP("\0\0is is a test!");
222 ZERO(3); MCMP("\0\0\0s is a test!");
223 ZERO(4); MCMP("\0\0\0\0 is a test!");
224 ZERO(5); MCMP("\0\0\0\0\0is a test!");
225 ZERO(6); MCMP("\0\0\0\0\0\0s a test!");
226 ZERO(7); MCMP("\0\0\0\0\0\0\0 a test!");
227 ZERO(8); MCMP("\0\0\0\0\0\0\0\0a test!");
228 ZERO(9); MCMP("\0\0\0\0\0\0\0\0\0 test!");
231 static void test_RtlUlonglongByteSwap(void)
233 ULONGLONG result;
235 result = pRtlUlonglongByteSwap( ((ULONGLONG)0x76543210 << 32) | 0x87654321 );
236 ok( (((ULONGLONG)0x21436587 << 32) | 0x10325476) == result,
237 "RtlUlonglongByteSwap(0x7654321087654321) returns 0x%llx, expected 0x2143658710325476\n",
238 result);
242 static void test_RtlUniform(void)
244 ULONGLONG num;
245 ULONG seed;
246 ULONG seed_bak;
247 ULONG expected;
248 ULONG result;
251 * According to the documentation RtlUniform is using D.H. Lehmer's 1948
252 * algorithm. This algorithm is:
254 * seed = (seed * const_1 + const_2) % const_3;
256 * According to the documentation the random number is distributed over
257 * [0..MAXLONG]. Therefore const_3 is MAXLONG + 1:
259 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
261 * Because MAXLONG is 0x7fffffff (and MAXLONG + 1 is 0x80000000) the
262 * algorithm can be expressed without division as:
264 * seed = (seed * const_1 + const_2) & MAXLONG;
266 * To find out const_2 we just call RtlUniform with seed set to 0:
268 seed = 0;
269 expected = 0x7fffffc3;
270 result = pRtlUniform(&seed);
271 ok(result == expected,
272 "RtlUniform(&seed (seed == 0)) returns %lx, expected %lx\n",
273 result, expected);
275 * The algorithm is now:
277 * seed = (seed * const_1 + 0x7fffffc3) & MAXLONG;
279 * To find out const_1 we can use:
281 * const_1 = RtlUniform(1) - 0x7fffffc3;
283 * If that does not work a search loop can try all possible values of
284 * const_1 and compare to the result to RtlUniform(1).
285 * This way we find out that const_1 is 0xffffffed.
287 * For seed = 1 the const_2 is 0x7fffffc4:
289 seed = 1;
290 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
291 result = pRtlUniform(&seed);
292 ok(result == expected,
293 "RtlUniform(&seed (seed == 1)) returns %lx, expected %lx\n",
294 result, expected);
296 * For seed = 2 the const_2 is 0x7fffffc3:
298 seed = 2;
299 expected = seed * 0xffffffed + 0x7fffffc3;
300 result = pRtlUniform(&seed);
301 ok(result == expected,
302 "RtlUniform(&seed (seed == 2)) returns %lx, expected %lx\n",
303 result, expected);
305 * More tests show that if seed is odd the result must be incremented by 1:
307 seed = 3;
308 expected = seed * 0xffffffed + 0x7fffffc3 + (seed & 1);
309 result = pRtlUniform(&seed);
310 ok(result == expected,
311 "RtlUniform(&seed (seed == 2)) returns %lx, expected %lx\n",
312 result, expected);
314 seed = 0x6bca1aa;
315 expected = seed * 0xffffffed + 0x7fffffc3;
316 result = pRtlUniform(&seed);
317 ok(result == expected,
318 "RtlUniform(&seed (seed == 0x6bca1aa)) returns %lx, expected %lx\n",
319 result, expected);
321 seed = 0x6bca1ab;
322 expected = seed * 0xffffffed + 0x7fffffc3 + 1;
323 result = pRtlUniform(&seed);
324 ok(result == expected,
325 "RtlUniform(&seed (seed == 0x6bca1ab)) returns %lx, expected %lx\n",
326 result, expected);
328 * When seed is 0x6bca1ac there is an exception:
330 seed = 0x6bca1ac;
331 expected = seed * 0xffffffed + 0x7fffffc3 + 2;
332 result = pRtlUniform(&seed);
333 ok(result == expected,
334 "RtlUniform(&seed (seed == 0x6bca1ac)) returns %lx, expected %lx\n",
335 result, expected);
337 * Note that up to here const_3 is not used
338 * (the highest bit of the result is not set).
340 * Starting with 0x6bca1ad: If seed is even the result must be incremented by 1:
342 seed = 0x6bca1ad;
343 expected = (seed * 0xffffffed + 0x7fffffc3) & MAXLONG;
344 result = pRtlUniform(&seed);
345 ok(result == expected,
346 "RtlUniform(&seed (seed == 0x6bca1ad)) returns %lx, expected %lx\n",
347 result, expected);
349 seed = 0x6bca1ae;
350 expected = (seed * 0xffffffed + 0x7fffffc3 + 1) & MAXLONG;
351 result = pRtlUniform(&seed);
352 ok(result == expected,
353 "RtlUniform(&seed (seed == 0x6bca1ae)) returns %lx, expected %lx\n",
354 result, expected);
356 * There are several ranges where for odd or even seed the result must be
357 * incremented by 1. You can see this ranges in the following test.
359 * For a full test use one of the following loop heads:
361 * for (num = 0; num <= 0xffffffff; num++) {
362 * seed = num;
363 * ...
365 * seed = 0;
366 * for (num = 0; num <= 0xffffffff; num++) {
367 * ...
369 seed = 0;
370 for (num = 0; num <= 100000; num++) {
372 expected = seed * 0xffffffed + 0x7fffffc3;
373 if (seed < 0x6bca1ac) {
374 expected = expected + (seed & 1);
375 } else if (seed == 0x6bca1ac) {
376 expected = (expected + 2) & MAXLONG;
377 } else if (seed < 0xd79435c) {
378 expected = (expected + (~seed & 1)) & MAXLONG;
379 } else if (seed < 0x1435e50b) {
380 expected = expected + (seed & 1);
381 } else if (seed < 0x1af286ba) {
382 expected = (expected + (~seed & 1)) & MAXLONG;
383 } else if (seed < 0x21af2869) {
384 expected = expected + (seed & 1);
385 } else if (seed < 0x286bca18) {
386 expected = (expected + (~seed & 1)) & MAXLONG;
387 } else if (seed < 0x2f286bc7) {
388 expected = expected + (seed & 1);
389 } else if (seed < 0x35e50d77) {
390 expected = (expected + (~seed & 1)) & MAXLONG;
391 } else if (seed < 0x3ca1af26) {
392 expected = expected + (seed & 1);
393 } else if (seed < 0x435e50d5) {
394 expected = (expected + (~seed & 1)) & MAXLONG;
395 } else if (seed < 0x4a1af284) {
396 expected = expected + (seed & 1);
397 } else if (seed < 0x50d79433) {
398 expected = (expected + (~seed & 1)) & MAXLONG;
399 } else if (seed < 0x579435e2) {
400 expected = expected + (seed & 1);
401 } else if (seed < 0x5e50d792) {
402 expected = (expected + (~seed & 1)) & MAXLONG;
403 } else if (seed < 0x650d7941) {
404 expected = expected + (seed & 1);
405 } else if (seed < 0x6bca1af0) {
406 expected = (expected + (~seed & 1)) & MAXLONG;
407 } else if (seed < 0x7286bc9f) {
408 expected = expected + (seed & 1);
409 } else if (seed < 0x79435e4e) {
410 expected = (expected + (~seed & 1)) & MAXLONG;
411 } else if (seed < 0x7ffffffd) {
412 expected = expected + (seed & 1);
413 } else if (seed < 0x86bca1ac) {
414 expected = (expected + (~seed & 1)) & MAXLONG;
415 } else if (seed == 0x86bca1ac) {
416 expected = (expected + 1) & MAXLONG;
417 } else if (seed < 0x8d79435c) {
418 expected = expected + (seed & 1);
419 } else if (seed < 0x9435e50b) {
420 expected = (expected + (~seed & 1)) & MAXLONG;
421 } else if (seed < 0x9af286ba) {
422 expected = expected + (seed & 1);
423 } else if (seed < 0xa1af2869) {
424 expected = (expected + (~seed & 1)) & MAXLONG;
425 } else if (seed < 0xa86bca18) {
426 expected = expected + (seed & 1);
427 } else if (seed < 0xaf286bc7) {
428 expected = (expected + (~seed & 1)) & MAXLONG;
429 } else if (seed == 0xaf286bc7) {
430 expected = (expected + 2) & MAXLONG;
431 } else if (seed < 0xb5e50d77) {
432 expected = expected + (seed & 1);
433 } else if (seed < 0xbca1af26) {
434 expected = (expected + (~seed & 1)) & MAXLONG;
435 } else if (seed < 0xc35e50d5) {
436 expected = expected + (seed & 1);
437 } else if (seed < 0xca1af284) {
438 expected = (expected + (~seed & 1)) & MAXLONG;
439 } else if (seed < 0xd0d79433) {
440 expected = expected + (seed & 1);
441 } else if (seed < 0xd79435e2) {
442 expected = (expected + (~seed & 1)) & MAXLONG;
443 } else if (seed < 0xde50d792) {
444 expected = expected + (seed & 1);
445 } else if (seed < 0xe50d7941) {
446 expected = (expected + (~seed & 1)) & MAXLONG;
447 } else if (seed < 0xebca1af0) {
448 expected = expected + (seed & 1);
449 } else if (seed < 0xf286bc9f) {
450 expected = (expected + (~seed & 1)) & MAXLONG;
451 } else if (seed < 0xf9435e4e) {
452 expected = expected + (seed & 1);
453 } else if (seed < 0xfffffffd) {
454 expected = (expected + (~seed & 1)) & MAXLONG;
455 } else {
456 expected = expected + (seed & 1);
457 } /* if */
458 seed_bak = seed;
459 result = pRtlUniform(&seed);
460 ok(result == expected,
461 "test: %llu RtlUniform(&seed (seed == %lx)) returns %lx, expected %lx\n",
462 num, seed_bak, result, expected);
463 ok(seed == expected,
464 "test: %llu RtlUniform(&seed (seed == %lx)) sets seed to %lx, expected %lx\n",
465 num, seed_bak, seed, expected);
466 } /* for */
468 * Further investigation shows: In the different regions the highest bit
469 * is set or cleared when even or odd seeds need an increment by 1.
470 * This leads to a simplified algorithm:
472 * seed = seed * 0xffffffed + 0x7fffffc3;
473 * if (seed == 0xffffffff || seed == 0x7ffffffe) {
474 * seed = (seed + 2) & MAXLONG;
475 * } else if (seed == 0x7fffffff) {
476 * seed = 0;
477 * } else if ((seed & 0x80000000) == 0) {
478 * seed = seed + (~seed & 1);
479 * } else {
480 * seed = (seed + (seed & 1)) & MAXLONG;
483 * This is also the algorithm used for RtlUniform of wine (see dlls/ntdll/rtl.c).
485 * Now comes the funny part:
486 * It took me one weekend, to find the complicated algorithm and one day more,
487 * to find the simplified algorithm. Several weeks later I found out: The value
488 * MAXLONG (=0x7fffffff) is never returned, neither with the native function
489 * nor with the simplified algorithm. In reality the native function and our
490 * function return a random number distributed over [0..MAXLONG-1]. Note
491 * that this is different from what native documentation states [0..MAXLONG].
492 * Expressed with D.H. Lehmer's 1948 algorithm it looks like:
494 * seed = (seed * const_1 + const_2) % MAXLONG;
496 * Further investigations show that the real algorithm is:
498 * seed = (seed * 0x7fffffed + 0x7fffffc3) % MAXLONG;
500 * This is checked with the test below:
502 seed = 0;
503 for (num = 0; num <= 100000; num++) {
504 expected = (seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
505 seed_bak = seed;
506 result = pRtlUniform(&seed);
507 ok(result == expected,
508 "test: %llu RtlUniform(&seed (seed == %lx)) returns %lx, expected %lx\n",
509 num, seed_bak, result, expected);
510 ok(seed == expected,
511 "test: %llu RtlUniform(&seed (seed == %lx)) sets seed to %lx, expected %lx\n",
512 num, seed_bak, seed, expected);
513 } /* for */
515 * More tests show that RtlUniform does not return 0x7ffffffd for seed values
516 * in the range [0..MAXLONG-1]. Additionally 2 is returned twice. This shows
517 * that there is more than one cycle of generated randon numbers ...
522 ULONG WINAPI my_RtlRandom(PULONG seed)
524 static ULONG saved_value[128] =
525 { /* 0 */ 0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626, 0x7f8bdafb, 0x5c37d02a, 0x0ab48f72, 0x2f0c4ffa,
526 /* 8 */ 0x290e1954, 0x6b635f23, 0x5d3885c0, 0x74b49ff8, 0x5155fa54, 0x6214ad3f, 0x111e9c29, 0x242a3a09,
527 /* 16 */ 0x75932ae1, 0x40ac432e, 0x54f7ba7a, 0x585ccbd5, 0x6df5c727, 0x0374dad1, 0x7112b3f1, 0x735fc311,
528 /* 24 */ 0x404331a9, 0x74d97781, 0x64495118, 0x323e04be, 0x5974b425, 0x4862e393, 0x62389c1d, 0x28a68b82,
529 /* 32 */ 0x0f95da37, 0x7a50bbc6, 0x09b0091c, 0x22cdb7b4, 0x4faaed26, 0x66417ccd, 0x189e4bfa, 0x1ce4e8dd,
530 /* 40 */ 0x5274c742, 0x3bdcf4dc, 0x2d94e907, 0x32eac016, 0x26d33ca3, 0x60415a8a, 0x31f57880, 0x68c8aa52,
531 /* 48 */ 0x23eb16da, 0x6204f4a1, 0x373927c1, 0x0d24eb7c, 0x06dd7379, 0x2b3be507, 0x0f9c55b1, 0x2c7925eb,
532 /* 56 */ 0x36d67c9a, 0x42f831d9, 0x5e3961cb, 0x65d637a8, 0x24bb3820, 0x4d08e33d, 0x2188754f, 0x147e409e,
533 /* 64 */ 0x6a9620a0, 0x62e26657, 0x7bd8ce81, 0x11da0abb, 0x5f9e7b50, 0x23e444b6, 0x25920c78, 0x5fc894f0,
534 /* 72 */ 0x5e338cbb, 0x404237fd, 0x1d60f80f, 0x320a1743, 0x76013d2b, 0x070294ee, 0x695e243b, 0x56b177fd,
535 /* 80 */ 0x752492e1, 0x6decd52f, 0x125f5219, 0x139d2e78, 0x1898d11e, 0x2f7ee785, 0x4db405d8, 0x1a028a35,
536 /* 88 */ 0x63f6f323, 0x1f6d0078, 0x307cfd67, 0x3f32a78a, 0x6980796c, 0x462b3d83, 0x34b639f2, 0x53fce379,
537 /* 96 */ 0x74ba50f4, 0x1abc2c4b, 0x5eeaeb8d, 0x335a7a0d, 0x3973dd20, 0x0462d66b, 0x159813ff, 0x1e4643fd,
538 /* 104 */ 0x06bc5c62, 0x3115e3fc, 0x09101613, 0x47af2515, 0x4f11ec54, 0x78b99911, 0x3db8dd44, 0x1ec10b9b,
539 /* 112 */ 0x5b5506ca, 0x773ce092, 0x567be81a, 0x5475b975, 0x7a2cde1a, 0x494536f5, 0x34737bb4, 0x76d9750b,
540 /* 120 */ 0x2a1f6232, 0x2e49644d, 0x7dddcbe7, 0x500cebdb, 0x619dab9e, 0x48c626fe, 0x1cda3193, 0x52dabe9d };
541 ULONG rand;
542 int pos;
543 ULONG result;
545 rand = (*seed * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
546 *seed = (rand * 0x7fffffed + 0x7fffffc3) % 0x7fffffff;
547 pos = *seed & 0x7f;
548 result = saved_value[pos];
549 saved_value[pos] = rand;
550 return(result);
554 static void test_RtlRandom(void)
556 ULONGLONG num;
557 ULONG seed;
558 ULONG seed_bak;
559 ULONG seed_expected;
560 ULONG result;
561 ULONG result_expected;
564 * Unlike RtlUniform, RtlRandom is not documented. We guess that for
565 * RtlRandom D.H. Lehmer's 1948 algorithm is used like stated in
566 * the documentation of the RtlUniform function. This algorithm is:
568 * seed = (seed * const_1 + const_2) % const_3;
570 * According to the RtlUniform documentation the random number is
571 * distributed over [0..MAXLONG], but in reality it is distributed
572 * over [0..MAXLONG-1]. Therefore const_3 might be MAXLONG + 1 or
573 * MAXLONG:
575 * seed = (seed * const_1 + const_2) % (MAXLONG + 1);
577 * or
579 * seed = (seed * const_1 + const_2) % MAXLONG;
581 * To find out const_2 we just call RtlRandom with seed set to 0:
583 seed = 0;
584 result_expected = 0x320a1743;
585 seed_expected =0x44b;
586 result = pRtlRandom(&seed);
587 ok(result == result_expected,
588 "pRtlRandom(&seed (seed == 0)) returns %lx, expected %lx\n",
589 result, result_expected);
590 ok(seed == seed_expected,
591 "pRtlRandom(&seed (seed == 0)) sets seed to %lx, expected %lx\n",
592 seed, seed_expected);
594 * Seed is not equal to result as with RtlUniform. To see more we
595 * call RtlRandom aggain with seed set to 0:
597 seed = 0;
598 result_expected = 0x7fffffc3;
599 seed_expected =0x44b;
600 result = pRtlRandom(&seed);
601 ok(result == result_expected,
602 "RtlRandom(&seed (seed == 0)) returns %lx, expected %lx\n",
603 result, result_expected);
604 ok(seed == seed_expected,
605 "RtlRandom(&seed (seed == 0)) sets seed to %lx, expected %lx\n",
606 seed, seed_expected);
608 * Seed is set to the same value as before but the result is different.
609 * To see more we call RtlRandom aggain with seed set to 0:
611 seed = 0;
612 result_expected = 0x7fffffc3;
613 seed_expected =0x44b;
614 result = pRtlRandom(&seed);
615 ok(result == result_expected,
616 "RtlRandom(&seed (seed == 0)) returns %lx, expected %lx\n",
617 result, result_expected);
618 ok(seed == seed_expected,
619 "RtlRandom(&seed (seed == 0)) sets seed to %lx, expected %lx\n",
620 seed, seed_expected);
622 * Seed is aggain set to the same value as before. This time we also
623 * have the same result as before. Interestingly the value of the
624 * result is 0x7fffffc3 which is the same value used in RtlUniform
625 * as const_2. If we do
627 * seed = 0;
628 * result = RtlUniform(&seed);
630 * we get the same result (0x7fffffc3) as with
632 * seed = 0;
633 * RtlRandom(&seed);
634 * seed = 0;
635 * result = RtlRandom(&seed);
637 * And there is another interesting thing. If we do
639 * seed = 0;
640 * RtlUniform(&seed);
641 * RtlUniform(&seed);
643 * seed is set to the value 0x44b which ist the same value that
645 * seed = 0;
646 * RtlRandom(&seed);
648 * assigns to seed. Putting this two findings together leads to
649 * the concluson that RtlRandom saves the value in some variable,
650 * like in the following algorithm:
652 * result = saved_value;
653 * saved_value = RtlUniform(&seed);
654 * RtlUniform(&seed);
655 * return(result);
657 * Now we do further tests with seed set to 1:
659 seed = 1;
660 result_expected = 0x7a50bbc6;
661 seed_expected =0x5a1;
662 result = pRtlRandom(&seed);
663 ok(result == result_expected,
664 "RtlRandom(&seed (seed == 1)) returns %lx, expected %lx\n",
665 result, result_expected);
666 ok(seed == seed_expected,
667 "RtlRandom(&seed (seed == 1)) sets seed to %lx, expected %lx\n",
668 seed, seed_expected);
670 * If there is just one saved_value the result now would be
671 * 0x7fffffc3. From this test we can see that there is more than
672 * one saved_value, like with this algorithm:
674 * result = saved_value[pos];
675 * saved_value[pos] = RtlUniform(&seed);
676 * RtlUniform(&seed);
677 * return(result);
679 * But how the value of pos is determined? The calls to RtlUniform
680 * create a sequence of random numbers. Every second random number
681 * is put into the saved_value array and is used in some later call
682 * of RtlRandom as result. The only reasonable source to determine
683 * pos are the random numbers generated by RtlUniform which are not
684 * put into the saved_value array. This are the values of seed
685 * between the two calls of RtlUniform as in this altorithm:
687 * rand = RtlUniform(&seed);
688 * RtlUniform(&seed);
689 * pos = position(seed);
690 * result = saved_value[pos];
691 * saved_value[pos] = rand;
692 * return(result);
694 * What remains to determine is: The size of the saved_value array,
695 * the initial values of the saved_value array and the function
696 * position(seed). This tests are not shown here.
697 * The result of this tests ist: The size of the saved_value array
698 * is 128, the initial values can be seen in the my_RtlRandom
699 * function and the position(seed) function is (seed & 0x7f).
701 * For a full test of RtlRandom use one of the following loop heads:
703 * for (num = 0; num <= 0xffffffff; num++) {
704 * seed = num;
705 * ...
707 * seed = 0;
708 * for (num = 0; num <= 0xffffffff; num++) {
709 * ...
711 seed = 0;
712 for (num = 0; num <= 100000; num++) {
713 seed_bak = seed;
714 seed_expected = seed;
715 result_expected = my_RtlRandom(&seed_expected);
716 /* The following corrections are necessary because the */
717 /* previous tests changed the saved_value array */
718 if (num == 0) {
719 result_expected = 0x7fffffc3;
720 } else if (num == 81) {
721 result_expected = 0x7fffffb1;
722 } /* if */
723 result = pRtlRandom(&seed);
724 ok(result == result_expected,
725 "test: %llu RtlUniform(&seed (seed == %lx)) returns %lx, expected %lx\n",
726 num, seed_bak, result, result_expected);
727 ok(seed == seed_expected,
728 "test: %llu RtlUniform(&seed (seed == %lx)) sets seed to %lx, expected %lx\n",
729 num, seed_bak, seed, seed_expected);
730 } /* for */
734 typedef struct {
735 ACCESS_MASK GrantedAccess;
736 ACCESS_MASK DesiredAccess;
737 BOOLEAN result;
738 } all_accesses_t;
740 static const all_accesses_t all_accesses[] = {
741 {0xFEDCBA76, 0xFEDCBA76, 1},
742 {0x00000000, 0xFEDCBA76, 0},
743 {0xFEDCBA76, 0x00000000, 1},
744 {0x00000000, 0x00000000, 1},
745 {0xFEDCBA76, 0xFEDCBA70, 1},
746 {0xFEDCBA70, 0xFEDCBA76, 0},
747 {0xFEDCBA76, 0xFEDC8A76, 1},
748 {0xFEDC8A76, 0xFEDCBA76, 0},
749 {0xFEDCBA76, 0xC8C4B242, 1},
750 {0xC8C4B242, 0xFEDCBA76, 0},
752 #define NB_ALL_ACCESSES (sizeof(all_accesses)/sizeof(*all_accesses))
755 static void test_RtlAreAllAccessesGranted(void)
757 size_t test_num;
758 BOOLEAN result;
760 for (test_num = 0; test_num < NB_ALL_ACCESSES; test_num++) {
761 result = pRtlAreAllAccessesGranted(all_accesses[test_num].GrantedAccess,
762 all_accesses[test_num].DesiredAccess);
763 ok(all_accesses[test_num].result == result,
764 "(test %d): RtlAreAllAccessesGranted(%08lx, %08lx) returns %d, expected %d\n",
765 test_num, all_accesses[test_num].GrantedAccess,
766 all_accesses[test_num].DesiredAccess,
767 result, all_accesses[test_num].result);
768 } /* for */
772 typedef struct {
773 ACCESS_MASK GrantedAccess;
774 ACCESS_MASK DesiredAccess;
775 BOOLEAN result;
776 } any_accesses_t;
778 static const any_accesses_t any_accesses[] = {
779 {0xFEDCBA76, 0xFEDCBA76, 1},
780 {0x00000000, 0xFEDCBA76, 0},
781 {0xFEDCBA76, 0x00000000, 0},
782 {0x00000000, 0x00000000, 0},
783 {0xFEDCBA76, 0x01234589, 0},
784 {0x00040000, 0xFEDCBA76, 1},
785 {0x00040000, 0xFED8BA76, 0},
786 {0xFEDCBA76, 0x00040000, 1},
787 {0xFED8BA76, 0x00040000, 0},
789 #define NB_ANY_ACCESSES (sizeof(any_accesses)/sizeof(*any_accesses))
792 static void test_RtlAreAnyAccessesGranted(void)
794 size_t test_num;
795 BOOLEAN result;
797 for (test_num = 0; test_num < NB_ANY_ACCESSES; test_num++) {
798 result = pRtlAreAnyAccessesGranted(any_accesses[test_num].GrantedAccess,
799 any_accesses[test_num].DesiredAccess);
800 ok(any_accesses[test_num].result == result,
801 "(test %d): RtlAreAnyAccessesGranted(%08lx, %08lx) returns %d, expected %d\n",
802 test_num, any_accesses[test_num].GrantedAccess,
803 any_accesses[test_num].DesiredAccess,
804 result, any_accesses[test_num].result);
805 } /* for */
808 static void test_RtlComputeCrc32()
810 DWORD crc = 0;
812 if (!pRtlComputeCrc32)
813 return;
815 crc = pRtlComputeCrc32(crc, src, LEN);
816 ok(crc == 0x40861dc2,"Expected 0x40861dc2, got %8lx\n", crc);
819 START_TEST(rtl)
821 InitFunctionPtrs();
823 if (pRtlCompareMemory)
824 test_RtlCompareMemory();
825 if (pRtlCompareMemoryUlong)
826 test_RtlCompareMemoryUlong();
827 if (pRtlMoveMemory)
828 test_RtlMoveMemory();
829 if (pRtlFillMemory)
830 test_RtlFillMemory();
831 if (pRtlFillMemoryUlong)
832 test_RtlFillMemoryUlong();
833 if (pRtlZeroMemory)
834 test_RtlZeroMemory();
835 if (pRtlUlonglongByteSwap)
836 test_RtlUlonglongByteSwap();
837 if (pRtlUniform)
838 test_RtlUniform();
839 if (pRtlRandom)
840 test_RtlRandom();
841 if (pRtlAreAllAccessesGranted)
842 test_RtlAreAllAccessesGranted();
843 if (pRtlAreAnyAccessesGranted)
844 test_RtlAreAnyAccessesGranted();
845 if (pRtlComputeCrc32)
846 test_RtlComputeCrc32();