compiler!
This commit is contained in:
79
code/code_test.go
Normal file
79
code/code_test.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package code
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMake(t *testing.T) {
|
||||
test := []struct {
|
||||
op Opcode
|
||||
operands []int
|
||||
expected []byte
|
||||
}{
|
||||
{OpConstant, []int{65534}, []byte{byte(OpConstant), 255, 254}},
|
||||
}
|
||||
|
||||
for _, tt := range test {
|
||||
instructions := Make(tt.op, tt.operands...)
|
||||
|
||||
if len(instructions) != len(tt.expected) {
|
||||
t.Errorf("instruction has wrong length. want=%d, got=%d", len(tt.expected), len(instructions))
|
||||
}
|
||||
|
||||
for i, b := range tt.expected {
|
||||
if instructions[i] != tt.expected[i] {
|
||||
t.Errorf("wrong byte at pos %d. want=%d, got=%d", i, b, instructions[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstructions(t *testing.T) {
|
||||
instructions := []Instructions{
|
||||
Make(OpConstant, 1),
|
||||
Make(OpConstant, 2),
|
||||
Make(OpConstant, 65535),
|
||||
}
|
||||
|
||||
expected := `0000 OpConstant 1
|
||||
0003 OpConstant 2
|
||||
0006 OpConstant 65535
|
||||
`
|
||||
|
||||
concatted := Instructions{}
|
||||
for _, ins := range instructions {
|
||||
concatted = append(concatted, ins...)
|
||||
}
|
||||
|
||||
if concatted.String() != expected {
|
||||
t.Errorf("instructions wrong formatted.\nwant=%q\ngot=%q", expected, concatted.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestOperands(t *testing.T) {
|
||||
tests := []struct {
|
||||
op Opcode
|
||||
operands []int
|
||||
bytesRead int
|
||||
}{
|
||||
{OpConstant, []int{65535}, 2},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
instruction := Make(tt.op, tt.operands...)
|
||||
|
||||
def, err := Lookup(byte(tt.op))
|
||||
if err != nil {
|
||||
t.Fatalf("definition not found: %q\n", err)
|
||||
}
|
||||
|
||||
operandsRead, n := ReadOperands(def, instruction[1:])
|
||||
if n != tt.bytesRead {
|
||||
t.Fatalf("n wrong. want=%d, got=%d", tt.bytesRead, n)
|
||||
}
|
||||
|
||||
for i, want := range tt.operands {
|
||||
if operandsRead[i] != want {
|
||||
t.Errorf("operand wrong. want=%d, got=%d", want, operandsRead[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user