Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added test case for the memory layout

  • Loading branch information...
commit 0d71afd04baddee88cd7ac41d343b36533018557 1 parent ae16df1
@ivanjovanovic authored
View
4 dcpu16/assembler/assembler.go
@@ -14,8 +14,8 @@ func Assemble(program []string) []dcpu16.Word {
/* labels := make(map[string]word)*/
/* labelPlaceholders := make(map[string][]word)*/
- for _, line := range program {
- line = line
+ for i, _ := range program {
+ byteCode[i] = dcpu16.Word(i)
}
return byteCode
View
65 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")
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.