Skip to content

Commit

Permalink
Parse then Print test.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmorgan committed Aug 23, 2024
1 parent 9203a17 commit 393a065
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions parser/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,46 @@ import (
"bufio"
"bytes"
"reflect"
"strings"
"testing"
)

var testprogram = ` >>>>>
[-]+++++
[->>+<<]
>
[
,
[<<<]
++
[->>++<<]
>>
[->+>+<<]
>
[->+++>+++<<]
]
>>
.
[->>-<<]
`

func TestPrint(t *testing.T) {
program := []Instruction{
{opNoop, 0},
{opAddDp, 5},
{opSetVal, 0},
{opAddVal, 5},
{opSetVal, 5},
{opMove, 2},
{opAddDp, 1},
{opJmpZ, 7},
{opIn, 1},
{opSkip, -3},
{opSetVal, 2},
{opMulVal, 2},
{opNoop, 2},
{opAddDp, 2},
{opDupVal, 1},
{opNoop, 2},
{opAddDp, 1},
{opVec, 1},
{opNoop, 2},
{opNoop, 3},
Expand All @@ -34,7 +56,22 @@ func TestPrint(t *testing.T) {
outputBuf := bufio.NewWriter(&buf)
Print(program, outputBuf)
got := buf.String()
want := " >>>>>\n [-]\n +++++\n [->>+<<]\n [\n\t ,\n\t [<<<]\n\t ++\n\t [->>++<<]\n\t [->+>+<<]\n\t [->+++>+++<<]\n ]\n >>\n .\n [->>-<<]\n"
want := testprogram

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
}

func TestParseThenPrint(t *testing.T) {
want := testprogram

program, _ := Tokenise(bufio.NewReader(strings.NewReader(testprogram)))

var buf bytes.Buffer
outputBuf := bufio.NewWriter(&buf)
Print(program, outputBuf)
got := buf.String()

if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
Expand Down

0 comments on commit 393a065

Please sign in to comment.