-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_signed.py
65 lines (53 loc) · 1.97 KB
/
test_signed.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
import pytest
from lambdas import SIGN, UNSIGN, NEG, ISPOS, ISNEG, SADD, SSUB, SMUL
from lambdas import TRUE, FALSE
from lambdas import ONE, TWO, THREE, FOUR
from lambdas import CONS, CAR, CDR
from lambdas import decode_number
@pytest.mark.parametrize('given, expected', [
(THREE, 3),
(FOUR, 4),
])
def test_sign_unsign(given, expected):
assert decode_number(UNSIGN(SIGN(given))) == expected
def test_sign_checks():
s = SIGN(TWO)
assert ISPOS(s) is TRUE
assert ISNEG(s) is FALSE
n = NEG(s)
assert ISPOS(n) is FALSE
assert ISNEG(n) is TRUE
@pytest.mark.parametrize('lsign, left, rsign, right, expsign, expvalue', [
(TRUE, ONE, TRUE, ONE, TRUE, 2), # 1 + 1 = 2
(TRUE, ONE, FALSE, TWO, FALSE, 1), # 1 - 2 = -1
(FALSE, TWO, TRUE, ONE, FALSE, 1), # -2 + 1 = -1
(FALSE, TWO, FALSE, ONE, FALSE, 3), # -2 - 1 = -3
])
def test_sadd(lsign, left, rsign, right, expsign, expvalue):
lv = CONS(lsign)(left)
rv = CONS(rsign)(right)
res = SADD(lv)(rv)
assert CAR(res) is expsign
assert decode_number(CDR(res)) == expvalue
@pytest.mark.parametrize('lsign, left, rsign, right, expsign, expvalue', [
(TRUE, TWO, TRUE, ONE, TRUE, 1), # 2 - 1 = 1
(TRUE, TWO, TRUE, THREE, FALSE, 1), # 2 - 3 = -1
])
def test_ssub(lsign, left, rsign, right, expsign, expvalue):
lv = CONS(lsign)(left)
rv = CONS(rsign)(right)
res = SSUB(lv)(rv)
assert CAR(res) is expsign
assert decode_number(CDR(res)) == expvalue
@pytest.mark.parametrize('lsign, left, rsign, right, expsign, expvalue', [
(TRUE, TWO, TRUE, THREE, TRUE, 6), # 2 * 3 = 6
(TRUE, TWO, FALSE, THREE, FALSE, 6), # 2 * (-3) = -6
(FALSE, TWO, TRUE, THREE, FALSE, 6), # -2 * 3 = -6
(FALSE, TWO, FALSE, THREE, TRUE, 6), # -2 * (-3) = 6
])
def test_smul(lsign, left, rsign, right, expsign, expvalue):
lv = CONS(lsign)(left)
rv = CONS(rsign)(right)
res = SMUL(lv)(rv)
assert CAR(res) is expsign
assert decode_number(CDR(res)) == expvalue