Skip to content

Commit 01bc017

Browse files
committed
implements new unit tests for expression_tree_op_node class
1 parent ee4d30c commit 01bc017

File tree

4 files changed

+102
-13
lines changed

4 files changed

+102
-13
lines changed

tests/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
if(BUILD_TESTING)
2+
3+
add_executable( operators_test operators.cpp )
4+
add_test( operators_test ${EXECUTABLE_OUTPUT_PATH}/operators_test )
5+
26
add_executable( expression_tree_leaf_node_test expression_tree_leaf_node.cpp )
37
add_test( expression_tree_leaf_node_test ${EXECUTABLE_OUTPUT_PATH}/expression_tree_leaf_node_test )
48

5-
add_executable( operators_test operators.cpp )
6-
add_test( operators_test ${EXECUTABLE_OUTPUT_PATH}/operators_test )
9+
add_executable( expression_tree_op_node_test expression_tree_op_node.cpp )
10+
add_test( expression_tree_op_node_test ${EXECUTABLE_OUTPUT_PATH}/expression_tree_op_node_test )
11+
712
endif()

tests/expression_tree_leaf_node.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
#include <attwoodn/expression_tree.hpp>
2+
#include "test_utils.hpp"
23
#include <limits>
34
#include <iostream>
45
#include <cassert>
56

67
using namespace attwoodn::expression_tree;
78

8-
struct test_fixture {
9-
std::string some_string;
10-
const std::string some_const_string = "this IS 4 T3s7 $tRing ";
11-
char* some_char_ptr;
12-
uint16_t some_uint;
13-
14-
bool is_some_uint_greater_than_zero() const {
15-
return some_uint;
16-
}
17-
};
18-
199
void test_string_evaluation();
2010
void test_char_ptr_evaluation();
2111
void test_uint_evaluation();

tests/expression_tree_op_node.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <attwoodn/expression_tree.hpp>
2+
#include "test_utils.hpp"
3+
#include <limits>
4+
#include <iostream>
5+
#include <cassert>
6+
7+
using namespace attwoodn::expression_tree;
8+
9+
void test_one_layer_op_node_evaluation();
10+
11+
int main(int argc, char** argv) {
12+
test_one_layer_op_node_evaluation();
13+
14+
return EXIT_SUCCESS;
15+
}
16+
17+
void test_one_layer_op_node_evaluation() {
18+
auto child_expr1 = make_expr(&test_fixture::some_string, op::equals, std::string("hello, world!"));
19+
auto child_expr2 = make_expr(&test_fixture::some_uint, op::less_than, (uint16_t) 500);
20+
21+
auto expr = make_op_node<test_fixture>(child_expr1, boolean_op::AND, child_expr2);
22+
23+
test_fixture fixture;
24+
fixture.some_string = "hello, world!";
25+
fixture.some_uint = 499;
26+
assert(expr->evaluate(fixture));
27+
28+
fixture.some_uint = 0;
29+
assert(expr->evaluate(fixture));
30+
31+
fixture.some_uint = 1;
32+
assert(expr->evaluate(fixture));
33+
34+
fixture.some_uint = 250;
35+
assert(expr->evaluate(fixture));
36+
37+
fixture.some_uint = 435;
38+
assert(expr->evaluate(fixture));
39+
40+
fixture.some_uint = 500;
41+
assert(!expr->evaluate(fixture));
42+
43+
fixture.some_uint = 501;
44+
assert(!expr->evaluate(fixture));
45+
46+
fixture.some_uint = 9999;
47+
assert(!expr->evaluate(fixture));
48+
49+
fixture.some_uint = std::numeric_limits<uint16_t>::max();
50+
assert(!expr->evaluate(fixture));
51+
52+
fixture.some_uint = 499;
53+
assert(expr->evaluate(fixture));
54+
55+
fixture.some_string = "hello!";
56+
assert(!expr->evaluate(fixture));
57+
58+
fixture.some_string = "hello world!";
59+
assert(!expr->evaluate(fixture));
60+
61+
fixture.some_string = "hello,world!";
62+
assert(!expr->evaluate(fixture));
63+
64+
fixture.some_string = "hello, world!";
65+
assert(expr->evaluate(fixture));
66+
}

tests/test_utils.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
#include <attwoodn/expression_tree.hpp>
4+
#include <string>
5+
6+
namespace et = attwoodn::expression_tree;
7+
8+
struct test_fixture {
9+
std::string some_string;
10+
const std::string some_const_string = "this IS 4 T3s7 $tRing ";
11+
char* some_char_ptr;
12+
uint16_t some_uint;
13+
14+
bool is_some_uint_greater_than_zero() const {
15+
return some_uint;
16+
}
17+
};
18+
19+
/**
20+
* Helper function for creating inner expression tree nodes. I'm undecided if this should be included in the public API
21+
*/
22+
template<typename Obj, typename LeftChild, typename RightChild>
23+
et::node::expression_tree_op_node<Obj, LeftChild, RightChild>* make_op_node(LeftChild* left, et::boolean_op op, RightChild* right) {
24+
auto node = new et::node::expression_tree_op_node<Obj, LeftChild, RightChild>(op);
25+
node->set_left(left);
26+
node->set_right(right);
27+
return node;
28+
}

0 commit comments

Comments
 (0)