/
test.py
63 lines (46 loc) · 1.52 KB
/
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
#!/usr/bin/env python3
import pytest
from itertools import product
from cfg import CFG
def test_old_behavior():
g = CFG(
{'S'},
{'a', 'b', 'c', 'λ'},
{('S', 'aSa'),
('S', 'bSb'),
('S', 'cSc'),
('S', 'λ')},
'S',
'λ'
)
observed = {x for x in map(''.join, product(*['abc'] * 4)) if g.cyk(x)}
expected = {'aaaa', 'abba', 'acca', 'baab', 'bbbb', 'bccb', 'caac', 'cbbc', 'cccc'}
assert observed == expected
def test_with_dict():
g = CFG(
{'S'},
{'a', 'b', 'c', 'λ'},
{'S': ['aSa', 'bSb', 'cSc', 'λ']},
'S',
'λ'
)
observed = {x for x in map(''.join, product(*['abc'] * 4)) if g.cyk(x)}
expected = {'aaaa', 'abba', 'acca', 'baab', 'bbbb', 'bccb', 'caac', 'cbbc', 'cccc'}
assert observed == expected
def test_without_start_variable_and_null_character():
g = CFG(
{'S'},
{'a', 'b', 'c', 'λ'},
{'S': ['aSa', 'bSb', 'cSc', 'λ']}
)
observed = {x for x in map(''.join, product(*['abc'] * 4)) if g.cyk(x)}
expected = {'aaaa', 'abba', 'acca', 'baab', 'bbbb', 'bccb', 'caac', 'cbbc', 'cccc'}
assert observed == expected
def test_without_variables():
g = CFG(
terminals={'a', 'b', 'c', 'λ'},
rules={'S': ['aSa', 'bSb', 'cSc', 'λ']}
)
observed = {x for x in map(''.join, product(*['abc'] * 4)) if g.cyk(x)}
expected = {'aaaa', 'abba', 'acca', 'baab', 'bbbb', 'bccb', 'caac', 'cbbc', 'cccc'}
assert observed == expected