Skip to content

Commit b574c7a

Browse files
committed
adds examples.cpp to assist with documentation
1 parent b833839 commit b574c7a

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

tests/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ if(BUILD_TESTING)
2020
target_compile_options( expression_tree_test PRIVATE -fsanitize=address )
2121
add_test( expression_tree_test ${EXECUTABLE_OUTPUT_PATH}/expression_tree_test )
2222

23+
add_executable( examples examples.cpp )
24+
target_link_libraries( examples "-fsanitize=address" )
25+
target_compile_options( examples PRIVATE -fsanitize=address )
26+
add_test( examples ${EXECUTABLE_OUTPUT_PATH}/examples )
27+
2328
endif()

tests/examples.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <attwoodn/expression_tree.hpp>
2+
#include <cassert>
3+
4+
using namespace attwoodn::expression_tree;
5+
6+
void expression_memory_safety_example();
7+
8+
struct my_type {
9+
int my_int = 5;
10+
bool my_bool = true;
11+
};
12+
13+
int main(int argc, char** argv) {
14+
expression_memory_safety_example();
15+
16+
return EXIT_SUCCESS;
17+
}
18+
19+
void expression_memory_safety_example() {
20+
// The heap-allocated expression node pointer returned by make_expr becomes owned by the expression_tree
21+
expression_tree<my_type> expr_tree_raw {
22+
make_expr(&my_type::my_bool, op::equals, true)
23+
};
24+
25+
26+
// ...
27+
28+
29+
// The heap-allocated expression node pointer returned by make_expr becomes owned by the unique_ptr
30+
std::unique_ptr<node::expression_tree_node<my_type>> smart_expr {
31+
make_expr(&my_type::my_bool, op::equals, true)
32+
};
33+
34+
// the expression_tree takes ownership of the unique_ptr
35+
expression_tree<my_type> expr_tree_smart(std::move(smart_expr));
36+
37+
38+
// ...
39+
40+
41+
// The heap-allocated expression node pointer returned by make_expr must be explicitly deleted
42+
auto* expr_raw = make_expr(&my_type::my_bool, op::equals, true);
43+
delete expr_raw;
44+
}

tests/expression_tree.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
using namespace attwoodn::expression_tree;
66

7+
void test_expression_tree_std_string_template();
78
void test_simple_expression_tree();
89
void test_complex_expression_tree();
910
void test_moved_expression_tree();
1011
void test_copied_expression_tree();
1112

1213
int main(int argc, char** argv) {
14+
test_expression_tree_std_string_template();
1315
test_simple_expression_tree();
1416
test_complex_expression_tree();
1517
test_moved_expression_tree();
@@ -18,6 +20,17 @@ int main(int argc, char** argv) {
1820
return EXIT_SUCCESS;
1921
}
2022

23+
void test_expression_tree_std_string_template() {
24+
expression_tree<std::string> expr {
25+
make_expr(&std::string::empty, op::equals, true)
26+
};
27+
28+
assert(expr.evaluate(""));
29+
assert(!expr.evaluate(" "));
30+
assert(!expr.evaluate("hello"));
31+
assert(!expr.evaluate("good bye"));
32+
}
33+
2134
void test_simple_expression_tree() {
2235
auto test_procedure = [](const expression_tree<test_fixture>& expr) {
2336
test_fixture fixture;

0 commit comments

Comments
 (0)