This repository was archived by the owner on Jul 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_fift.py
More file actions
339 lines (245 loc) · 8.97 KB
/
test_fift.py
File metadata and controls
339 lines (245 loc) · 8.97 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from fift.fift import *
def test_create_empty_script():
@script()
def main():
pass
assert main() == ''
class TestStackManipulationWords:
def test_dup(self):
assert str(dup()) == 'dup'
assert str(dup2()) == '2dup'
assert str(dupnz()) == '?dup'
def test_drop(self):
assert str(drop()) == 'drop'
assert str(drop2()) == '2drop'
def test_swap(self):
assert str(swap()) == 'swap'
assert str(swap2()) == '2swap'
def test_rot(self):
assert str(rot()) == 'rot'
assert str(nrot()) == '-rot'
def test_over(self):
assert str(over()) == 'over'
def test_tuck(self):
assert str(tuck()) == 'tuck'
def test_nip(self):
assert str(nip()) == 'nip'
def test_pick(self):
assert str(pick(0)) == '0 pick'
def test_roll(self):
assert str(roll(1)) == '1 roll'
assert str(nroll(1)) == '1 -roll'
def test_exch(self):
assert str(exch(1)) == '1 exch'
assert str(exch2(1, 3)) == '1 3 exch2'
class TestString:
def test_create(self):
assert str(string('abc')) == '"abc"'
def test_print(self):
assert str(string('abc').print()) == '."abc"'
def test_cr(self):
assert str(string('abc').print(cr=True)) == '."abc" cr'
def test_concatenate(self):
assert str(string('a', 'b', 'c', 1, 2)) == '"a" "b" $+ "c" $+ 1 (.) $+ 2 (.) $+'
def test_multi_print(self):
assert str(string('a', 'b', 'c', 1).print()) == '."a" ."b" ."c" 1 (.) type'
def test_convert_number_to_str(self):
assert str(string(1)) == '1 (.)'
def test_usage(self):
@script()
def main():
string('abc')
string('a', 'b', 1)
string('abc').print()
string('abc').print(cr=True)
assert main() == '"abc"\n' \
'"a" "b" $+ 1 (.) $+\n' \
'."abc"\n' \
'."abc" cr'
class TestInclude:
def test_simple(self):
assert str(include('Asm.fif')) == '"Asm.fif" include'
def test_as_const(self):
assert str(include('Asm.fif').const('asm')) == '"Asm.fif" include constant asm'
class TestConstants:
def test_def_number(self):
assert str(const('a', 1)) == '1 constant a'
def test_def_number2(self):
assert str(const('b', '2')) == '2 constant b'
def test_def_string(self):
assert str(const('c', string('abc'))) == '"abc" constant c'
def test_def_dict(self):
assert isinstance(const('d', {}), Dict)
assert str(const('d', {})) == 'dictnew constant d'
def test_add_to_dict(self):
@script()
def main():
a = const('a', {})
a.add(1, (4, 'u'), builder())
a[2] = ((4, 'u'), builder())
# generate an error if a key is duplicated
a[2] = ((4, 'u'), builder(), 'Cannot be added')
assert main() == 'dictnew constant a\n' \
'<b b> <s 1 @\' a 4 udict! =: a\n' \
'<b b> <s 2 @\' a 4 udict! =: a\n' \
'<b b> <s 2 @\' a 4 udict!+ not abort"Cannot be added" =: a'
def test_read(self):
assert const('a', 1).read() == '@\' a'
def test_read_predefined(self):
assert str(const('$0')) == '@\' $0'
def test_type(self):
assert str(const('a', 1).type()) == '@\' a type'
def test_usage(self):
@script()
def main():
a = const('a', 1)
b = const('b', 2)
c = const('c', {})
d = const('$0')
assert main() == '1 constant a\n' \
'2 constant b\n' \
'dictnew constant c\n' \
'@\' $0'
class TestAssign:
def test_simple(self):
assert str(assign('a', 1)) == '1 =: a'
def test_double(self):
assert str(assign2('a', 1, 2)) == '1 2 2=: a'
class TestBlock:
def test_empty_block(self):
assert str(block()) == '{ }'
class TestWord:
def test_empty_word(self):
assert str(word('test')) == '{ } : test'
def test_word_with_args(self):
assert str(word('square', dup(), '*')) == '{ dup * } : square'
def test_call_word(self):
@script()
def main():
square = word('square', dup(), '*')
square(2)
assert main() == '{ dup * } : square\n' \
'2 square'
def test_usage(self):
@script()
def main():
square = word('square', dup(), '*')
power5 = word('**5', square(square(dup())), '*')
power5(3)
assert main() == '{ dup * } : square\n' \
'{ dup square square * } : **5\n' \
'3 **5'
class TestBuilderPrimitive:
def test_empty(self):
assert str(builder()) == '<b b>'
def test_unsigned(self):
assert str(builder().u(0, 32)) == '<b 0 32 u, b>'
def test_signed(self):
assert str(builder().i(2, 4)) == '<b 2 4 i, b>'
def test_bytes(self):
assert str(builder().b(swap())) == '<b swap B, b>'
def test_bits(self):
assert str(builder().s('b{1001}')) == '<b b{1001} s, b>'
assert str(builder().s('x{4A}')) == '<b x{4A} s, b>'
def test_ref(self):
assert str(builder().r(swap())) == '<b swap ref, b>'
def test_dict(self):
assert str(builder().d()) == '<b null dict, b>'
def test_inspect_empty(self):
assert str(builder().inspect()) == '<b .s b>'
def test_inspect_full(self):
assert str(builder().u(3, 16).inspect()) == '<b 3 16 u, .s b>'
def test_usage(self):
@script()
def main():
d = const('d', {})
builder().d(d)
assert main() == 'dictnew constant d\n' \
'<b @\' d dict, b>'
class TestSlice:
def test_create(self):
assert str(slice()) == '<s '
def test_u(self):
assert str(slice().u(7)) == '<s 7 u@+'
def test_s(self):
assert str(slice().s(32)) == '<s 32 s@+'
def test_b(self):
assert str(slice().b(256)) == '<s 256 B@+'
def test_r(self):
assert str(slice().r()) == '<s ref@+'
def test_d(self):
assert str(slice().d()) == '<s dict@+'
def test_multiple(self):
assert str(slice().u(1).s(16).b(256).r().d()) == '<s 1 u@+ 16 s@+ 256 B@+ ref@+ dict@+'
def test_silent(self):
assert str(slice(silent=True).u(1).s(16)) == '<s 1 u@?+ 16 s@?+'
def test_save_to_const(self):
@script()
def main():
a = const('a', 'null')
b = const('b', 'null')
slice().u(7, s2c=a).s(32, s2c=b)
assert main() == 'null constant a\n' \
'null constant b\n' \
'<s 7 u@+ =: a 32 s@+ =: b'
class TestFile:
def test_read(self):
assert str(file('test.bin').read()) == '"test.bin" file>B'
def test_write(self):
assert str(file('test.bin').write()) == '"test.bin" B>file'
def test_filename_from_const(self):
@script()
def main():
fn = const('fn', String('test.bin'))
file(fn).read()
assert main() == '"test.bin" constant fn\n' \
'@\' fn file>B'
def test_read_file_and_save_to_const(self):
@script()
def main():
data = file('test.bin').read()
const('fd', data)
assert main() == '"test.bin" file>B constant fd'
def test_deserialize(self):
@script()
def main():
file('test.bin').deserialize().u(7).s(2).b(256).r()
assert main() == '"test.bin" file>B B>boc\n' \
'<s 7 u@+ 2 s@+ 256 B@+ ref@+'
def test_deserialize_to_constants(self):
@script()
def main():
c = const('c', 'null')
d = const('d', 'null')
(file('test.bin').deserialize()
.u(7)
.s(2)
.b(256, s2c=c)
.r(s2c=d))
assert main() == 'null constant c\n' \
'null constant d\n' \
'"test.bin" file>B B>boc\n' \
'<s 7 u@+ 2 s@+ 256 B@+ =: c ref@+ =: d'
class TestIsDefined:
def test_is_def(self):
assert str(is_def('$1')) == 'def? $1'
class TestIf:
def test_if(self):
assert str(is_def('$1')) == 'def? $1'
class TestCond:
def test_empty(self):
assert str(cond()) == '{ } { } cond'
def test_simple(self):
assert str(cond(
1,
(String('true').print(),),
(String('false').print(),))) == '1 { ."true" } { ."false" } cond'
def test_usage(self):
@script()
def main():
check = word('?', (cond()
.pos(String('true').print())
.neg(String('false').print())))
check(2, 3, '<')
assert main() == '{ { ."true" } { ."false" } cond } : ?\n' \
'2 3 < ?'