diff options
| author | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2022-09-01 18:04:48 +0200 |
|---|---|---|
| committer | Jeroen van Rijn <Kelimion@users.noreply.github.com> | 2022-09-01 18:04:48 +0200 |
| commit | 36473b2774d8c6d08d2c1c73c5d35e3f58458cd9 (patch) | |
| tree | 7dadf053f1d372bf73dac9ac3ec5787f00b030b4 /tests | |
| parent | e89f0de232372155ce16afdb7cfae971d717c005 (diff) | |
Add test for core:slice.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/core/build.bat | 7 | ||||
| -rw-r--r-- | tests/core/slice/test_core_slice.odin | 97 |
2 files changed, 103 insertions, 1 deletions
diff --git a/tests/core/build.bat b/tests/core/build.bat index 77ff38038..94f8e0fd8 100644 --- a/tests/core/build.bat +++ b/tests/core/build.bat @@ -69,4 +69,9 @@ echo --- echo ---
echo Running core:text/i18n tests
echo ---
-%PATH_TO_ODIN% run text\i18n %COMMON% -out:test_core_i18n.exe
\ No newline at end of file +%PATH_TO_ODIN% run text\i18n %COMMON% -out:test_core_i18n.exe
+
+echo ---
+echo Running core:slice tests
+echo ---
+%PATH_TO_ODIN% run slice %COMMON% -out:test_core_slice.exe
\ No newline at end of file diff --git a/tests/core/slice/test_core_slice.odin b/tests/core/slice/test_core_slice.odin new file mode 100644 index 000000000..12577ab20 --- /dev/null +++ b/tests/core/slice/test_core_slice.odin @@ -0,0 +1,97 @@ +package test_core_slice + +import "core:slice" +import "core:testing" +import "core:fmt" +import "core:os" +import "core:math/rand" + +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) + } +} + +main :: proc() { + t := testing.T{} + test_sort_with_indices(&t) + + fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count) + if TEST_fail > 0 { + os.exit(1) + } +} + +@test +test_sort_with_indices :: proc(t: ^testing.T) { + seed := rand.uint64() + fmt.printf("Random seed: %v\n", seed) + + // Test sizes are all prime. + test_sizes :: []int{7, 13, 347, 1031, 10111, 100003} + + for test_size in test_sizes { + fmt.printf("Sorting %v random u64 values along with index.\n", test_size) + + r := rand.create(seed) + + vals := make([]u64, test_size) + f_idx := make([]int, test_size) // Forward index, will be sorted + r_idx := make([]int, test_size) // Reverse index + + defer { + delete(vals) + delete(f_idx) + delete(r_idx) + } + + // Set up test values + for _, i in vals { + vals[i] = rand.uint64(&r) + f_idx[i] = i + } + + // Sort + slice.sort_with_indices(vals, f_idx) + + // Verify sorted test values + rand.init(&r, seed) + + for v, i in f_idx { + r_idx[v] = i + } + + last: u64 + for v, i in vals { + if i > 0 { + val_pass := v >= last + expect(t, val_pass, "Expected values to have been sorted.") + if !val_pass { + break + } + } + + idx_pass := vals[r_idx[i]] == rand.uint64(&r) + expect(t, idx_pass, "Expected index to have been sorted.") + if !idx_pass { + break + } + last = v + } + } +}
\ No newline at end of file |