1
exercism fetch scala forth

src/test/scala/ForthTest.scala

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import org.scalatest.{Matchers, FunSuite}

/** @version 1.2.0 */
class ForthTest extends FunSuite with Matchers {

  private val forth = new Forth

  test("parsing and numbers - empty input results in empty stack") {
    forth.eval("").fold(_ => "", _.toString) should be ("")
  }

  test("parsing and numbers - numbers just get pushed onto the stack") {
    pending
    forth.eval("1 2 3 4 5").fold(_ => "", _.toString) should be ("1 2 3 4 5")
  }

  test("addition - can add two numbers") {
    pending
    forth.eval("1 2 +").fold(_ => "", _.toString) should be ("3")
  }

  test("addition - errors if there is nothing on the stack") {
    pending
    forth.eval("+").isLeft should be (true)
  }

  test("addition - errors if there is only one value on the stack") {
    pending
    forth.eval("1 +").isLeft should be (true)
  }

  test("subtraction - can subtract two numbers") {
    pending
    forth.eval("3 4 -").fold(_ => "", _.toString) should be ("-1")
  }

  test("subtraction - errors if there is nothing on the stack") {
    pending
    forth.eval("-").isLeft should be (true)
  }

  test("subtraction - errors if there is only one value on the stack") {
    pending
    forth.eval("1 -").isLeft should be (true)
  }

  test("multiplication - can multiply two numbers") {
    pending
    forth.eval("2 4 *").fold(_ => "", _.toString) should be ("8")
  }

  test("multiplication - errors if there is nothing on the stack") {
    pending
    forth.eval("*").isLeft should be (true)
  }

  test("multiplication - errors if there is only one value on the stack") {
    pending
    forth.eval("1 *").isLeft should be (true)
  }

  test("division - can divide two numbers") {
    pending
    forth.eval("12 3 /").fold(_ => "", _.toString) should be ("4")
  }

  test("division - performs integer division") {
    pending
    forth.eval("8 3 /").fold(_ => "", _.toString) should be ("2")
  }

  test("division - errors if dividing by zero") {
    pending
    forth.eval("4 0 /").isLeft should be (true)
  }

  test("division - errors if there is nothing on the stack") {
    pending
    forth.eval("/").isLeft should be (true)
  }

  test("division - errors if there is only one value on the stack") {
    pending
    forth.eval("1 /").isLeft should be (true)
  }

  test("combined arithmetic - addition and subtraction") {
    pending
    forth.eval("1 2 + 4 -").fold(_ => "", _.toString) should be ("-1")
  }

  test("combined arithmetic - multiplication and division") {
    pending
    forth.eval("2 4 * 3 /").fold(_ => "", _.toString) should be ("2")
  }

  test("dup - copies the top value on the stack") {
    pending
    forth.eval("1 DUP").fold(_ => "", _.toString) should be ("1 1")
  }

  test("dup - is case-insensitive") {
    pending
    forth.eval("1 2 Dup").fold(_ => "", _.toString) should be ("1 2 2")
  }

  test("dup - errors if there is nothing on the stack") {
    pending
    forth.eval("dup").isLeft should be (true)
  }

  test("drop - removes the top value on the stack if it is the only one") {
    pending
    forth.eval("1 drop").fold(_ => "", _.toString) should be ("")
  }

  test("drop - removes the top value on the stack if it is not the only one") {
    pending
    forth.eval("1 2 drop").fold(_ => "", _.toString) should be ("1")
  }

  test("drop - errors if there is nothing on the stack") {
    pending
    forth.eval("drop").isLeft should be (true)
  }

  test("swap - swaps the top two values on the stack if they are the only ones") {
    pending
    forth.eval("1 2 swap").fold(_ => "", _.toString) should be ("2 1")
  }

  test("swap - swaps the top two values on the stack if they are not the only ones") {
    pending
    forth.eval("1 2 3 swap").fold(_ => "", _.toString) should be ("1 3 2")
  }

  test("swap - errors if there is nothing on the stack") {
    pending
    forth.eval("swap").isLeft should be (true)
  }

  test("swap - errors if there is only one value on the stack") {
    pending
    forth.eval("1 swap").isLeft should be (true)
  }

  test("over - copies the second element if there are only two") {
    pending
    forth.eval("1 2 over").fold(_ => "", _.toString) should be ("1 2 1")
  }

  test("over - copies the second element if there are more than two") {
    pending
    forth.eval("1 2 3 over").fold(_ => "", _.toString) should be ("1 2 3 2")
  }

  test("over - errors if there is nothing on the stack") {
    pending
    forth.eval("over").isLeft should be (true)
  }

  test("over - errors if there is only one value on the stack") {
    pending
    forth.eval("1 over").isLeft should be (true)
  }

  test("user-defined words - can consist of built-in words") {
    pending
    forth.eval(": dup-twice dup dup ; 1 dup-twice").fold(_ => "", _.toString) should be ("1 1 1")
  }

  test("user-defined words - execute in the right order") {
    pending
    forth.eval(": countup 1 2 3 ; countup").fold(_ => "", _.toString) should be ("1 2 3")
  }

  test("user-defined words - can override other user-defined words") {
    pending
    forth.eval(": foo dup ; : foo dup dup ; 1 foo").fold(_ => "", _.toString) should be ("1 1 1")
  }

  test("user-defined words - can override built-in words") {
    pending
    forth.eval(": swap dup ; 1 swap").fold(_ => "", _.toString) should be ("1 1")
  }

  test("user-defined words - can override built-in operators") {
    pending
    forth.eval(": + * ; 3 4 +").fold(_ => "", _.toString) should be ("12")
  }

  test("user-defined words - cannot redefine numbers") {
    pending
    forth.eval(": 1 2 ;").isLeft should be (true)
  }

  test("user-defined words - errors if executing a non-existent word") {
    pending
    forth.eval("foo").isLeft should be (true)
  }
}