forked from robpike/filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply_test.go2
119 lines (101 loc) · 2.98 KB
/
apply_test.go2
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package filter
import (
"reflect"
"testing"
)
func triple(a int) int {
return a * 3
}
func tripleString(a string) string {
return a + a + a
}
func tripleToFloat(a int) float64 {
return float64(a * 3)
}
func floatToInt(a float64) int {
return int(a)
}
func isEven(a int) bool {
return a%2 == 0
}
func isEvenString(a string) bool {
return a[0]%2 == 0
}
func is18(a int) bool {
return a == 18
}
func TestApply(t *testing.T) {
a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
expect := []int{3, 6, 9, 12, 15, 18, 21, 24, 27}
result := Apply(a, triple)
if !reflect.DeepEqual(expect, result) {
t.Fatalf("Apply failed: expect %v got %v", expect, result)
}
}
func TestApplyString(t *testing.T) {
a := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"}
expect := []string{"111", "222", "333", "444", "555", "666", "777", "888", "999"}
result := Apply(a, tripleString)
if !reflect.DeepEqual(expect, result) {
t.Fatalf("Apply failed: expect %v got %v", expect, result)
}
}
func TestApplyDifferentTypes(t *testing.T) {
a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
expect := []int{3, 6, 9, 12, 15, 18, 21, 24, 27}
result := Apply(Apply(a, tripleToFloat), floatToInt)
if !reflect.DeepEqual(expect, result) {
t.Fatalf("Apply failed: expect %v got %v", expect, result)
}
}
func TestChoose(t *testing.T) {
a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
expect := []int{2, 4, 6, 8}
result := Choose(a, isEven)
if !reflect.DeepEqual(expect, result) {
t.Fatalf("Choose failed: expect %v got %v", expect, result)
}
}
func TestDrop(t *testing.T) {
a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
expect := []int{1, 3, 5, 7, 9}
result := Drop(a, isEven)
if !reflect.DeepEqual(expect, result) {
t.Fatalf("Drop failed: expect %v got %v", expect, result)
}
}
func TestApplyInPlace(t *testing.T) {
a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
expect := []int{3, 6, 9, 12, 15, 18, 21, 24, 27}
ApplyInPlace(a, triple)
if !reflect.DeepEqual(expect, a) {
t.Fatalf("Apply failed: expect %v got %v", expect, a)
}
}
func TestApplyInPlaceString(t *testing.T) {
a := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"}
expect := []string{"111", "222", "333", "444", "555", "666", "777", "888", "999"}
ApplyInPlace(a, tripleString)
if !reflect.DeepEqual(expect, a) {
t.Fatalf("Apply failed: expect %v got %v", expect, a)
}
}
func TestChooseInPlaceString(t *testing.T) {
a := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"}
expect := []string{"2", "4", "6", "8"}
ChooseInPlace(&a, isEvenString)
if !reflect.DeepEqual(expect, a) {
t.Fatalf("ChooseInPlace failed: expect %v got %v", expect, a)
}
}
func TestDropInPlaceString(t *testing.T) {
a := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"}
expect := []string{"1", "3", "5", "7", "9"}
DropInPlace(&a, isEvenString)
if !reflect.DeepEqual(expect, a) {
t.Fatalf("DropInPlace failed: expect %v got %v", expect, a)
}
}