-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
executable file
·68 lines (60 loc) · 2.23 KB
/
test.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
#!/usr/bin/env python
import os
import logging
import subprocess
from colorama import init, Fore, Style
INTERPRETER = './interpreter.native'
TEST_DIR = 'tests/'
TESTS = {
'00_print.vsl': { 'in': '', 'out': 'Hello 17' },
'01_op.vsl': { 'in': '', 'out': '5' },
'02_var.vsl': { 'in': '', 'out': '17' },
'03_array.vsl': { 'in': '', 'out': '17,42' },
'04_read.vsl': { 'in': '17\n42', 'out': '18,43' },
'05_if.vsl': { 'in': '', 'out': '1,0,0,0' },
'06_while.vsl': { 'in': '', 'out': '3,2,1,0' },
'07_variance.vsl': {
'in': '5\n1\n0\n-2\n42\n7\n9\n5\n17\n0',
'out': 't[0]? t[1]? t[2]? t[3]? t[4]? t[5]? t[6]? t[7]? t[8]? t[9]? 84,8,153',
},
'08_sort.vsl': {
'in': '5\n1\n0\n-2\n42\n7\n9\n5\n17\n0',
'out': 't[0]? t[1]? t[2]? t[3]? t[4]? t[5]? t[6]? t[7]? t[8]? t[9]? -2,0,0,1,5,5,7,9,17,42',
},
'10_block.vsl': { 'in': '', 'out': '2,1,3,0,1,3' },
'11_intfun.vsl': { 'in': '', 'out': '8,625' },
'12_voidfun.vsl': { 'in': '', 'out': '2,2' },
'13_return.vsl': { 'in': '', 'out': '2' },
}
def red(msg):
return '{}{}{}{}{}'.format(Fore.RED, Style.BRIGHT, msg, Style.NORMAL, Fore.RESET)
def green(msg):
return '{}{}{}{}{}'.format(Fore.GREEN, Style.BRIGHT, msg, Style.NORMAL, Fore.RESET)
def test(interpreter, filename, input, expected_out):
p = subprocess.Popen([interpreter, filename], stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(o, e) = p.communicate(input=input.encode())
out = o.decode()
err = e.decode()
if err != '':
print('{}: {}'.format(red('ERROR'), filename))
print(red('>>> Got:'))
print(err)
return
if out == expected_out:
print('{}: {}'.format(green('PASS'), filename))
else:
print('{}: {}'.format(red('FAIL'), filename))
print(green('>>> Expected:'))
print(expected_out)
print(red('>>> Got:'))
print(out)
if __name__ == '__main__':
init()
for filename in sorted(os.listdir(TEST_DIR)):
try:
t = TESTS[filename]
except KeyError:
print('Skipped: {}'.format(filename))
else:
test(INTERPRETER, os.path.join(TEST_DIR, filename), t['in'], t['out'])