-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
98 lines (85 loc) · 2.8 KB
/
run.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
import sys
import subprocess
import inputs
import random
N = 1000
M = 20
COMP = "gcc -Wall -Wextra -Werror -g3 -fsanitize=address "
tests = []
if (len(sys.argv) != 2):
print("Wrong usage: sh run.sh <path-of-ftprintf>")
exit()
PATH = sys.argv[1]
if (PATH[-1] != "/"):
PATH += "/"
def compile_lib():
result = subprocess.run("make -C {}".format(PATH), shell=True)
def function_content():
format = ""
args = []
for _ in range(random.randint(0, 10)):
if (random.random() < .6):
format += inputs.random_string(random.randint(1, 5))
else:
temp = inputs.random_arg()
format += temp[0]
for arg in temp[1]:
args.append(arg)
res = "\"{}\"".format(format)
for arg in args:
if (arg != ""):
res += ", " + arg
return res
def generate_test():
content = function_content()
res1 = "\tft_printf(\"-- %d --\\n\", ft_printf({}));\n".format(content)
res2 = "\tprintf(\"-- %d --\\n\", printf({}));\n".format(content)
tests.append(content)
return (res1, res2)
def generate_tests():
res1 = ""
res2 = ""
for _ in range(N):
test = generate_test()
res1 += test[0]
res2 += test[1]
return (res1, res2)
def generate_mains():
content = ""
with open("main_template.c", "r") as file:
content = file.read()
tests = generate_tests()
content_ftprintf = content.replace("$$$1", tests[0])
content_printf = content.replace("$$$1", tests[1])
with open("main_ftprintf.c", "w+") as file:
file.write(content_ftprintf)
with open("main_printf.c", "w+") as file:
file.write(content_printf)
def launch_tests():
result1 = subprocess.run("{} -L{} -lftprintf -o tests_ftprintf main_ftprintf.c".format(COMP, PATH[:-1]), shell=True)
result2 = subprocess.run("{} -L{} -lftprintf -o tests_printf main_printf.c".format(COMP, PATH[:-1]), shell=True)
result3 = subprocess.run("./tests_ftprintf > ftprintf_output", shell=True)
result3 = subprocess.run("./tests_printf > printf_output", shell=True)
def diff():
ok = True
print("Comparing outputs:")
print()
ftprintf_output = open("ftprintf_output", "r").readlines()
printf_output = open("printf_output", "r").readlines()
for i in range(len(tests)):
if (ftprintf_output[i] != printf_output[i]):
ok = False
print("Diff for printf({});".format(tests[i]))
print(" printf: |{}|".format(printf_output[i][:-1]))
print("ftprintf: |{}|".format(ftprintf_output[i][:-1]))
print()
if (ok):
print("No differences, well done !")
compile_lib()
for i in range(M):
print()
print("Generating mains of {} tests ({}/{})".format(N, i + 1, M))
tests = []
generate_mains()
launch_tests()
diff()