forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_test.go
43 lines (38 loc) · 1.64 KB
/
binary_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
package search
import "testing"
func TestBinary(t *testing.T) {
for _, test := range searchTests {
actualValue, actualError := Binary(test.data, test.key, 0, len(test.data)-1)
if actualValue != test.expected {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected '%d', get '%d'", test.name, test.data, test.key, test.expected, actualValue)
}
if actualError != test.expectedError {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected error '%s', get error '%s'", test.name, test.data, test.key, test.expectedError, actualError)
}
}
}
func TestBinaryIterative(t *testing.T) {
for _, test := range searchTests {
actualValue, actualError := BinaryIterative(test.data, test.key, 0, len(test.data)-1)
if actualValue != test.expected {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected '%d', get '%d'", test.name, test.data, test.key, test.expected, actualValue)
}
if actualError != test.expectedError {
t.Errorf("test '%s' failed: input array '%v' with key '%d', expected error '%s', get error '%s'", test.name, test.data, test.key, test.expectedError, actualError)
}
}
}
func BenchmarkBinary(b *testing.B) {
testCase := generateBenchmarkTestCase()
b.ResetTimer() // this is important because the generateBenchmarkTestCase() is expensive
for i := 0; i < b.N; i++ {
_, _ = Binary(testCase, i, 0, len(testCase)-1)
}
}
func BenchmarkBinaryIterative(b *testing.B) {
testCase := generateBenchmarkTestCase()
b.ResetTimer() // this is important because the generateBenchmarkTestCase() is expensive
for i := 0; i < b.N; i++ {
_, _ = BinaryIterative(testCase, i, 0, len(testCase)-1)
}
}