From 6960f8870fd6a65955db7f9f5ee04aa6848d56ac Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 4 Jun 2017 01:01:31 +0200 Subject: Add tests --- eval_test.go | 35 +++++++++++++++++++++++++++++++++++ parse_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 eval_test.go create mode 100644 parse_test.go diff --git a/eval_test.go b/eval_test.go new file mode 100644 index 0000000..d60f7ab --- /dev/null +++ b/eval_test.go @@ -0,0 +1,35 @@ +package j1 + +import ( + "fmt" + "testing" +) + +func TestEval(t *testing.T) { + testCases := []struct { + before J1 + after J1 + ins Instruction + }{ + {ins: ALU{}, before: J1{}, after: J1{pc: 1}}, + {ins: Jump(0xff), before: J1{}, after: J1{pc: 0xff}}, + {ins: Cond(0xff), before: J1{st0: 1, dsp: 1}, after: J1{pc: 1}}, + {ins: Cond(0xff), before: J1{st0: 0, dsp: 1}, after: J1{pc: 0xff}}, + {ins: Call(0xff), before: J1{}, after: J1{pc: 0xff, rstack: [32]uint16{1}, rsp: 1}}, + {ins: Lit(0xff), before: J1{}, after: J1{pc: 1, st0: 0xff, dstack: [32]uint16{0xff}, dsp: 1}}, + {ins: Lit(0xfe), + before: J1{pc: 1, st0: 0xff, dstack: [32]uint16{0xff}, dsp: 1}, + after: J1{pc: 2, st0: 0xfe, dstack: [32]uint16{0xff, 0xfe}, dsp: 2}}, + // to be continued + } + + for _, tc := range testCases { + t.Run(fmt.Sprint(tc.ins), func(t *testing.T) { + state := &tc.before + state.eval(tc.ins) + if *state != tc.after { + t.Errorf("got %v, want %v", state, &tc.after) + } + }) + } +} diff --git a/parse_test.go b/parse_test.go new file mode 100644 index 0000000..cb29333 --- /dev/null +++ b/parse_test.go @@ -0,0 +1,43 @@ +package j1 + +import ( + "fmt" + "testing" +) + +func TestDecode(t *testing.T) { + testCases := []struct { + bin uint16 + ins Instruction + }{ + {0x0000, Jump(0x0000)}, + {0x1fff, Jump(0x1fff)}, + {0x2000, Cond(0x0000)}, + {0x3fff, Cond(0x1fff)}, + {0x4000, Call(0x0000)}, + {0x5fff, Call(0x1fff)}, + {0x8000, Lit(0x0000)}, + {0xffff, Lit(0x7fff)}, + {0x6000, ALU{Opcode: 0}}, + {0x7000, ALU{Opcode: 0, RtoPC: true}}, + {0x6080, ALU{Opcode: 0, TtoN: true}}, + {0x6040, ALU{Opcode: 0, TtoR: true}}, + {0x6020, ALU{Opcode: 0, NtoAtT: true}}, + {0x600c, ALU{Opcode: 0, Rdir: -1}}, + {0x6004, ALU{Opcode: 0, Rdir: 1}}, + {0x6003, ALU{Opcode: 0, Ddir: -1}}, + {0x6001, ALU{Opcode: 0, Ddir: 1}}, + {0x6f00, ALU{Opcode: 15}}, + {0x70e5, ALU{Opcode: 0, RtoPC: true, TtoN: true, TtoR: true, NtoAtT: true, Rdir: 1, Ddir: 1}}, + {0x7fef, ALU{Opcode: 15, RtoPC: true, TtoN: true, TtoR: true, NtoAtT: true, Rdir: -1, Ddir: -1}}, + } + + for _, tc := range testCases { + t.Run(fmt.Sprint(tc.ins), func(t *testing.T) { + ins := Decode(tc.bin) + if ins != tc.ins { + t.Errorf("got %v, want %v", ins, tc.ins) + } + }) + } +} -- cgit v1.2.3