-
Notifications
You must be signed in to change notification settings - Fork 7
/
max_test.go
86 lines (79 loc) · 1.59 KB
/
max_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
package statistics
import (
"fmt"
"math"
"testing"
)
func ExampleMax() {
vals := []uint8{8, 7, 3, 2, 6, 11, 6, 7, 2, 1, 7}
max := Max(vals...)
fmt.Printf("The max of %v is %v.\n", vals, max)
// Output:
// The max of [8 7 3 2 6 11 6 7 2 1 7] is 11.
}
func TestMax(t *testing.T) {
cases := []testArrayCase{
{
name: "no numbers",
input: []uint{},
expected: "0",
},
{
name: "negatives",
input: []int{-10, -20},
expected: "-10",
},
{
name: "mixed",
input: []int{-10, -20, 5, 15},
expected: "15",
},
{
name: "float +INF start",
input: []float64{math.Inf(1), 5, 10},
expected: "+Inf",
},
{
name: "float -INF end",
input: []float64{math.Inf(-1), 5, 10},
expected: "10",
},
{
name: "float NaN end",
input: []float64{5, 10, math.NaN()},
expected: "NaN",
},
{
name: "float NaN start",
input: []float64{math.NaN(), 5, 10},
expected: "NaN",
},
}
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
var actual interface{}
switch c.input.(type) {
case []uint:
actual = Max(c.input.([]uint)...)
case []uint64:
actual = Max(c.input.([]uint64)...)
case []int:
actual = Max(c.input.([]int)...)
case []float64:
actual = Max(c.input.([]float64)...)
}
if fmt.Sprintf("%v", actual) != c.expected {
t.Logf("expected %v, got %v", c.expected, actual)
t.FailNow()
}
})
}
}
var testMaxVals []uint = make([]uint, 1000)
func BenchmarkMax(b *testing.B) {
for i := 0; i < b.N; i++ {
Max(testMaxVals...)
}
}