flabk/pkg/coll/vector_test.go

153 lines
3.5 KiB
Go
Raw Normal View History

2022-08-02 13:18:08 +01:00
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.Equal(6, v.Len())
that.Len(v.Slice(), 6)
that.Equal([]int{0, 1, 2, 3, 4, 5}, v.Slice())
},
"len": func(that *require.Assertions) {
v := coll.From([]int{1, 2, 3})
that.Equal(3, v.Len())
that.Len(v.Slice(), v.Len())
},
"from": func(that *require.Assertions) {
v := coll.From([]int{1, 2, 3})
that.Equal(3, v.Len())
that.Len(v.Slice(), 3)
},
"pop": func(that *require.Assertions) {
v := coll.From([]int{1, 2, 3})
t, v := v.Pop()
that.Equal(3, t)
that.Equal(2, v.Len())
that.Equal([]int{1, 2}, v.Slice())
},
"withcap": func(that *require.Assertions) {
v := coll.WithCap[int](3)
that.Zero(v.Len())
that.Len(v.Slice(), 0)
},
"remove": func(that *require.Assertions) {
v := coll.New[int]().Push(0).Push(1).Push(2)
r1 := v.Remove(1)
that.Equal(2, r1.Len())
that.Equal([]int{0, 2}, r1.Slice())
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, v.Len())
vSlice := v.Slice()
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, v.Len())
that.Equal([]int{1, 2, 3, 4, 5, 6}, v.Slice())
},
"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, v1.Len())
that.Equal([]int{1, 1, 3}, v1.Slice())
that.Equal([]int{1, 2, 3}, v.Slice())
},
"set": func(that *require.Assertions) {
v := coll.From([]int{1, 2, 3})
v1 := v.Clone().Set(1, 1)
that.Equal(3, v1.Len())
that.Equal([]int{1, 1, 3}, v1.Slice())
v2 := v.Clone().Set(2, 1)
that.Equal(3, v2.Len())
that.Equal([]int{1, 2, 1}, v2.Slice())
v3 := v.Clone().Set(0, 16)
that.Equal(3, v3.Len())
that.Equal([]int{16, 2, 3}, v3.Slice())
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, v1.Len())
that.Equal([]int{2, 3}, v1.Slice())
v2 := v.Clone().Sub(2, 5)
that.Equal(3, v2.Len())
that.Equal([]int{2, 3, 4}, v2.Slice())
v3 := v.Clone().Sub(5, -1)
that.Equal(2, v3.Len())
that.Equal([]int{5, 6}, v3.Slice())
v4 := v.Clone().Sub(-1, 2)
that.Equal(2, v4.Len())
that.Equal([]int{0, 1}, v4.Slice())
that.Panics(func() {
v.Sub(0, 1000)
})
that.Panics(func() {
v.Sub(4000, 4002)
})
that.Panics(func() {
v.Sub(2, 1)
})
},
}
for name, test := range tests {
t.Run(name, func(tt *testing.T) {
test(require.New(tt))
})
}
}