From 9c1b464c945b607a104f5e01ae734373f9300316 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Sat, 12 Nov 2022 17:25:42 +0100 Subject: [PATCH] Add tests for new map implementation. --- .github/workflows/ci.yml | 7 ++ tests/internal/test_map.odin | 122 +++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 tests/internal/test_map.odin diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 882c21e75..689c1d67a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -146,6 +146,13 @@ jobs: cd tests\vendor call build.bat timeout-minutes: 10 + - name: Odin internals tests + shell: cmd + run: | + call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat + cd tests\internal + call build.bat + timeout-minutes: 10 - name: core:math/big tests shell: cmd run: | diff --git a/tests/internal/test_map.odin b/tests/internal/test_map.odin new file mode 100644 index 000000000..c318b6bdc --- /dev/null +++ b/tests/internal/test_map.odin @@ -0,0 +1,122 @@ +package test_internal_map + +import "core:fmt" +import "core:intrinsics" +import "core:math/rand" +import "core:mem" +import "core:os" +import "core:testing" + +seed: u64 + +ENTRY_COUNTS := []int{11, 101} // , 1_001, 10_001, 100_001, 1_000_001} + +@test +map_insert_random_key_value :: proc(t: ^testing.T) { + for entries in ENTRY_COUNTS { + fmt.printf("[map_insert_random_key_value] Testing %v entries.\n", entries) + m: map[i64]i64 + + unique_keys := 0 + r := rand.create(seed) + for _ in 0.. 5 { + fmt.println("... and more") + break + } + expect(t, false, fmt.tprintf("Unexpected value. Expected m[%v] = %v, got %v", k, v, m[k])) + } + } + clear(&m) + delete(m) + } +} + +// -------- -------- -------- -------- -------- -------- -------- -------- -------- -------- + +main :: proc() { + t := testing.T{} + + // Allow tests to be repeatable + SEED :: #config(SEED, -1) + when SEED > 0 { + seed = u64(SEED) + } else { + seed = u64(intrinsics.read_cycle_counter()) + } + fmt.println("Initialized seed to", seed) + + mem_track_test(&t, map_insert_random_key_value) + + fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count) + if TEST_fail > 0 { + os.exit(1) + } +} + +mem_track_test :: proc(t: ^testing.T, test: proc(t: ^testing.T)) { + track: mem.Tracking_Allocator + mem.tracking_allocator_init(&track, context.allocator) + context.allocator = mem.tracking_allocator(&track) + + test(t) + + expect(t, len(track.allocation_map) == 0, "Expected no leaks.") + expect(t, len(track.bad_free_array) == 0, "Expected no leaks.") + + for _, leak in track.allocation_map { + fmt.printf("%v leaked %v bytes\n", leak.location, leak.size) + } + for bad_free in track.bad_free_array { + fmt.printf("%v allocation %p was freed badly\n", bad_free.location, bad_free.memory) + } +} + +TEST_count := 0 +TEST_fail := 0 + +when ODIN_TEST { + expect :: testing.expect + log :: testing.log +} else { + expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) { + TEST_count += 1 + if !condition { + TEST_fail += 1 + fmt.printf("[%v] %v\n", loc, message) + return + } + } + log :: proc(t: ^testing.T, v: any, loc := #caller_location) { + fmt.printf("[%v] ", loc) + fmt.printf("log: %v\n", v) + } +} \ No newline at end of file