From 92dd557177420833879aef78f465321e563c7005 Mon Sep 17 00:00:00 2001 From: Russell Champoux Date: Wed, 1 Dec 2021 15:31:51 -0500 Subject: [PATCH] add unit testing --- day-1/main.go | 33 ++++++++++++++++++--------------- day-1/main_test.go | 30 ++++++++++++++++++++++++++++++ main.go.template | 2 +- makefile | 9 ++++++--- utils/testing/assert.go | 10 ++++++++++ utils/timer/timer_test.go | 11 +++++++++++ 6 files changed, 76 insertions(+), 19 deletions(-) create mode 100644 day-1/main_test.go create mode 100644 utils/testing/assert.go create mode 100644 utils/timer/timer_test.go diff --git a/day-1/main.go b/day-1/main.go index 6d9de2f..9c46907 100644 --- a/day-1/main.go +++ b/day-1/main.go @@ -9,8 +9,8 @@ import ( "strconv" ) -const DEBUG = true -const WIDTH = 100 +var DEBUG = true +var WIDTH = 100 func check(e error) { if e != nil { @@ -37,8 +37,8 @@ func part1() { fmt.Println(" Answer: How many measurements are larger than the previous measurement?") input, err := os.Open("./input.txt") - check(err) defer input.Close() + check(err) scanner := bufio.NewScanner(input) @@ -53,7 +53,7 @@ func part1() { if lastValue != -1 && lastValue < value { increases++ } - printPip(lastValue, lastValue, value, measurements) + fmt.Print(GetPip(lastValue, lastValue, value, measurements)) lastValue = value } @@ -67,8 +67,8 @@ func part2() { fmt.Println(" Answer: How many sums are larger than the previous sum?") input, err := os.Open("./input.txt") - check(err) defer input.Close() + check(err) scanner := bufio.NewScanner(input) @@ -88,7 +88,7 @@ func part2() { increases++ } - printPip(w1, previous, current, measurements) + fmt.Print(GetPip(w1, previous, current, measurements)) // shift over the window w1, w2, w3 = w2, w3, value @@ -97,19 +97,22 @@ func part2() { fmt.Printf("Read %d measurements resulting in %d increases\n", measurements, increases) } -func printPip(first int, previous int, current int, total int) { +func GetPip(first int, previous int, current int, total int) string { + retVal := "" if !DEBUG { - return + return retVal } - if first != -1 && current > previous { - fmt.Print("+") - } else if first != -1 && current < previous { - fmt.Print("-") - } else { - fmt.Print(".") + switch { + case first != -1 && current > previous: + retVal = "+" + case first != -1 && current < previous: + retVal = "-" + default: + retVal = "." } if total%WIDTH == 0 { - fmt.Println() // linebreak + retVal += "\n" } + return retVal } diff --git a/day-1/main_test.go b/day-1/main_test.go new file mode 100644 index 0000000..d06a15b --- /dev/null +++ b/day-1/main_test.go @@ -0,0 +1,30 @@ +package main + +import ( + assert "AoC2021/utils/testing" + "testing" +) + +func TestPrintPip(t *testing.T) { + DEBUG = false + str := GetPip(1, 1, 1, 100) + assert.StrEquals(t, str, "", "Should return an empty string") + + DEBUG = true + cases := []struct { + first, previous, current, total int + expected, msg string + }{ + {-1, -1, -1, -1, ".", ""}, + {-1, 0, 0, 42, ".", ""}, + {100, 100, 200, 42, "+", ""}, + {100, 200, 100, 42, "-", ""}, + {100, 100, 100, 100, ".\n", "should include newline"}, + } + + for _, c := range cases { + str := GetPip(c.first, c.previous, c.current, c.total) + assert.StrEquals(t, str, c.expected, c.msg) + } + +} diff --git a/main.go.template b/main.go.template index 8d065e4..056916b 100644 --- a/main.go.template +++ b/main.go.template @@ -33,8 +33,8 @@ func part1() { fmt.Println(" Answer: ") input, err := os.Open("./input.txt") - check(err) defer input.Close() + check(err) scanner := bufio.NewScanner(input) lines := 0 diff --git a/makefile b/makefile index 5f2ead0..3468bf7 100644 --- a/makefile +++ b/makefile @@ -1,8 +1,11 @@ -default: 1 +default: build day=1 # This is my 2021 Advent of Code project init: mkdir day-$(day); sed 's/#DAY#/$(day)/g' main.go.template | sed 's/#TITLE#/$(title)/g' > day-$(day)/main.go; touch day-$(day)/input.txt -0 1: - cd day-$@; go run . \ No newline at end of file +build: + cd day-$(day); go run . + +test: + cd day-$(day); go test \ No newline at end of file diff --git a/utils/testing/assert.go b/utils/testing/assert.go new file mode 100644 index 0000000..9d89a1b --- /dev/null +++ b/utils/testing/assert.go @@ -0,0 +1,10 @@ +package assert + +import "testing" + +func StrEquals(t *testing.T, actual, expected, message string) { + if actual != expected { + t.Logf("Expected value '%s' but got '%s': %s", expected, actual, message) + t.Fail() + } +} diff --git a/utils/timer/timer_test.go b/utils/timer/timer_test.go new file mode 100644 index 0000000..a396d78 --- /dev/null +++ b/utils/timer/timer_test.go @@ -0,0 +1,11 @@ +package timer + +import "testing" + +func TestTimer(t *testing.T) { + + Start() + Tick() + + t.Log("Yup") +}