-
Notifications
You must be signed in to change notification settings - Fork 0
/
determining_dna_health_test.go
94 lines (88 loc) · 1.87 KB
/
determining_dna_health_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
92
93
94
// Package determining_dna_health contains the solution for HackerRank problem
// https://www.hackerrank.com/challenges/determining-dna-health/problem.
package determining_dna_health_test
import (
"bufio"
"io/ioutil"
"os"
"strconv"
"strings"
"testing"
"github.com/sepetrov/hackerrank/determining_dna_health"
)
const bufSize = 1024 * 1024
func TestRun(t *testing.T) {
type out struct {
min, max int
}
parseOutput := func(filename string) out {
body, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
mm := strings.Fields(string(body))
min, err := strconv.Atoi(mm[0])
if err != nil {
panic(err)
}
max, err := strconv.Atoi(mm[1])
if err != nil {
panic(err)
}
return out{min, max}
}
tests := []struct {
in string
out out
}{
{
"testdata/input.txt",
parseOutput("testdata/output.txt"),
},
// {
// "testdata/input02.txt",
// parseOutput("testdata/output02.txt"),
// },
// {
// "testdata/input07.txt",
// parseOutput("testdata/output07.txt"),
// },
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
f, err := os.Open(tt.in)
if err != nil {
t.Fatal(err)
}
defer f.Close()
min, max := determining_dna_health.Run(bufio.NewReaderSize(f, bufSize))
if min != tt.out.min {
t.Errorf("Run() min = %d, want %d", min, tt.out.min)
}
if max != tt.out.max {
t.Errorf("Run() max = %d, want %d", max, tt.out.max)
}
})
}
}
func BenchmarkRun(b *testing.B) {
for _, f := range []string{
"testdata/input.txt",
"testdata/input02.txt",
"testdata/input07.txt",
} {
b.Run(f, func(b *testing.B) {
f, err := os.Open(f)
if err != nil {
b.Fatal(err)
}
defer f.Close() // In case Run() panics before we can close f.
buf := bufio.NewReaderSize(f, bufSize)
b.ResetTimer()
for n := 0; n < b.N; n++ {
determining_dna_health.Run(buf)
}
f.Close()
})
}
}