From df54692a9b05f6a9e0a86d97ebc3f05ab3fd9e37 Mon Sep 17 00:00:00 2001 From: TheRadischen Date: Sun, 16 Aug 2026 12:22:25 +0200 Subject: [PATCH] addedtest --- core/slice/sort_private.odin | 2 +- tests/core/slice/test_core_slice.odin | 45 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/core/slice/sort_private.odin b/core/slice/sort_private.odin index 35886c8c4..f568671a8 100644 --- a/core/slice/sort_private.odin +++ b/core/slice/sort_private.odin @@ -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) diff --git a/tests/core/slice/test_core_slice.odin b/tests/core/slice/test_core_slice.odin index b0fb791a6..4f1148983 100644 --- a/tests/core/slice/test_core_slice.odin +++ b/tests/core/slice/test_core_slice.odin @@ -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.. 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