You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hal/hal_test.go

213 lines
3.5 KiB

4 years ago
package main
import (
"git.jfdev.de/JonasFranzDEV/hal/hal"
"git.jfdev.de/JonasFranzDEV/hal/parser"
"github.com/stretchr/testify/assert"
"math"
4 years ago
"testing"
)
func TestAddition(t *testing.T) {
input := []string{
"1 START",
"2 IN 0",
"3 STORE 10",
"4 IN 0",
"5 ADD 10",
"6 OUT 1",
"7 STOP",
}
program, err := parser.ParseProgram(input)
assert.NoError(t, err)
module, err := hal.NewHALModule(program, 256, 2, false)
assert.NoError(t, err)
module.IO[0] = 10
err = module.Run()
assert.NoError(t, err)
assert.Equal(t, float64(20), module.IO[1])
}
func TestMax(t *testing.T) {
input := []string{
"00 IN 0",
"01 STORE 1",
"02 IN 1",
"03 STORE 2",
"04 SUB 1",
"05 JUMPPOS 9",
"06 LOAD 1",
"07 OUT 1",
"08 STOP",
"09 LOAD 2",
"10 OUT 1",
"11 STOP",
}
program, err := parser.ParseProgram(input)
assert.NoError(t, err)
module, err := hal.NewHALModule(program, 256, 2, false)
assert.NoError(t, err)
module.IO[0] = 10
module.IO[1] = 15
err = module.Run()
assert.NoError(t, err)
assert.Equal(t, float64(15), module.IO[1])
}
func TestNewton1(t *testing.T) {
input := []string{
"01 START",
"02 LOADNUM 1",
"03 STORE 10",
"10 MUL 10",
"11 MUL 10",
"12 STORE 12",
"13 MUL 10",
"14 MUL 10",
"15 STORE 11",
"21 LOAD 12",
"22 MULNUM 5",
"30 ADD 11",
"31 SUBNUM 5",
"32 STORE 20",
"33 SUB 50",
"34 JUMPNULL 66",
"35 LOAD 20",
"36 STORE 50",
"40 LOAD 10",
"41 MUL 10",
"42 STORE 21",
"43 MUL 10",
"44 MUL 10",
"45 MULNUM 5",
"46 STORE 22",
"47 LOAD 21",
"48 MULNUM 15",
"49 ADD 22",
"50 STORE 30",
"60 LOAD 20",
"61 DIV 30",
"62 STORE 40",
"63 LOAD 10",
"64 SUB 40",
"65 JUMP 03",
"66 LOAD 10",
"67 OUT 1",
"68 STOP",
}
program, err := parser.ParseProgram(input)
assert.NoError(t, err)
module, err := hal.NewHALModule(program, 256, 2, false)
assert.NoError(t, err)
err = module.Run()
assert.NoError(t, err)
calculateNewton := func(start float64) float64 {
f1 := func(x float64) float64 {
return math.Pow(x, 5) + 5*math.Pow(x, 3) - 5
}
f1derived := func(x float64) float64 {
return 5*math.Pow(x, 4) + 15*math.Pow(x, 2)
}
var last float64
x := start
for x != last {
last = x
x = x - f1(x)/f1derived(x)
}
return x
}
assert.Equal(t, calculateNewton(1), module.IO[1])
}
func TestNewton2(t *testing.T) {
input := []string{
"01 START",
"02 LOADNUM 1",
"03 STORE 10",
"10 MUL 10",
"11 STORE 11",
"12 MUL 10",
"13 MUL 10",
"14 MUL 10",
"15 STORE 20",
"16 MUL 10",
"17 MULNUM 8",
"18 STORE 12",
"19 LOAD 11",
"20 MULNUM 3",
"21 ADD 12",
"22 SUBNUM 3",
"23 STORE 15",
"24 SUB 50",
"25 JUMPNULL 45",
"26 LOAD 15",
"27 STORE 50",
"30 LOAD 20",
"31 MULNUM 48",
"32 STORE 21",
"33 LOAD 10",
"34 MULNUM 6",
"35 ADD 21",
"36 STORE 25",
"40 LOAD 15",
"41 DIV 25",
"42 MULNUM -1",
"43 ADD 10",
"44 JUMP 03",
"45 LOAD 10",
"46 OUT 1",
"47 STOP",
}
program, err := parser.ParseProgram(input)
assert.NoError(t, err)
module, err := hal.NewHALModule(program, 256, 2, false)
assert.NoError(t, err)
err = module.Run()
assert.NoError(t, err)
calculateNewton := func(start float64) float64 {
f1 := func(x float64) float64 {
return 8*math.Pow(x, 6) + 3*math.Pow(x, 2) - 3
}
f1derived := func(x float64) float64 {
return 48*math.Pow(x, 5) + 6*x
}
var last float64
x := start
for x != last {
last = x
x = x - f1(x)/f1derived(x)
}
return x
}
assert.Equal(t, calculateNewton(1), module.IO[1])
}