1
exercism fetch python forth

forth_test.py

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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import unittest

from forth import evaluate, StackUnderflowError


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.0
# Tests for case-insensitivity are track-specific

class ForthParsingTest(unittest.TestCase):
    def test_empty_input_empty_stack(self):
        input_data = []
        expected = []
        self.assertEqual(evaluate(input_data), expected)

    def test_empty_line_empty_stack(self):
        input_data = [""]
        expected = []
        self.assertEqual(evaluate(input_data), expected)

    def test_numbers_just_get_pushed_to_stack(self):
        input_data = ["1 2 3 4 5"]
        expected = [1, 2, 3, 4, 5]
        self.assertEqual(evaluate(input_data), expected)


class ForthAdditionTest(unittest.TestCase):
    def test_can_add_two_numbers(self):
        input_data = ["1 2 +"]
        expected = [3]
        self.assertEqual(evaluate(input_data), expected)

    def test_errors_if_there_is_nothing_on_the_stack(self):
        input_data = ["+"]
        with self.assertRaises(StackUnderflowError):
            evaluate(input_data)

    def test_errors_if_there_is_only_one_value_on_the_stack(self):
        input_data = ["1 +"]
        with self.assertRaises(StackUnderflowError):
            evaluate(input_data)


class ForthSubtractionTest(unittest.TestCase):
    def test_can_subtract_two_numbers(self):
        input_data = ["3 4 -"]
        expected = [-1]
        self.assertEqual(evaluate(input_data), expected)

    def test_errors_if_there_is_nothing_on_the_stack(self):
        input_data = ["-"]
        with self.assertRaises(StackUnderflowError):
            evaluate(input_data)

    def test_errors_if_there_is_only_one_value_on_the_stack(self):
        input_data = ["1 -"]
        with self.assertRaises(StackUnderflowError):
            evaluate(input_data)


class ForthMultiplicationTest(unittest.TestCase):
    def test_can_multiply_two_numbers(self):
        input_data = ["2 4 *"]
        expected = [8]
        self.assertEqual(evaluate(input_data), expected)

    def test_errors_if_there_is_nothing_on_the_stack(self):
        input_data = ["*"]
        with self.assertRaises(StackUnderflowError):
            evaluate(input_data)

    def test_errors_if_there_is_only_one_value_on_the_stack(self):
        input_data = ["1 *"]
        with self.assertRaises(StackUnderflowError):
            evaluate(input_data)


class ForthDivisionTest(unittest.TestCase):
    def test_can_divide_two_numbers(self):
        input_data = ["12 3 /"]
        expected = [4]
        self.assertEqual(evaluate(input_data), expected)

    def test_performs_integer_division(self):
        input_data = ["8 3 /"]
        expected = [2]
        self.assertEqual(evaluate(input_data), expected)

    def test_errors_if_dividing_by_zero(self):
        input_data = ["4 0 /"]
        with self.assertRaises(ZeroDivisionError):
            evaluate(input_data)

    def test_errors_if_there_is_nothing_on_the_stack(self):
        input_data = ["/"]
        with self.assertRaises(StackUnderflowError):
            evaluate(input_data)

    def test_errors_if_there_is_only_one_value_on_the_stack(self):
        input_data = ["1 /"]
        with self.assertRaises(StackUnderflowError):
            evaluate(input_data)


class ForthCombinedArithmeticTest(unittest.TestCase):
    def test_addition_and_subtraction(self):
        input_data = ["1 2 + 4 -"]
        expected = [-1]
        self.assertEqual(evaluate(input_data), expected)

    def test_multiplication_and_division(self):
        input_data = ["2 4 * 3 /"]
        expected = [2]
        self.assertEqual(evaluate(input_data), expected)


class ForthDupTest(unittest.TestCase):
    def test_copies_the_top_value_on_the_stack(self):
        input_data = ["1 DUP"]
        expected = [1, 1]
        self.assertEqual(evaluate(input_data), expected)

    def test_is_case_insensitive(self):
        input_data = ["1 2 Dup"]
        expected = [1, 2, 2]
        self.assertEqual(evaluate(input_data), expected)

    def test_errors_if_there_is_nothing_on_the_stack(self):
        input_data = ["dup"]
        with self.assertRaises(StackUnderflowError):
            evaluate(input_data)


