Skip to content

Commit

Permalink
Added test case for the memory layout
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjovanovic committed Apr 13, 2012
1 parent ae16df1 commit 0d71afd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
4 changes: 2 additions & 2 deletions dcpu16/assembler/assembler.go
Expand Up @@ -14,8 +14,8 @@ func Assemble(program []string) []dcpu16.Word {
/* labels := make(map[string]word)*/ /* labels := make(map[string]word)*/
/* labelPlaceholders := make(map[string][]word)*/ /* labelPlaceholders := make(map[string][]word)*/


for _, line := range program { for i, _ := range program {
line = line byteCode[i] = dcpu16.Word(i)
} }


return byteCode return byteCode
Expand Down
65 changes: 64 additions & 1 deletion dcpu16/assembler/assembler_test.go
@@ -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")
}
}

0 comments on commit 0d71afd

Please sign in to comment.