Skip to content

Commit e13f115

Browse files
committed
adds expression tree leaf node tests for evaluating char* type
1 parent 62c6bf0 commit e13f115

File tree

2 files changed

+125
-12
lines changed

2 files changed

+125
-12
lines changed

include/attwoodn/expression_tree.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,16 @@ namespace attwoodn::expression_tree {
216216

217217
}
218218

219+
template<class T> struct type_id{typedef T type;};
219220

221+
template<typename Obj, typename CompValue, typename Op = typename type_id<bool (*)(CompValue*, CompValue*)>::type,
222+
typename std::enable_if<std::is_convertible<CompValue, CompValue*>::value, int>::type = 0>
223+
node::expression_tree_leaf_node<Obj, Op, CompValue>* make_expr( CompValue Obj::* member_var, Op op, CompValue comp_value ) {
224+
return new node::expression_tree_leaf_node<Obj, Op, CompValue>( member_var, op, comp_value );
225+
}
226+
227+
template<typename Obj, typename CompValue, typename Op = typename type_id<bool (*)(CompValue, CompValue)>::type>
228+
node::expression_tree_leaf_node<Obj, Op, CompValue>* make_expr( CompValue Obj::* member_var, Op op, CompValue comp_value ) {
229+
return new node::expression_tree_leaf_node<Obj, Op, CompValue>( member_var, op, comp_value );
230+
}
220231
}

tests/expression_tree_leaf_node.cpp

Lines changed: 114 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,130 @@ struct test_fixture {
99
char* some_char_ptr;
1010
};
1111

12-
template<class T> struct type_id{typedef T type;};
12+
void test_string_evaluation();
13+
void test_char_ptr_evaluation();
1314

14-
template<typename Obj, typename CompValue, typename Op = typename type_id<bool (*)(CompValue*, CompValue*)>::type,
15-
typename std::enable_if<std::is_convertible<CompValue, CompValue*>::value, int>::type = 0>
16-
node::expression_tree_leaf_node<Obj, Op, CompValue>* make_leaf_node( CompValue Obj::* member_var, Op op, CompValue comp_value ) {
17-
return new node::expression_tree_leaf_node<Obj, Op, CompValue>( member_var, op, comp_value );
18-
}
15+
int main(int argc, char** argv) {
16+
test_string_evaluation();
17+
test_char_ptr_evaluation();
1918

20-
template<typename Obj, typename CompValue, typename Op = typename type_id<bool (*)(CompValue, CompValue)>::type>
21-
node::expression_tree_leaf_node<Obj, Op, CompValue>* make_leaf_node( CompValue Obj::* member_var, Op op, CompValue comp_value ) {
22-
return new node::expression_tree_leaf_node<Obj, Op, CompValue>( member_var, op, comp_value );
19+
return EXIT_SUCCESS;
2320
}
2421

25-
int main(int argc, char** argv) {
22+
void test_string_evaluation() {
2623
test_fixture fixture;
2724
fixture.some_string = "hello world!";
2825

29-
auto node = make_leaf_node(&test_fixture::some_string, &op::equals, std::string("hello world!"));
26+
auto node = make_expr(&test_fixture::some_string, &op::equals, std::string("hello world!"));
3027
assert(node->evaluate(fixture));
3128

3229
fixture.some_string = "hey, world!";
3330
assert(!node->evaluate(fixture));
31+
}
3432

35-
return EXIT_SUCCESS;
33+
void test_char_ptr_evaluation() {
34+
char a = 'a';
35+
char a_too = 'a';
36+
char b = 'b';
37+
char b_too = 'b';
38+
char c = 'c';
39+
char c_too = 'c';
40+
41+
test_fixture fixture;
42+
43+
// test equals
44+
{
45+
// make an expression : given char* == 'a'
46+
auto expr = make_expr(&test_fixture::some_char_ptr, &op::equals, &a);
47+
48+
fixture.some_char_ptr = &a;
49+
assert(expr->evaluate(fixture));
50+
51+
fixture.some_char_ptr = &a_too;
52+
assert(expr->evaluate(fixture));
53+
54+
fixture.some_char_ptr = &b;
55+
assert(!expr->evaluate(fixture));
56+
57+
fixture.some_char_ptr = &b_too;
58+
assert(!expr->evaluate(fixture));
59+
60+
fixture.some_char_ptr = &c;
61+
assert(!expr->evaluate(fixture));
62+
63+
fixture.some_char_ptr = &c_too;
64+
assert(!expr->evaluate(fixture));
65+
}
66+
67+
// test not_equals
68+
{
69+
// make an expression : given char* != 'a'
70+
auto expr = make_expr(&test_fixture::some_char_ptr, &op::not_equals, &a);
71+
72+
fixture.some_char_ptr = &a;
73+
assert(!expr->evaluate(fixture));
74+
75+
fixture.some_char_ptr = &a_too;
76+
assert(!expr->evaluate(fixture));
77+
78+
fixture.some_char_ptr = &b;
79+
assert(expr->evaluate(fixture));
80+
81+
fixture.some_char_ptr = &b_too;
82+
assert(expr->evaluate(fixture));
83+
84+
fixture.some_char_ptr = &c;
85+
assert(expr->evaluate(fixture));
86+
87+
fixture.some_char_ptr = &c_too;
88+
assert(expr->evaluate(fixture));
89+
}
90+
91+
// test less_than
92+
{
93+
// make an expression : given char* < 'b'
94+
auto expr = make_expr(&test_fixture::some_char_ptr, &op::less_than, &b);
95+
96+
fixture.some_char_ptr = &a;
97+
assert(expr->evaluate(fixture));
98+
99+
fixture.some_char_ptr = &a_too;
100+
assert(expr->evaluate(fixture));
101+
102+
fixture.some_char_ptr = &b;
103+
assert(!expr->evaluate(fixture));
104+
105+
fixture.some_char_ptr = &b_too;
106+
assert(!expr->evaluate(fixture));
107+
108+
fixture.some_char_ptr = &c;
109+
assert(!expr->evaluate(fixture));
110+
111+
fixture.some_char_ptr = &c_too;
112+
assert(!expr->evaluate(fixture));
113+
}
114+
115+
// test greater_than
116+
{
117+
// make an expression : given char* > 'b'
118+
auto expr = make_expr(&test_fixture::some_char_ptr, &op::greater_than, &b);
119+
120+
fixture.some_char_ptr = &a;
121+
assert(!expr->evaluate(fixture));
122+
123+
fixture.some_char_ptr = &a_too;
124+
assert(!expr->evaluate(fixture));
125+
126+
fixture.some_char_ptr = &b;
127+
assert(!expr->evaluate(fixture));
128+
129+
fixture.some_char_ptr = &b_too;
130+
assert(!expr->evaluate(fixture));
131+
132+
fixture.some_char_ptr = &c;
133+
assert(expr->evaluate(fixture));
134+
135+
fixture.some_char_ptr = &c_too;
136+
assert(expr->evaluate(fixture));
137+
}
36138
}

0 commit comments

Comments
 (0)