-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·65 lines (55 loc) · 1.54 KB
/
test.py
File metadata and controls
executable file
·65 lines (55 loc) · 1.54 KB
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
#!/usr/bin/env python3
# Test script for CFG2CNF
import subprocess
import sys
def test_grammar(grammar, description):
print(f"\n===== Testing {description} =====")
print("Input Grammar:")
print("\n".join(grammar))
print("\nOutput:")
# Run the main.py script with the grammar as input
process = subprocess.run(
["python3", "main.py"],
input="\n".join(grammar) + "\n*\n",
text=True,
capture_output=True
)
# Print the output
print(process.stdout)
if process.returncode != 0:
print(f"Error (return code {process.returncode}):")
print(process.stderr)
return False
return True
def main():
# Test cases
test_cases = [
(
["S -> SaB | aB", "B -> bB | $"],
"Basic grammar from README"
),
(
["S -> AB | BC", "A -> a | aA", "B -> b", "C -> cC | c"],
"Grammar with multiple rules"
),
(
["S -> aSb | $"],
"Grammar with epsilon"
),
(
["S -> A | B", "A -> a", "B -> b"],
"Grammar with unit productions"
),
(
["S -> aBc", "B -> bB | $"],
"Grammar with mixed terminals and non-terminals"
)
]
# Run the tests
success = 0
for grammar, description in test_cases:
if test_grammar(grammar, description):
success += 1
print(f"\nTests: {success}/{len(test_cases)} successful")
if __name__ == "__main__":
main()