From ad52ab691306802ddd6138669d9ad6d6550328a0 Mon Sep 17 00:00:00 2001 From: Luca Dillenburg Date: Wed, 19 Aug 2020 21:48:01 -0300 Subject: [PATCH] update command tests with label param + jmp does support label in commandAssembler (not in assembler.go yet) --- Assembler/core/commandAssembler.go | 28 +++----- Assembler/core/commandAssembler_test.go | 94 ++++++++++++++++++++++--- 2 files changed, 94 insertions(+), 28 deletions(-) diff --git a/Assembler/core/commandAssembler.go b/Assembler/core/commandAssembler.go index a2fc0d8..32e52a4 100644 --- a/Assembler/core/commandAssembler.go +++ b/Assembler/core/commandAssembler.go @@ -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 ¶m, 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) { @@ -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, }, } diff --git a/Assembler/core/commandAssembler_test.go b/Assembler/core/commandAssembler_test.go index b6ab2c3..f8e0e33 100644 --- a/Assembler/core/commandAssembler_test.go +++ b/Assembler/core/commandAssembler_test.go @@ -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}, @@ -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 ¶m +} +func newCmdStringParam(str string) *data.CommandParameter { + param := data.NewStringParam(str) + return ¶m +} + func TestAssembleCommand(t *testing.T) { if len(commands) != 12 { t.Errorf("Tests were not updated") @@ -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}, @@ -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 { @@ -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") }