mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
add tests for sort_by_indices
This commit is contained in:
@@ -45,11 +45,12 @@ sort_by_indices :: proc(data: $T/[]$E, indices: []int, allocator := context.allo
|
|||||||
for v, i in indices {
|
for v, i in indices {
|
||||||
sorted[i] = data[v]
|
sorted[i] = data[v]
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sort_by_indices_overwrite :: proc(data: $T/[]$E, indices: []int) {
|
sort_by_indices_overwrite :: proc(data: $T/[]$E, indices: []int) {
|
||||||
assert(len(data) == len(indices))
|
assert(len(data) == len(indices))
|
||||||
temp := make([]int, len(data), context.temp_allocator)
|
temp := make([]int, len(data), context.allocator)
|
||||||
defer delete(temp)
|
defer delete(temp)
|
||||||
for v, i in indices {
|
for v, i in indices {
|
||||||
temp[i] = data[v]
|
temp[i] = data[v]
|
||||||
|
|||||||
@@ -92,3 +92,68 @@ test_sort_with_indices :: proc(t: ^testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test
|
||||||
|
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 :: []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)
|
||||||
|
r_idx := make([]int, test_size) // Reverse index
|
||||||
|
defer {
|
||||||
|
delete(vals)
|
||||||
|
delete(r_idx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up test values
|
||||||
|
for _, i in vals {
|
||||||
|
vals[i] = rand.uint64(&r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort
|
||||||
|
f_idx := slice.sort_with_indices(vals)
|
||||||
|
defer delete(f_idx)
|
||||||
|
|
||||||
|
// Verify sorted test values
|
||||||
|
rand.init(&r, seed)
|
||||||
|
|
||||||
|
{
|
||||||
|
indices := make([]int, test_size)
|
||||||
|
for _, i in indices {
|
||||||
|
indices[i] = i
|
||||||
|
}
|
||||||
|
|
||||||
|
sorted_indices := slice.sort_by_indices(indices, f_idx)
|
||||||
|
for v, i in sorted_indices {
|
||||||
|
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")
|
||||||
|
if !idx_pass {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
indices := make([]int, test_size)
|
||||||
|
for _, i in indices {
|
||||||
|
indices[i] = i
|
||||||
|
}
|
||||||
|
|
||||||
|
slice.sort_by_indices_overwrite(indices, f_idx)
|
||||||
|
for v, i in indices {
|
||||||
|
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")
|
||||||
|
if !idx_pass {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user