mirror of
https://github.com/RussellChamp/advent-of-code-2021.git
synced 2026-10-27 21:52:32 +00:00
add unit testing
This commit is contained in:
1 parent
2992d3ba43
commit
92dd557177
6 files changed
+76
-19
No files matched your search
+18
-15
@@ -9,8 +9,8 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const DEBUG = true
|
var DEBUG = true
|
||||||
const WIDTH = 100
|
var WIDTH = 100
|
||||||
|
|
||||||
func check(e error) {
|
func check(e error) {
|
||||||
if e != nil {
|
if e != nil {
|
||||||
@@ -37,8 +37,8 @@ func part1() {
|
|||||||
fmt.Println(" Answer: How many measurements are larger than the previous measurement?")
|
fmt.Println(" Answer: How many measurements are larger than the previous measurement?")
|
||||||
|
|
||||||
input, err := os.Open("./input.txt")
|
input, err := os.Open("./input.txt")
|
||||||
check(err)
|
|
||||||
defer input.Close()
|
defer input.Close()
|
||||||
|
check(err)
|
||||||
|
|
||||||
scanner := bufio.NewScanner(input)
|
scanner := bufio.NewScanner(input)
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ func part1() {
|
|||||||
if lastValue != -1 && lastValue < value {
|
if lastValue != -1 && lastValue < value {
|
||||||
increases++
|
increases++
|
||||||
}
|
}
|
||||||
printPip(lastValue, lastValue, value, measurements)
|
fmt.Print(GetPip(lastValue, lastValue, value, measurements))
|
||||||
|
|
||||||
lastValue = value
|
lastValue = value
|
||||||
}
|
}
|
||||||
@@ -67,8 +67,8 @@ func part2() {
|
|||||||
fmt.Println(" Answer: How many sums are larger than the previous sum?")
|
fmt.Println(" Answer: How many sums are larger than the previous sum?")
|
||||||
|
|
||||||
input, err := os.Open("./input.txt")
|
input, err := os.Open("./input.txt")
|
||||||
check(err)
|
|
||||||
defer input.Close()
|
defer input.Close()
|
||||||
|
check(err)
|
||||||
|
|
||||||
scanner := bufio.NewScanner(input)
|
scanner := bufio.NewScanner(input)
|
||||||
|
|
||||||
@@ -88,7 +88,7 @@ func part2() {
|
|||||||
increases++
|
increases++
|
||||||
}
|
}
|
||||||
|
|
||||||
printPip(w1, previous, current, measurements)
|
fmt.Print(GetPip(w1, previous, current, measurements))
|
||||||
|
|
||||||
// shift over the window
|
// shift over the window
|
||||||
w1, w2, w3 = w2, w3, value
|
w1, w2, w3 = w2, w3, value
|
||||||
@@ -97,19 +97,22 @@ func part2() {
|
|||||||
fmt.Printf("Read %d measurements resulting in %d increases\n", measurements, increases)
|
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 {
|
if !DEBUG {
|
||||||
return
|
return retVal
|
||||||
}
|
}
|
||||||
|
|
||||||
if first != -1 && current > previous {
|
switch {
|
||||||
fmt.Print("+")
|
case first != -1 && current > previous:
|
||||||
} else if first != -1 && current < previous {
|
retVal = "+"
|
||||||
fmt.Print("-")
|
case first != -1 && current < previous:
|
||||||
} else {
|
retVal = "-"
|
||||||
fmt.Print(".")
|
default:
|
||||||
|
retVal = "."
|
||||||
}
|
}
|
||||||
if total%WIDTH == 0 {
|
if total%WIDTH == 0 {
|
||||||
fmt.Println() // linebreak
|
retVal += "\n"
|
||||||
}
|
}
|
||||||
|
return retVal
|
||||||
}
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+1
-1
@@ -33,8 +33,8 @@ func part1() {
|
|||||||
fmt.Println(" Answer: ")
|
fmt.Println(" Answer: ")
|
||||||
|
|
||||||
input, err := os.Open("./input.txt")
|
input, err := os.Open("./input.txt")
|
||||||
check(err)
|
|
||||||
defer input.Close()
|
defer input.Close()
|
||||||
|
check(err)
|
||||||
|
|
||||||
scanner := bufio.NewScanner(input)
|
scanner := bufio.NewScanner(input)
|
||||||
lines := 0
|
lines := 0
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
default: 1
|
default: build day=1
|
||||||
# This is my 2021 Advent of Code project
|
# This is my 2021 Advent of Code project
|
||||||
|
|
||||||
init:
|
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
|
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:
|
build:
|
||||||
cd day-$@; go run .
|
cd day-$(day); go run .
|
||||||
|
|
||||||
|
test:
|
||||||
|
cd day-$(day); go test
|
||||||
@@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package timer
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestTimer(t *testing.T) {
|
||||||
|
|
||||||
|
Start()
|
||||||
|
Tick()
|
||||||
|
|
||||||
|
t.Log("Yup")
|
||||||
|
}
|
||||||
Reference in new issue
Block a user