-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselect_minimum_maximum_test.go
91 lines (82 loc) · 2.06 KB
/
select_minimum_maximum_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
package selectminimummaximum
import (
"testing"
)
func TestMinimum(t *testing.T) {
tests := []struct {
name string
intArray []int
expectedMinimum int
expectedErrorString string
}{
{
name: "3 is minimum",
intArray: []int{8, 3, 9, 4, 6},
expectedMinimum: 3,
},
{
name: "-6 is minimum",
intArray: []int{8, 3, 9, 4, 6, 7, -1, -6, -3},
expectedMinimum: -6,
},
{
name: "Empty array",
intArray: []int{},
expectedErrorString: "Error finding minimum of array: length of array is 0",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
if r != test.expectedErrorString {
t.Errorf("Expected error %s, received %s instead.", test.expectedErrorString, r)
}
}
}()
received := minimum(test.intArray)
if received != test.expectedMinimum {
t.Errorf("Expected index %d, received %d instead.", test.expectedMinimum, received)
}
})
}
}
func TestMaximum(t *testing.T) {
tests := []struct {
name string
intArray []int
expectedMaximum int
expectedErrorString string
}{
{
name: "9 is maximum",
intArray: []int{8, 3, 9, 4, 6},
expectedMaximum: 9,
},
{
name: "23 is minimum",
intArray: []int{8, 3, 9, 4, 6, 7, 23, -1, -6, -3},
expectedMaximum: 23,
},
{
name: "Empty array",
intArray: []int{},
expectedErrorString: "Error finding maximum of array: length of array is 0",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
if r != test.expectedErrorString {
t.Errorf("Expected error %s, received %s instead.", test.expectedErrorString, r)
}
}
}()
received := maximum(test.intArray)
if received != test.expectedMaximum {
t.Errorf("Expected index %d, received %d instead.", test.expectedMaximum, received)
}
})
}
}