-
Notifications
You must be signed in to change notification settings - Fork 1
/
binarygap_test.go
55 lines (50 loc) · 993 Bytes
/
binarygap_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
package goexercises
import "testing"
func TestBinaryGap(t *testing.T) {
testCases := []struct {
input int
expected int
}{
{5, 1},
{1, 0},
{4, 0},
{10, 1},
{9, 2},
}
for _, test := range testCases {
observed := BinaryGap(test.input)
if observed != test.expected {
t.Errorf("for input '%d', expected '%d', observed '%d'\n",
test.input, test.expected, observed)
}
}
}
func BenchmarkBinaryGap(b *testing.B) {
for i := 0; i < b.N; i++ {
BinaryGap(10000)
}
}
func TestCountGaps(t *testing.T) {
var testCases = []struct {
input string
expected int
}{
{"10001", 3},
{"1", 0},
{"10", 1},
{"101", 1},
{"0", 1},
}
for _, test := range testCases {
observed := countGaps(test.input)
if observed != test.expected {
t.Errorf("for input '%s', expected '%d', observed '%d'\n",
test.input, test.expected, observed)
}
}
}
func BenchmarkCountGaps(b *testing.B) {
for i := 0; i < b.N; i++ {
countGaps("1000001111011110110001")
}
}