mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-09 21:11:36 -07:00
Add test for core:slice.
This commit is contained in:
@@ -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
|
||||
%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
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user