2019/day2/main_test.go
2019-12-02 21:02:53 +01:00

84 lines
1.7 KiB
Go

package main
import (
"strconv"
"strings"
"testing"
)
func equalsArray(arr1 []int, arr2 []int) bool {
if len(arr1) != len(arr2) {
return false
}
for i := 0; i < len(arr1); i++ {
if arr1[i] != arr2[i] {
return false
}
}
return true
}
func join(arr []int) string {
valuesText := []string{}
for i := range arr {
number := arr[i]
text := strconv.Itoa(number)
valuesText = append(valuesText, text)
}
return strings.Join(valuesText, ",")
}
func TestCalcFuelRecusive1(t *testing.T) {
arr := []int{1, 0, 0, 0, 99}
err := ParseArray(arr)
if err != nil {
t.Errorf("Error where no error should be: %v", err)
}
sol := []int{2, 0, 0, 0, 99}
if !equalsArray(arr, sol) {
t.Errorf("Array was incorrect, got: {%s}, want: {%s}.", join(arr), join(sol))
}
}
func TestCalcFuelRecusive2(t *testing.T) {
arr := []int{2, 3, 0, 3, 99}
err := ParseArray(arr)
if err != nil {
t.Errorf("Error where no error should be: %v", err)
}
sol := []int{2, 3, 0, 6, 99}
if !equalsArray(arr, sol) {
t.Errorf("Array was incorrect, got: {%s}, want: {%s}.", join(arr), join(sol))
}
}
func TestCalcFuelRecusive3(t *testing.T) {
arr := []int{2, 4, 4, 5, 99, 0}
err := ParseArray(arr)
if err != nil {
t.Errorf("Error where no error should be: %v", err)
}
sol := []int{2, 4, 4, 5, 99, 9801}
if !equalsArray(arr, sol) {
t.Errorf("Array was incorrect, got: {%s}, want: {%s}.", join(arr), join(sol))
}
}
func TestCalcFuelRecusive4(t *testing.T) {
arr := []int{1, 1, 1, 4, 99, 5, 6, 0, 99}
err := ParseArray(arr)
if err != nil {
t.Errorf("Error where no error should be: %v", err)
}
sol := []int{30, 1, 1, 4, 2, 5, 6, 0, 99}
if !equalsArray(arr, sol) {
t.Errorf("Array was incorrect, got: {%s}, want: {%s}.", join(arr), join(sol))
}
}