mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Port tests\core\slice
This commit is contained in:
+1
-1
@@ -97,7 +97,7 @@ runtime_test:
|
|||||||
$(ODIN) test runtime $(COMMON) -out:test_core_runtime
|
$(ODIN) test runtime $(COMMON) -out:test_core_runtime
|
||||||
|
|
||||||
slice_test:
|
slice_test:
|
||||||
$(ODIN) run slice $(COMMON) -out:test_core_slice
|
$(ODIN) test slice $(COMMON) -out:test_core_slice
|
||||||
|
|
||||||
strings_test:
|
strings_test:
|
||||||
$(ODIN) run strings $(COMMON) -out:test_core_strings
|
$(ODIN) run strings $(COMMON) -out:test_core_strings
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ echo ---
|
|||||||
echo ---
|
echo ---
|
||||||
echo Running core:slice tests
|
echo Running core:slice tests
|
||||||
echo ---
|
echo ---
|
||||||
%PATH_TO_ODIN% run slice %COMMON% -out:test_core_slice.exe || exit /b
|
%PATH_TO_ODIN% test slice %COMMON% -out:test_core_slice.exe || exit /b
|
||||||
|
|
||||||
echo ---
|
echo ---
|
||||||
echo Running core:strings tests
|
echo Running core:strings tests
|
||||||
|
|||||||
@@ -1,56 +1,16 @@
|
|||||||
package test_core_slice
|
package test_core_slice
|
||||||
|
|
||||||
import "core:slice"
|
import "core:slice"
|
||||||
import "core:strings"
|
|
||||||
import "core:testing"
|
import "core:testing"
|
||||||
import "core:fmt"
|
|
||||||
import "core:os"
|
|
||||||
import "core:math/rand"
|
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)
|
|
||||||
test_binary_search(&t)
|
|
||||||
|
|
||||||
fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
|
|
||||||
if TEST_fail > 0 {
|
|
||||||
os.exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@test
|
@test
|
||||||
test_sort_with_indices :: proc(t: ^testing.T) {
|
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 are all prime.
|
||||||
test_sizes :: []int{7, 13, 347, 1031, 10111, 100003}
|
test_sizes :: []int{7, 13, 347, 1031, 10111, 100003}
|
||||||
|
|
||||||
for test_size in test_sizes {
|
for test_size in test_sizes {
|
||||||
fmt.printf("Sorting %v random u64 values along with index.\n", test_size)
|
r := rand.create(t.seed)
|
||||||
|
|
||||||
r := rand.create(seed)
|
|
||||||
|
|
||||||
vals := make([]u64, test_size)
|
vals := make([]u64, test_size)
|
||||||
r_idx := make([]int, test_size) // Reverse index
|
r_idx := make([]int, test_size) // Reverse index
|
||||||
@@ -61,7 +21,7 @@ test_sort_with_indices :: proc(t: ^testing.T) {
|
|||||||
|
|
||||||
// Set up test values
|
// Set up test values
|
||||||
for _, i in vals {
|
for _, i in vals {
|
||||||
vals[i] = rand.uint64(&r)
|
vals[i] = rand.uint64(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort
|
// Sort
|
||||||
@@ -69,7 +29,7 @@ test_sort_with_indices :: proc(t: ^testing.T) {
|
|||||||
defer delete(f_idx)
|
defer delete(f_idx)
|
||||||
|
|
||||||
// Verify sorted test values
|
// Verify sorted test values
|
||||||
rand.init(&r, seed)
|
rand.init(&r, t.seed)
|
||||||
|
|
||||||
for v, i in f_idx {
|
for v, i in f_idx {
|
||||||
r_idx[v] = i
|
r_idx[v] = i
|
||||||
@@ -79,14 +39,14 @@ test_sort_with_indices :: proc(t: ^testing.T) {
|
|||||||
for v, i in vals {
|
for v, i in vals {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
val_pass := v >= last
|
val_pass := v >= last
|
||||||
expect(t, val_pass, "Expected values to have been sorted.")
|
testing.expect(t, val_pass, "Expected randomized test values to have been sorted")
|
||||||
if !val_pass {
|
if !val_pass {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
idx_pass := vals[r_idx[i]] == rand.uint64(&r)
|
idx_pass := vals[r_idx[i]] == rand.uint64(&r)
|
||||||
expect(t, idx_pass, "Expected index to have been sorted.")
|
testing.expect(t, idx_pass, "Expected index to have been sorted")
|
||||||
if !idx_pass {
|
if !idx_pass {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -97,16 +57,11 @@ test_sort_with_indices :: proc(t: ^testing.T) {
|
|||||||
|
|
||||||
@test
|
@test
|
||||||
test_sort_by_indices :: proc(t: ^testing.T) {
|
test_sort_by_indices :: proc(t: ^testing.T) {
|
||||||
seed := rand.uint64()
|
|
||||||
fmt.printf("Random seed: %v\n", seed)
|
|
||||||
|
|
||||||
// Test sizes are all prime.
|
// Test sizes are all prime.
|
||||||
test_sizes :: []int{7, 13, 347, 1031, 10111, 100003}
|
test_sizes :: []int{7, 13, 347, 1031, 10111, 100003}
|
||||||
|
|
||||||
for test_size in test_sizes {
|
for test_size in test_sizes {
|
||||||
fmt.printf("Sorting %v random u64 values along with index.\n", test_size)
|
r := rand.create(t.seed)
|
||||||
|
|
||||||
r := rand.create(seed)
|
|
||||||
|
|
||||||
vals := make([]u64, test_size)
|
vals := make([]u64, test_size)
|
||||||
r_idx := make([]int, test_size) // Reverse index
|
r_idx := make([]int, test_size) // Reverse index
|
||||||
@@ -117,7 +72,7 @@ test_sort_by_indices :: proc(t: ^testing.T) {
|
|||||||
|
|
||||||
// Set up test values
|
// Set up test values
|
||||||
for _, i in vals {
|
for _, i in vals {
|
||||||
vals[i] = rand.uint64(&r)
|
vals[i] = rand.uint64(&r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort
|
// Sort
|
||||||
@@ -125,7 +80,7 @@ test_sort_by_indices :: proc(t: ^testing.T) {
|
|||||||
defer delete(f_idx)
|
defer delete(f_idx)
|
||||||
|
|
||||||
// Verify sorted test values
|
// Verify sorted test values
|
||||||
rand.init(&r, seed)
|
rand.init(&r, t.seed)
|
||||||
|
|
||||||
{
|
{
|
||||||
indices := make([]int, test_size)
|
indices := make([]int, test_size)
|
||||||
@@ -138,7 +93,7 @@ test_sort_by_indices :: proc(t: ^testing.T) {
|
|||||||
defer delete(sorted_indices)
|
defer delete(sorted_indices)
|
||||||
for v, i in sorted_indices {
|
for v, i in sorted_indices {
|
||||||
idx_pass := v == f_idx[i]
|
idx_pass := v == f_idx[i]
|
||||||
expect(t, idx_pass, "Expected the sorted index to be the same as the result from sort_with_indices")
|
testing.expect(t, idx_pass, "Expected the sorted index to be the same as the result from sort_with_indices")
|
||||||
if !idx_pass {
|
if !idx_pass {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -154,7 +109,7 @@ test_sort_by_indices :: proc(t: ^testing.T) {
|
|||||||
slice.sort_by_indices_overwrite(indices, f_idx)
|
slice.sort_by_indices_overwrite(indices, f_idx)
|
||||||
for v, i in indices {
|
for v, i in indices {
|
||||||
idx_pass := v == f_idx[i]
|
idx_pass := v == f_idx[i]
|
||||||
expect(t, idx_pass, "Expected the sorted index to be the same as the result from sort_with_indices")
|
testing.expect(t, idx_pass, "Expected the sorted index to be the same as the result from sort_with_indices")
|
||||||
if !idx_pass {
|
if !idx_pass {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -174,7 +129,7 @@ test_sort_by_indices :: proc(t: ^testing.T) {
|
|||||||
slice.sort_by_indices(indices, swap, f_idx)
|
slice.sort_by_indices(indices, swap, f_idx)
|
||||||
for v, i in swap {
|
for v, i in swap {
|
||||||
idx_pass := v == f_idx[i]
|
idx_pass := v == f_idx[i]
|
||||||
expect(t, idx_pass, "Expected the sorted index to be the same as the result from sort_with_indices")
|
testing.expect(t, idx_pass, "Expected the sorted index to be the same as the result from sort_with_indices")
|
||||||
if !idx_pass {
|
if !idx_pass {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -185,61 +140,48 @@ test_sort_by_indices :: proc(t: ^testing.T) {
|
|||||||
|
|
||||||
@test
|
@test
|
||||||
test_binary_search :: proc(t: ^testing.T) {
|
test_binary_search :: proc(t: ^testing.T) {
|
||||||
builder := strings.Builder{}
|
|
||||||
defer strings.builder_destroy(&builder)
|
|
||||||
|
|
||||||
test_search :: proc(t: ^testing.T, b: ^strings.Builder, s: []i32, v: i32) -> (int, bool) {
|
|
||||||
log(t, fmt.sbprintf(b, "Searching for %v in %v", v, s))
|
|
||||||
strings.builder_reset(b)
|
|
||||||
index, found := slice.binary_search(s, v)
|
|
||||||
log(t, fmt.sbprintf(b, "index: %v, found: %v", index, found))
|
|
||||||
strings.builder_reset(b )
|
|
||||||
|
|
||||||
return index, found
|
|
||||||
}
|
|
||||||
|
|
||||||
index: int
|
index: int
|
||||||
found: bool
|
found: bool
|
||||||
|
|
||||||
s := []i32{0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55}
|
s := []i32{0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55}
|
||||||
|
|
||||||
index, found = test_search(t, &builder, s, 13)
|
index, found = slice.binary_search(s, 13)
|
||||||
expect(t, index == 9, "Expected index to be 9.")
|
testing.expect(t, index == 9, "Expected index to be 9")
|
||||||
expect(t, found == true, "Expected found to be true.")
|
testing.expect(t, found == true, "Expected found to be true")
|
||||||
|
|
||||||
index, found = test_search(t, &builder, s, 4)
|
index, found = slice.binary_search(s, 4)
|
||||||
expect(t, index == 7, "Expected index to be 7.")
|
testing.expect(t, index == 7, "Expected index to be 7.")
|
||||||
expect(t, found == false, "Expected found to be false.")
|
testing.expect(t, found == false, "Expected found to be false.")
|
||||||
|
|
||||||
index, found = test_search(t, &builder, s, 100)
|
index, found = slice.binary_search(s, 100)
|
||||||
expect(t, index == 13, "Expected index to be 13.")
|
testing.expect(t, index == 13, "Expected index to be 13.")
|
||||||
expect(t, found == false, "Expected found to be false.")
|
testing.expect(t, found == false, "Expected found to be false.")
|
||||||
|
|
||||||
index, found = test_search(t, &builder, s, 1)
|
index, found = slice.binary_search(s, 1)
|
||||||
expect(t, index >= 1 && index <= 4, "Expected index to be 1, 2, 3, or 4.")
|
testing.expect(t, index >= 1 && index <= 4, "Expected index to be 1, 2, 3, or 4.")
|
||||||
expect(t, found == true, "Expected found to be true.")
|
testing.expect(t, found == true, "Expected found to be true.")
|
||||||
|
|
||||||
index, found = test_search(t, &builder, s, -1)
|
index, found = slice.binary_search(s, -1)
|
||||||
expect(t, index == 0, "Expected index to be 0.")
|
testing.expect(t, index == 0, "Expected index to be 0.")
|
||||||
expect(t, found == false, "Expected found to be false.")
|
testing.expect(t, found == false, "Expected found to be false.")
|
||||||
|
|
||||||
a := []i32{}
|
a := []i32{}
|
||||||
|
|
||||||
index, found = test_search(t, &builder, a, 13)
|
index, found = slice.binary_search(a, 13)
|
||||||
expect(t, index == 0, "Expected index to be 0.")
|
testing.expect(t, index == 0, "Expected index to be 0.")
|
||||||
expect(t, found == false, "Expected found to be false.")
|
testing.expect(t, found == false, "Expected found to be false.")
|
||||||
|
|
||||||
b := []i32{1}
|
b := []i32{1}
|
||||||
|
|
||||||
index, found = test_search(t, &builder, b, 13)
|
index, found = slice.binary_search(b, 13)
|
||||||
expect(t, index == 1, "Expected index to be 1.")
|
testing.expect(t, index == 1, "Expected index to be 1.")
|
||||||
expect(t, found == false, "Expected found to be false.")
|
testing.expect(t, found == false, "Expected found to be false.")
|
||||||
|
|
||||||
index, found = test_search(t, &builder, b, 1)
|
index, found = slice.binary_search(b, 1)
|
||||||
expect(t, index == 0, "Expected index to be 0.")
|
testing.expect(t, index == 0, "Expected index to be 0.")
|
||||||
expect(t, found == true, "Expected found to be true.")
|
testing.expect(t, found == true, "Expected found to be true.")
|
||||||
|
|
||||||
index, found = test_search(t, &builder, b, 0)
|
index, found = slice.binary_search(b, 0)
|
||||||
expect(t, index == 0, "Expected index to be 0.")
|
testing.expect(t, index == 0, "Expected index to be 0.")
|
||||||
expect(t, found == false, "Expected found to be false.")
|
testing.expect(t, found == false, "Expected found to be false.")
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user