-
Notifications
You must be signed in to change notification settings - Fork 0
/
zip_test.go
99 lines (83 loc) · 2.26 KB
/
zip_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package functools
import (
"reflect"
"testing"
)
type testZipStruct struct {
a int
b string
}
func TestZipMore(t *testing.T) {
cases := []struct {
in []interface{}
want interface{}
}{
{[]interface{}{[][]int{{1, 4}, {2, 5}, {3, 6}}, []int{4, 5, 6}},
[][]int{{1, 4}, {2, 5}, {3, 6}}},
{[]interface{}{[]int{1, 2, 3}, []int{4, 5, 6}},
[][]int{{1, 4}, {2, 5}, {3, 6}}},
}
for _, c := range cases {
got := Zip(c.in...)
if !reflect.DeepEqual(got, c.want) {
t.Errorf("Zip(%v...) == %v want %v", c.in, got, c.want)
}
}
}
func TestZip(t *testing.T) {
cases := []struct {
in []interface{}
want interface{}
}{
{[]interface{}{[]int{1, 2, 3}, []int{4, 5, 6}},
[][]int{{1, 4}, {2, 5}, {3, 6}}},
{[]interface{}{[]int{1, 2, 3}, []int{4, 5, 6}, []int{7, 8, 9}},
[][]int{{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}},
{[]interface{}{[]float64{1.1, 2.2}, []float64{3.3, 4.4}},
[][]float64{{1.1, 3.3}, {2.2, 4.4}}},
{[]interface{}{[]int{1, 2, 3, 4, 5}, []int{6, 7, 8}},
[][]int{{1, 6}, {2, 7}, {3, 8}, {4, 0}, {5, 0}}},
{[]interface{}{[]int{}, []int{1, 2, 3}},
[][]int{{0, 1}, {0, 2}, {0, 3}}},
{[]interface{}{
[]testZipStruct{
{1, "a"},
{2, "b"},
{3, "c"},
},
[]testZipStruct{
{4, "d"},
},
}, [][]testZipStruct{
{testZipStruct{1, "a"}, testZipStruct{4, "d"}},
{testZipStruct{2, "b"}, testZipStruct{0, ""}},
{testZipStruct{3, "c"}, testZipStruct{0, ""}},
}},
{[]interface{}{[]string{"a1", "a2", "a3"}, []string{"b1", "b2", "b3"}},
[][]string{{"a1", "b1"}, {"a2", "b2"}, {"a3", "b3"}}},
}
for _, c := range cases {
got := Zip(c.in...)
if !reflect.DeepEqual(got, c.want) {
t.Errorf("Zip(%v...) == %v want %v", c.in, got, c.want)
}
}
}
func TestZipSafe(t *testing.T) {
_, err := ZipSafe([]int{1, 2, 3})
if err == nil {
t.Error("ZipSafe should raise error '2 or more slices allowed'")
}
_, err = ZipSafe([3]int{1, 2, 3}, [3]int{4, 5, 6})
if err == nil {
t.Error("ZipSafe should raise error 'slices only allowed'")
}
_, err = ZipSafe([]int{1, 2, 3}, []int8{1, 2, 3})
if err == nil {
t.Error("ZipSafe should raise error 'params must be the same type'")
}
_, err = ZipSafe([]int{}, []int{})
if err == nil {
t.Error("ZipSafe should raise error 'at least one slice must have non-zero length'")
}
}