-
Notifications
You must be signed in to change notification settings - Fork 11
/
tcgen.py
196 lines (146 loc) · 4.28 KB
/
tcgen.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from random import randint, choice, sample
import string
import time
import re
values = {}
def get_value(s_val):
numptn = re.compile(r'^-?([0-9]+)$')
varptn = re.compile(r'^([a-zA-Z]+)$')
arrptn = re.compile(r'^([a-zA-Z]+)([0-9]+)$')
if numptn.match(s_val):
return int(s_val)
elif varptn.match(s_val):
return values[s_val]
elif arrptn.match(s_val):
ptnsch = re.search(arrptn, s_val)
return values[ptnsch.group(1)][int(ptnsch.group(2))]
def generate_int(line):
minv = get_value(line[3])
maxv = get_value(line[4])
randn = randint(minv, maxv)
values[line[2]] = randn
return str(randn)
def generate_drarray(line):
output = ""
ssize = line[1].split('_')[1]
size = get_value(ssize)
minv = get_value(line[3])
maxv = get_value(line[4])
rns = sample(range(minv, maxv+1), size)
values[line[2]] = rns
for val in rns:
output += str(val) + " "
return output
def generate_rarray(line):
output = ""
ssize = line[1].split('_')[1]
size = get_value(ssize)
minv = get_value(line[3])
maxv = get_value(line[4])
rns = [randint(minv, maxv) for _ in range(size)]
values[line[2]] = rns
for val in rns:
output += str(val) + " "
return output
def generate_rlstring(line):
output = ""
minlen = line[1].split('_')[1]
maxlen = line[1].split('_')[2]
minlen = get_value(minlen)
maxlen = get_value(maxlen)
rnlen = randint(minlen, maxlen)
letters = ""
if line[3] == "?":
letters += line[4].strip()
else:
if "u" in line[3]:
letters += string.ascii_uppercase
if "l" in line[3]:
letters += string.ascii_lowercase
if "d" in line[3]:
letters += string.digits
for _ in range(rnlen):
output += choice(letters)
return output
def generate_flstring(line):
output = ""
flen = line[1].split('_')[1]
flen = get_value(flen)
letters = ""
if line[3] == "?":
letters += line[4].strip()
else:
if "u" in line[3]:
letters += string.ascii_uppercase
if "l" in line[3]:
letters += string.ascii_lowercase
if "d" in line[3]:
letters += string.digits
for _ in range(flen):
output += choice(letters)
return output
def generate_loop(i, synlist):
output = ""
line = synlist[i]
ittrs = line[3]
ittrs = get_value(ittrs)
nll = 0
for ln in range(i+1, len(synlist)):
nspaces = synlist[ln][0] - line[0]
if nspaces > 0 and nspaces % 4 == 0:
nll += 1
else:
break
for _ in range(ittrs-1):
output += generate_tc(synlist[i+1:i+1+nll])
return output
def generate_tc(synlist):
output = ""
i = 0
while True:
if i >= len(synlist):
break
line = synlist[i]
if line[1] == 'int':
output += generate_int(line)
elif "drarray" in line[1]:
output += generate_drarray(line)
elif "rarray" in line[1]:
output += generate_rarray(line)
elif "rlstring" in line[1]:
output += generate_rlstring(line)
elif "flstring" in line[1]:
output += generate_flstring(line)
elif "loop" in line[1]:
output += generate_loop(i, synlist)
if output and output[-1] != '\n':
if line[-1] == ';':
output += "\n"
elif output[-1] != " ":
output += " "
i += 1
return output
def get_tokens(line):
synline = line.strip().split()
if not synline:
return None
indentspace = len(line) - len(line.lstrip())
tokens = [indentspace]
tokens.extend(synline)
if tokens[-1][-1] == ';':
tokens[-1] = tokens[-1][:-1]
tokens.append(';')
return tokens
def get_tcs(tc_syntax, tc_output):
with open(tc_syntax, 'r') as syntax_file:
synlist = []
for line in syntax_file.readlines():
tkns = get_tokens(line)
if tkns:
synlist.append(tkns)
start_time = time.time()
tc_out = generate_tc(synlist)
end_time = time.time()
with open(tc_output, 'w') as out_file:
out_file.write(tc_out)
return end_time - start_time