123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622 |
- package help
- import (
- "math"
- "reflect"
- "testing"
- "unsafe"
- )
- func TestAll(t *testing.T) {
- input := []int{2, 4, 6, 8}
- r1 := From(input).All(func(i interface{}) bool {
- return i.(int)%2 == 0
- })
- r2 := From(input).All(func(i interface{}) bool {
- return i.(int)%2 != 0
- })
- if !r1 {
- t.Errorf("From(%v).All()=%v", input, r1)
- }
- if r2 {
- t.Errorf("From(%v).All()=%v", input, r2)
- }
- }
- func TestAllT_PanicWhenPredicateFnIsInvalid(t *testing.T) {
- mustPanicWithError(t, "AllT: parameter [predicateFn] has a invalid function signature. Expected: 'func(T)bool', actual: 'func(int)int'", func() {
- From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).AllT(func(item int) int { return item + 2 })
- })
- }
- func TestAny(t *testing.T) {
- tests := []struct {
- input interface{}
- want bool
- }{
- {[]int{1, 2, 2, 3, 1}, true},
- {[9]int{1, 1, 1, 2, 1, 2, 3, 4, 2}, true},
- {"sstr", true},
- {[]int{}, false},
- }
- for _, test := range tests {
- if r := From(test.input).Any(); r != test.want {
- t.Errorf("From(%v).Any()=%v expected %v", test.input, r, test.want)
- }
- }
- }
- func TestAnyWith(t *testing.T) {
- tests := []struct {
- input interface{}
- want bool
- }{
- {[]int{1, 2, 2, 3, 1}, false},
- {[9]int{1, 1, 1, 2, 1, 2, 3, 4, 2}, true},
- {[]int{}, false},
- }
- for _, test := range tests {
- if r := From(test.input).AnyWith(func(i interface{}) bool {
- return i.(int) == 4
- }); r != test.want {
- t.Errorf("From(%v).Any()=%v expected %v", test.input, r, test.want)
- }
- }
- }
- func TestAnyWithT_PanicWhenPredicateFnIsInvalid(t *testing.T) {
- mustPanicWithError(t, "AnyWithT: parameter [predicateFn] has a invalid function signature. Expected: 'func(T)bool', actual: 'func(int)int'", func() {
- From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).AnyWithT(func(item int) int { return item + 2 })
- })
- }
- func TestAverage(t *testing.T) {
- tests := []struct {
- input interface{}
- want float64
- }{
- {[]int{1, 2, 2, 3, 1}, 1.8},
- {[5]uint{1, 2, 5, 7, 10}, 5.},
- {[]float32{1., 1.}, 1.},
- }
- for _, test := range tests {
- if r := From(test.input).Average(); r != test.want {
- t.Errorf("From(%v).Average()=%v expected %v", test.input, r, test.want)
- }
- }
- }
- func TestAverageForNaN(t *testing.T) {
- if r := From([]int{}).Average(); !math.IsNaN(r) {
- t.Errorf("From([]int{}).Average()=%v expected %v", r, math.NaN())
- }
- }
- func TestContains(t *testing.T) {
- tests := []struct {
- input interface{}
- value interface{}
- want bool
- }{
- {[]int{1, 2, 2, 3, 1}, 10, false},
- {[5]uint{1, 2, 5, 7, 10}, uint(5), true},
- {[]float32{}, 1., false},
- }
- for _, test := range tests {
- if r := From(test.input).Contains(test.value); r != test.want {
- t.Errorf("From(%v).Contains(%v)=%v expected %v", test.input, test.value, r, test.want)
- }
- }
- }
- func TestCount(t *testing.T) {
- tests := []struct {
- input interface{}
- want int
- }{
- {[]int{1, 2, 2, 3, 1}, 5},
- {[7]uint{1, 2, 5, 7, 10, 12, 15}, 7},
- {[]float32{}, 0},
- }
- for _, test := range tests {
- if r := From(test.input).Count(); r != test.want {
- t.Errorf("From(%v).Count()=%v expected %v", test.input, r, test.want)
- }
- }
- }
- func TestCountWith(t *testing.T) {
- tests := []struct {
- input interface{}
- want int
- }{
- {[]int{1, 2, 2, 3, 1}, 4},
- {[]int{}, 0},
- }
- for _, test := range tests {
- if r := From(test.input).CountWith(func(i interface{}) bool {
- return i.(int) <= 2
- }); r != test.want {
- t.Errorf("From(%v).CountWith()=%v expected %v", test.input, r, test.want)
- }
- }
- }
- func TestCountWithT_PanicWhenPredicateFnIsInvalid(t *testing.T) {
- mustPanicWithError(t, "CountWithT: parameter [predicateFn] has a invalid function signature. Expected: 'func(T)bool', actual: 'func(int)int'", func() {
- From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).CountWithT(func(item int) int { return item + 2 })
- })
- }
- func TestFirst(t *testing.T) {
- tests := []struct {
- input interface{}
- want interface{}
- }{
- {[]int{1, 2, 2, 3, 1}, 1},
- {[]int{}, nil},
- }
- for _, test := range tests {
- if r := From(test.input).First(); r != test.want {
- t.Errorf("From(%v).First()=%v expected %v", test.input, r, test.want)
- }
- }
- }
- func TestFirstWith(t *testing.T) {
- tests := []struct {
- input interface{}
- want interface{}
- }{
- {[]int{1, 2, 2, 3, 1}, 3},
- {[]int{}, nil},
- }
- for _, test := range tests {
- if r := From(test.input).FirstWith(func(i interface{}) bool {
- return i.(int) > 2
- }); r != test.want {
- t.Errorf("From(%v).FirstWith()=%v expected %v", test.input, r, test.want)
- }
- }
- }
- func TestFirstWithT_PanicWhenPredicateFnIsInvalid(t *testing.T) {
- mustPanicWithError(t, "FirstWithT: parameter [predicateFn] has a invalid function signature. Expected: 'func(T)bool', actual: 'func(int)int'", func() {
- From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).FirstWithT(func(item int) int { return item + 2 })
- })
- }
- func TestForEach(t *testing.T) {
- tests := []struct {
- input interface{}
- want interface{}
- }{
- {[5]int{1, 2, 2, 35, 111}, []int{2, 4, 4, 70, 222}},
- {[]int{}, []int{}},
- }
- for _, test := range tests {
- output := []int{}
- From(test.input).ForEach(func(item interface{}) {
- output = append(output, item.(int)*2)
- })
- if !reflect.DeepEqual(output, test.want) {
- t.Fatalf("From(%#v).ForEach()=%#v expected=%#v", test.input, output, test.want)
- }
- }
- }
- func TestForEachT_PanicWhenActionFnIsInvalid(t *testing.T) {
- mustPanicWithError(t, "ForEachT: parameter [actionFn] has a invalid function signature. Expected: 'func(T)', actual: 'func(int,int)'", func() {
- From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).ForEachT(func(item, idx int) { item = item + 2 })
- })
- }
- func TestForEachIndexed(t *testing.T) {
- tests := []struct {
- input interface{}
- want interface{}
- }{
- {[5]int{1, 2, 2, 35, 111}, []int{1, 3, 4, 38, 115}},
- {[]int{}, []int{}},
- }
- for _, test := range tests {
- output := []int{}
- From(test.input).ForEachIndexed(func(index int, item interface{}) {
- output = append(output, item.(int)+index)
- })
- if !reflect.DeepEqual(output, test.want) {
- t.Fatalf("From(%#v).ForEachIndexed()=%#v expected=%#v", test.input, output, test.want)
- }
- }
- }
- func TestForEachIndexedT_PanicWhenActionFnIsInvalid(t *testing.T) {
- mustPanicWithError(t, "ForEachIndexedT: parameter [actionFn] has a invalid function signature. Expected: 'func(int,T)', actual: 'func(int)'", func() {
- From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).ForEachIndexedT(func(item int) { item = item + 2 })
- })
- }
- func TestLast(t *testing.T) {
- tests := []struct {
- input interface{}
- want interface{}
- }{
- {[]int{1, 2, 2, 3, 1}, 1},
- {[]int{}, nil},
- }
- for _, test := range tests {
- if r := From(test.input).Last(); r != test.want {
- t.Errorf("From(%v).Last()=%v expected %v", test.input, r, test.want)
- }
- }
- }
- func TestLastWith(t *testing.T) {
- tests := []struct {
- input interface{}
- want interface{}
- }{
- {[]int{1, 2, 2, 3, 1, 4, 2, 5, 1, 1}, 5},
- {[]int{}, nil},
- }
- for _, test := range tests {
- if r := From(test.input).LastWith(func(i interface{}) bool {
- return i.(int) > 2
- }); r != test.want {
- t.Errorf("From(%v).LastWith()=%v expected %v", test.input, r, test.want)
- }
- }
- }
- func TestLastWithT_PanicWhenPredicateFnIsInvalid(t *testing.T) {
- mustPanicWithError(t, "LastWithT: parameter [predicateFn] has a invalid function signature. Expected: 'func(T)bool', actual: 'func(int)int'", func() {
- From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).LastWithT(func(item int) int { return item + 2 })
- })
- }
- func TestMax(t *testing.T) {
- tests := []struct {
- input interface{}
- want interface{}
- }{
- {[]int{1, 2, 2, 3, 1}, 3},
- {[]int{1}, 1},
- {[]int{}, nil},
- }
- for _, test := range tests {
- if r := From(test.input).Max(); r != test.want {
- t.Errorf("From(%v).Max()=%v expected %v", test.input, r, test.want)
- }
- }
- }
- func TestMin(t *testing.T) {
- tests := []struct {
- input interface{}
- want interface{}
- }{
- {[]int{1, 2, 2, 3, 0}, 0},
- {[]int{1}, 1},
- {[]int{}, nil},
- }
- for _, test := range tests {
- if r := From(test.input).Min(); r != test.want {
- t.Errorf("From(%v).Min()=%v expected %v", test.input, r, test.want)
- }
- }
- }
- func TestResults(t *testing.T) {
- input := []int{1, 2, 3}
- want := []interface{}{1, 2, 3}
- if r := From(input).Results(); !reflect.DeepEqual(r, want) {
- t.Errorf("From(%v).Raw()=%v expected %v", input, r, want)
- }
- }
- func TestSequenceEqual(t *testing.T) {
- tests := []struct {
- input interface{}
- input2 interface{}
- want bool
- }{
- {[]int{1, 2, 2, 3, 1}, []int{4, 6}, false},
- {[]int{1, -1, 100}, []int{1, -1, 100}, true},
- {[]int{}, []int{}, true},
- }
- for _, test := range tests {
- if r := From(test.input).SequenceEqual(From(test.input2)); r != test.want {
- t.Errorf("From(%v).SequenceEqual(%v)=%v expected %v", test.input, test.input2, r, test.want)
- }
- }
- }
- func TestSingle(t *testing.T) {
- tests := []struct {
- input interface{}
- want interface{}
- }{
- {[]int{1, 2, 2, 3, 1}, nil},
- {[]int{1}, 1},
- {[]int{}, nil},
- }
- for _, test := range tests {
- if r := From(test.input).Single(); r != test.want {
- t.Errorf("From(%v).Single()=%v expected %v", test.input, r, test.want)
- }
- }
- }
- func TestSingleWith(t *testing.T) {
- tests := []struct {
- input interface{}
- want interface{}
- }{
- {[]int{1, 2, 2, 3, 1}, 3},
- {[]int{1, 1, 1}, nil},
- {[]int{5, 1, 1, 10, 2, 2}, nil},
- {[]int{}, nil},
- }
- for _, test := range tests {
- if r := From(test.input).SingleWith(func(i interface{}) bool {
- return i.(int) > 2
- }); r != test.want {
- t.Errorf("From(%v).SingleWith()=%v expected %v", test.input, r, test.want)
- }
- }
- }
- func TestSingleWithT_PanicWhenPredicateFnIsInvalid(t *testing.T) {
- mustPanicWithError(t, "SingleWithT: parameter [predicateFn] has a invalid function signature. Expected: 'func(T)bool', actual: 'func(int)int'", func() {
- From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).SingleWithT(func(item int) int { return item + 2 })
- })
- }
- func TestSumInts(t *testing.T) {
- tests := []struct {
- input interface{}
- want int64
- }{
- {[]int{1, 2, 2, 3, 1}, 9},
- {[]int{1}, 1},
- {[]int{}, 0},
- }
- for _, test := range tests {
- if r := From(test.input).SumInts(); r != test.want {
- t.Errorf("From(%v).SumInts()=%v expected %v", test.input, r, test.want)
- }
- }
- }
- func TestSumUInts(t *testing.T) {
- tests := []struct {
- input interface{}
- want uint64
- }{
- {[]uint{1, 2, 2, 3, 1}, 9},
- {[]uint{1}, 1},
- {[]uint{}, 0},
- }
- for _, test := range tests {
- if r := From(test.input).SumUInts(); r != test.want {
- t.Errorf("From(%v).SumInts()=%v expected %v", test.input, r, test.want)
- }
- }
- }
- func TestSumFloats(t *testing.T) {
- tests := []struct {
- input interface{}
- want float64
- }{
- {[]float32{1., 2., 2., 3., 1.}, 9.},
- {[]float64{1.}, 1.},
- {[]float32{}, 0.},
- }
- for _, test := range tests {
- if r := From(test.input).SumFloats(); r != test.want {
- t.Errorf("From(%v).SumFloats()=%v expected %v", test.input, r, test.want)
- }
- }
- }
- func TestToChannel(t *testing.T) {
- c := make(chan interface{})
- input := []int{1, 2, 3, 4, 5}
- go func() {
- From(input).ToChannel(c)
- }()
- result := []int{}
- for value := range c {
- result = append(result, value.(int))
- }
- if !reflect.DeepEqual(result, input) {
- t.Errorf("From(%v).ToChannel()=%v expected %v", input, result, input)
- }
- }
- func TestToChannelT(t *testing.T) {
- c := make(chan string)
- input := []string{"1", "2", "3", "4", "5"}
- go From(input).ToChannelT(c)
- result := []string{}
- for value := range c {
- result = append(result, value)
- }
- if !reflect.DeepEqual(result, input) {
- t.Errorf("From(%v).ToChannelT()=%v expected %v", input, result, input)
- }
- }
- func TestToMap(t *testing.T) {
- input := make(map[int]bool)
- input[1] = true
- input[2] = false
- input[3] = true
- result := make(map[int]bool)
- From(input).ToMap(&result)
- if !reflect.DeepEqual(result, input) {
- t.Errorf("From(%v).ToMap()=%v expected %v", input, result, input)
- }
- }
- func TestToMapBy(t *testing.T) {
- input := make(map[int]bool)
- input[1] = true
- input[2] = false
- input[3] = true
- result := make(map[int]bool)
- From(input).ToMapBy(&result,
- func(i interface{}) interface{} {
- return i.(KeyValue).Key
- },
- func(i interface{}) interface{} {
- return i.(KeyValue).Value
- })
- if !reflect.DeepEqual(result, input) {
- t.Errorf("From(%v).ToMapBy()=%v expected %v", input, result, input)
- }
- }
- func TestToMapByT_PanicWhenKeySelectorFnIsInvalid(t *testing.T) {
- mustPanicWithError(t, "ToMapByT: parameter [keySelectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(int,int)int'", func() {
- result := make(map[int]bool)
- From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).ToMapByT(
- &result,
- func(item, j int) int { return item + 2 },
- func(item int) int { return item + 2 },
- )
- })
- }
- func TestToMapByT_PanicWhenValueSelectorFnIsInvalid(t *testing.T) {
- mustPanicWithError(t, "ToMapByT: parameter [valueSelectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(int,int)int'", func() {
- result := make(map[int]bool)
- From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).ToMapByT(
- &result,
- func(item int) int { return item + 2 },
- func(item, j int) int { return item + 2 },
- )
- })
- }
- func TestToSlice(t *testing.T) {
- tests := []struct {
- input []int
- output []int
- want []int
- wantedOutputCap int
- outputIsANewSlice bool
- }{
- // output is nil slice
- {
- []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
- nil,
- []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
- 16,
- true},
- // output is empty slice (cap=0)
- {
- []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
- []int{},
- []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
- 16,
- true},
- // ToSlice() overwrites existing elements and reslices.
- {[]int{1, 2, 3},
- []int{99, 98, 97, 96, 95},
- []int{1, 2, 3},
- 5,
- false},
- // cap(out)>len(result): we get the same slice, resliced. cap unchanged.
- {[]int{1, 2, 3, 4, 5},
- make([]int, 0, 11),
- []int{1, 2, 3, 4, 5},
- 11,
- false},
- // cap(out)==len(result): we get the same slice, cap unchanged.
- {[]int{1, 2, 3, 4, 5},
- make([]int, 0, 5),
- []int{1, 2, 3, 4, 5},
- 5,
- false},
- // cap(out)<len(result): we get a new slice with len(out)=len(result) and cap doubled: cap(out')==2*cap(out)
- {[]int{1, 2, 3, 4, 5},
- make([]int, 0, 4),
- []int{1, 2, 3, 4, 5},
- 8,
- true},
- // cap(out)<<len(result): trigger capacity to double more than once (26 -> 52 -> 104)
- {make([]int, 100),
- make([]int, 0, 26),
- make([]int, 100),
- 104,
- true},
- // len(out) > len(result): we get the same slice with len(out)=len(result) and cap unchanged: cap(out')==cap(out)
- {[]int{1, 2, 3, 4, 5},
- make([]int, 0, 50),
- []int{1, 2, 3, 4, 5},
- 50,
- false},
- }
- for c, test := range tests {
- initialOutputValue := test.output
- From(test.input).ToSlice(&test.output)
- modifiedOutputValue := test.output
- // test slice values
- if !reflect.DeepEqual(test.output, test.want) {
- t.Fatalf("case #%d: From(%#v).ToSlice()=%#v expected=%#v", c, test.input, test.output, test.want)
- }
- // test capacity of output slice
- if cap(test.output) != test.wantedOutputCap {
- t.Fatalf("case #%d: cap(output)=%d expected=%d", c, cap(test.output), test.wantedOutputCap)
- }
- // test if a new slice is allocated
- inPtr := (*reflect.SliceHeader)(unsafe.Pointer(&initialOutputValue)).Data
- outPtr := (*reflect.SliceHeader)(unsafe.Pointer(&modifiedOutputValue)).Data
- isNewSlice := inPtr != outPtr
- if isNewSlice != test.outputIsANewSlice {
- t.Fatalf("case #%d: isNewSlice=%v (in=0x%X out=0x%X) expected=%v", c, isNewSlice, inPtr, outPtr, test.outputIsANewSlice)
- }
- }
- }
|