-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
intersect_test.go
74 lines (64 loc) · 1.29 KB
/
intersect_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package linq_test
import (
"reflect"
"testing"
"github.com/makiuchi-d/linq/v2"
)
func TestIntersect(t *testing.T) {
fst := linq.FromSlice([]int{1, 2, 3, 4, 5, 6, 7})
snd := linq.FromSlice([]int{0, 2, 4, 6, 8, 10})
e := linq.Intersect(
fst, snd,
func(a, b int) (bool, error) { return a == b, nil },
func(a int) (int, error) { return a / 3, nil })
r, err := linq.ToSlice(e)
if err != nil {
t.Fatalf("%v", err)
}
exp := []int{2, 4, 6}
if !reflect.DeepEqual(r, exp) {
t.Fatalf("%v, wants %v", r, exp)
}
}
func TestIntersectBy(t *testing.T) {
type PlanetType int
const (
Rock PlanetType = iota
Ice
Gas
Liquid
)
type Planet struct {
Name string
Type PlanetType
OrderFromSun int
}
p1 := []Planet{
{"Marcury", Rock, 1},
{"Venus", Rock, 2},
{"Earth", Rock, 3},
{"Jupiter", Gas, 5},
}
p2 := []Planet{
{"Marcury", Rock, 1},
{"Earth", Rock, 3},
{"Mars", Rock, 4},
{"Jupiter", Gas, 5},
}
e := linq.IntersectBy(
linq.FromSlice(p1),
linq.FromSlice(p2),
func(p Planet) (string, error) { return p.Name, nil })
r, err := linq.ToSlice(e)
if err != nil {
t.Fatalf("%v", err)
}
exp := []Planet{
{"Marcury", Rock, 1},
{"Earth", Rock, 3},
{"Jupiter", Gas, 5},
}
if !reflect.DeepEqual(r, exp) {
t.Fatalf("%v, wants %v", r, exp)
}
}