Repository navigation
Expand file tree
/
Copy pathforth_test.go
More file actions
107 lines (99 loc) · 2.56 KB
/
Copy pathforth_test.go
File metadata and controls
107 lines (99 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package forth
import (
"fmt"
"os"
"reflect"
"testing"
)
type forthTest struct {
val string
res Cell
err string
}
var forthTests = []forthTest{
{"hostname", "", ""},
{"2", "2", ""},
{"", "2", "Empty stack"},
{"2 2 +", "4", ""},
{"4 2 -", "2", ""},
{"4 2 *", "8", ""},
{"4 2 /", "2", ""},
{"5 2 %", "1", ""},
{"sb43 hostbase", "43", ""},
{"sb43 hostbase dup 20 / swap 20 % dup ifelse", "3", ""},
{"sb40 hostbase dup 20 / swap 20 % dup ifelse", "2", ""},
{"2 4 swap /", "2", ""},
{"0 1 1 ifelse", "1", ""},
{"0 1 0 ifelse", "0", ""},
{"str cat strcat", "strcat", ""},
{"1 dup +", "2", ""},
{"4095 4096 roundup", "4096", ""},
{"4097 8192 roundup", "8192", ""},
{"2 x +", nil, "parsing \"x\": invalid syntax"},
{"1 dd +", "2", ""},
{"1 d3d", "3", ""},
}
func TestForth(t *testing.T) {
forthTests[0].res, _ = os.Hostname()
f := New()
if f.Length() != 0 {
t.Errorf("Test: stack is %d and should be 0", f.Length())
}
if f.Empty() != true {
t.Errorf("Test: stack is %v and should be false", f.Empty())
}
f.Push("test")
if f.Length() != 1 {
t.Errorf("Test: stack is %d and should be 1", f.Length())
}
if f.Empty() == true {
t.Errorf("Test: stack is %v and should be false", f.Empty())
}
f.Reset()
if f.Length() != 0 {
t.Errorf("Test: After Reset(): stack is %d and should be 0", f.Length())
}
if f.Empty() != true {
t.Errorf("Test: After Reset(): stack is %v and should be true", f.Empty())
}
NewWord(f, "dd", "dup")
NewWord(f, "d3d", "dup dup + +")
for _, tt := range forthTests {
var err error
res, err := Eval(f, tt.val)
if res == tt.res || (err != nil && err.Error() == tt.err) {
if err != nil {
/* stack is not going to be right; reset it. */
f.Reset()
}
t.Logf("Test: '%v' '%v' '%v': Pass\n", tt.val, res, err)
} else {
t.Errorf("Test: '%v' got (%v, %v): want (%v, %v): Fail\n", tt.val, res, err.Error(), tt.res, tt.err)
t.Logf("ops %v\n", Ops())
continue
}
if f.Length() != 0 {
t.Errorf("Test: %v: stack is %d and should be empty", tt, f.Length())
}
if f.Empty() != true {
t.Errorf("Test: %v: stack is %v and should be empty", tt, f.Empty())
}
}
}
func TestBadPop(t *testing.T) {
var b [3]byte
f := New()
f.Push(b)
res, err := Eval(f, "2 +")
t.Logf("%v, %v", res, err)
nan := fmt.Errorf("NaN: %T", b)
if !reflect.DeepEqual(err, nan) {
t.Errorf("got %v, want %v", err, nan)
}
if res != nil {
t.Errorf("got %v, want nil", res)
}
}