blob: 8b4e5faae5c0606bc0c5a773879fdce9c9566987 [file] [log] [blame]
Tudor Timi7b8793f2020-01-02 19:44:10 +01001import mmap
Tudor Timi2c270442019-12-23 19:14:29 +01002import os
Tudor Timi81be42d2019-12-23 19:17:57 +01003import pathlib
Tudor Timi7b8793f2020-01-02 19:44:10 +01004import re
Tudor Timi8788ae42019-12-27 17:32:06 +01005import shutil
Tudor Timi2c270442019-12-23 19:14:29 +01006import subprocess
7
8
Tudor Timi81be42d2019-12-23 19:17:57 +01009def clean_paths(rm_paths):
10 for rm_path in rm_paths:
11 for p in pathlib.Path('.').glob(rm_path):
12 p.unlink()
13
14
Tudor Timi2c270442019-12-23 19:14:29 +010015def create_unit_test(name):
16 subprocess.check_call(['create_unit_test.pl', name])
17
18
Tudor Timi8788ae42019-12-27 17:32:06 +010019def get_simulators():
20 result = []
21
22 if shutil.which('irun'):
23 result.append('irun')
24 if shutil.which('vcs'):
25 result.append('vcs')
26 if shutil.which('vlog'):
27 result.append('modelsim')
28
29 assert result, 'None of irun, modelsim or vcs are in your path. You need at least 1 simulator to regress svunit-code!'
30
31 return result
32
33
34def get_svunit_root():
35 return os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
36
37
Tudor Timi2c270442019-12-23 19:14:29 +010038def golden_class_unit_test(FILE, MYNAME):
39 template = open('{}/test/templates/class_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
40 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
41 for line in template:
42 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
43
Tudor Timi63b4c812019-12-25 16:34:31 +010044def golden_module_unit_test(FILE, MYNAME):
45 template = open('{}/test/templates/module_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
46 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
47 for line in template:
48 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
49
Tudor Timi4b574fc2019-12-25 16:38:01 +010050def golden_if_unit_test(FILE, MYNAME):
51 template = open('{}/test/templates/if_unit_test.gold'.format(os.environ['SVUNIT_INSTALL']))
52 with open('{}_unit_test.gold'.format(FILE), 'w') as output:
53 for line in template:
54 output.write(line.replace('FILE', FILE).replace('MYNAME', MYNAME))
55
Tudor Timi2c270442019-12-23 19:14:29 +010056def golden_testsuite_with_1_unittest(MYNAME):
57 template = open('{}/test/templates/testsuite_with_1_unittest.gold'.format(os.environ['SVUNIT_INSTALL']))
58 with open('testsuite.gold', 'w') as output:
59 for line in template:
60 output.write(line.replace('MYNAME', MYNAME))
61
Tudor Timi1544f402020-01-02 18:39:20 +010062def golden_testsuite_with_2_unittests(MYNAME1, MYNAME2):
63 template = open('{}/test/templates/testsuite_with_2_unittest.gold'.format(os.environ['SVUNIT_INSTALL']))
64 with open('testsuite.gold', 'w') as output:
65 for line in template:
66 output.write(line.replace('MYNAME1', MYNAME1).replace('MYNAME2', MYNAME2))
67
Tudor Timi2c270442019-12-23 19:14:29 +010068def golden_testrunner_with_1_testsuite():
69 template = open('{}/test/templates/testrunner_with_1_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
70 with open('testrunner.gold', 'w') as output:
71 for line in template:
72 output.write(line)
73
Tudor Timi8f95e9b2019-12-24 14:42:04 +010074def golden_testrunner_with_2_testsuites():
75 template = open('{}/test/templates/testrunner_with_2_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
76 with open('testrunner.gold', 'w') as output:
77 for line in template:
78 output.write(line)
79
Tudor Timibd0006b2019-12-25 16:22:04 +010080def golden_testrunner_with_3_testsuites():
81 template = open('{}/test/templates/testrunner_with_3_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
82 with open('testrunner.gold', 'w') as output:
83 for line in template:
84 output.write(line)
85
Tudor Timi02cc3c12019-12-25 11:15:45 +010086def golden_testrunner_with_4_testsuites():
87 template = open('{}/test/templates/testrunner_with_4_testsuite.gold'.format(os.environ['SVUNIT_INSTALL']))
88 with open('testrunner.gold', 'w') as output:
89 for line in template:
90 output.write(line)
91
Tudor Timi2c270442019-12-23 19:14:29 +010092
93def verify_file(file0, file1):
94 result = subprocess.run(['diff', '-wbB', file0, file1], stdout=subprocess.PIPE)
95 assert result.returncode in [0, 1]
96 if result.returncode == 1:
97 assert result.stdout == b''
98
Tudor Timi4c2bea72020-01-02 18:30:10 +010099def verify_testsuite(testsuite, dir=''):
Tudor Timi2c270442019-12-23 19:14:29 +0100100 PWD = '_'
101 file = open(testsuite)
102 with open('.{}'.format(testsuite), 'w') as output:
103 for line in file:
104 output.write(line.replace('PWD', "{}{}".format(PWD, dir)))
105 verify_file(output.name, '.{}{}_testsuite.sv'.format(PWD, dir))
106
107def verify_testrunner(testrunner, ts0, ts1='', ts2='', ts3='', tr=''):
108 if tr == '':
109 tr = '.testrunner.sv'
110 file = open(testrunner)
111 with open('.{}'.format(testrunner), 'w') as output:
112 for line in file:
113 output.write(line.replace('TS0', ts0).replace('TS1', ts1).replace('TS2', ts2).replace('TS3', ts3))
114 verify_file(output.name, tr)
Tudor Timi7b8793f2020-01-02 19:44:10 +0100115
116
117def expect_testrunner_pass(logfile_path):
118 with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log:
119 return re.search(br'INFO: \[.*\]\[testrunner\]: PASSED (. of . suites passing) \[$SVUnitVersion\]', log)
Tudor Timi8e4a36e2020-01-02 19:55:48 +0100120
Tudor Timi209d9332020-01-02 20:25:46 +0100121def expect_testrunner_fail(logfile_path):
122 with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log:
123 return re.search(br'INFO: \[.*\]\[testrunner\]: FAILED', log)
124
Tudor Timi8e4a36e2020-01-02 19:55:48 +0100125def expect_string(pattern, logfile_path):
126 with open(logfile_path) as file, mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as log:
127 return re.search(pattern, log)
Tudor Timi80274d62020-01-02 20:04:38 +0100128
129def expect_file(path):
130 return os.path.exists(path)
131
132def expect_file_does_contain(pattern, file_path):
133 return expect_string(pattern, file_path)
Tudor Timida4126d2020-01-03 15:16:10 +0100134
135
136def expect_passing_example(dir, sim):
137 with dir.as_cwd():
138 subprocess.check_call(['runSVUnit', '-s', sim])
139
140 expect_file('run.log')
141 expect_testrunner_pass('run.log')