forked from robpike/filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduce_test.go2
69 lines (60 loc) · 1.34 KB
/
reduce_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
// 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 (
"testing"
)
func mul(a, b int) int {
return a * b
}
func addFloat(a, b float64) float64 {
return a + b
}
func TestReduce(t *testing.T) {
a := make([]int, 10)
for i := range a {
a[i] = i + 1
}
// Compute 10!
out := Reduce(a, mul, 1)
expect := 1
for i := range a {
expect *= a[i]
}
if expect != out {
t.Fatalf("expected %d got %d", expect, out)
}
}
func TestReduceNonZeroInitialNilSlice(t *testing.T) {
var a []float64
out := Reduce(a, addFloat, 273.15)
expect := 273.15
if expect != out {
t.Fatalf("expected %v got %v", expect, out)
}
}
func TestReduceNonZeroInitialEmptySlice(t *testing.T) {
a := []float64{}
out := Reduce(a, addFloat, 273.15)
expect := 273.15
if expect != out {
t.Fatalf("expected %v got %v", expect, out)
}
}
func TestReduceNonZeroInitialSingleElementSlice(t *testing.T) {
a := []float64{1.1}
out := Reduce(a, addFloat, 273.15)
expect := 274.25
if expect != out {
t.Fatalf("expected %v got %v", expect, out)
}
}
func TestReduceNonZeroInitialWithSlice(t *testing.T) {
a := []float64{1, 2, 3}
out := Reduce(a, addFloat, 273.15)
expect := 279.15
if expect != out {
t.Fatalf("expected %v got %v", expect, out)
}
}