Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Nov 10, 2024
1 parent e549127 commit 68f3695
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions internal/sparse/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sparse

import (
"math/rand/v2"
"slices"
"testing"
)

Expand All @@ -18,13 +19,15 @@ func TestSparseArrayCount(t *testing.T) {

for i := range 10_000 {
a.InsertAt(uint(i), i)
a.InsertAt(uint(i), i)
}
if c := a.Count(); c != 10_000 {
t.Errorf("Count, expected 10_000, got %d", c)
}

for i := range 5_000 {
a.DeleteAt(uint(i))
a.DeleteAt(uint(i))
}
if c := a.Count(); c != 5_000 {
t.Errorf("Count, expected 5_000, got %d", c)
Expand Down Expand Up @@ -74,8 +77,8 @@ func TestSparseArrayMustGetPanic(t *testing.T) {
a.InsertAt(uint(i), i)
}

// it panics, runtime error: index out of range [-1]
_ = a.MustGet(0)
// must panic, runtime error: index out of range [-1]
a.MustGet(0)
}

func TestSparseArrayUpdate(t *testing.T) {
Expand Down Expand Up @@ -110,3 +113,33 @@ func TestSparseArrayUpdate(t *testing.T) {
}
}
}

func TestSparseArrayAllSetBits(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Errorf("buffer too small, expected panic")
}
}()

const n = 32
a := NewArray[int]()

want := make([]uint, 0, n)

for i := range n {
a.InsertAt(uint(i), i)
want = append(want, uint(i))
}

backingBuf := make([]uint, n)
got := a.AllSetBits(backingBuf)

if !slices.Equal(want, got) {
t.Errorf("AllSetBits, want:\n%v\ngot:\n%v\n", want, got)
}

// must panic
backingBuf = make([]uint, n/2)
a.AllSetBits(backingBuf)
}

0 comments on commit 68f3695

Please sign in to comment.