-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcalc_test.py
108 lines (81 loc) · 2.15 KB
/
calc_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
"""
Doctests for calc.py
Parser::
>>> parse('1.0')
1.0
>>> parse('alpha')
'alpha'
>>> parse('(+ 1 2.5)')
['+', 1, 2.5]
>>> parse('(* 4 (+ 1 2.5))')
['*', 4, ['+', 1, 2.5]]
Evaluator::
>>> evaluate(1.0)
1.0
>>> evaluate('+')
<built-in function add>
>>> evaluate(['*', 6, 7])
42
>>> evaluate(parse('(/ (* (- 100.4 32) 5) 9)'))
38.0
High-level function `calc`::
>>> calc('+')
<built-in function add>
>>> calc('(* 111 111)')
12321
Global environment::
>>> global_env['-']
<built-in function sub>
>>> calc('(define n 1729)')
>>> global_env['n']
1729
"""
from typing import Optional
from pytest import mark
from calc import parse, evaluate, calc, Expression, global_env
# tests for parse
@mark.parametrize( 'source, expected', [
('7', 7),
('x', 'x'),
('(sum 1 2 3)', ['sum', 1, 2, 3]),
('(+ (* 2 100) (* 1 10))', ['+', ['*', 2, 100], ['*', 1, 10]]),
])
def test_parse(source: str, expected: Expression) -> None:
got = parse(source)
assert got == expected
# Norvig's tests for the calculator subset of Scheme
@mark.parametrize( 'source, expected', [
("(+ 2 2)", 4),
("(+ (* 2 100) (* 1 10))", 210),
("(define x 3)", None),
("x", 3),
("(+ x x)", 6),
])
def test_evaluate(source: str, expected: Optional[Expression]) -> None:
got = evaluate(parse(source))
assert got == expected
# tests for each of the cases in evaluate
def test_evaluate_variable() -> None:
global_env['x'] = 10
source = 'x'
expected = 10
got = evaluate(parse(source))
assert got == expected
def test_evaluate_literal() -> None:
source = '3.3'
expected = 3.3
got = evaluate(parse(source))
assert got == expected
def test_define() -> None:
source = '(define answer (* 6 7))'
got = evaluate(parse(source))
assert got is None
assert global_env['answer'] == 42
def test_invocation_builtin_cos() -> None:
source = '(cos 0)'
got = evaluate(parse(source))
assert got == 1.0
def test_invocation_builtin_random() -> None:
source = '(random)'
got = evaluate(parse(source))
assert 0 <= got < 1