Files
subcoding/test/asm_test.go

76 lines
1.4 KiB
Go
Raw Permalink Normal View History

2021-11-19 16:14:38 -10:00
package test
import "testing"
import "github.com/firestuff/subcoding/asm"
func TestTooManyOperands(t *testing.T) {
_, err := asm.AssembleString(`
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 16:14:38 -10:00
functions:
- - [nop, 0]
`)
if err == nil {
t.Fatal("Lack of error when passing operand to nop")
}
}
func TestTooFewOperands(t *testing.T) {
_, err := asm.AssembleString(`
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 16:14:38 -10:00
functions:
- - [mov, g0]
`)
if err == nil {
t.Fatal("Lack of error when passing only one operand to mov")
}
}
func TestIncorrectSigned(t *testing.T) {
_, err := asm.AssembleString(`
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 16:14:38 -10:00
functions:
- - [ltu, 0, -1]
`)
if err == nil {
t.Fatal("Lack of error when passing signed value to ltu")
}
}
2021-11-20 10:19:08 -10:00
func TestHex(t *testing.T) {
prog, err := asm.AssembleString(`
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-20 10:19:08 -10:00
functions:
- - [mov, g0, 0xfeedc0de]
`)
if err != nil {
t.Fatal(err)
}
val := prog.Functions[0].Instructions[0].Operands[1].Value
if val != 4276994270 {
t.Fatalf("Mismatch: expected=4276994270, actual=%d", val)
}
}
func TestBinary(t *testing.T) {
prog, err := asm.AssembleString(`
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-20 10:19:08 -10:00
functions:
- - [mov, g0, 0b100101]
`)
if err != nil {
t.Fatal(err)
}
val := prog.Functions[0].Instructions[0].Operands[1].Value
if val != 37 {
t.Fatalf("Mismatch: expected=37, actual=%d", val)
}
}