Skip to content

Commit

Permalink
update command tests with label param + jmp does support label in com…
Browse files Browse the repository at this point in the history
…mandAssembler (not in assembler.go yet)
  • Loading branch information
LucaDillenburg committed Aug 20, 2020
1 parent 62dde9e commit ad52ab6
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 28 deletions.
28 changes: 8 additions & 20 deletions Assembler/core/commandAssembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,11 @@ func getParamNoParam(commandName string, words []string) (*data.CommandParameter
}

func getSecondWordAsInt(commandName string, words []string) (*data.CommandParameter, *myerrors.CustomError) {
if len(words) != 2 {
if len(words) < 2 {
err := myerrors.WrongNumberOfParamsError(commandName, 1, 0, []string{})
return nil, myerrors.NewCodeError(err)
}

remainingParams := getCommandParams(words)
err := myerrors.WrongNumberOfParamsError(commandName, 1, len(remainingParams), remainingParams)
return nil, myerrors.NewCodeError(err)
}
return getSecondWord(commandName, words, false)
}

num, err := utils.StrToPositiveInt(words[1])
if err != nil {
return nil, myerrors.NewCodeError(err)
}
param := data.NewIntParam(num)
return &param, nil
func getSecondWordAsIntOrString(commandName string, words []string) (*data.CommandParameter, *myerrors.CustomError) {
return getSecondWord(commandName, words, true)
}

func getSecondWord(commandName string, words []string, acceptStringParam bool) (*data.CommandParameter, *myerrors.CustomError) {
Expand Down Expand Up @@ -128,19 +116,19 @@ var commands = map[string]commandConfig{
code: 0x9,
},
"jmp": commandConfig{
getParam: getSecondWordAsInt,
getParam: getSecondWordAsIntOrString,
code: 0xA,
},
"jg": commandConfig{
getParam: getSecondWordAsInt,
getParam: getSecondWordAsIntOrString,
code: 0xB,
},
"je": commandConfig{
getParam: getSecondWordAsInt,
getParam: getSecondWordAsIntOrString,
code: 0xD,
},
"jl": commandConfig{
getParam: getSecondWordAsInt,
getParam: getSecondWordAsIntOrString,
code: 0xF,
},
}
94 changes: 86 additions & 8 deletions Assembler/core/commandAssembler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@ func TestGetNoParam(t *testing.T) {
}
}

func TestGetSecondParam(t *testing.T) {
func TestGetSecondParamAsInt(t *testing.T) {
var tests = []struct {
line string
expected int
expectsErr bool
}{
// Decimal Number
{"mov 1", 1, false},
// Hexadecimal Number
{"mov 0x1a", 26, false},
{"mov 0x001", 1, false},
// Conversion
{"mov 0x0f", 15, false},
{"mov 0xff", 255, false},
{"mov 0x0ff", 255, false},
// Variable
{"mov 0xx0ff", 0, true},
{"mov x1", 0, true},
{"mov 0x1g", 0, true},
Expand All @@ -46,12 +48,73 @@ func TestGetSecondParam(t *testing.T) {
t.Errorf("[%d] Expected error: %t, Got error: %t", i, test.expectsErr, gotError)
}

if !test.expectsErr && !gotError && test.expected != got.Num {
t.Errorf("[%d] Expected int: %d, Got int: %d", i, test.expected, got.Num)
if !test.expectsErr && !gotError {
if got.IsStr {
t.Errorf("[%d] Expecting int parameter", i)
}

if test.expected != got.Num {
t.Errorf("[%d] Expected int: %d, Got int: %d", i, test.expected, got.Num)
}
}
}
}

func TestGetSecondParamAsIntOrString(t *testing.T) {
var tests = []struct {
line string
expected *data.CommandParameter
expectsErr bool
}{
// Decimal Number
{"jmp 1", newCmdIntParam(1), false},
// Hexadecimal Number
{"jmp 0x001", newCmdIntParam(1), false},
{"jmp 0x0f", newCmdIntParam(15), false},
{"jmp 0xff", newCmdIntParam(255), false},
{"jmp 0x0ff", newCmdIntParam(255), false},
// Variable
{"jmp a8", newCmdStringParam("a8"), false},
{"jmp x1", newCmdStringParam("x1"), false},
{"jmp a1", newCmdStringParam("a1"), false},
// Errors 1 param
{"jmp 0xx0ff", nil, true},
{"jmp 0x1g", nil, true},
{"jmp 1a", nil, true},
// Erros amnt params
{"jmp", nil, true},
{"jmp a b", nil, true},
{"jmp a b c", nil, true},
}

for i, test := range tests {
arrayWords := strings.Split(test.line, " ")
got, err := getSecondWord(arrayWords[0], arrayWords, true)
gotError := err != nil

if test.expectsErr != gotError {
t.Errorf("[%d] Expected error: %t, Got error: %t", i, test.expectsErr, gotError)
}

if !test.expectsErr && !gotError {
if got.IsStr != test.expected.IsStr {
t.Errorf("[%d] Expected IsStr: %t, Got IsStr: %t", i, test.expected.IsStr, got.IsStr)
}
if *test.expected != *got {
t.Errorf("[%d] Expected: %v, Got: %v", i, test.expected, got)
}
}
}
}
func newCmdIntParam(num int) *data.CommandParameter {
param := data.NewIntParam(num)
return &param
}
func newCmdStringParam(str string) *data.CommandParameter {
param := data.NewStringParam(str)
return &param
}

func TestAssembleCommand(t *testing.T) {
if len(commands) != 12 {
t.Errorf("Tests were not updated")
Expand All @@ -62,7 +125,7 @@ func TestAssembleCommand(t *testing.T) {
expected *data.Command
expectsErr bool
}{
// Success
// Success Number
{"nop", getCommand(0x0, 0), false},
{"copy 0x10", getCommand(0x1, 16), false},
{"store 0x10", getCommand(0x2, 16), false},
Expand All @@ -75,10 +138,21 @@ func TestAssembleCommand(t *testing.T) {
{"jg 0x8", getCommand(0xB, 8), false},
{"je 0x8", getCommand(0xD, 8), false},
{"jl 0x8", getCommand(0xF, 8), false},
// Fail
// Success Label
{"jmp label", getCommandStr(0xA, "label"), false},
{"jg label", getCommandStr(0xB, "label"), false},
{"je label", getCommandStr(0xD, "label"), false},
{"jl label", getCommandStr(0xF, "label"), false},
// Fail: Wrong Command
{"nope", nil, true},
// Fail: No label as param
{"add x10", nil, true},
// Fail: Wrong param
{"add 1x10", nil, true},
// Fail: Amnt params
{"kill 0", nil, true},
{"output", nil, true},
{"output 8 1", nil, true},
}

for i, test := range tests {
Expand Down Expand Up @@ -106,10 +180,14 @@ func getCommand(code int, param int) *data.Command {
cmd, _ := data.NewCommand(code, data.NewIntParam(param))
return cmd
}
func getCommandStr(code int, param string) *data.Command {
cmd, _ := data.NewCommand(code, data.NewStringParam(param))
return cmd
}

func TestGetCommandParams(t *testing.T) {
expected := []string{"0x1", "1"}
got := getCommandParams([]string{"mov", "0x1", "1"})
expected := []string{"0x1", "1", "label"}
got := getCommandParams([]string{"mov", "0x1", "1", "label"})
if !reflect.DeepEqual(got, expected) {
t.Errorf("Error")
}
Expand Down

0 comments on commit ad52ab6

Please sign in to comment.