Skip to content

Commit

Permalink
feat: cpp symbols lookup test
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-olivier committed Jul 14, 2024
1 parent f3acc1d commit b78c48d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 35 deletions.
26 changes: 9 additions & 17 deletions tests/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,27 @@

extern "C" {

LIB_EXPORT double pi_value = 3.14159;
LIB_EXPORT void *ptr = (void *)1;
LIB_EXPORT double pi_value_c = 3.14159;
LIB_EXPORT void *ptr_c = (void *)1;

LIB_EXPORT double adder(double a, double b) {
LIB_EXPORT double adder_c(double a, double b) {
return a + b;
}

LIB_EXPORT void print_hello() {
LIB_EXPORT void print_hello_c() {
std::cout << "Hello" << std::endl;
}

} // extern "C"

LIB_EXPORT double do_you_find_me(double a, double b) {
return a + b;
}
LIB_EXPORT double meaning_of_life = 42;

namespace toto {
LIB_EXPORT double and_now(double a, double b) {
namespace tools {
LIB_EXPORT double adder(double a, double b) {
return a + b;
}

LIB_EXPORT double and_now(double a, std::string b) {
return a + b.size();
LIB_EXPORT std::string adder(std::string a, std::string b) {
return a + b;
}
}

LIB_EXPORT double zaza = 12;

namespace tata {
LIB_EXPORT double zozo = 11;
}
54 changes: 36 additions & 18 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ TEST(example, example_test) {
testing::internal::CaptureStdout();
dylib lib("./", "dynamic_lib");

auto adder = lib.get_function<double(double, double)>("adder");
auto adder = lib.get_function<double(double, double)>("adder_c");
EXPECT_EQ(adder(5, 10), 15);

auto printer = lib.get_function<void()>("print_hello");
auto printer = lib.get_function<void()>("print_hello_c");
printer();
EXPECT_EQ(testing::internal::GetCapturedStdout(), "Hello\n");

double pi_value = lib.get_variable<double>("pi_value");
double pi_value = lib.get_variable<double>("pi_value_c");
EXPECT_EQ(pi_value, 3.14159);

void *ptr = lib.get_variable<void *>("ptr");
void *ptr = lib.get_variable<void *>("ptr_c");
EXPECT_EQ(ptr, (void *)1);
}

Expand Down Expand Up @@ -65,16 +65,16 @@ TEST(get_variable, bad_symbol) {
TEST(get_variable, alter_variables) {
dylib lib("./", "dynamic_lib");

auto &pi = lib.get_variable<double>("pi_value");
auto &pi = lib.get_variable<double>("pi_value_c");
EXPECT_EQ(pi, 3.14159);
pi = 123;
auto &pi1 = lib.get_variable<double>("pi_value");
auto &pi1 = lib.get_variable<double>("pi_value_c");
EXPECT_EQ(pi1, 123);

auto &ptr = lib.get_variable<void *>("ptr");
auto &ptr = lib.get_variable<void *>("ptr_c");
EXPECT_EQ(ptr, (void *)1);
ptr = &lib;
auto &ptr1 = lib.get_variable<void *>("ptr");
auto &ptr1 = lib.get_variable<void *>("ptr_c");
EXPECT_EQ(ptr1, &lib);
}

Expand Down Expand Up @@ -106,20 +106,20 @@ TEST(invalid_argument, null_pointer) {

TEST(manual_decorations, basic_test) {
dylib lib(".", dylib::filename_components::prefix + std::string("dynamic_lib") + dylib::filename_components::suffix, dylib::no_filename_decorations);
auto pi = lib.get_variable<double>("pi_value");
auto pi = lib.get_variable<double>("pi_value_c");
EXPECT_EQ(pi, 3.14159);
}

TEST(std_move, basic_test) {
try {
dylib lib("./", "dynamic_lib");
dylib other(std::move(lib));
auto pi = other.get_variable<double>("pi_value");
auto pi = other.get_variable<double>("pi_value_c");
EXPECT_EQ(pi, 3.14159);
lib = std::move(other);
auto ptr = lib.get_variable<void *>("ptr");
auto ptr = lib.get_variable<void *>("ptr_c");
EXPECT_EQ(ptr, (void *)1);
other.get_variable<double>("pi_value");
other.get_variable<double>("pi_value_c");
EXPECT_EQ(true, false);
}
catch (const std::logic_error &) {
Expand All @@ -131,20 +131,20 @@ TEST(has_symbol, basic_test) {
dylib dummy("./", "dynamic_lib");
dylib lib(std::move(dummy));

EXPECT_TRUE(lib.has_symbol("pi_value"));
EXPECT_TRUE(lib.has_symbol("pi_value_c"));
EXPECT_FALSE(lib.has_symbol("bad_symbol"));
EXPECT_FALSE(lib.has_symbol(nullptr));
EXPECT_FALSE(dummy.has_symbol("pi_value"));
EXPECT_FALSE(dummy.has_symbol("pi_value_c"));
}

TEST(handle_management, basic_test) {
dylib lib("./", "dynamic_lib");
EXPECT_FALSE(lib.native_handle() == nullptr);
auto handle = lib.native_handle();
#if (defined(_WIN32) || defined(_WIN64))
auto sym = GetProcAddress(handle, "adder");
auto sym = GetProcAddress(handle, "adder_c");
#else
auto sym = dlsym(handle, "adder");
auto sym = dlsym(handle, "adder_c");
#endif
EXPECT_FALSE(sym == nullptr);
#if (defined(__GNUC__) && __GNUC__ >= 8)
Expand All @@ -167,15 +167,15 @@ TEST(system_lib, basic_test) {

#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
TEST(filesystem, basic_test) {
bool has_sym = dylib(std::filesystem::path("."), "dynamic_lib").has_symbol("pi_value");
bool has_sym = dylib(std::filesystem::path("."), "dynamic_lib").has_symbol("pi_value_c");
EXPECT_TRUE(has_sym);

bool found = false;
for (const auto &file : std::filesystem::recursive_directory_iterator(".")) {
if (file.path().extension() == dylib::filename_components::suffix) {
try {
dylib lib(file.path());
if (lib.has_symbol("pi_value"))
if (lib.has_symbol("pi_value_c"))
found = true;
} catch (const std::exception &) {}
}
Expand All @@ -184,6 +184,24 @@ TEST(filesystem, basic_test) {
}
#endif

TEST(cpp_symbols, basic_test) {
dylib lib("./", "dynamic_lib");

auto mean = lib.get_variable<double>("meaning_of_life");
EXPECT_EQ(mean, 42);

EXPECT_THROW(lib.get_function<void()>("tools::adder"), dylib::symbol_error);

auto d_adder = lib.get_function<double(double, double)>("tools::adder(double, double)");
EXPECT_EQ(d_adder(11, 11), 22);

auto str_rep = "std::basic_string<char, std::char_traits<char>, std::allocator<char>>";

auto s_adder = lib.get_function<std::string(std::string, std::string)>
(std::string("tools::adder(") + str_rep + ", " + str_rep + ")");
EXPECT_EQ(s_adder("Hello", "World"), "HelloWorld");
}

int main(int ac, char **av) {
testing::InitGoogleTest(&ac, av);
return RUN_ALL_TESTS();
Expand Down

0 comments on commit b78c48d

Please sign in to comment.