odin-lang / Odin

Odin Programming Language
https://odin-lang.org
BSD 3-Clause "New" or "Revised" License
6.12k stars 550 forks source link

`slice.unique` still returns incorrect slice #3769

Closed skaman closed 2 weeks ago

skaman commented 2 weeks ago

Context

Odin: dev-2024-06-nightly:f745a1c47 OS: Windows 11 Professional (version: 23H2), build 22631.3737 CPU: AMD Ryzen Threadripper 3970X 32-Core Processor RAM: 65414 MiB Backend: LLVM 17.0.1

Expected Behavior

Given this example code

tmp := slice.unique([]int{1, 2, 4, 4, 5})
for v, i in tmp {
    fmt.printfln("index: %d, value: %d", i, v)
}

It should output:

index: 0, value: 1
index: 1, value: 2
index: 2, value: 4
index: 3, value: 5

Current Behavior

The output that currently i get is:

index: 0, value: 1
index: 1, value: 4
index: 2, value: 5

Failure Information (for bugs)

I think the bug is related to:

In detail the index i not increase when it's equal with j but it should.

Steps to Reproduce

  1. Paste the example code from "Expected Behavior" into the main proc
  2. Run it
Kelimion commented 2 weeks ago

Interesting. The previous fix did indeed fix the examples given there:

    s := []int{2,2,2}
    res := slice.unique(s)
    assert(slice.equal(res,[]int{2}))

    res = slice.unique([]int{1,1,1,2,2,3,3,3,3})
    assert(slice.equal(res, []int{1,2,3}))

    res = slice.unique_proc([]int{1,1,1,2,2,3,3,3,3}, proc(a, b: int) -> bool {
        return a == b
    })
    assert(slice.equal(res, []int{1,2,3}))

But I can also reproduce the bug you found using that old fix. I'll have a look at your fix momentarily and will write more extensive test coverage for tests/core/slice.

Kelimion commented 2 weeks ago

Fixed by #3770.