|
| 1 | +/* |
| 2 | + * BreakHack - A dungeone crawler RPG |
| 3 | + * Copyright (C) 2026 Linus Probert <linus.probert@gmail.com> |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "cmocka_include.h" |
| 20 | +#include "../src/random.h" |
| 21 | + |
| 22 | +/* set_random_seed / get_random_seed round-trip */ |
| 23 | +static void |
| 24 | +test_seed_roundtrip(void **state) |
| 25 | +{ |
| 26 | + (void)state; |
| 27 | + |
| 28 | + set_random_seed(42); |
| 29 | + assert_int_equal(get_random_seed(), 42); |
| 30 | + |
| 31 | + set_random_seed(0xDEADBEEF); |
| 32 | + assert_int_equal(get_random_seed(), 0xDEADBEEF); |
| 33 | +} |
| 34 | + |
| 35 | +/* Same seed must produce identical map seeds for all 20 levels */ |
| 36 | +static void |
| 37 | +test_map_seeds_are_deterministic(void **state) |
| 38 | +{ |
| 39 | + (void)state; |
| 40 | + |
| 41 | + unsigned int first[20], second[20]; |
| 42 | + |
| 43 | + set_random_seed(12345); |
| 44 | + for (int i = 0; i < 20; ++i) |
| 45 | + first[i] = get_random_map_seed(i + 1); |
| 46 | + |
| 47 | + set_random_seed(12345); |
| 48 | + for (int i = 0; i < 20; ++i) |
| 49 | + second[i] = get_random_map_seed(i + 1); |
| 50 | + |
| 51 | + for (int i = 0; i < 20; ++i) |
| 52 | + assert_int_equal(first[i], second[i]); |
| 53 | +} |
| 54 | + |
| 55 | +/* Two different seeds must not produce the same map seed sequence */ |
| 56 | +static void |
| 57 | +test_different_seeds_differ(void **state) |
| 58 | +{ |
| 59 | + (void)state; |
| 60 | + |
| 61 | + unsigned int a[20], b[20]; |
| 62 | + |
| 63 | + set_random_seed(11111); |
| 64 | + for (int i = 0; i < 20; ++i) |
| 65 | + a[i] = get_random_map_seed(i + 1); |
| 66 | + |
| 67 | + set_random_seed(22222); |
| 68 | + for (int i = 0; i < 20; ++i) |
| 69 | + b[i] = get_random_map_seed(i + 1); |
| 70 | + |
| 71 | + /* At least one level must differ — all matching would be a collision */ |
| 72 | + int any_differ = 0; |
| 73 | + for (int i = 0; i < 20; ++i) { |
| 74 | + if (a[i] != b[i]) { |
| 75 | + any_differ = 1; |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + assert_true(any_differ); |
| 80 | +} |
| 81 | + |
| 82 | +/* get_random(max) must always return a value in [0, max] */ |
| 83 | +static void |
| 84 | +test_get_random_bounds(void **state) |
| 85 | +{ |
| 86 | + (void)state; |
| 87 | + |
| 88 | + set_random_seed(99999); |
| 89 | + |
| 90 | + for (unsigned int max = 0; max <= 100; ++max) { |
| 91 | + for (int trial = 0; trial < 50; ++trial) { |
| 92 | + unsigned int r = get_random(max); |
| 93 | + assert_true(r <= max); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/* |
| 99 | + * Seed 0 is the "uninitialised" sentinel in init_seed(). |
| 100 | + * set_random_seed(0) works fine, but get_random_seed() will subsequently |
| 101 | + * re-init with time(NULL) because init_seed() sees seed==0 as unset. |
| 102 | + * Verify that the code doesn't crash and that get_random_seed returns |
| 103 | + * something non-zero after that lazy re-init. |
| 104 | + */ |
| 105 | +static void |
| 106 | +test_seed_zero_reinit(void **state) |
| 107 | +{ |
| 108 | + (void)state; |
| 109 | + |
| 110 | + set_random_seed(0); |
| 111 | + /* get_random_seed() triggers init_seed() which replaces 0 with time(NULL) */ |
| 112 | + assert_int_not_equal(get_random_seed(), 0); |
| 113 | + /* map seeds are now populated from the time-based seed — must not crash */ |
| 114 | + (void)get_random_map_seed(1); |
| 115 | + (void)get_random_map_seed(20); |
| 116 | +} |
| 117 | + |
| 118 | +int |
| 119 | +main(void) |
| 120 | +{ |
| 121 | + const struct CMUnitTest tests[] = { |
| 122 | + cmocka_unit_test(test_seed_roundtrip), cmocka_unit_test(test_map_seeds_are_deterministic), |
| 123 | + cmocka_unit_test(test_different_seeds_differ), cmocka_unit_test(test_get_random_bounds), |
| 124 | + cmocka_unit_test(test_seed_zero_reinit), |
| 125 | + }; |
| 126 | + |
| 127 | + return cmocka_run_group_tests(tests, NULL, NULL); |
| 128 | +} |
0 commit comments