1570af302Sopenharmony_ci#define _XOPEN_SOURCE 700
2570af302Sopenharmony_ci#include <stdio.h>
3570af302Sopenharmony_ci#include <stdlib.h>
4570af302Sopenharmony_ci#include "test.h"
5570af302Sopenharmony_ci
6570af302Sopenharmony_ci/* naive statistical checks */
7570af302Sopenharmony_ci
8570af302Sopenharmony_ci/* error p ~ 1.6e-6 */
9570af302Sopenharmony_cistatic int chkmissing(long *x)
10570af302Sopenharmony_ci{
11570af302Sopenharmony_ci	int d[8] = {0};
12570af302Sopenharmony_ci	int i;
13570af302Sopenharmony_ci	for (i = 0; i < 100; i++)
14570af302Sopenharmony_ci		d[x[i]%8]++;
15570af302Sopenharmony_ci	for (i = 0; i < 8; i++)
16570af302Sopenharmony_ci		if (d[i]==0)
17570af302Sopenharmony_ci			return 1;
18570af302Sopenharmony_ci	return 0;
19570af302Sopenharmony_ci}
20570af302Sopenharmony_ci
21570af302Sopenharmony_ci/* error p ~ 4e-6 */
22570af302Sopenharmony_cistatic int chkrepeat(long *x)
23570af302Sopenharmony_ci{
24570af302Sopenharmony_ci	int i, j;
25570af302Sopenharmony_ci	for (i = 0; i < 100; i++)
26570af302Sopenharmony_ci		for (j = 0; j < i; j++)
27570af302Sopenharmony_ci			if (x[i] == x[j])
28570af302Sopenharmony_ci				return 1;
29570af302Sopenharmony_ci	return 0;
30570af302Sopenharmony_ci}
31570af302Sopenharmony_ci
32570af302Sopenharmony_ci/* error p ~ 1e-6 */
33570af302Sopenharmony_cistatic unsigned orx;
34570af302Sopenharmony_cistatic int chkones(long *x)
35570af302Sopenharmony_ci{
36570af302Sopenharmony_ci	int i;
37570af302Sopenharmony_ci	orx = 0;
38570af302Sopenharmony_ci	for (i = 0; i < 20; i++)
39570af302Sopenharmony_ci		orx |= x[i];
40570af302Sopenharmony_ci	return orx != 0x7fffffff;
41570af302Sopenharmony_ci}
42570af302Sopenharmony_ci
43570af302Sopenharmony_civoid checkseed(unsigned seed, long *x)
44570af302Sopenharmony_ci{
45570af302Sopenharmony_ci	int i;
46570af302Sopenharmony_ci	srandom(seed);
47570af302Sopenharmony_ci	for (i = 0; i < 100; i++)
48570af302Sopenharmony_ci		x[i] = random();
49570af302Sopenharmony_ci	if (chkmissing(x))
50570af302Sopenharmony_ci		t_error("weak seed %d, missing pattern in low bits\n", seed);
51570af302Sopenharmony_ci	if (chkrepeat(x))
52570af302Sopenharmony_ci		t_error("weak seed %d, exact repeats\n", seed);
53570af302Sopenharmony_ci	if (chkones(x))
54570af302Sopenharmony_ci		t_error("weak seed %d, or pattern: 0x%08x\n", seed, orx);
55570af302Sopenharmony_ci}
56570af302Sopenharmony_ci
57570af302Sopenharmony_ciint main()
58570af302Sopenharmony_ci{
59570af302Sopenharmony_ci	long x[100];
60570af302Sopenharmony_ci	long y,z;
61570af302Sopenharmony_ci	int i;
62570af302Sopenharmony_ci	char state[128];
63570af302Sopenharmony_ci	char *p;
64570af302Sopenharmony_ci	char *q;
65570af302Sopenharmony_ci
66570af302Sopenharmony_ci	for (i = 0; i < 100; i++)
67570af302Sopenharmony_ci		x[i] = random();
68570af302Sopenharmony_ci	p = initstate(1, state, sizeof state);
69570af302Sopenharmony_ci	for (i = 0; i < 100; i++)
70570af302Sopenharmony_ci		if (x[i] != (y = random()))
71570af302Sopenharmony_ci			t_error("initstate(1) is not default: (%d) default: %ld, seed1: %ld\n", i, x[i], y);
72570af302Sopenharmony_ci	for (i = 0; i < 10; i++) {
73570af302Sopenharmony_ci		z = random();
74570af302Sopenharmony_ci		q = setstate(p);
75570af302Sopenharmony_ci		if (z != (y = random()))
76570af302Sopenharmony_ci			t_error("setstate failed (%d) orig: %ld, reset: %ld\n", i, z, y);
77570af302Sopenharmony_ci		p = setstate(q);
78570af302Sopenharmony_ci	}
79570af302Sopenharmony_ci	srandom(1);
80570af302Sopenharmony_ci	for (i = 0; i < 100; i++)
81570af302Sopenharmony_ci		if (x[i] != (y = random()))
82570af302Sopenharmony_ci			t_error("srandom(1) is not default: (%d) default: %ld, seed1: %ld\n", i, x[i], y);
83570af302Sopenharmony_ci	checkseed(0x7fffffff, x);
84570af302Sopenharmony_ci	for (i = 0; i < 10; i++)
85570af302Sopenharmony_ci		checkseed(i, x);
86570af302Sopenharmony_ci	return t_status;
87570af302Sopenharmony_ci}
88