Skip to content

Commit

Permalink
Add a test to use catch2 for xlat.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmrf committed Jan 16, 2020
1 parent 0e39d29 commit ce4c48c
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 3 deletions.
9 changes: 8 additions & 1 deletion plugins/xlat/Xlat.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#include "SystemCClang.h"

#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTImporter.h"
#
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FileSystem.h"
Expand All @@ -14,12 +18,15 @@ using namespace hnode;


class Xlat : public SystemCConsumer {

public:
Xlat( CompilerInstance& ci, std::string topModule )
: SystemCConsumer( ci, topModule ) {
}

Xlat(ASTUnit *from_ast, std::string top = "!none")
: SystemCConsumer(from_ast) {
}

bool postFire();
void xlatport(ModuleDecl::portMapType pmap, hNode::hdlopsEnum h_op, hNodep &h_info);
void xlatsig(ModuleDecl::signalMapType pmap, hNode::hdlopsEnum h_op, hNodep &h_info);
Expand Down
6 changes: 4 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ set( UNIT_TEST_LIST
# t4-matchers
t5-template-matching
# subtree-matcher-test
# XLAT tests
xlat-sreg-test
)

include(CTest)
Expand All @@ -36,8 +38,8 @@ foreach(NAME IN LISTS UNIT_TEST_LIST)
set(TARGET_NAME ${NAME})
add_executable(${TARGET_NAME} main.cpp ${TEST_SOURCE_NAME} )

target_link_libraries(${TARGET_NAME} PUBLIC libsystemc-clang ${CLANG_LIBS} ${LLVM_LIBS} ${LLVM_SYSTEM_LIBS})
target_include_directories(${TARGET_NAME} PUBLIC ../externals/catch2/ PUBLIC ../tests/ PUBLIC ${CMAKE_BINARY_DIR}/tests/ )
target_link_libraries(${TARGET_NAME} PUBLIC libsystemc-clang PUBLIC libxlat ${CLANG_LIBS} ${LLVM_LIBS} ${LLVM_SYSTEM_LIBS})
target_include_directories(${TARGET_NAME} PUBLIC ../externals/catch2/ PUBLIC ../tests/ PUBLIC ../plugins/xlat/ PUBLIC ${CMAKE_BINARY_DIR}/tests/ )
add_test( NAME ${TARGET_NAME} COMMAND ${TARGET_NAME}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/tests/data/ )
endforeach()
Expand Down
133 changes: 133 additions & 0 deletions tests/xlat-sreg-test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include "catch.hpp"

#include "clang/AST/ASTImporter.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Parse/ParseAST.h"
#include "clang/Tooling/Tooling.h"

#include "PluginAction.h"
#include "SystemCClang.h"
#include "Xlat.h"

// This is automatically generated from cmake.
#include "ClangArgs.h"
#include "Testing.h"

using namespace clang;
using namespace clang::tooling;
using namespace clang::ast_matchers;
using namespace scpar;

TEST_CASE("sreg example", "[llnl-examples]") {
std::string code{systemc_clang::read_systemc_file(
systemc_clang::test_data_dir, "/llnl-examples/sreg.cpp")};
INFO(systemc_clang::test_data_dir);

auto catch_test_args = systemc_clang::catch_test_args;
catch_test_args.push_back("-I" + systemc_clang::test_data_dir +
"/llnl-examples/");

ASTUnit *from_ast =
tooling::buildASTFromCodeWithArgs(code, catch_test_args).release();

//SystemCConsumer sc{from_ast};
Xlat sc{from_ast};
sc.HandleTranslationUnit(from_ast->getASTContext());
auto model{sc.getSystemCModel()};
// These are instances.
auto module_decl{model->getModuleDecl()};

llvm::outs() << "\n";
for (const auto &decl : module_decl) {
llvm::outs() << "[ " << decl.first << " ]" << decl.second << " "
<< decl.second->getInstanceName() << "\n";
}

// Testing instances.

auto test_module{model->getInstance("testing")};
auto sreg_bypass{model->getInstance("sreg_bypass")};
auto sreg_fwd{model->getInstance("sreg_fwd")};
auto sreg_fwd_rev{model->getInstance("sreg_fwd_rev")};

/*
auto test_module{std::find_if(
module_decl.begin(), module_decl.end(), [](const auto &element) {
return element.second->getInstanceName() == "testing";
})};
auto sreg_bypass{std::find_if(
module_decl.begin(), module_decl.end(), [](const auto &element) {
return element.second->getInstanceName() == "sreg_bypass";
})};
auto sreg_fwd{std::find_if(
module_decl.begin(), module_decl.end(), [](const auto &element) {
return element.second->getInstanceName() == "sreg_fwd";
})};
auto sreg_fwd_rev{std::find_if(
module_decl.begin(), module_decl.end(), [](const auto &element) {
return element.second->getInstanceName() == "sreg_fwd_rev";
})};
*/

//
// Begin the tests.
//
//
SECTION("sreg instance and port tests", "[instances]") {
INFO("ERROR: number of sc_module declarations found: "
<< module_decl.size());
CHECK(module_decl.size() == 4);

REQUIRE(test_module != nullptr);
REQUIRE(sreg_bypass != nullptr);
REQUIRE(sreg_fwd != nullptr);
REQUIRE(sreg_fwd_rev != nullptr);

//
//
//
INFO("Checking sreg_bypass ports.");
auto sreg_bypass_decl{sreg_bypass};

REQUIRE(sreg_bypass_decl->getIPorts().size() == 2);
REQUIRE(sreg_bypass_decl->getOPorts().size() == 0);
REQUIRE(sreg_bypass_decl->getIOPorts().size() == 0);
REQUIRE(sreg_bypass_decl->getSignals().size() == 0);
REQUIRE(sreg_bypass_decl->getOtherVars().size() == 0);
REQUIRE(sreg_bypass_decl->getInputStreamPorts().size() == 1);
REQUIRE(sreg_bypass_decl->getOutputStreamPorts().size() == 1);

//
//
//
INFO("Checking sreg_fwd ports.");
auto sreg_fwd_decl{sreg_fwd};

REQUIRE(sreg_fwd_decl->getIPorts().size() == 2);
REQUIRE(sreg_fwd_decl->getOPorts().size() == 0);
REQUIRE(sreg_fwd_decl->getIOPorts().size() == 0);
REQUIRE(sreg_fwd_decl->getSignals().size() == 1);
REQUIRE(sreg_fwd_decl->getOtherVars().size() == 0);
REQUIRE(sreg_fwd_decl->getInputStreamPorts().size() == 1);
REQUIRE(sreg_fwd_decl->getOutputStreamPorts().size() == 1);

//
//
//
INFO("Checking sreg_fwd_rev ports.");

auto sreg_fwd_rev_decl{sreg_fwd_rev};

REQUIRE(sreg_fwd_rev_decl->getIPorts().size() == 2);
REQUIRE(sreg_fwd_rev_decl->getOPorts().size() == 0);
REQUIRE(sreg_fwd_rev_decl->getIOPorts().size() == 0);
REQUIRE(sreg_fwd_rev_decl->getSignals().size() == 7);
REQUIRE(sreg_fwd_rev_decl->getOtherVars().size() == 0);
REQUIRE(sreg_fwd_rev_decl->getInputStreamPorts().size() == 1);
REQUIRE(sreg_fwd_rev_decl->getOutputStreamPorts().size() == 1);
}
}

0 comments on commit ce4c48c

Please sign in to comment.