flabk/pkg/coll/vector_test.go

161 lines
3.9 KiB
Go

package coll_test
import (
"testing"
"github.com/stretchr/testify/require"
"sectorinf.com/emilis/flabk/pkg/coll"
)
func TestVector(t *testing.T) {
tests := map[string]func(that *require.Assertions){
"push": func(that *require.Assertions) {
v := coll.New[int]()
v = v.Push(0).Push(1).Push(2).Push(3).Push(4).Push(5)
that.Len(v, 6)
that.EqualValues([]int{0, 1, 2, 3, 4, 5}, v)
},
"len": func(that *require.Assertions) {
v := coll.From([]int{1, 2, 3})
that.Len(v, len(v))
},
"from": func(that *require.Assertions) {
v := coll.From([]int{1, 2, 3})
that.Len(v, 3)
},
"pop": func(that *require.Assertions) {
v := coll.From([]int{1, 2, 3})
t, v := v.Pop()
that.Equal(3, t)
that.EqualValues([]int{1, 2}, v)
},
"withcap": func(that *require.Assertions) {
v := coll.WithCap[int](3)
that.Zero(len(v))
that.Len(v, 0)
},
"remove": func(that *require.Assertions) {
v := coll.New[int]().Push(0).Push(1).Push(2)
r1 := v.Remove(1)
that.Equal(2, len(r1))
that.EqualValues([]int{0, 2}, r1)
that.Panics(func() {
v.Remove(-1)
})
that.Panics(func() {
v.Remove(100000)
})
},
"remove unordered": func(that *require.Assertions) {
v := coll.New[int]().Push(0).Push(1).Push(2)
v = v.RemoveUnordered(1)
that.Equal(2, len(v))
vSlice := v
if vSlice[0] == 0 {
that.Equal(2, vSlice[1])
} else {
that.Equal(2, vSlice[0])
that.Zero(vSlice[1])
}
},
"append": func(that *require.Assertions) {
v := coll.From([]int{1, 2, 3}).Append(4, 5, 6)
that.Equal(6, len(v))
that.EqualValues([]int{1, 2, 3, 4, 5, 6}, v)
},
"getsoft": func(that *require.Assertions) {
v := coll.From([]int{1, 2, 3})
that.Equal(2, v.GetSoft(1))
that.Zero(v.GetSoft(-1000))
that.Zero(v.GetSoft(1000))
},
"get": func(that *require.Assertions) {
v := coll.From([]int{1, 2, 3})
that.Equal(2, v.Get(1))
that.Panics(func() {
v.Get(-1000)
})
that.Panics(func() {
v.Get(1000)
})
},
"clone": func(that *require.Assertions) {
v := coll.From([]int{1, 2, 3})
v1 := v.Clone().Set(1, 1)
that.Equal(3, len(v1))
that.EqualValues([]int{1, 1, 3}, v1)
that.EqualValues([]int{1, 2, 3}, v)
},
"set": func(that *require.Assertions) {
v := coll.From([]int{1, 2, 3})
v1 := v.Clone().Set(1, 1)
that.Equal(3, len(v1))
that.EqualValues([]int{1, 1, 3}, v1)
v2 := v.Clone().Set(2, 1)
that.Equal(3, len(v2))
that.EqualValues([]int{1, 2, 1}, v2)
v3 := v.Clone().Set(0, 16)
that.Equal(3, len(v3))
that.EqualValues([]int{16, 2, 3}, v3)
that.Panics(func() {
v.Set(-1000, 1)
})
that.Panics(func() {
v.Set(3, 1)
})
},
"sub": func(that *require.Assertions) {
v := coll.From([]int{0, 1, 2, 3, 4, 5, 6})
v1 := v.Clone().Sub(2, 4)
that.Equal(2, len(v1))
that.EqualValues([]int{2, 3}, v1)
v2 := v.Clone().Sub(2, 5)
that.Equal(3, len(v2))
that.EqualValues([]int{2, 3, 4}, v2)
v3 := v.Clone().Sub(5, -1)
that.Equal(2, len(v3))
that.EqualValues([]int{5, 6}, v3)
v4 := v.Clone().Sub(-1, 2)
that.Equal(2, len(v4))
that.EqualValues([]int{0, 1}, v4)
that.Panics(func() {
v.Sub(0, 1000)
})
that.Panics(func() {
v.Sub(4000, 4002)
})
that.Panics(func() {
v.Sub(2, 1)
})
},
"filter": func(that *require.Assertions) {
v := coll.From([]int{1, 2, 3, 4, 5, 6})
v = v.Filter(func(i int) bool { return i%2 == 0 })
that.Len(v, 3)
},
"any": func(that *require.Assertions) {
that.True(coll.From([]int{1, 2, 3, 4, 5, 6}).Any(func(i int) bool { return i == 3 }))
that.False(coll.From([]int{1, 2, 3, 4, 5, 6}).Any(func(i int) bool { return i == 666 }))
},
"take": func(that *require.Assertions) {
that.EqualValues([]int{1, 2, 3}, coll.From([]int{1, 2, 3, 4, 5, 6}).Take(3))
},
}
for name, test := range tests {
t.Run(name, func(tt *testing.T) {
test(require.New(tt))
})
}
}