-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_test.go
110 lines (98 loc) · 2.67 KB
/
basic_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
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sanity
import (
"strconv"
"testing"
)
func TestIf(t *testing.T) {
AssertEqual(t, If(true, 1, 2), 1)
AssertEqual(t, If(false, 1, 2), 2)
AssertEqual(t, If(true, "1", "2"), "1")
AssertEqual(t, If(false, "1", "2"), "2")
}
func TestMap(t *testing.T) {
AssertSliceEqual(t,
Map([]string{"1", "3", "5"},
func(s string) float64 {
if v, err := strconv.ParseFloat(s, 32); err == nil {
return v
} else {
panic(err)
}
}),
[]float64{1, 3, 5})
}
func TestReduce(t *testing.T) {
s := []string{"ab", "cd", "efg"}
AssertEqual(t,
Reduce(s, "", func(x, y string) string { return x + y }),
"abcdefg")
AssertEqual(t,
Reduce(s, "__", func(x, y string) string { return x + y }),
"__abcdefg")
}
func TestAnyAll(t *testing.T) {
arr := []string{"ab", "cd", "efg"}
Assert(t, Any(arr, func(s string) bool { return s[0] == 'c' }))
Assert(t, !Any(arr, func(s string) bool { return s[0] == 'z' }))
Assert(t, All(arr, func(s string) bool { return s[0] != 'z' }))
Assert(t, !All(arr, func(s string) bool { return s[0] != 'c' }))
}
func TestSum(t *testing.T) {
AssertEqual(t, Sum([]int64{1, 2, 3, 5}), 11)
AssertEqual(t, Sum([]float32{1.8, 2, 3, 5}), 11.8)
}
func TestFilter(t *testing.T) {
AssertSliceEqual(t,
Filter([]uint64{0, 2, 4, 6, 8, 10},
func(i int, v uint64) bool {
return i == 0 || v > 5
}),
[]uint64{0, 6, 8, 10})
}
func TestChannelFns(t *testing.T) {
makeTestChan := func() <-chan string {
c := make(chan string)
go func() {
c <- "56"
c <- "78"
c <- "34"
c <- "12"
close(c)
}()
return c
}
{
// FilterChan
cf := FilterChan(makeTestChan(), func(v string) bool { return v[0] >= '5' })
AssertEqual(t, <-cf, "56")
AssertEqual(t, <-cf, "78")
if _, ok := <-cf; ok {
t.Errorf("Filtered channel isn't closed")
}
}
{
s := ChanToSlice(makeTestChan())
AssertSliceEqual(t, s, []string{"56", "78", "34", "12"})
}
}
func TestSaneSort(t *testing.T) {
lessfn := func(a, b string) bool { return a < b }
values := []string{"56", "78", "34", "12"}
SaneSortSlice(values, lessfn)
AssertSliceEqual(t,
values,
[]string{"12", "34", "56", "78"})
}