addedtest

This commit is contained in:
TheRadischen
2026-08-16 12:22:25 +02:00
parent 267c745ac7
commit df54692a9b
2 changed files with 46 additions and 1 deletions

View File

@@ -116,7 +116,7 @@ _stable_sort_general :: proc(data: $T/[]$E, call: $P, $KIND: Sort_Kind) {
left2 = first_cut
}
slice.rotate_left(data[first_cut:second_cut], left - first_cut)
rotate_left(data[first_cut:second_cut], left - first_cut)
new_mid := first_cut + right2
merge(data[:new_mid], left2, right2, call)

View File

@@ -140,6 +140,51 @@ test_sort_by_indices :: proc(t: ^testing.T) {
}
}
@test
test_sort_stability :: proc(t: ^testing.T) {
// Test sizes are all prime.
test_sizes :: []int{7, 13, 347, 1031, 10111, 100003}
Data :: struct {
rand: int,
index: int,
}
for test_size in test_sizes {
rand.reset(t.seed)
vals := make([]Data, test_size)
defer {
delete(vals)
}
// Set up test values
for _, i in vals {
vals[i].rand = rand.int_max(10)
vals[i].index = i
}
// Sort
slice.stable_sort_by(vals, proc(l, r: Data) -> bool {return l.rand < r.rand})
// Verify sorted test values
rand.reset(t.seed)
for i in 1..<len(vals) {
if vals[i - 1].rand < vals[i].rand {
continue
}
if vals[i - 1].rand > vals[i].rand {
testing.expect(t, false, "Expected slice to be sorted")
}
if vals[i - 1].index > vals[i].index {
testing.expect(t, false, "Expected slice to be stable")
}
}
}
}
@test
test_binary_search :: proc(t: ^testing.T) {
index: int