-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdltest.cc
101 lines (87 loc) · 3.28 KB
/
dltest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <iostream>
#include <sstream>
#include <string>
#include "dlmm.hh"
int main(void) {
Dl dl("./libuseless.so");
// Reading variables from a dynamic library
std::cout << "Reading and modifying a local copy of variable in library..."
<< std::endl;
int my_answer =
dl.symbol<int>("answer_to_life_the_universe_and_everything");
my_answer = -1;
(void)my_answer;
if (dl.symbol<int>("answer_to_life_the_universe_and_everything") != 42) {
std::cout << "TEST FAILED: variable was changed inside library."
<< std::endl;
return -1;
}
std::cout << "Reading and changing a referenced variable in library..."
<< std::endl;
int &my_better_answer =
dl.symbol<int>("answer_to_life_the_universe_and_everything");
if (my_better_answer != 42) {
std::cout << "TEST FAILED: Expected 42, got " << my_better_answer
<< std::endl;
return -1;
}
my_better_answer = 11; // This one goes to eleven!
if (dl.symbol<int>("answer_to_life_the_universe_and_everything") != 11) {
std::cout << "TEST FAILED: could not change variable inside library."
<< std::endl;
return -1;
}
// Calling functions from a dynamic library
std::cout << "Calling function in library... " << std::endl;
auto print_and_increment =
dl.symbol<int(std::ostringstream &, std::string const &, int)>(
"print_and_increment");
std::string input_string = "Hello World!";
std::ostringstream output_string;
int incremented_via_reference =
print_and_increment(output_string, input_string, 100);
if (output_string.str() != input_string) {
std::cout << "TEST FAILED: function did not stream correct value "
<< "(expected " << input_string << ", got "
<< output_string.str() << ")." << std::endl;
return -1;
}
if (101 != incremented_via_reference) {
std::cout << "TEST FAILED: function did not return correct value "
<< "(expected " << 101 << ", got "
<< incremented_via_reference << ")." << std::endl;
return -1;
}
// Catching exceptions
std::cout << "Loading nonexistent library..." << std::endl;
try {
Dl dl2("DOES_NOT_EXIST");
std::cout << "TEST FAILED: nonexistent library did not throw."
<< std::endl;
return -1;
} catch (std::runtime_error &) {
}
std::cout << "Loading nonexistent symbol..." << std::endl;
try {
std::cout << dl.symbol<int>("DOES_NOT_EXIST");
std::cout << "TEST FAILED: nonexistent symbol did not throw."
<< std::endl;
return -1;
} catch (std::runtime_error &) {
}
std::cout << "Loading nonexistent version of an existing symbol..."
<< std::endl;
try {
std::cout << dl.symbol<int>(
"answer_to_life_the_universe_and_everything", "DOES_NOT_EXIST");
std::cout << "TEST FAILED: nonexistent version of existing symbol did "
"not throw."
<< std::endl;
return -1;
} catch (std::runtime_error &) {
}
std::cout << "Done testing." << std::endl;
}