hackathon/question/q02/q02_test.go

59 lines
1.1 KiB
Go

package q02
import (
"strconv"
"strings"
"testing"
"github.com/hhhapz/codequest/models"
"github.com/hhhapz/codequest/question"
)
func TestQ02(t *testing.T) {
u := &models.User{
ID: "1203120957198056",
}
q := question.QuestionByID("saturnalia")
raw := q.Generate(u)
var input []int
for _, s := range strings.Split(raw, "\n") {
i, _ := strconv.Atoi(s)
input = append(input, i)
}
res := solveP1(input)
t.Logf("part 1 result: %d", res)
if !q.Validate(u, question.Part1, strconv.FormatInt(res, 10)) {
t.Errorf("Expected question 1 part 1(%v) to be correct!", res)
}
res = solveP2(input)
if !q.Validate(u, question.Part2, strconv.FormatInt(res, 10)) {
t.Errorf("Expected question 2 part 2(%v) to be correct!", res)
}
if q.Validate(u, question.Part1, "") {
t.Errorf("Expected bad input to be invalid")
}
t.Logf("Input:\n%v", raw)
}
func TestQ02Idempotent(t *testing.T) {
q := question.QuestionByID("saturnalia")
u1 := &models.User{ID: "1"}
raw1 := q.Generate(u1)
u2 := &models.User{ID: "2"}
_ = q.Generate(u2)
raw2 := q.Generate(u1)
if raw1 != raw2 {
t.Errorf("Expected raw1 and raw2 to be the same")
}
}