Skip to content

Commit

Permalink
A new test that breaks current conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuanhao-wu committed Jan 13, 2020
1 parent 626496a commit 6317b89
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 55 deletions.
27 changes: 27 additions & 0 deletions tests/verilog-conversion/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Fixtures for testing"""
import pytest
from util.conf import LLNLExampleTestingConfigurations
import driver as drv


@pytest.fixture
def llnldriver():
"""fixture for llnl example driver"""
conf = LLNLExampleTestingConfigurations()
llnl_driver = drv.SystemCClangDriver(conf)
return llnl_driver


def pytest_addoption(parser):
parser.addoption("--tool-output", action="store_true")


def pytest_generate_tests(metafunc):
# This is called for every test. Only get/set command line arguments
# if the argument is specified in the list of test "fixturenames".
if 'tool_output' in metafunc.fixturenames:
if metafunc.config.getoption("tool_output"):
verbose = True
else:
verbose = False
metafunc.parametrize("tool_output", [verbose])
12 changes: 10 additions & 2 deletions tests/verilog-conversion/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def generate_verilog_from_sexp(self, path, output_folder, keep_v=False, verbose=
SystemCClangDriver.PYTHON_CONVERT_TEMPLATE,
path
])
if verbose:
print('cmdline', cmdline)
try:
if verbose:
subprocess.run(cmdline, shell=True)
Expand Down Expand Up @@ -112,6 +114,7 @@ def generate_sexp(self, path, output_folder, keep_sexp=False, verbose=False):
ok = True
try:
if verbose:
print('cmd: ', ' '.join(cmdline))
subprocess.run(' '.join(cmdline), shell=True)
else:
with open(os.devnull, 'wb') as null:
Expand All @@ -137,6 +140,11 @@ def generate_sexp(self, path, output_folder, keep_sexp=False, verbose=False):
If any step fails, an exeption will be thrown
"""
def generate_verilog(self, path, output_folder, verbose):
succ_sexp, sexp_filename = generate_sexp(path, output_folder, keep_sexp=True, verbose=verbose)
succ_v, v_filename = generate_verilog(sexp_filename, output_folder, keep_v=True, verbose=verbose)
succ_sexp, sexp_filename = self.generate_sexp(path, output_folder, keep_sexp=True, verbose=verbose)
if succ_sexp:
assert os.path.isfile(sexp_filename), 'Cannot find generated sexp_filename: {}'.format(sexp_filename)
succ_v, v_filename = self.generate_verilog_from_sexp(sexp_filename, output_folder, keep_v=True, verbose=verbose)
return succ_v, v_filename
else:
return False, None

39 changes: 1 addition & 38 deletions tests/verilog-conversion/run.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,7 @@
from driver import *
from util.vparser import VerilogParser
from util.conf import LLNLExampleTestingConfigurations
import tempfile
import contextlib

class TestingConfigurations(object):
"""parameters in this configuration might change"""

@property
def extra_header_folders(self):
# SystemC Clang build llnl-examples
return self.header_folders

@property
def positional_arguments(self):
headers = []
extra_header_folders = self.extra_header_folders
for h in extra_header_folders:
headers.extend(["-I", h])
return headers

def __init__(self, header_folders):
self.header_folders = header_folders

class LLNLExampleTestingConfigurations(TestingConfigurations):

def __init__(self, header_folders=None):
if header_folders is None:
header_folders = []
this_folders = header_folders + [
'/home/allen/working/systemc-clang/examples/llnl-examples/'
]
super(LLNLExampleTestingConfigurations, self).__init__(this_folders)

@contextlib.contextmanager
def make_temp_directory():
temp_dir = tempfile.mkdtemp()
try:
yield temp_dir
finally:
shutil.rmtree(temp_dir)

def main():
conf = LLNLExampleTestingConfigurations()
Expand Down
73 changes: 62 additions & 11 deletions tests/verilog-conversion/test_sreg.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,67 @@
import os
import sys
import pytest
from llnl import run_verilog_matched_test, run_sexp_matched_test
from util.sexpdiff import sexpdiff
from util.vparser import VerilogParser
from shutil import copy

@pytest.mark.xfail(reason="the golden standard now is incorrect")
def test_sreg_sexp(args=None):
assert run_sexp_matched_test('sreg', args), "sreg sexp should match golden standard"

@pytest.mark.skip(reason="no golden standard available for now")
def test_sreg_verilog(args=None):
assert run_verilog_matched_test('sreg', args), "sreg verilog should match golden standard"
def test_sreg_sexp(tmpdir, llnldriver, tool_output):
conf = llnldriver.conf
filename = conf.get_module_name('sreg.cpp')
output_folder = tmpdir
res, filename = llnldriver.generate_sexp(
path=filename,
output_folder=output_folder,
verbose=tool_output,
keep_sexp=True
)
assert res, "should convert to sexpression"
diff_res, diff_str = sexpdiff(
filename,
conf.get_golden_sexp_name('sreg_hdl.txt')
)
if diff_res:
print(diff_str)
assert not diff_res, 'should match golden standard'


def test_sreg_verilog(tmpdir, llnldriver, tool_output):
conf = llnldriver.conf
filename = conf.get_module_name('sreg.cpp')
output_folder = tmpdir
res, filename = llnldriver.generate_verilog(
path=filename,
output_folder=output_folder,
verbose=tool_output
)
assert res, "should convert to Verilog from cpp"
diff_info = VerilogParser.diff(
filename,
conf.get_golden_verilog_name('sreg_hdl.txt.v')
)
print(str(diff_info))
assert diff_info is None, 'should be no diff in Verilog'