class ForthDropTest(unittest.TestCase):
    def test_removes_the_top_value_on_the_stack_if_it_is_the_only_one(self):
        input_data = ["1 DROP"]
        expected = []
        self.assertEqual(evaluate(input_data), expected)

    def test_removes_the_top_value_on_the_stack_if_it_not_the_only_one(self):
        input_data = ["3 4 DROP"]
        expected = [3]
        self.assertEqual(evaluate(input_data), expected)

    def test_is_case_insensitive(self):
        input_data = ["1 2 Drop"]
        expected = [1]
        self.assertEqual(evaluate(input_data), expected)

    def test_errors_if_there_is_nothing_on_the_stack(self):
        input_data = ["drop"]
        with self.assertRaises(StackUnderflowError):
            evaluate(input_data)


class ForthSwapTest(unittest.TestCase):
    def test_swaps_only_two_values_on_stack(self):
        input_data = ["1 2 SWAP"]
        expected = [2, 1]
        self.assertEqual(evaluate(input_data), expected)

    def test_swaps_two_two_values_on_stack(self):
        input_data = ["1 2 3 SWAP"]
        expected = [1, 3, 2]
        self.assertEqual(evaluate(input_data), expected)

    def test_is_case_insensitive(self):
        input_data = ["3 4 Swap"]
        expected = [4, 3]
        self.assertEqual(evaluate(input_data), expected)

    def test_errors_if_there_is_nothing_on_the_stack(self):
        input_data = ["swap"]
        with self.assertRaises(StackUnderflowError):
            evaluate(input_data)

    def test_errors_if_there_is_only_one_value_on_the_stack(self):
        input_data = ["1 swap"]
        with self.assertRaises(StackUnderflowError):
            evaluate(input_data)


class ForthOverTest(unittest.TestCase):
    def test_copies_the_second_element_if_there_are_only_two(self):
        input_data = ["1 2 OVER"]
        expected = [1, 2, 1]
        self.assertEqual(evaluate(input_data), expected)

    def test_copies_the_second_element_if_there_are_more_than_two(self):
        input_data = ["1 2 3 OVER"]
        expected = [1, 2, 3, 2]
        self.assertEqual(evaluate(input_data), expected)

    def test_is_case_insensitive(self):
        input_data = ["3 4 Over"]
        expected = [3, 4, 3]
        self.assertEqual(evaluate(input_data), expected)

    def test_errors_if_there_is_nothing_on_the_stack(self):
        input_data = ["over"]
        with self.assertRaises(StackUnderflowError):
            evaluate(input_data)

    def test_errors_if_there_is_only_one_value_on_the_stack(self):
        input_data = ["1 over"]
        with self.assertRaises(StackUnderflowError):
            evaluate(input_data)


class ForthUserDefinedWordsTest(unittest.TestCase):
    def test_can_consist_of_built_in_words(self):
        input_data = [
            ": dup-twice dup dup ;",
            "1 dup-twice"
        ]
        expected = [1, 1, 1]
        self.assertEqual(evaluate(input_data), expected)

    def test_execute_in_the_right_order(self):
        input_data = [
            ": countup 1 2 3 ;",
            "countup"
        ]
        expected = [1, 2, 3]
        self.assertEqual(evaluate(input_data), expected)

    def test_can_override_other_user_defined_words(self):
        input_data = [
            ": foo dup ;",
            ": foo dup dup ;",
            "1 foo"
        ]
        expected = [1, 1, 1]
        self.assertEqual(evaluate(input_data), expected)

    def test_can_override_built_in_words(self):
        input_data = [
            ": swap dup ;",
            "1 swap"
        ]
        expected = [1, 1]
        self.assertEqual(evaluate(input_data), expected)

    def test_can_override_built_in_operators(self):
        input_data = [
            ": + * ;",
            "3 4 +"
        ]
        expected = [12]
        self.assertEqual(evaluate(input_data), expected)

    def test_is_case_insensitive(self):
        input_data = [
            ": foo dup ;",
            "1 FOO"
        ]
        expected = [1, 1]
        self.assertEqual(evaluate(input_data), expected)

    def test_cannot_redefine_numbers(self):
        input_data = [": 1 2 ;"]
        with self.assertRaises(ValueError):
            evaluate(input_data)

    def test_errors_if_executing_a_non_existent_word(self):
        input_data = ["foo"]
        with self.assertRaises(ValueError):
            evaluate(input_data)


if __name__ == '__main__':
    unittest.main()