]> git.draconx.ca Git - cdecl99.git/blob - t/rng-test.c
3f4cf322dbcbfe71c10d0d5b8918d2e7db943594
[cdecl99.git] / t / rng-test.c
1 /*
2  * Simple random number generator for testing.
3  * Copyright © 2022 Nick Bowler
4  *
5  * Directly compare the test lib RNG against the reference implementation.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include <stdlib.h>
23
24 #include "rng.c"
25 #include "xos256p.c"
26
27 int main(void)
28 {
29         unsigned long long seed_state = 0xdeadbeeff00dcafe;
30         unsigned long long test_result, ref_result;
31         unsigned long long ref_state[4], test_state[4];
32         int i, ret = 0;
33
34         printf("1..200\n");
35         for (i = 0; i < 100; i++) {
36                 s[0] = ref_state[0] = test_state[0] = splitmix64(&seed_state);
37                 s[1] = ref_state[1] = test_state[1] = splitmix64(&seed_state);
38                 s[2] = ref_state[2] = test_state[2] = splitmix64(&seed_state);
39                 s[3] = ref_state[3] = test_state[3] = splitmix64(&seed_state);
40
41                 ref_result = next();
42                 test_result = xoshiro256p(test_state);
43
44                 if (ref_result != test_result) {
45                         printf("not ok %d rng output\n", 2*i+1);
46                         printf("# Failed, unexpected result\n");
47                         printf("#   with initial state %llx %llx %llx %llx\n",
48                                ref_state[0], ref_state[1],
49                                ref_state[2], ref_state[3]);
50                         printf("#   received: %llx\n", test_result);
51                         printf("#   expected: %llx\n", ref_result);
52                         ret = EXIT_FAILURE;
53                 } else {
54                         printf("ok %d rng output\n", 2*i+1);
55                 }
56
57                 if (s[0] != test_state[0] || s[1] != test_state[1]
58                     || s[2] != test_state[2] || s[3] != test_state[3])
59                 {
60                         printf("not ok %d rng state update\n", 2*i+2);
61                         printf("# Failed, state update differed\n");
62                         printf("#   with initial state %llx %llx %llx %llx\n",
63                                ref_state[0], ref_state[1],
64                                ref_state[2], ref_state[3]);
65                         printf("#   received: %llx %llx %llx %llx\n",
66                                test_state[0], test_state[1],
67                                test_state[2], test_state[3]);
68                         printf("#   expected: %llx %llx %llx %llx\n",
69                                (unsigned long long)s[0],
70                                (unsigned long long)s[1],
71                                (unsigned long long)s[2],
72                                (unsigned long long)s[3]);
73                         ret = EXIT_FAILURE;
74                 } else {
75                         printf("ok %d rng state update\n", 2*i+2);
76                 }
77         }
78
79         return ret;
80 }