-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
82 lines (69 loc) · 2.13 KB
/
main_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
package main
import (
"testing"
)
func runTest(t *testing.T, problem Problem, expectedPart1 int, expectedPart2 int) {
lines, err := readLines(problem.FilePath())
if err != nil {
t.Fatalf("Failed to read input: %s", problem.FilePath())
}
part1, err := problem.Part1(lines)
if err != nil {
t.Errorf("Part1 unexpected error: %s", err.Error())
}
if part1 != expectedPart1 {
t.Errorf("Part1 returns wrong answer. Expected: %d, actual: %d", expectedPart1, part1)
}
part2, err := problem.Part2(lines)
if err != nil {
t.Errorf("Part2 unexpected error: %s", err.Error())
}
if part2 != expectedPart2 {
t.Errorf("Part2 returns wrong answer. Expected: %d, actual: %d", expectedPart2, part2)
}
}
func runBench(b *testing.B, problem Problem) {
lines, err := readLines(problem.FilePath())
if err != nil {
b.Fatalf("Failed to read input: %s", problem.FilePath())
}
b.Run("Part1", func(b *testing.B) {
for n := 0; n < b.N; n++ {
problem.Part1(lines)
}
})
b.Run("Part2", func(b *testing.B) {
for n := 0; n < b.N; n++ {
problem.Part2(lines)
}
})
}
func TestDay1(t *testing.T) {
runTest(t, Day1{filePath: "inputs/day1/test1.txt"}, 142, 142)
runTest(t, Day1{filePath: "inputs/day1/test2.txt"}, 209, 281)
runTest(t, Day1{filePath: "inputs/day1/problem.txt"}, 54388, 53515)
}
func BenchmarkDay1(b *testing.B) {
runBench(b, Day1{filePath: "inputs/day1/problem.txt"})
}
func TestDay2(t *testing.T) {
runTest(t, Day2{filePath: "inputs/day2/test1.txt"}, 8, 2286)
runTest(t, Day2{filePath: "inputs/day2/problem.txt"}, 2207, 62241)
}
func BenchmarkDay2(b *testing.B) {
runBench(b, Day2{filePath: "inputs/day2/problem.txt"})
}
func TestDay3(t *testing.T) {
runTest(t, Day3{filePath: "inputs/day3/test1.txt"}, 4361, 467835)
runTest(t, Day3{filePath: "inputs/day3/problem.txt"}, 528819, 80403602)
}
func BenchmarkDay3(b *testing.B) {
runBench(b, Day3{filePath: "inputs/day3/problem.txt"})
}
func TestDay4(t *testing.T) {
runTest(t, Day4{filePath: "inputs/day4/test1.txt"}, 13, 30)
runTest(t, Day4{filePath: "inputs/day4/problem.txt"}, 25571, 8805731)
}
func BenchmarkDay4(b *testing.B) {
runBench(b, Day4{filePath: "inputs/day4/problem.txt"})
}