-
Notifications
You must be signed in to change notification settings - Fork 2
/
task10Test.py
152 lines (130 loc) · 3.88 KB
/
task10Test.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
# -*- coding: utf-8 -*-
__author__ = "Hamahmi"
"""
Please put this file in the same dir as your .g4 file after compiling it.
"""
import argparse
import os
import random
import sys
parser = argparse.ArgumentParser(description="Task 10 Test")
parser.add_argument(
"--name",
"-n",
type=str,
help="Your file name : T07_37_1234_John_Smith",
required=True,
)
parser.add_argument(
"--random",
"-r",
dest="rt",
action="store_true",
help="add -r or --random to get randomized test cases",
)
parser.add_argument(
"--size",
"-s",
type=int,
default=23,
help="if you used the random option, you can set the number of test cases",
)
parser.add_argument(
"--print",
"-p",
dest="print",
action="store_true",
help="add -p or --print to print the test cases",
)
args = parser.parse_args()
def generate_expression(size, previous_not):
if size == 1:
return str(random.randint(0, 1))
random_operator = ["+", "&"][random.randint(0, 1)]
add_par = random.randint(0, 1) == 1
add_not_first = (not previous_not) and random.randint(0, 1) == 1
add_not_second = random.randint(0, 1) == 1
first_part = (
("!" if add_not_first else "")
+ str(random.randint(0, 1))
+ random_operator
+ ("!" if add_not_second else "")
)
if add_par:
return first_part + "(" + generate_expression(size - 1, add_not_second) + ")"
else:
return first_part + generate_expression(size - 1, add_not_second)
def generate_random_expression():
return generate_expression(random.randint(1, 10), False)
def evaluate_expression(expression):
python_expression = (
expression.replace("+", " or ").replace("&", " and ").replace("!", " not ")
)
return "1" if eval(python_expression) else "0"
def generate_random_test_cases(size):
output = dict()
counter = 0
while counter < (size):
expression = generate_random_expression()
if not output.__contains__(expression):
output[expression] = evaluate_expression(expression)
counter += 1
return output
if __name__ == "__main__":
random.seed(23)
if args.rt:
test_cases = generate_random_test_cases(args.size)
else:
test_cases = {
"1 + 1 & 0": "1",
"!1 + 1 & ((0 + !1))": "0",
"0 + (1 & 0) + !(!(0 & 1) + 1)": "0",
}
if args.print:
for case, expected_output in test_cases.items():
print(case + " -> " + expected_output)
wrong = dict()
test_size = len(test_cases.keys())
i = 0
for case, expected_output in test_cases.items():
with open("temp.txt", "w") as f:
f.write(case)
output = (
os.popen("java org.antlr.v4.gui.TestRig " + args.name + " start temp.txt")
.read()
.replace(" ", "")
.replace("\n", "")
.replace("\r", "")
)
if output == "":
print("Please compile first!")
sys.exit()
ex_out = expected_output.replace(" ", "").replace("\n", "").replace("\r", "")
if output != ex_out:
wrong[case] = (ex_out, output)
i += 1
print("{:.1f}".format(i / test_size * 100.0) + " %", end="\r")
if sys.platform.startswith("win"):
os.system("del temp.txt")
else:
os.system("rm temp.txt")
print()
if wrong == {}:
print("Passed all " + str(test_size) + " test cases :)")
else:
print(
"Wrong answer in "
+ str(len(wrong.keys()))
+ " / "
+ str(test_size)
+ " :( !!"
)
for case, outputs in wrong.items():
print(
"In test case : "
+ case
+ " the output : "
+ outputs[0]
+ " was expected, but got : "
+ outputs[1]
)