Skip to content

Commit 74d9704

Browse files
committed
expands unit tests for make_expr safety
1 parent 42e776b commit 74d9704

File tree

3 files changed

+56
-48
lines changed

3 files changed

+56
-48
lines changed

tests/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +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 )
23+
add_executable( make_expr_safety_test make_expr_safety.cpp )
24+
target_link_libraries( make_expr_safety_test "-fsanitize=address" )
25+
target_compile_options( make_expr_safety_test PRIVATE -fsanitize=address )
26+
add_test( make_expr_safety_test ${EXECUTABLE_OUTPUT_PATH}/make_expr_safety_test )
2727

2828
endif()

tests/examples.cpp

Lines changed: 0 additions & 44 deletions
This file was deleted.

tests/make_expr_safety.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <attwoodn/expression_tree.hpp>
2+
#include <cassert>
3+
4+
using namespace attwoodn::expression_tree;
5+
6+
void make_expr_memory_safety_test();
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+
make_expr_memory_safety_test();
15+
16+
return EXIT_SUCCESS;
17+
}
18+
19+
void make_expr_memory_safety_test() {
20+
21+
{
22+
// The heap-allocated expression node pointer returned by make_expr becomes owned by the expression_tree
23+
expression_tree<my_type> expr_tree_raw {
24+
make_expr(&my_type::my_bool, op::equals, true)
25+
};
26+
27+
expression_tree<my_type> expr_tree_raw_AND {
28+
make_expr(&my_type::my_bool, op::equals, true)
29+
->AND(make_expr(&my_type::my_int, op::greater_than, 10))
30+
};
31+
}
32+
33+
{
34+
// The heap-allocated expression node pointer returned by make_expr becomes owned by the unique_ptr
35+
std::unique_ptr<node::expression_tree_node<my_type>> smart_expr {
36+
make_expr(&my_type::my_bool, op::equals, true)
37+
};
38+
39+
// the expression_tree takes ownership of the unique_ptr
40+
expression_tree<my_type> expr_tree_smart(std::move(smart_expr));
41+
}
42+
43+
{
44+
// The heap-allocated expression node pointer returned by make_expr must be explicitly deleted
45+
auto* expr_raw = make_expr(&my_type::my_bool, op::equals, true);
46+
delete expr_raw;
47+
48+
auto* expr_raw_AND = make_expr(&my_type::my_bool, op::equals, true)
49+
->AND(make_expr(&my_type::my_int, op::greater_than, 10));
50+
delete expr_raw_AND;
51+
}
52+
}

0 commit comments

Comments
 (0)