Browse Source

table test puzzles

master
Alexander Avery 3 weeks ago
parent
commit
10554b420f
  1. 67
      2024/puzzle_test.go

67
2024/puzzle_test.go

@ -1,6 +1,7 @@
package adventoc2024 package adventoc2024
import ( import (
"fmt"
"io" "io"
"strings" "strings"
"testing" "testing"
@ -10,43 +11,55 @@ func dup(input string) (io.Reader, io.Reader) {
return strings.NewReader(input), strings.NewReader(input) return strings.NewReader(input), strings.NewReader(input)
} }
func TestDay01(t *testing.T) { type puzzle interface {
input := `3 4 Part1(io.Reader) string
Part2(io.Reader) string
}
func TestPuzzles(t *testing.T) {
tests := []struct {
solver puzzle
ans1 string
ans2 string
input string
}{
{
solver: Day1{},
ans1: "11",
ans2: "31",
input: `3 4
4 3 4 3
2 5 2 5
1 3 1 3
3 9 3 9
3 3` 3 3`,
},
p1, p2 := dup(input) {
solver := Day1{} solver: Day2{},
ans1: "2",
if got, expected := solver.Part1(p1), "11"; got != expected { ans2: "4",
t.Errorf("day01 part 1 = %s; expected %s", got, expected) input: `7 6 4 2 1
}
if got, expected := solver.Part2(p2), "31"; got != expected {
t.Errorf("day01 part 2 = %s; expected %s", got, expected)
}
}
func TestDay02(t *testing.T) {
input := `7 6 4 2 1
1 2 7 8 9 1 2 7 8 9
9 7 6 2 1 9 7 6 2 1
1 3 2 4 5 1 3 2 4 5
8 6 4 4 1 8 6 4 4 1
1 3 6 7 9` 1 3 6 7 9`,
},
p1, p2 := dup(input)
solver := Day2{}
if got, expected := solver.Part1(p1), "2"; got != expected {
t.Errorf("day 2 part 1 = %s; expected %s", got, expected)
} }
if got, expected := solver.Part2(p2), "4"; got != expected { for i, tt := range tests {
t.Errorf("day 2 part 2 = %s; expected %s", got, expected) name := fmt.Sprintf("Day%d", i+1)
t.Run(name, func(t *testing.T) {
p1, p2 := dup(tt.input)
if got := tt.solver.Part1(p1); got != tt.ans1 {
t.Errorf("Part 1 = %s; expected %s", got, tt.ans1)
}
if got := tt.solver.Part2(p2); got != tt.ans2 {
t.Errorf("Part 2 = %s; expected %s", got, tt.ans2)
}
})
} }
} }

Loading…
Cancel
Save