Skip to content

Commit

Permalink
✔️ TEST: Add timeout to tests
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Dillenburg <luca.assumpcao.dillenburg@gmail.com>
  • Loading branch information
LucaDillenburg committed Mar 15, 2021
1 parent 395fe4b commit 5e886f8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
16 changes: 12 additions & 4 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}

Expand All @@ -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
Expand All @@ -49,7 +57,7 @@ ffff # max (0x38)
# print a - b (7-8)
2032
8032
{expect 0xEECD=61133}
{expect print 0xEECD=61133}

---

Expand All @@ -70,7 +78,7 @@ ffff # max (0x38)

# print b (F)
8033
{expect 0x1234=4660}
{expect print 0x1234=4660}

---

Expand Down
33 changes: 23 additions & 10 deletions test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,45 @@ 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")

if err != nil {
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++
Expand Down
2 changes: 1 addition & 1 deletion test/program
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5e886f8

Please sign in to comment.