Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test case for the memory layout
- Loading branch information
1 parent
ae16df1
commit 0d71afd
Showing
2 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1 +1,64 @@ | |||
package assembler | package assembler | ||
|
|||
import "testing" | |||
import "os" | |||
import "bufio" | |||
import "io" | |||
import "github.com/ivanjovanovic/0x10c/dcpu16" | |||
import "fmt" | |||
|
|||
func readLines(filename string) []string { | |||
file, _ := os.Open(filename) | |||
reader := bufio.NewReader(file) | |||
lines := []string{} | |||
|
|||
for { | |||
line, _, _ := reader.ReadLine() | |||
// is there a better way to get to EOF | |||
if _, err := reader.Peek(1); err == io.EOF { | |||
break | |||
} | |||
lines = append(lines, string(line)) | |||
} | |||
return lines | |||
} | |||
|
|||
func assertSame(specified, assembled []string) bool { | |||
for i, _ := range specified { | |||
if specified[i] != assembled[i] { | |||
return false | |||
} | |||
} | |||
return true | |||
} | |||
|
|||
func castMemoryContent(memory []dcpu16.Word) []string { | |||
memString := []string{} | |||
|
|||
for i, _ := range memory { | |||
memString = append(memString, fmt.Sprintf("%04x", int(memory[i]))) | |||
} | |||
return memString | |||
} | |||
|
|||
func printMemory(spec, assembled []string) { | |||
fmt.Print("Specified:\tAssembled\t\n") | |||
for i, val := range spec { | |||
fmt.Printf(" %s\t\t %s\n", val, assembled[i]) | |||
} | |||
} | |||
|
|||
func TestAssembler(t *testing.T) { | |||
inputLines := readLines("test/spec.dasm") | |||
memoryImage := Assemble(inputLines) | |||
memorySpec := readLines("test/spec.mem") | |||
|
|||
relevantMemoryImage := castMemoryContent(memoryImage)[0:len(memorySpec)] | |||
|
|||
same := assertSame(memorySpec, relevantMemoryImage) | |||
|
|||
if !same { | |||
printMemory(memorySpec, relevantMemoryImage) | |||
t.Error("Returned memory different from specified") | |||
} | |||
} |