Skip to content

Commit

Permalink
Merge pull request #19315 from mmorel-35/testifier/pkg
Browse files Browse the repository at this point in the history
fix: use testify instead of t.Fatal or t.Error in pkg package
  • Loading branch information
ahrtr authored Feb 3, 2025
2 parents d4173b2 + cad3fce commit f6713f8
Show file tree
Hide file tree
Showing 21 changed files with 223 additions and 473 deletions.
82 changes: 22 additions & 60 deletions pkg/adt/interval_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -272,32 +273,18 @@ func TestIntervalTreeIntersects(t *testing.T) {
ivt := NewIntervalTree()
ivt.Insert(NewStringInterval("1", "3"), 123)

if ivt.Intersects(NewStringPoint("0")) {
t.Errorf("contains 0")
}
if !ivt.Intersects(NewStringPoint("1")) {
t.Errorf("missing 1")
}
if !ivt.Intersects(NewStringPoint("11")) {
t.Errorf("missing 11")
}
if !ivt.Intersects(NewStringPoint("2")) {
t.Errorf("missing 2")
}
if ivt.Intersects(NewStringPoint("3")) {
t.Errorf("contains 3")
}
assert.Falsef(t, ivt.Intersects(NewStringPoint("0")), "contains 0")
assert.Truef(t, ivt.Intersects(NewStringPoint("1")), "missing 1")
assert.Truef(t, ivt.Intersects(NewStringPoint("11")), "missing 11")
assert.Truef(t, ivt.Intersects(NewStringPoint("2")), "missing 2")
assert.Falsef(t, ivt.Intersects(NewStringPoint("3")), "contains 3")
}

func TestIntervalTreeStringAffine(t *testing.T) {
ivt := NewIntervalTree()
ivt.Insert(NewStringAffineInterval("8", ""), 123)
if !ivt.Intersects(NewStringAffinePoint("9")) {
t.Errorf("missing 9")
}
if ivt.Intersects(NewStringAffinePoint("7")) {
t.Errorf("contains 7")
}
assert.Truef(t, ivt.Intersects(NewStringAffinePoint("9")), "missing 9")
assert.Falsef(t, ivt.Intersects(NewStringAffinePoint("7")), "contains 7")
}

func TestIntervalTreeStab(t *testing.T) {
Expand All @@ -310,27 +297,13 @@ func TestIntervalTreeStab(t *testing.T) {

tr := ivt.(*intervalTree)
require.Equalf(t, 0, tr.root.max.Compare(StringComparable("8")), "wrong root max got %v, expected 8", tr.root.max)
if x := len(ivt.Stab(NewStringPoint("0"))); x != 3 {
t.Errorf("got %d, expected 3", x)
}
if x := len(ivt.Stab(NewStringPoint("1"))); x != 2 {
t.Errorf("got %d, expected 2", x)
}
if x := len(ivt.Stab(NewStringPoint("2"))); x != 1 {
t.Errorf("got %d, expected 1", x)
}
if x := len(ivt.Stab(NewStringPoint("3"))); x != 0 {
t.Errorf("got %d, expected 0", x)
}
if x := len(ivt.Stab(NewStringPoint("5"))); x != 1 {
t.Errorf("got %d, expected 1", x)
}
if x := len(ivt.Stab(NewStringPoint("55"))); x != 1 {
t.Errorf("got %d, expected 1", x)
}
if x := len(ivt.Stab(NewStringPoint("6"))); x != 1 {
t.Errorf("got %d, expected 1", x)
}
assert.Len(t, ivt.Stab(NewStringPoint("0")), 3)
assert.Len(t, ivt.Stab(NewStringPoint("1")), 2)
assert.Len(t, ivt.Stab(NewStringPoint("2")), 1)
assert.Empty(t, ivt.Stab(NewStringPoint("3")))
assert.Len(t, ivt.Stab(NewStringPoint("5")), 1)
assert.Len(t, ivt.Stab(NewStringPoint("55")), 1)
assert.Len(t, ivt.Stab(NewStringPoint("6")), 1)
}

type xy struct {
Expand Down Expand Up @@ -368,15 +341,11 @@ func TestIntervalTreeRandom(t *testing.T) {
require.NotEmptyf(t, ivt.Stab(NewInt64Point(v)), "expected %v stab non-zero for [%+v)", v, xy)
require.Truef(t, ivt.Intersects(NewInt64Point(v)), "did not get %d as expected for [%+v)", v, xy)
}
if !ivt.Delete(NewInt64Interval(ab.x, ab.y)) {
t.Errorf("did not delete %v as expected", ab)
}
assert.Truef(t, ivt.Delete(NewInt64Interval(ab.x, ab.y)), "did not delete %v as expected", ab)
delete(ivs, ab)
}

if ivt.Len() != 0 {
t.Errorf("got ivt.Len() = %v, expected 0", ivt.Len())
}
assert.Equalf(t, 0, ivt.Len(), "got ivt.Len() = %v, expected 0", ivt.Len())
}

