-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_op_stack.py
84 lines (78 loc) · 2.38 KB
/
test_op_stack.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
"""Tests of stack operators for stilted."""
import pytest
from error import StiltedError
from evaluate import evaluate
from dtypes import MARK
from test_helpers import compare_stacks
@pytest.mark.parametrize(
"text, stack",
[
# clear
("1 2 3 clear", []),
# cleartomark
("1 [ 2 3 cleartomark", [1]),
# copy
("(a) (b) (c) 2 copy", ["a", "b", "c", "b", "c"]),
("(a) (b) (c) 0 copy", ["a", "b", "c"]),
# count
("count", [0]),
("1 2 3 count", [1, 2, 3, 3]),
# counttomark
("mark 1 2 3 counttomark", [MARK, 1, 2, 3, 3]),
("mark counttomark", [MARK, 0]),
# dup
("1 123 dup", [1, 123, 123]),
# exch
("1 123 456 exch", [1, 456, 123]),
# index
("(a) (b) (c) (d) 0 index", ["a", "b", "c", "d", "d"]),
("(a) (b) (c) (d) 3 index", ["a", "b", "c", "d", "a"]),
# mark
("1 mark", [1, MARK]),
("1 [", [1, MARK]),
# pop
("1 123 pop", [1]),
# roll
("(a) (b) (c) 3 -1 roll", ["b", "c", "a"]),
("(a) (b) (c) 3 1 roll", ["c", "a", "b"]),
("(a) (b) (c) 3 0 roll", ["a", "b", "c"]),
],
)
def test_evaluate(text, stack):
compare_stacks(evaluate(text).ostack, stack)
@pytest.mark.parametrize(
"text, error",
[
# cleartomark
("1 2 3 cleartomark", "unmatchedmark"),
# copy
("copy", "stackunderflow"),
("3.5 3.5 copy", "typecheck"),
("(a) (b) 3 copy", "stackunderflow"),
("(a) (b) -1 copy", "rangecheck"),
# counttomark
("1 2 3 counttomark", "unmatchedmark"),
# dup
("dup", "stackunderflow"),
# exch
("exch", "stackunderflow"),
("1 exch", "stackunderflow"),
# index
("index", "stackunderflow"),
("(a) index", "typecheck"),
("(a) (b) (c) (d) 4 index", "stackunderflow"),
("(a) (b) (c) (d) -1 index", "rangecheck"),
# pop
("pop", "stackunderflow"),
# roll
("1 2 3 4 1 roll", "stackunderflow"),
("roll", "stackunderflow"),
("1 0 roll", "stackunderflow"),
("1 2 3 (a) 1 roll", "typecheck"),
("1 2 3 1 (a) roll", "typecheck"),
("1 2 3 10 1 roll", "stackunderflow"),
],
)
def test_evaluate_error(text, error):
with pytest.raises(StiltedError, match=error):
evaluate(text)