def test_sreg_sexp_to_verilog(tmpdir, llnldriver, tool_output):
conf = llnldriver.conf
filename = conf.get_golden_sexp_name('sreg_hdl.txt')
output_folder = tmpdir
copy(filename, str(output_folder) + '/')
res, filename = llnldriver.generate_verilog_from_sexp(
path=str(output_folder) + '/sreg_hdl.txt',
output_folder=output_folder,
keep_v=True,
verbose=tool_output
)
assert res, "should convert to Verilog from sexp"
print('filename: ', filename)
print('golden: ', conf.get_golden_verilog_name('sreg_hdl.txt.v'))
diff_info = VerilogParser.diff(
filename,
conf.get_golden_verilog_name('sreg_hdl.txt.v')
)
print(str(diff_info))
assert diff_info is None, 'should be no diff in Verilog'

if __name__ == '__main__':
test_sreg_verilog(args)
test_sreg_sexp(args)
test_sreg_verilog()
test_sreg_sexp()
Empty file.
64 changes: 64 additions & 0 deletions tests/verilog-conversion/util/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""Configuration classes for handling directories etc"""
import os


class TestingConfigurations(object):
"""parameters in this configuration might change"""
TEST_DATA_ROOT = os.environ['SYSTEMC_CLANG_BUILD_DIR'] + "../systemc-clang/examples/"

@property
def extra_header_folders(self):
"""acessor for header folders"""
# SystemC Clang build llnl-examples
return self.header_folders

@property
def positional_arguments(self):
"""positional arguments for the systemc-clang command"""
headers = []
extra_header_folders = self.extra_header_folders
for header in extra_header_folders:
headers.extend(["-I", header])
return headers

def get_module_name(self, basename):
return self.root_folder + '/' + basename

def get_golden_sexp_name(self, basename):
return self.golden_folder + '/' + basename

def get_golden_verilog_name(self, basename):
return self.golden_folder + '/' + basename

def __init__(self, root_folder, golden_folder, header_folders):
self.header_folders = header_folders
self.root_folder = root_folder
self.golden_folder = golden_folder


class LLNLExampleTestingConfigurations(TestingConfigurations):
"""parameters for LLNL examples"""

def __init__(self, header_folders=None):
if header_folders is None:
header_folders = []
this_folders = header_folders + [
'{}/llnl-examples/'.format(TestingConfigurations.TEST_DATA_ROOT)
]
root_folder = os.environ['SYSTEMC_CLANG_BUILD_DIR'] + '/' + 'tests/data/verilog-conversion/llnl-examples/'
golden_folder = root_folder + 'handcrafted/'
super(LLNLExampleTestingConfigurations, self).__init__(root_folder, golden_folder, this_folders)


class ExampleTestingConfigurations(TestingConfigurations):
"""parameters for ex_* examples"""

def __init__(self, root_folder, ex_id, header_folders=None):
if header_folders is None:
header_folders = []
this_folders = header_folders + [
'{}/ex_{}/'.format(TestingConfigurations.TEST_DATA_ROOT, ex_id)
]
root_folder = '{}/ex_{}/'.format(TestingConfigurations.TEST_DATA_ROOT, ex_id)
golden_folder = root_folder + 'handcrafted/'
super(ExampleTestingConfigurations, self).__init__(root_folder, golden_folder, this_folders)
17 changes: 17 additions & 0 deletions tests/verilog-conversion/util/sexpdiff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import difflib

def sexpdiff(this_filename, that_filename):
# Test the equality of golden versus the generated
with open(this_filename, 'r') as f:
sexp_lines = f.readlines()
with open(that_filename, 'r') as f:
golden_lines = f.readlines()

diff_res = difflib.ndiff(golden_lines, sexp_lines)
diff_only = list(filter(lambda x: x[0] != ' ', diff_res))
if diff_only:
diff_str = ''.join(diff_res)
else:
diff_str = None
return len(diff_only) != 0, diff_str

3 changes: 1 addition & 2 deletions tests/verilog-conversion/util/vdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class VerilogASTDiff(object):
@staticmethod
def _diff_traverse(this_node, that_node):
"""traverses two asts using dfs, stops when different node encountered"""
print("at: {}".format(this_node))
if type(this_node) != type(that_node):
return False

Expand All @@ -48,7 +47,6 @@ def _diff_traverse(this_node, that_node):
if a in that_names:
that_attr = getattr(that_node, a)
if this_attr != that_attr:
print(this_attr, that_attr)
diff.append((a, this_attr, a, that_attr))
else:
diff.append((a, this_attr, '', ''))
Expand Down Expand Up @@ -79,3 +77,4 @@ def _diff_traverse(this_node, that_node):
def diff_info(this_verilog_ast, that_verilog_ast):
"""shows the diff information of two ast, including line number and node name"""
return VerilogASTDiff._diff_traverse(this_verilog_ast, that_verilog_ast)

4 changes: 2 additions & 2 deletions tests/verilog-conversion/util/vparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def diff(this_verilog_filename, that_verilog_filename,
this_ast = VerilogParser.parse(this_verilog_filename, include_list, define_list)
that_ast = VerilogParser.parse(that_verilog_filename, include_list, define_list)

this_ast.show()
that_ast.show()
# this_ast.show()
# that_ast.show()

# show the diff
diff_info = VerilogASTDiff.diff_info(this_ast, that_ast)
Expand Down

0 comments on commit 6317b89

Please sign in to comment.