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