blob: 23bef2cf031b660702c5d9795312fb886ea570fd [file] [log] [blame]
Tudor Timi2c270442019-12-23 19:14:29 +01001import os
Tudor Timi81be42d2019-12-23 19:17:57 +01002import pathlib
Tudor Timi8788ae42019-12-27 17:32:06 +01003import shutil
Tudor Timi2c270442019-12-23 19:14:29 +01004import subprocess
5
6
Tudor Timi81be42d2019-12-23 19:17:57 +01007def clean_paths(rm_paths):
8 for rm_path in rm_paths:
9 for p in pathlib.Path('.').glob(rm_path):
10 p.unlink()
11
12
Tudor Timi2c270442019-12-23 19:14:29 +010013def create_unit_test(name):
14 subprocess.check_call(['create_unit_test.pl', name])
15
16
Tudor Timi8788ae42019-12-27 17:32:06 +010017def get_simulators():
18 result = []
19
20 if shutil.which('irun'):
21 result.append('irun')
22 if shutil.which('vcs'):
23 result.append('vcs')
24 if shutil.which('vlog'):
25 result.append('modelsim')
26
27 assert result, 'None of irun, modelsim or vcs are in your path. You need at least 1 simulator to regress svunit-code!'
28
29 return result
30
31
32def get_svunit_root():
33 return os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
34
35
Tudor Timi2c270442019-12-23 19:14:29 +010036def golden_class_unit_test(FILE, MYNAME):
37 template = open('{}/test/templates/class_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
38 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
39 for line in template:
40 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
41
Tudor Timi63b4c812019-12-25 16:34:31 +010042def golden_module_unit_test(FILE, MYNAME):
43 template = open('{}/test/templates/module_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
44 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
45 for line in template:
46 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
47
Tudor Timi4b574fc2019-12-25 16:38:01 +010048def golden_if_unit_test(FILE, MYNAME):
49 template = open('{}/test/templates/if_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
50 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
51 for line in template:
52 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
53
Tudor Timi2c270442019-12-23 19:14:29 +010054def golden_testsuite_with_1_unittest(MYNAME):
55 template = open('{}/test/templates/testsuite_with_1_unittest.gold'.format(os.environ['SVUNIT_INSTALL']))
56 with open('testsuite.gold', 'w') as output:
57 for line in template:
58 output.write(line.replace('MYNAME', MYNAME))
59
Tudor Timi1544f402020-01-02 18:39:20 +010060def golden_testsuite_with_2_unittests(MYNAME1, MYNAME2):
61 template = open('{}/test/templates/testsuite_with_2_unittest.gold'.format(os.environ['SVUNIT_INSTALL']))
62 with open('testsuite.gold', 'w') as output:
63 for line in template:
64 output.write(line.replace('MYNAME1', MYNAME1).replace('MYNAME2', MYNAME2))
65
Tudor Timi2c270442019-12-23 19:14:29 +010066def golden_testrunner_with_1_testsuite():
67 template = open('{}/test/templates/testrunner_with_1_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
68 with open('testrunner.gold', 'w') as output:
69 for line in template:
70 output.write(line)
71
Tudor Timi8f95e9b2019-12-24 14:42:04 +010072def golden_testrunner_with_2_testsuites():
73 template = open('{}/test/templates/testrunner_with_2_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
74 with open('testrunner.gold', 'w') as output:
75 for line in template:
76 output.write(line)
77
Tudor Timibd0006b2019-12-25 16:22:04 +010078def golden_testrunner_with_3_testsuites():
79 template = open('{}/test/templates/testrunner_with_3_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
80 with open('testrunner.gold', 'w') as output:
81 for line in template:
82 output.write(line)
83
Tudor Timi02cc3c12019-12-25 11:15:45 +010084def golden_testrunner_with_4_testsuites():
85 template = open('{}/test/templates/testrunner_with_4_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
86 with open('testrunner.gold', 'w') as output:
87 for line in template:
88 output.write(line)
89
Tudor Timi2c270442019-12-23 19:14:29 +010090
91def verify_file(file0, file1):
92 result = subprocess.run(['diff', '-wbB', file0, file1], stdout=subprocess.PIPE)
93 assert result.returncode in [0, 1]
94 if result.returncode == 1:
95 assert result.stdout == b''
96
Tudor Timi4c2bea72020-01-02 18:30:10 +010097def verify_testsuite(testsuite, dir=''):
Tudor Timi2c270442019-12-23 19:14:29 +010098 PWD = '_'
99 file = open(testsuite)
100 with open('.{}'.format(testsuite), 'w') as output:
101 for line in file:
102 output.write(line.replace('PWD', "{}{}".format(PWD, dir)))
103 verify_file(output.name, '.{}{}_testsuite.sv'.format(PWD, dir))
104
105def verify_testrunner(testrunner, ts0, ts1='', ts2='', ts3='', tr=''):
106 if tr == '':
107 tr = '.testrunner.sv'
108 file = open(testrunner)
109 with open('.{}'.format(testrunner), 'w') as output:
110 for line in file:
111 output.write(line.replace('TS0', ts0).replace('TS1', ts1).replace('TS2', ts2).replace('TS3', ts3))
112 verify_file(output.name, tr)