Skip to content

Commit

Permalink
✔️ TEST: Add converting testing and refactor
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 18786bf commit 395fe4b
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 47 deletions.
5 changes: 2 additions & 3 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Test

## Run
## Run Test
```sh
go run main.go # from main.go
go test # from main_test.go
go test
```

---
Expand Down
46 changes: 3 additions & 43 deletions test/main.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
86 changes: 85 additions & 1 deletion test/main_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,85 @@
// TODO: migrate main.go to test
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)
}
}
}

0 comments on commit 395fe4b

Please sign in to comment.