]> git.draconx.ca Git - rrace.git/blob - t/rng-test.c
tests: Skip reference RNG test on old compilers.
[rrace.git] / t / rng-test.c
1 /*
2  * Test case for xoshiro256** implementation.
3  * Copyright © 2022-2024 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 "tap.h"
23
24 #if !HAVE_FOR_DECLS
25 int main(void)
26 {
27         tap_skip_all("cannot compile reference xoshiro256**");
28 }
29 #else
30 #include "game-notime.h"
31 #include "game.c"
32 #include "xos256ss.c"
33
34 int main(void)
35 {
36         unsigned long long seed_state = 0xdeadbeeff00dcafe;
37         unsigned long long test_result, ref_result;
38         unsigned long long ref_state[4], test_state[4];
39         int i, ret = 0;
40
41         tap_plan(200);
42         for (i = 0; i < 100; i++) {
43                 s[0] = ref_state[0] = test_state[0] = splitmix64(&seed_state);
44                 s[1] = ref_state[1] = test_state[1] = splitmix64(&seed_state);
45                 s[2] = ref_state[2] = test_state[2] = splitmix64(&seed_state);
46                 s[3] = ref_state[3] = test_state[3] = splitmix64(&seed_state);
47
48                 ref_result = next();
49                 test_result = xoshiro256ss(test_state);
50
51                 if (!tap_result(ref_result == test_result, "rng output")) {
52                         tap_diag("Failed, unexpected result");
53                         tap_diag("   with initial state %llx %llx %llx %llx",
54                                                   ref_state[0], ref_state[1],
55                                                   ref_state[2], ref_state[3]);
56                         tap_diag("   received: %llx", test_result);
57                         tap_diag("   expected: %llx", ref_result);
58                 }
59
60                 if (!tap_result(s[0] == test_state[0] && s[1] == test_state[1]
61                              && s[2] == test_state[2] && s[3] == test_state[3],
62                                 "rng state update"))
63                 {
64                         tap_diag("Failed, state update differed");
65                         tap_diag("   with initial state %llx %llx %llx %llx",
66                                                   ref_state[0], ref_state[1],
67                                                   ref_state[2], ref_state[3]);
68                         tap_diag("   received: %llx %llx %llx %llx",
69                                        test_state[0], test_state[1],
70                                        test_state[2], test_state[3]);
71                         tap_diag("   expected: %llx %llx %llx %llx",
72                                        (unsigned long long)s[0],
73                                        (unsigned long long)s[1],
74                                        (unsigned long long)s[2],
75                                        (unsigned long long)s[3]);
76                 }
77         }
78
79         tap_done();
80 }
81 #endif