From 5e886f8a2eaf7c04c9e2a9c2ce94ea2ecf05617d Mon Sep 17 00:00:00 2001 From: Luca Dillenburg Date: Mon, 15 Mar 2021 12:35:40 -0300 Subject: [PATCH] :heavy_check_mark: TEST: Add timeout to tests Signed-off-by: Luca Dillenburg --- test/README.md | 16 ++++++++++++---- test/main_test.go | 33 +++++++++++++++++++++++---------- test/program | 2 +- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/test/README.md b/test/README.md index 42a4a38..1e26fdb 100644 --- a/test/README.md +++ b/test/README.md @@ -12,7 +12,7 @@ Code: 16 bits = instruction (4 bits) + parameter (12 bits). For example, in the ### Ram ``` -0000 code 9000 0000x10 memory +code 9000 0000x10 memory ``` ### Memory @@ -30,6 +30,14 @@ ffff # max (0x38) ### Code ```sh + +{test execution of first command} + +8038 +{expect print 0xffff=65535} + +--- + {test sum and subtraction: 0x3 and 0x4} {switch a and b and print a position} @@ -40,7 +48,7 @@ ffff # max (0x38) # print a + b (3-4) 2032 8032 -{expect 0x1335=4917} +{expect print 0x1335=4917} # a - b (5-6) 1033 @@ -49,7 +57,7 @@ ffff # max (0x38) # print a - b (7-8) 2032 8032 -{expect 0xEECD=61133} +{expect print 0xEECD=61133} --- @@ -70,7 +78,7 @@ ffff # max (0x38) # print b (F) 8033 -{expect 0x1234=4660} +{expect print 0x1234=4660} --- diff --git a/test/main_test.go b/test/main_test.go index 5cc704b..f34e32d 100644 --- a/test/main_test.go +++ b/test/main_test.go @@ -5,8 +5,15 @@ import ( "os" "strings" "testing" + "time" ) +func getInput(input chan string, scanner *bufio.Scanner) { + scanner.Scan() + line := scanner.Text() + input <- line +} + func TestProgram(t *testing.T) { stdout, err := runProgram("program") @@ -14,23 +21,29 @@ func TestProgram(t *testing.T) { os.Exit(1) } - expected := []string{"1335", "EECD", "1234", "f000", "0000", "f000", "0001", "0000", "0001", "8888"} + expected := []string{"ffff", "1335", "EECD", "1234", "f000", "0000", "f000", "0001", "0000", "0001", "8888"} scanner := bufio.NewScanner(stdout) scanner.Scan() // ignores first print var i = 0 - for scanner.Scan() { - line := scanner.Text() + for { + input := make(chan string, 1) + go getInput(input, scanner) - binaryStr := strings.ReplaceAll(line, " ", "")[0:16] - gotNum, errBinary := binaryStringToNumber(binaryStr) + select { + case line := <-input: + binaryStr := strings.ReplaceAll(line, " ", "")[0:16] + gotNum, errBinary := binaryStringToNumber(binaryStr) - expectedNum, errHex := hexStringToNumber(expected[i]) + expectedNum, errHex := hexStringToNumber(expected[i]) - if errBinary != nil || errHex != nil { - t.Errorf("Parsing error. ErrBinary: '%t', ErrHex: '%t'.", errBinary, errHex) - } else if gotNum != expectedNum { - t.Errorf("Got different then expected. Expected: %d, but got: %d.\n", expectedNum, gotNum) + if errBinary != nil || errHex != nil { + t.Errorf("[%d] Parsing error. ErrBinary: '%t', ErrHex: '%t'.", i, errBinary, errHex) + } else if gotNum != expectedNum { + t.Errorf("[%d] Got different then expected. Expected: %d, but got: %d.\n", i, expectedNum, gotNum) + } + case <-time.After(3000 * time.Millisecond): + t.FailNow() } i++ diff --git a/test/program b/test/program index e58e693..b3d1062 100644 --- a/test/program +++ b/test/program @@ -1,2 +1,2 @@ v2.0 raw -0000 1033 3034 2032 8032 1033 4034 2032 8032 1033 2032 1034 2033 1032 2034 8033 a012 8038 1037 b015 8037 1035 b018 8035 1036 b01B 8038 1037 d01E 8037 1036 d021 8036 1035 d024 8038 1035 f027 8035 1036 f02A 8036 1037 f02D 8038 8039 9000 0000 0000 0000 0000 0101 1234 0000 0001 f000 ffff 8888 +8038 1033 3034 2032 8032 1033 4034 2032 8032 1033 2032 1034 2033 1032 2034 8033 a012 8038 1037 b015 8037 1035 b018 8035 1036 b01B 8038 1037 d01E 8037 1036 d021 8036 1035 d024 8038 1035 f027 8035 1036 f02A 8036 1037 f02D 8038 8039 9000 0000 0000 0000 0000 0101 1234 0000 0001 f000 ffff 8888