CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
RuntimeLibrary_test.cpp
Go to the documentation of this file.
1 #define BOOST_TEST_DYN_LINK
2 #include <boost/test/unit_test.hpp>
3 
6 
8 #include "casm/casm_io/Log.hh"
9 
11 #include <boost/filesystem.hpp>
12 
13 using namespace CASM;
14 
15 BOOST_AUTO_TEST_SUITE(RuntimeLibraryTest)
16 
17 BOOST_AUTO_TEST_CASE(FunctionTest) {
18 
19  BOOST_CHECK_EQUAL(true, true);
20  std::string cc_filename_base = "tests/unit/system/runtime_lib";
21  fs::path cc_filename {cc_filename_base + ".cc"};
22  BOOST_CHECK_EQUAL(true, true);
23 
24  fs::ofstream file(cc_filename);
25  file << "#include <iostream>\n"
26  "extern \"C\" int forty_two() {\n"
27  " return 42;\n"
28  "}\n"
29  "\n"
30  "extern \"C\" int add(int a, int b) {\n"
31  " return a + b;\n"
32  "}\n";
33  file.close();
34  BOOST_CHECK_EQUAL(true, true);
35 
36  std::string compile_opt = RuntimeLibrary::default_cxx().first + " " +
38  std::string so_opt = RuntimeLibrary::default_cxx().first + " " +
39  RuntimeLibrary::default_soflags().first + " " +
41  BOOST_CHECK_EQUAL(true, true);
42 
43  RuntimeLibrary lib(
44  cc_filename_base,
45  compile_opt,
46  so_opt,
47  "Compiling RuntimeLibrary test code",
48  Logging());
49 
50  BOOST_CHECK_EQUAL(true, true);
51 
52  // get the 'int forty_two()' function
53  std::function<int()> forty_two = lib.get_function<int()>("forty_two");
54 
55  // use it to do something
56  BOOST_CHECK_EQUAL(42, forty_two());
57 
58  // get the 'int add(int, int)' function
59  std::function<int(int, int)> add = lib.get_function<int(int, int)>("add");
60 
61  // use it to do something
62  BOOST_CHECK_EQUAL(5, add(2, 3));
63 
64  // delete the library
65  lib.rm();
66 
67  BOOST_CHECK_EQUAL(true, true);
68 
69 }
70 
71 BOOST_AUTO_TEST_SUITE_END()
static std::pair< std::string, std::string > default_cxxflags()
Default c++ compiler options.
std::string link_path(const fs::path &dir)
static std::pair< std::string, std::string > default_cxx()
Return default compiler.
Write, compile, load and use code at runtime.
Main CASM namespace.
Definition: complete.cpp:8
static std::pair< fs::path, std::string > default_boost_libdir()
Return default libdir for boost.
static std::pair< std::string, std::string > default_soflags()
Default c++ compiler options.
std::function< Signature > get_function(std::string function_name) const
Obtain a function from the current library.
BOOST_AUTO_TEST_CASE(FunctionTest)
void rm()
Remove the current library and source code.