-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
178 lines (148 loc) · 7.42 KB
/
tests.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
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
#!/usr/bin/env python3
"""
Unit tests for the Grammar class and for the GrammarAnalyzer class.
Tests each grammar in the "grammars" folder against a variety of strings.
"""
import unittest
from grammar import Grammar
from grammaranalyzer import GrammarAnalyzer
class TestGrammar(unittest.TestCase):
def test_nonexistent_file(self):
# Ensure no exceptions are thrown.
grammar = Grammar("nonexistent.json")
self.assertEqual(grammar.get_desc(), "")
def test_grammar_load(self):
grammar = Grammar("grammars/grammar1.json")
self.assertEqual(grammar.get_desc(), "{a^n # b^n | n > 0}")
def test_grammar_productions(self):
grammar = Grammar("grammars/grammar1.json")
# Check start variable productions.
rules = grammar.produces("S")
self.assertEqual(rules, ["aAb"])
rules = grammar.produces("A")
self.assertEqual(rules, ["aAb", "#"])
# Check nonexistent variable productions.
rules = grammar.produces("N")
self.assertFalse(rules)
def test_grammar_rules(self):
grammar = Grammar("grammars/grammar1.json")
# Check that the correct rules are returned.
rule = grammar.get_rule("S", "a")
self.assertEqual(rule, "aAb")
rule = grammar.get_rule("A", "#")
self.assertEqual(rule, "#")
# Check nonexistent input symbol.
rule = grammar.get_rule("S", "k")
self.assertFalse(rule)
# Check nonexistent variable.
rule = grammar.get_rule("N", "a")
self.assertFalse(rule)
class TestGrammarAnalyzer(unittest.TestCase):
def test_grammar1(self):
grammar = Grammar("grammars/grammar1.json")
grammar_analyzer = GrammarAnalyzer(grammar)
# Check accepted strings.
self.assertTrue(grammar_analyzer.test_string("a#b"))
self.assertTrue(grammar_analyzer.test_string("aa#bb"))
self.assertTrue(grammar_analyzer.test_string("aaa#bbb"))
self.assertTrue(grammar_analyzer.test_string("aaaa#bbbb"))
self.assertTrue(grammar_analyzer.test_string("aaaaa#bbbbb"))
self.assertTrue(grammar_analyzer.test_string("aaaaaa#bbbbbb"))
self.assertTrue(grammar_analyzer.test_string("aaaaaaa#bbbbbbb"))
self.assertTrue(grammar_analyzer.test_string("aaaaaaaa#bbbbbbbb"))
self.assertTrue(grammar_analyzer.test_string("aaaaaaaaa#bbbbbbbbb"))
self.assertTrue(grammar_analyzer.test_string("aaaaaaaaaa#bbbbbbbbbb"))
# Check rejected strings.
self.assertFalse(grammar_analyzer.test_string("xxx"))
self.assertFalse(grammar_analyzer.test_string(""))
self.assertFalse(grammar_analyzer.test_string("#"))
self.assertFalse(grammar_analyzer.test_string("a"))
self.assertFalse(grammar_analyzer.test_string("aa#b"))
self.assertFalse(grammar_analyzer.test_string("a#bb"))
self.assertFalse(grammar_analyzer.test_string("asdf"))
self.assertFalse(grammar_analyzer.test_string("aaaa#bbbbbb"))
def test_grammar2(self):
grammar = Grammar("grammars/grammar2.json")
grammar_analyzer = GrammarAnalyzer(grammar)
# Check accepted strings.
self.assertTrue(grammar_analyzer.test_string("#"))
self.assertTrue(grammar_analyzer.test_string("0#0"))
self.assertTrue(grammar_analyzer.test_string("1#1"))
self.assertTrue(grammar_analyzer.test_string("01#10"))
self.assertTrue(grammar_analyzer.test_string("10#01"))
self.assertTrue(grammar_analyzer.test_string("010#010"))
self.assertTrue(grammar_analyzer.test_string("1111#1111"))
self.assertTrue(grammar_analyzer.test_string("010001#100010"))
self.assertTrue(grammar_analyzer.test_string("0100011#1100010"))
self.assertTrue(grammar_analyzer.test_string("01000101#10100010"))
# Check rejected strings.
self.assertFalse(grammar_analyzer.test_string("xxx"))
self.assertFalse(grammar_analyzer.test_string(""))
self.assertFalse(grammar_analyzer.test_string("0"))
self.assertFalse(grammar_analyzer.test_string("0#1"))
self.assertFalse(grammar_analyzer.test_string("1#10"))
self.assertFalse(grammar_analyzer.test_string("01#01"))
self.assertFalse(grammar_analyzer.test_string("11#111"))
self.assertFalse(grammar_analyzer.test_string("111#11"))
self.assertFalse(grammar_analyzer.test_string("111#110"))
self.assertFalse(grammar_analyzer.test_string("0111#110"))
def test_grammar3(self):
grammar = Grammar("grammars/grammar3.json")
grammar_analyzer = GrammarAnalyzer(grammar)
# Check accepted strings.
self.assertTrue(grammar_analyzer.test_string("a#b#c#"))
self.assertTrue(grammar_analyzer.test_string("a#b#cc#"))
self.assertTrue(grammar_analyzer.test_string("a#b#ccc#"))
self.assertTrue(grammar_analyzer.test_string("a#b#cccc#"))
self.assertTrue(grammar_analyzer.test_string("a#b#ccccc#"))
self.assertTrue(grammar_analyzer.test_string("aa#bb#c#"))
self.assertTrue(grammar_analyzer.test_string("aa#bb#cc#"))
self.assertTrue(grammar_analyzer.test_string("aa#bb#ccc#"))
self.assertTrue(grammar_analyzer.test_string("aa#bb#cccc#"))
self.assertTrue(grammar_analyzer.test_string("aa#bb#ccccc#"))
self.assertTrue(grammar_analyzer.test_string("aaaa#bbbb#c#"))
self.assertTrue(grammar_analyzer.test_string("aaaaa#bbbbb#c#"))
self.assertTrue(grammar_analyzer.test_string("aaaaa#bbbbb#cc#"))
self.assertTrue(grammar_analyzer.test_string("aaaaa#bbbbb#ccc#"))
self.assertTrue(grammar_analyzer.test_string("aaaaa#bbbbb#cccc#"))
self.assertTrue(grammar_analyzer.test_string("aaaaa#bbbbb#ccccc#"))
# Check rejected strings.
self.assertFalse(grammar_analyzer.test_string("xxx"))
self.assertFalse(grammar_analyzer.test_string(""))
self.assertFalse(grammar_analyzer.test_string("a"))
self.assertFalse(grammar_analyzer.test_string("a#b"))
self.assertFalse(grammar_analyzer.test_string("a#b#c"))
self.assertFalse(grammar_analyzer.test_string("####"))
self.assertFalse(grammar_analyzer.test_string("abcd"))
self.assertFalse(grammar_analyzer.test_string("aaaaa#bbb#c#"))
self.assertFalse(grammar_analyzer.test_string("aaaaa##ccccc#"))
self.assertFalse(grammar_analyzer.test_string("aaaa##ccccc#"))
self.assertFalse(grammar_analyzer.test_string("aaa##ccccc#"))
def test_grammar4(self):
grammar = Grammar("grammars/grammar4.json")
grammar_analyzer = GrammarAnalyzer(grammar)
# Check accepted strings.
self.assertTrue(grammar_analyzer.test_string("a#b#c#d"))
self.assertTrue(grammar_analyzer.test_string("aa#bb#c#d"))
self.assertTrue(grammar_analyzer.test_string("a#b#cc#dd"))
self.assertTrue(grammar_analyzer.test_string("aaa#bbb#c#d"))
self.assertTrue(grammar_analyzer.test_string("a#b#ccc#ddd"))
self.assertTrue(grammar_analyzer.test_string("aaaa#bbbb#c#d"))
self.assertTrue(grammar_analyzer.test_string("a#b#cccc#dddd"))
self.assertTrue(grammar_analyzer.test_string("aa#bb#cccc#dddd"))
self.assertTrue(grammar_analyzer.test_string("aaa#bbb#cccc#dddd"))
self.assertTrue(grammar_analyzer.test_string("aaaa#bbbb#ccccc#ddddd"))
self.assertTrue(grammar_analyzer.test_string("a#b#cccccc#dddddd"))
self.assertTrue(grammar_analyzer.test_string("aaaaaaa#bbbbbbb#c#d"))
# Check rejected strings.
self.assertFalse(grammar_analyzer.test_string("xxx"))
self.assertFalse(grammar_analyzer.test_string(""))
self.assertFalse(grammar_analyzer.test_string("#"))
self.assertFalse(grammar_analyzer.test_string("a#b#c#"))
self.assertFalse(grammar_analyzer.test_string("#b#c#d"))
self.assertFalse(grammar_analyzer.test_string("a#bb#c#d"))
self.assertFalse(grammar_analyzer.test_string("a#b#c#dd"))
self.assertFalse(grammar_analyzer.test_string("a#bb#c#dd"))
self.assertFalse(grammar_analyzer.test_string("aa#bb#cc#dd#"))
self.assertFalse(grammar_analyzer.test_string("aaa#bbb#ccc#dddd"))
self.assertFalse(grammar_analyzer.test_string("aaa#bbb#ccc#dddd##"))