/* * Simple random number generator for testing. * Copyright © 2022-2024 Nick Bowler * * Directly compare the test lib RNG against the reference implementation. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include "tap.h" #if !HAVE_FOR_DECLS int main(void) { tap_skip_all("cannot compile reference xoshiro256+"); } #else #define TEST_RNG_NO_EXTERNAL_API 1 #include "rng.c" #include "xos256p.c" int main(void) { unsigned long long test_result, ref_result; unsigned long long ref_state[4], test_state[4]; uint_least32_t seed_state = 0xdeadbeef; int i, ret = 0; tap_plan(200); for (i = 0; i < 100; i++) { s[0] = ref_state[0] = test_state[0] = seed64(&seed_state); s[1] = ref_state[1] = test_state[1] = seed64(&seed_state); s[2] = ref_state[2] = test_state[2] = seed64(&seed_state); s[3] = ref_state[3] = test_state[3] = seed64(&seed_state); ref_result = next(); test_result = xoshiro256p(test_state); if (!tap_result(ref_result == test_result, "rng output")) { tap_diag("Failed, unexpected result"); tap_diag(" with initial state %llx %llx %llx %llx", ref_state[0], ref_state[1], ref_state[2], ref_state[3]); tap_diag(" received: %llx", test_result); tap_diag(" expected: %llx", ref_result); } if (!tap_result(s[0] == test_state[0] && s[1] == test_state[1] && s[2] == test_state[2] && s[3] == test_state[3], "rng state update")) { tap_diag("Failed, state update differed"); tap_diag(" with initial state %llx %llx %llx %llx", ref_state[0], ref_state[1], ref_state[2], ref_state[3]); tap_diag(" received: %llx %llx %llx %llx", test_state[0], test_state[1], test_state[2], test_state[3]); tap_diag(" expected: %llx %llx %llx %llx", (unsigned long long)s[0], (unsigned long long)s[1], (unsigned long long)s[2], (unsigned long long)s[3]); } } tap_done(); } #endif