Support deterministic random seeding of tests

Add a new option `ODIN_TEST_RANDOM_SEED` which is picked from the cycle
counter at startup, if it's not specified by the user.

This number is sent to every test in the `T` struct and reset every test
(just in case).
This commit is contained in:
Feoramund
2024-06-02 14:47:07 -04:00
parent a27b167218
commit 1f6a6f2cd3
2 changed files with 24 additions and 0 deletions
+14
View File
@@ -9,6 +9,7 @@ import "core:encoding/base64"
import "core:fmt" import "core:fmt"
import "core:io" import "core:io"
import pkg_log "core:log" import pkg_log "core:log"
import "core:math/rand"
import "core:mem" import "core:mem"
import "core:os" import "core:os"
import "core:slice" import "core:slice"
@@ -38,6 +39,9 @@ FANCY_OUTPUT : bool : #config(ODIN_TEST_FANCY, true)
USE_CLIPBOARD : bool : #config(ODIN_TEST_CLIPBOARD, false) USE_CLIPBOARD : bool : #config(ODIN_TEST_CLIPBOARD, false)
// How many test results to show at a time per package. // How many test results to show at a time per package.
PROGRESS_WIDTH : int : #config(ODIN_TEST_PROGRESS_WIDTH, 24) PROGRESS_WIDTH : int : #config(ODIN_TEST_PROGRESS_WIDTH, 24)
// This is the random seed that will be sent to each test.
// If it is unspecified, it will be set to the system cycle counter at startup.
SHARED_RANDOM_SEED : u64 : #config(ODIN_TEST_RANDOM_SEED, 0)
end_t :: proc(t: ^T) { end_t :: proc(t: ^T) {
@@ -333,6 +337,12 @@ runner :: proc(internal_tests: []Internal_Test) -> bool {
defer bytes.buffer_destroy(&clipboard_buffer) defer bytes.buffer_destroy(&clipboard_buffer)
} }
when SHARED_RANDOM_SEED == 0 {
shared_random_seed := cast(u64)intrinsics.read_cycle_counter()
} else {
shared_random_seed := SHARED_RANDOM_SEED
}
// -- Setup initial tasks. // -- Setup initial tasks.
// NOTE(Feoramund): This is the allocator that will be used by threads to // NOTE(Feoramund): This is the allocator that will be used by threads to
@@ -356,6 +366,7 @@ runner :: proc(internal_tests: []Internal_Test) -> bool {
defer run_index += 1 defer run_index += 1
data.it = it data.it = it
data.t.seed = shared_random_seed
#no_bounds_check data.t.channel = chan.as_send(task_channels[task_index].channel) #no_bounds_check data.t.channel = chan.as_send(task_channels[task_index].channel)
data.t._log_allocator = shared_log_allocator data.t._log_allocator = shared_log_allocator
data.allocator_index = task_index data.allocator_index = task_index
@@ -383,6 +394,8 @@ runner :: proc(internal_tests: []Internal_Test) -> bool {
draw_status_bar(stdout, thread_count_status_string, total_done_count, total_test_count) draw_status_bar(stdout, thread_count_status_string, total_done_count, total_test_count)
} }
pkg_log.infof("The random seed sent to every test is: %v", shared_random_seed)
when TRACKING_MEMORY { when TRACKING_MEMORY {
when ALWAYS_REPORT_MEMORY { when ALWAYS_REPORT_MEMORY {
pkg_log.info("Memory tracking is enabled. Tests will log their memory usage when complete.") pkg_log.info("Memory tracking is enabled. Tests will log their memory usage when complete.")
@@ -428,6 +441,7 @@ runner :: proc(internal_tests: []Internal_Test) -> bool {
defer run_index += 1 defer run_index += 1
data.it = it data.it = it
data.t.seed = shared_random_seed
data.t.error_count = 0 data.t.error_count = 0
thread.pool_add_task(&pool, task.allocator, run_test_task, data, run_index) thread.pool_add_task(&pool, task.allocator, run_test_task, data, run_index)
+10
View File
@@ -29,6 +29,16 @@ Internal_Cleanup :: struct {
T :: struct { T :: struct {
error_count: int, error_count: int,
// If your test needs to perform random operations, it's advised to use
// this value to seed a local random number generator rather than relying
// on the non-thread-safe global one.
//
// This way, your results will be deterministic.
//
// This value is chosen at startup of the test runner, logged, and may be
// specified by the user. It is the same for all tests of a single run.
seed: u64,
channel: Update_Channel_Sender, channel: Update_Channel_Sender,
cleanups: [dynamic]Internal_Cleanup, cleanups: [dynamic]Internal_Cleanup,