// TestIntervalTreeSortedVisit tests that intervals are visited in sorted order.
Expand Down Expand Up @@ -417,17 +386,13 @@ func TestIntervalTreeSortedVisit(t *testing.T) {
last := tt.ivls[0].Begin
count := 0
chk := func(iv *IntervalValue) bool {
if last.Compare(iv.Ivl.Begin) > 0 {
t.Errorf("#%d: expected less than %d, got interval %+v", i, last, iv.Ivl)
}
assert.LessOrEqualf(t, last.Compare(iv.Ivl.Begin), 0, "#%d: expected less than %d, got interval %+v", i, last, iv.Ivl)
last = iv.Ivl.Begin
count++
return true
}
ivt.Visit(tt.visitRange, chk)
if count != len(tt.ivls) {
t.Errorf("#%d: did not cover all intervals. expected %d, got %d", i, len(tt.ivls), count)
}
assert.Lenf(t, tt.ivls, count, "#%d: did not cover all intervals. expected %d, got %d", i, len(tt.ivls), count)
}
}

Expand Down Expand Up @@ -468,9 +433,7 @@ func TestIntervalTreeVisitExit(t *testing.T) {
count++
return tt.f(n)
})
if count != tt.wcount {
t.Errorf("#%d: expected count %d, got %d", i, tt.wcount, count)
}
assert.Equalf(t, count, tt.wcount, "#%d: expected count %d, got %d", i, tt.wcount, count)
}
}

Expand Down Expand Up @@ -530,8 +493,7 @@ func TestIntervalTreeContains(t *testing.T) {
for _, ivl := range tt.ivls {
ivt.Insert(ivl, struct{}{})
}
if v := ivt.Contains(tt.chkIvl); v != tt.wContains {
t.Errorf("#%d: ivt.Contains got %v, expected %v", i, v, tt.wContains)
}
v := ivt.Contains(tt.chkIvl)
assert.Equalf(t, v, tt.wContains, "#%d: ivt.Contains got %v, expected %v", i, v, tt.wContains)
}
}
31 changes: 8 additions & 23 deletions pkg/crc/crc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -21,38 +22,22 @@ func TestHash32(t *testing.T) {
// create a new hash with stdhash.Sum32() as initial crc
hash := New(stdhash.Sum32(), crc32.IEEETable)

wsize := stdhash.Size()
if g := hash.Size(); g != wsize {
t.Errorf("size = %d, want %d", g, wsize)
}
wbsize := stdhash.BlockSize()
if g := hash.BlockSize(); g != wbsize {
t.Errorf("block size = %d, want %d", g, wbsize)
}
wsum32 := stdhash.Sum32()
if g := hash.Sum32(); g != wsum32 {
t.Errorf("Sum32 = %d, want %d", g, wsum32)
}
assert.Equalf(t, hash.Size(), stdhash.Size(), "size")
assert.Equalf(t, hash.BlockSize(), stdhash.BlockSize(), "block size")
assert.Equalf(t, hash.Sum32(), stdhash.Sum32(), "Sum32")
wsum := stdhash.Sum(make([]byte, 32))
if g := hash.Sum(make([]byte, 32)); !reflect.DeepEqual(g, wsum) {
t.Errorf("sum = %v, want %v", g, wsum)
}
g := hash.Sum(make([]byte, 32))
assert.Truef(t, reflect.DeepEqual(g, wsum), "sum")

// write something
_, err = stdhash.Write([]byte("test data"))
require.NoErrorf(t, err, "unexpected write error: %v", err)
_, err = hash.Write([]byte("test data"))
require.NoErrorf(t, err, "unexpected write error: %v", err)
wsum32 = stdhash.Sum32()
if g := hash.Sum32(); g != wsum32 {
t.Errorf("Sum32 after write = %d, want %d", g, wsum32)
}
assert.Equalf(t, hash.Sum32(), stdhash.Sum32(), "Sum32 after write")

// reset
stdhash.Reset()
hash.Reset()
wsum32 = stdhash.Sum32()
if g := hash.Sum32(); g != wsum32 {
t.Errorf("Sum32 after reset = %d, want %d", g, wsum32)
}
assert.Equalf(t, hash.Sum32(), stdhash.Sum32(), "Sum32 after reset")
}
Loading

0 comments on commit f6713f8

Please sign in to comment.