From 395fe4b5287f10df401ef9f6ef685ba0d0423436 Mon Sep 17 00:00:00 2001 From: Luca Dillenburg Date: Mon, 15 Mar 2021 11:48:22 -0300 Subject: [PATCH] :heavy_check_mark: TEST: Add converting testing and refactor Signed-off-by: Luca Dillenburg --- test/README.md | 5 ++- test/main.go | 46 ++----------------------- test/main_test.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 90 insertions(+), 47 deletions(-) diff --git a/test/README.md b/test/README.md index 97e156c..42a4a38 100644 --- a/test/README.md +++ b/test/README.md @@ -1,9 +1,8 @@ # Test -## Run +## Run Test ```sh -go run main.go # from main.go -go test # from main_test.go +go test ``` --- diff --git a/test/main.go b/test/main.go index aacfabc..9ac794b 100644 --- a/test/main.go +++ b/test/main.go @@ -1,54 +1,14 @@ package main import ( - "bufio" - "fmt" "io" - "os" "os/exec" "strconv" - "strings" ) -func main() { - stdout, err := runCommand("java", "-jar", "../logisim-evolution.jar", "../main.circ", "-load", "program", "-tty", "table") - if err != nil { - os.Exit(1) - } - - expected := []string{"1335", "EECD", "1234", "f000", "0000", "f000", "0001", "0000", "0001", "8888"} - - fmt.Printf("started!\n") - - scanner := bufio.NewScanner(stdout) - scanner.Scan() // ignores first print - var i = 0 - for scanner.Scan() { - line := scanner.Text() - - binaryStr := strings.ReplaceAll(line, " ", "")[0:16] - gotNum, errBinary := binaryStringToNumber(binaryStr) - - expectedNum, errHex := hexStringToNumber(expected[i]) - - if errBinary != nil || errHex != nil { - fmt.Printf("Parsing error. ErrBinary: '%t', ErrHex: '%t'.", errBinary, errHex) - } else if gotNum != expectedNum { - fmt.Printf("Got different then expected. Expected: %d, but got: %d.\n", expectedNum, gotNum) - } else { - fmt.Printf("%d is right!\n", i) - } - - i++ - fmt.Printf("len(expected) == i || %d == %d || expected: %d\n", len(expected), i, expectedNum) - if len(expected) == i { - fmt.Print("breaked") - break - } - } - - fmt.Print("closed") - stdout.Close() +func runProgram(programPath string) (io.ReadCloser, error) { + stdout, err := runCommand("java", "-jar", "../logisim-evolution.jar", "../main.circ", "-load", programPath, "-tty", "table") + return stdout, err } func binaryStringToNumber(s string) (uint64, error) { diff --git a/test/main_test.go b/test/main_test.go index edf67bf..5cc704b 100644 --- a/test/main_test.go +++ b/test/main_test.go @@ -1 +1,85 @@ -// TODO: migrate main.go to test \ No newline at end of file +package main + +import ( + "bufio" + "os" + "strings" + "testing" +) + +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"} + + scanner := bufio.NewScanner(stdout) + scanner.Scan() // ignores first print + var i = 0 + for scanner.Scan() { + line := scanner.Text() + + binaryStr := strings.ReplaceAll(line, " ", "")[0:16] + gotNum, errBinary := binaryStringToNumber(binaryStr) + + 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) + } + + i++ + if len(expected) == i { + break + } + } + + stdout.Close() +} + +func TestBinaryStringToNumber(t *testing.T) { + tests := []struct { + param string + expect uint64 + }{ + {param: "0000000000000001", expect: 1}, + {param: "1000000000000001", expect: 32769}, + {param: "0001001100110101", expect: 4917}, + {param: "1111111111111111", expect: 65535}, + } + + for i, test := range tests { + got, err := binaryStringToNumber(test.param) + if err != nil { + t.Errorf("[%d] Error: %t", i, err) + } else if got != test.expect { + t.Errorf("[%d] Got: %d | Expected: %d", i, got, test.expect) + } + } +} + +func TestHexStringToNumber(t *testing.T) { + tests := []struct { + param string + expect uint64 + }{ + {param: "0001", expect: 1}, + {param: "8001", expect: 32769}, + {param: "1335", expect: 4917}, + {param: "ffff", expect: 65535}, + } + + for i, test := range tests { + got, err := hexStringToNumber(test.param) + if err != nil { + t.Errorf("[%d] Error: %t", i, err) + } else if got != test.expect { + t.Errorf("[%d] Got: %d | Expected: %d", i, got, test.expect) + } + } +}