]> git.draconx.ca Git - rrace.git/blob - t/rng-test.c
Mix in PID with initial seed.
[rrace.git] / t / rng-test.c
1 /*
2  * Test case for xoshiro256** implementation.
3  * Copyright © 2022-2023 Nick Bowler
4  *
5  * Directly compare the game 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 <stdio.h>
23 #include <stdlib.h>
24
25 #include "game-notime.h"
26 #include "game.c"
27 #include "xos256ss.c"
28
29 int main(void)
30 {
31         unsigned long long seed_state = 0xdeadbeeff00dcafe;
32         unsigned long long test_result, ref_result;
33         unsigned long long ref_state[4], test_state[4];
34         int i, ret = 0;
35
36         printf("1..200\n");
37         for (i = 0; i < 100; i++) {
38                 s[0] = ref_state[0] = test_state[0] = splitmix64(&seed_state);
39                 s[1] = ref_state[1] = test_state[1] = splitmix64(&seed_state);
40                 s[2] = ref_state[2] = test_state[2] = splitmix64(&seed_state);
41                 s[3] = ref_state[3] = test_state[3] = splitmix64(&seed_state);
42
43                 ref_result = next();
44                 test_result = xoshiro256ss(test_state);
45
46                 if (ref_result != test_result) {
47                         printf("not ok %d rng output\n", 2*i+1);
48                         printf("# Failed, unexpected result\n");
49                         printf("#   with initial state %llx %llx %llx %llx\n",
50                                ref_state[0], ref_state[1],
51                                ref_state[2], ref_state[3]);
52                         printf("#   received: %llx\n", test_result);
53                         printf("#   expected: %llx\n", ref_result);
54                         ret = EXIT_FAILURE;
55                 } else {
56                         printf("ok %d rng output\n", 2*i+1);
57                 }
58
59                 if (s[0] != test_state[0] || s[1] != test_state[1]
60                     || s[2] != test_state[2] || s[3] != test_state[3])
61                 {
62                         printf("not ok %d rng state update\n", 2*i+2);
63                         printf("# Failed, state update differed\n");
64                         printf("#   with initial state %llx %llx %llx %llx\n",
65                                ref_state[0], ref_state[1],
66                                ref_state[2], ref_state[3]);
67                         printf("#   received: %llx %llx %llx %llx\n",
68                                test_state[0], test_state[1],
69                                test_state[2], test_state[3]);
70                         printf("#   expected: %llx %llx %llx %llx\n",
71                                (unsigned long long)s[0],
72                                (unsigned long long)s[1],
73                                (unsigned long long)s[2],
74                                (unsigned long long)s[3]);
75                         ret = EXIT_FAILURE;
76                 } else {
77                         printf("ok %d rng state update\n", 2*i+2);
78                 }
79         }
80
81         return ret;
82 }