-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_test.go
89 lines (69 loc) · 1.57 KB
/
array_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
package array
import (
"fmt"
"testing"
)
func TestForEach(t *testing.T) {
items := []int64{0, 1, 2}
result := 0
ForEach(items, func(item int64) {
result++
})
want := 3
if result != want {
t.Errorf("Count is wrong. Expected: %#v, Received: %#v", want, result)
}
}
// Map should create an array with a new value for each value of the original array
func TestMapEach(t *testing.T) {
items := []int64{0, 1, 2}
result := Map(items, func(item int64) string {
return "" + fmt.Sprint(item)
})
want := []string{"0", "1", "2"}
if !Equal(want, result) {
t.Errorf("Count is wrong. Expected: %#v, Received: %#v", want, result)
}
}
func TestNestedMap(t *testing.T) {
items := []int64{0, 1, 2}
result := Map(Map(Map(
items,
func(item int64) float64 {
return float64(item) / 2
}),
func(item float64) string {
return "" + fmt.Sprint(item)
}),
func(item string) string {
return "a" + item
})
want := []string{"a0", "a0.5", "a1"}
if !Equal(want, result) {
t.Errorf("Count is wrong. Expected: %#v, Received: %#v", want, result)
}
}
func TestReduce(t *testing.T) {
items := []int{0, 1, 2}
target := 0
result := Reduce(items, func(target int, item int) int {
return target + item
}, target)
want := 3
if result != want {
t.Errorf("Count is wrong. Expected: %#v, Received: %#v", want, result)
}
}
func TestFilter(t *testing.T) {
items := []int{0, 3, 1, 4, 2, 5}
result := Filter(items, func(item int) bool {
if item >= 3 {
return true
} else {
return false
}
})
if !Equal(result, []int{3, 4, 5}) {
t.Error("Count is wrong")
}
}