1 #define _XOPEN_SOURCE 700
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "test.h"
5
6 /* naive statistical checks */
7
8 /* error p ~ 1.6e-6 */
chkmissing(long * x)9 static int chkmissing(long *x)
10 {
11 int d[8] = {0};
12 int i;
13 for (i = 0; i < 100; i++)
14 d[x[i]%8]++;
15 for (i = 0; i < 8; i++)
16 if (d[i]==0)
17 return 1;
18 return 0;
19 }
20
21 /* error p ~ 4e-6 */
chkrepeat(long * x)22 static int chkrepeat(long *x)
23 {
24 int i, j;
25 for (i = 0; i < 100; i++)
26 for (j = 0; j < i; j++)
27 if (x[i] == x[j])
28 return 1;
29 return 0;
30 }
31
32 /* error p ~ 1e-6 */
33 static unsigned orx;
chkones(long * x)34 static int chkones(long *x)
35 {
36 int i;
37 orx = 0;
38 for (i = 0; i < 20; i++)
39 orx |= x[i];
40 return orx != 0x7fffffff;
41 }
42
checkseed(unsigned seed,long * x)43 void checkseed(unsigned seed, long *x)
44 {
45 int i;
46 srandom(seed);
47 for (i = 0; i < 100; i++)
48 x[i] = random();
49 if (chkmissing(x))
50 t_error("weak seed %d, missing pattern in low bits\n", seed);
51 if (chkrepeat(x))
52 t_error("weak seed %d, exact repeats\n", seed);
53 if (chkones(x))
54 t_error("weak seed %d, or pattern: 0x%08x\n", seed, orx);
55 }
56
main()57 int main()
58 {
59 long x[100];
60 long y,z;
61 int i;
62 char state[128];
63 char *p;
64 char *q;
65
66 for (i = 0; i < 100; i++)
67 x[i] = random();
68 p = initstate(1, state, sizeof state);
69 for (i = 0; i < 100; i++)
70 if (x[i] != (y = random()))
71 t_error("initstate(1) is not default: (%d) default: %ld, seed1: %ld\n", i, x[i], y);
72 for (i = 0; i < 10; i++) {
73 z = random();
74 q = setstate(p);
75 if (z != (y = random()))
76 t_error("setstate failed (%d) orig: %ld, reset: %ld\n", i, z, y);
77 p = setstate(q);
78 }
79 srandom(1);
80 for (i = 0; i < 100; i++)
81 if (x[i] != (y = random()))
82 t_error("srandom(1) is not default: (%d) default: %ld, seed1: %ld\n", i, x[i], y);
83 checkseed(0x7fffffff, x);
84 for (i = 0; i < 10; i++)
85 checkseed(i, x);
86 return t_status;
87 }
88