AdventOfCode2021/16/main_test.go

62 lines
1.2 KiB
Go
Raw Normal View History

2021-12-20 22:52:18 +00:00
package main
import (
"testing"
)
func TestVersionParse(t *testing.T) {
2021-12-20 23:45:35 +00:00
tests := map[int][]uint8{
2021-12-20 22:52:18 +00:00
0: {0, 0, 0},
1: {0, 0, 1},
2: {0, 1, 0},
3: {0, 1, 1},
4: {1, 0, 0},
5: {1, 0, 1},
6: {1, 1, 0},
7: {1, 1, 1},
}
for result, input := range tests {
if result != getPacketVersion(input) {
t.Fail()
}
if result != getPacketType(input) {
t.Fail()
}
}
}
func TestParsePayload(t *testing.T) {
if n, q := parsePayload([]uint8{0, 1, 0, 1, 0}); n != 10 || q != 5 {
t.Log(n, q)
t.Fail()
}
if n, q := parsePayload([]uint8{1, 0, 0, 0, 1, 0, 0, 1, 0, 0}); n != 20 || q != 10 {
t.Log(n, q)
t.Fail()
}
}
func TestCases(t *testing.T) {
inputs := map[string]int{
"D2FE28": 6,
"38006F45291200": 9,
"EE00D40C823060": 14,
"8A004A801A8002F478": 16,
"620080001611562C8802118E34": 12,
"C0015000016115A2E0802F182340": 23,
"A0016C880162017C3686B18A3D4780": 31,
}
for c, r := range inputs {
stream := []uint8{}
for _, hex := range c {
stream = append(stream, hexMap[hex]...)
}
_, parsedPacket := parsePackets(stream)
if r != countVersions(parsedPacket) {
t.Log(c, countVersions(parsedPacket))
t.Fail()
}
}
}