package main import ( "bytes" "testing" ) func TestAdd(t *testing.T) { s := Stack{ values: []int{1, 2}, } b := Builtins{} err := b.Add(&s)() if err != nil { t.Fail() } if len(s.values) != 1 { t.Fail() } if s.values[0] != 3 { t.Fail() } } func TestSub(t *testing.T) { s := Stack{ values: []int{1, 2}, } b := Builtins{} err := b.Sub(&s)() if err != nil { t.Fail() } if len(s.values) != 1 { t.Fail() } if s.values[0] != 1 { t.Fail() } } func TestMul(t *testing.T) { b := Builtins{} s := Stack{ values: []int{4, 2}, } err := b.Mul(&s)() if err != nil { t.Fail() } if len(s.values) != 1 { t.Fail() } if s.values[0] != 8 { t.Fail() } s = Stack{ values: []int{-4, 2}, } err = b.Mul(&s)() if err != nil { t.Fail() } if len(s.values) != 1 { t.Fail() } if s.values[0] != -8 { t.Fail() } } func TestDiv(t *testing.T) { b := Builtins{} s := Stack{ values: []int{2, 4}, } err := b.Div(&s)() if err != nil { t.Log(err) t.Fail() } if len(s.values) != 1 { t.Log(s.values) t.Fail() } if s.values[0] != 2 { t.Log(s.values[0]) t.Fail() } s = Stack{ values: []int{2, -4}, } err = b.Div(&s)() if err != nil { t.Log(err) t.Fail() } if len(s.values) != 1 { t.Log(s.values) t.Fail() } if s.values[0] != -2 { t.Log(s.values[0]) t.Fail() } } func TestPrint(t *testing.T) { b := Builtins{} s := Stack{ values: []int{200}, } out := new(bytes.Buffer) err := b.Print(out, &s)() if err != nil { t.Log(err) t.Fail() } if len(s.values) != 0 { t.Log(s.values) t.Fail() } if string(out.Bytes()) != "200 " { t.Log(string(out.Bytes()), out.Bytes()) t.Fail() } } func TestDup(t *testing.T) { b := Builtins{} s := Stack{ values: []int{200}, } err := b.Dup(&s)() if err != nil { t.Log(err) t.Fail() } if len(s.values) != 2 { t.Log(s.values) t.Fail() } if s.values[0] != s.values[1] { t.Log(s.values) t.Fail() } } func TestSwap(t *testing.T) { b := Builtins{} s := Stack{ values: []int{200, 100}, } err := b.Swap(&s)() if err != nil { t.Log(err) t.Fail() } if len(s.values) != 2 { t.Log(s.values) t.Fail() } if s.values[0] != 100 || s.values[1] != 200 { t.Log(s.values) t.Fail() } } func TestOver(t *testing.T) { b := Builtins{} s := Stack{ values: []int{200, 100}, } err := b.Over(&s)() if err != nil { t.Log(err) t.Fail() } if len(s.values) != 3 { t.Log(s.values) t.Fail() } if s.values[0] != 100 || s.values[1] != 200 || s.values[2] != 100 { t.Log(s.values) t.Fail() } } func TestDrop(t *testing.T) { b := Builtins{} s := Stack{ values: []int{200, 100}, } err := b.Drop(&s)() if err != nil { t.Log(err) t.Fail() } if len(s.values) != 1 { t.Log(s.values) t.Fail() } if s.values[0] != 100 { t.Log(s.values) t.Fail() } } func TestRot(t *testing.T) { b := Builtins{} s := Stack{ values: []int{100, 200, 300}, } err := b.Rot(&s)() if err != nil { t.Log(err) t.Fail() } if len(s.values) != 3 { t.Log(s.values) t.Fail() } if s.values[0] != 300 || s.values[1] != 100 || s.values[2] != 200 { t.Log(s.values) t.Fail() } } func TestEmit(t *testing.T) { b := Builtins{} s := Stack{ values: []int{42}, } out := new(bytes.Buffer) err := b.Emit(out, &s)() if err != nil { t.Log(err) t.Fail() } if len(s.values) != 0 { t.Log(s.values) t.Fail() } if string(out.Bytes()) != "* " { t.Log(string(out.Bytes()), out.Bytes()) t.Fail() } } func TestToR(t *testing.T) { b := Builtins{} s := Stack{ values: []int{200, 100}, } r := Stack{ values: []int{}, } err := b.ToR(&s, &r)() if err != nil { t.Log(err) t.Fail() } if len(s.values) != 1 { t.Log(s.values) t.Fail() } if len(r.values) != 1 { t.Log(r.values) t.Fail() } if s.values[0] != 100 || r.values[0] != 200 { t.Log(s.values, r.values) t.Fail() } } func TestRFrom(t *testing.T) { b := Builtins{} s := Stack{ values: []int{}, } r := Stack{ values: []int{200, 100}, } err := b.RFrom(&s, &r)() if err != nil { t.Log(err) t.Fail() } if len(s.values) != 1 { t.Log(s.values) t.Fail() } if len(r.values) != 1 { t.Log(r.values) t.Fail() } if s.values[0] != 200 || r.values[0] != 100 { t.Log(s.values, r.values) t.Fail() } } func TestRFetch(t *testing.T) { b := Builtins{} s := Stack{ values: []int{}, } r := Stack{ values: []int{200, 100}, } err := b.RFetch(&s, &r)() if err != nil { t.Log(err) t.Fail() } if len(s.values) != 1 { t.Log(s.values) t.Fail() } if len(r.values) != 2 { t.Log(r.values) t.Fail() } if s.values[0] != 200 || r.values[0] != 200 || r.values[1] != 100 { t.Log(s.values, r.values) t.Fail() } }