mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
95 lines
2.0 KiB
Go
95 lines
2.0 KiB
Go
|
package one
|
||
|
|
||
|
import (
|
||
|
"slices"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestComputerRunProgram1(t *testing.T) {
|
||
|
tt := map[string]struct {
|
||
|
registers map[string]int
|
||
|
program []int
|
||
|
}{
|
||
|
"register C contains 9 with program 2,6, should set register B to 1": {
|
||
|
registers: map[string]int{
|
||
|
"A": 0,
|
||
|
"B": 0,
|
||
|
"C": 9,
|
||
|
},
|
||
|
program: []int{2, 6},
|
||
|
},
|
||
|
}
|
||
|
|
||
|
c := newComputer(nil)
|
||
|
for name, tc := range tt {
|
||
|
t.Run(name, func(t *testing.T) {
|
||
|
c.registers = tc.registers
|
||
|
c.run(tc.program)
|
||
|
if c.registers["B"] != 1 {
|
||
|
t.Errorf("register B got: %v, expected %v", c.registers["B"], 1)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestComputerRunProgram2(t *testing.T) {
|
||
|
tt := map[string]struct {
|
||
|
registers map[string]int
|
||
|
program []int
|
||
|
expected []int
|
||
|
}{
|
||
|
"register A contains 10 with program 5,0,5,1,5,4, should output 0,1,2": {
|
||
|
registers: map[string]int{
|
||
|
"A": 10,
|
||
|
"B": 0,
|
||
|
"C": 0,
|
||
|
},
|
||
|
program: []int{5, 0, 5, 1, 5, 4},
|
||
|
expected: []int{0, 1, 2},
|
||
|
},
|
||
|
}
|
||
|
|
||
|
c := newComputer(nil)
|
||
|
for name, tc := range tt {
|
||
|
t.Run(name, func(t *testing.T) {
|
||
|
c.registers = tc.registers
|
||
|
got := c.run(tc.program)
|
||
|
if !slices.Equal(got, tc.expected) {
|
||
|
t.Errorf("output: %q, expected %q", got, tc.expected)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestComputerRunProgram3(t *testing.T) {
|
||
|
tt := map[string]struct {
|
||
|
registers map[string]int
|
||
|
program []int
|
||
|
expected []int
|
||
|
}{
|
||
|
"register A contains 2024 with program 0, 1, 5, 4, 3, 0, should leave 0 in register A and output 4, 2, 5, 6, 7, 7, 7, 7, 3, 1, 0": {
|
||
|
registers: map[string]int{
|
||
|
"A": 2024,
|
||
|
"B": 0,
|
||
|
"C": 0,
|
||
|
},
|
||
|
program: []int{0, 1, 5, 4, 3, 0},
|
||
|
expected: []int{4, 2, 5, 6, 7, 7, 7, 7, 3, 1, 0},
|
||
|
},
|
||
|
}
|
||
|
|
||
|
c := newComputer(nil)
|
||
|
for name, tc := range tt {
|
||
|
t.Run(name, func(t *testing.T) {
|
||
|
c.registers = tc.registers
|
||
|
got := c.run(tc.program)
|
||
|
if c.registers["A"] != 0 {
|
||
|
t.Errorf("register A got: %v, expected %v", c.registers["A"], 0)
|
||
|
}
|
||
|
if !slices.Equal(got, tc.expected) {
|
||
|
t.Errorf("output got: %v, expected %v", got, tc.expected)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|