-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs_ordering_test.go
54 lines (50 loc) · 1.33 KB
/
funcs_ordering_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package handy
import (
"cmp"
"github.com/hsldymq/goiter"
"slices"
"testing"
)
func TestOrder(t *testing.T) {
e := NewEnumerator(goiter.SliceElems([]int{3, 2, 1}))
actual := []int{}
for v := range Order[int](e).Iter() {
actual = append(actual, v)
}
expect := []int{1, 2, 3}
if !slices.Equal(expect, actual) {
t.Fatalf("test Order, expect: %v, actual: %v", expect, actual)
}
e = NewEnumerator(goiter.SliceElems([]int{1, 2, 3}))
actual = []int{}
for v := range Order[int](e, true).Iter() {
actual = append(actual, v)
}
expect = []int{3, 2, 1}
if !slices.Equal(expect, actual) {
t.Fatalf("test Order, expect: %v, actual: %v", expect, actual)
}
}
func TestOrderBy(t *testing.T) {
type person struct {
Name string
Age int
}
e := NewEnumerator(goiter.SliceElems([]person{
{"Alice", 20},
{"Charlie", 22},
{"Bob", 18},
}))
actual := []person{}
for v := range orderBy(e, func(a, b person) int { return cmp.Compare(a.Age, b.Age) }).Iter() {
actual = append(actual, v)
}
expect := []person{
{"Bob", 18},
{"Alice", 20},
{"Charlie", 22},
}
if !slices.Equal(expect, actual) {
t.Fatalf("test orderBy, expect: %v, actual: %v", expect, actual)
}
}