From e41878a64f3de4ba1eefe9f2364156f3065e4a40 Mon Sep 17 00:00:00 2001 From: Sandro Cavazzoni Date: Sun, 16 Jun 2024 11:36:20 +0200 Subject: [PATCH 1/2] Fix `slice.unique` wrong result When you try to make this array unique `[]int{1, 2, 4, 4, 5}` you get `[]int{1, 4, 5}` instead of `[]int{1, 2, 4, 5}`. Our index `i` should be increased even with both indices `i` and `j` have the same value --- core/slice/slice.odin | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/core/slice/slice.odin b/core/slice/slice.odin index d8f4df88e..06f08fda2 100644 --- a/core/slice/slice.odin +++ b/core/slice/slice.odin @@ -495,8 +495,10 @@ unique :: proc(s: $S/[]$T) -> S where intrinsics.type_is_comparable(T) #no_bound } i := 1 for j in 1.. bool) -> S #no_bounds_check { } i := 1 for j in 1.. Date: Sun, 16 Jun 2024 13:41:25 +0200 Subject: [PATCH 2/2] Add tests for `slice.unique` and `slice.unique_proc` --- tests/core/slice/test_core_slice.odin | 62 ++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/tests/core/slice/test_core_slice.odin b/tests/core/slice/test_core_slice.odin index 432636664..76aa4756c 100644 --- a/tests/core/slice/test_core_slice.odin +++ b/tests/core/slice/test_core_slice.odin @@ -203,7 +203,7 @@ test_permutation_iterator :: proc(t: ^testing.T) { permutations_counted: int for slice.permute(&iter) { n := 0 - for item, index in s { + for item in s { n *= 10 n += item } @@ -218,3 +218,63 @@ test_permutation_iterator :: proc(t: ^testing.T) { testing.expect_value(t, len(seen), FAC_5) testing.expect_value(t, permutations_counted, FAC_5) } + +// Test inputs from #3276 and #3769 +UNIQUE_TEST_VECTORS :: [][2][]int{ + {{2,2,2}, {2}}, + {{1,1,1,2,2,3,3,3,3}, {1,2,3}}, + {{1,2,4,4,5}, {1,2,4,5}}, +} + +@test +test_unique :: proc(t: ^testing.T) { + for v in UNIQUE_TEST_VECTORS { + assorted := v[0] + expected := v[1] + + uniq := slice.unique(assorted) + testing.expectf(t, slice.equal(uniq, expected), "Expected slice.uniq(%v) == %v, got %v", v[0], v[1], uniq) + } + + for v in UNIQUE_TEST_VECTORS { + assorted := v[0] + expected := v[1] + + uniq := slice.unique_proc(assorted, proc(a, b: int) -> bool { + return a == b + }) + testing.expectf(t, slice.equal(uniq, expected), "Expected slice.unique_proc(%v, ...) == %v, got %v", v[0], v[1], uniq) + } + + r := rand.create(t.seed) + context.random_generator = rand.default_random_generator(&r) + + // 10_000 random tests + for _ in 0..<10_000 { + assorted: [dynamic]i64 + expected: [dynamic]i64 + + // Prime with 1 value + old := rand.int63() + append(&assorted, old) + append(&expected, old) + + // Add 99 additional random values + for _ in 1..<100 { + new := rand.int63() + append(&assorted, new) + if old != new { + append(&expected, new) + } + old = new + } + + original := slice.clone(assorted[:]) + uniq := slice.unique(assorted[:]) + testing.expectf(t, slice.equal(uniq, expected[:]), "Expected slice.uniq(%v) == %v, got %v", original, expected, uniq) + + delete(assorted) + delete(original) + delete(expected) + } +} \ No newline at end of file