Skip to content

Commit 14ea9f0

Browse files
committed
adds support for const member variable types, and adds new unit test cases for const strings
1 parent e13f115 commit 14ea9f0

File tree

2 files changed

+141
-13
lines changed

2 files changed

+141
-13
lines changed

include/attwoodn/expression_tree.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace attwoodn::expression_tree {
77
namespace op {
88

99
template<typename T>
10-
inline bool less_than(T a, T b) {
10+
inline bool less_than(const T a, const T b) {
1111
return a < b;
1212
}
1313

@@ -18,7 +18,7 @@ namespace attwoodn::expression_tree {
1818
}
1919

2020
template<typename T>
21-
inline bool greater_than(T a, T b) {
21+
inline bool greater_than(const T a, const T b) {
2222
return a > b;
2323
}
2424

@@ -29,7 +29,7 @@ namespace attwoodn::expression_tree {
2929
}
3030

3131
template<typename T>
32-
inline bool equals(T a, T b) {
32+
inline bool equals(const T a, const T b) {
3333
return a == b;
3434
}
3535

@@ -41,7 +41,7 @@ namespace attwoodn::expression_tree {
4141
}
4242

4343
template<typename T>
44-
inline bool not_equals(T a, T b) {
44+
inline bool not_equals(const T a, const T b) {
4545
return a != b;
4646
}
4747

@@ -164,7 +164,7 @@ namespace attwoodn::expression_tree {
164164
/**
165165
* @brief Constructor that accepts a reference to a member variable of Obj
166166
*/
167-
expression_tree_leaf_node(CompValue Obj::* obj_mem_var, Op op, CompValue comp_value)
167+
expression_tree_leaf_node(const CompValue Obj::* obj_mem_var, Op op, CompValue comp_value)
168168
: member_var_(obj_mem_var),
169169
logical_op_(op),
170170
comp_value_(comp_value) {}
@@ -209,7 +209,7 @@ namespace attwoodn::expression_tree {
209209

210210
private:
211211
CompValue (Obj::* member_func_)() const = nullptr;
212-
CompValue Obj::* member_var_ = nullptr;
212+
const CompValue Obj::* member_var_ = nullptr;
213213
Op logical_op_;
214214
CompValue comp_value_;
215215
};
@@ -219,13 +219,13 @@ namespace attwoodn::expression_tree {
219219
template<class T> struct type_id{typedef T type;};
220220

221221
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>
222+
typename std::enable_if<std::is_convertible<CompValue, CompValue*>::value, int>::type = 0>
223223
node::expression_tree_leaf_node<Obj, Op, CompValue>* make_expr( CompValue Obj::* member_var, Op op, CompValue comp_value ) {
224224
return new node::expression_tree_leaf_node<Obj, Op, CompValue>( member_var, op, comp_value );
225225
}
226226

227227
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 ) {
228+
node::expression_tree_leaf_node<Obj, Op, CompValue>* make_expr( const CompValue Obj::* member_var, Op op, CompValue comp_value ) {
229229
return new node::expression_tree_leaf_node<Obj, Op, CompValue>( member_var, op, comp_value );
230230
}
231231
}

tests/expression_tree_leaf_node.cpp

Lines changed: 133 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using namespace attwoodn::expression_tree;
66

77
struct test_fixture {
88
std::string some_string;
9+
const std::string some_const_string = "this IS 4 T3s7 $tRing ";
910
char* some_char_ptr;
1011
};
1112

@@ -21,13 +22,140 @@ int main(int argc, char** argv) {
2122

2223
void test_string_evaluation() {
2324
test_fixture fixture;
24-
fixture.some_string = "hello world!";
2525

26-
auto node = make_expr(&test_fixture::some_string, &op::equals, std::string("hello world!"));
27-
assert(node->evaluate(fixture));
26+
// test const string
27+
{
28+
// test equals
29+
{
30+
auto expr1 = make_expr(&test_fixture::some_const_string, &op::equals, std::string("hello world!"));
31+
assert(!expr1->evaluate(fixture));
32+
33+
auto expr2 = make_expr(&test_fixture::some_const_string, &op::equals, std::string("h"));
34+
assert(!expr2->evaluate(fixture));
35+
36+
auto expr3 = make_expr(&test_fixture::some_const_string, &op::equals, std::string("t"));
37+
assert(!expr3->evaluate(fixture));
38+
39+
auto expr4 = make_expr(&test_fixture::some_const_string, &op::equals, std::string("this "));
40+
assert(!expr4->evaluate(fixture));
41+
42+
auto expr5 = make_expr(&test_fixture::some_const_string, &op::equals, std::string("this IS 4 T3s7 $tRing "));
43+
assert(!expr5->evaluate(fixture));
44+
45+
auto expr6 = make_expr(&test_fixture::some_const_string, &op::equals, std::string("this IS 4 T3s7 $tRing"));
46+
assert(!expr6->evaluate(fixture));
47+
48+
auto expr7 = make_expr(&test_fixture::some_const_string, &op::equals, std::string("this IS 4 T3s7 $tRing "));
49+
assert(expr7->evaluate(fixture));
50+
51+
auto expr8 = make_expr(&test_fixture::some_const_string, &op::equals, fixture.some_const_string);
52+
assert(expr8->evaluate(fixture));
53+
}
54+
55+
// test not_equals
56+
{
57+
auto expr1 = make_expr(&test_fixture::some_const_string, &op::not_equals, std::string("hello world!"));
58+
assert(expr1->evaluate(fixture));
59+
60+
auto expr2 = make_expr(&test_fixture::some_const_string, &op::not_equals, std::string("h"));
61+
assert(expr2->evaluate(fixture));
62+
63+
auto expr3 = make_expr(&test_fixture::some_const_string, &op::not_equals, std::string("t"));
64+
assert(expr3->evaluate(fixture));
65+
66+
auto expr4 = make_expr(&test_fixture::some_const_string, &op::not_equals, std::string("this "));
67+
assert(expr4->evaluate(fixture));
68+
69+
auto expr5 = make_expr(&test_fixture::some_const_string, &op::not_equals, std::string("this IS 4 T3s7 $tRing "));
70+
assert(expr5->evaluate(fixture));
71+
72+
auto expr6 = make_expr(&test_fixture::some_const_string, &op::not_equals, std::string("this IS 4 T3s7 $tRing"));
73+
assert(expr6->evaluate(fixture));
74+
75+
auto expr7 = make_expr(&test_fixture::some_const_string, &op::not_equals, std::string("this IS 4 T3s7 $tRing "));
76+
assert(!expr7->evaluate(fixture));
77+
78+
auto expr8 = make_expr(&test_fixture::some_const_string, &op::not_equals, fixture.some_const_string);
79+
assert(!expr8->evaluate(fixture));
80+
}
81+
82+
// test less_than
83+
{
84+
auto expr1 = make_expr(&test_fixture::some_const_string, &op::less_than, std::string("u"));
85+
assert(expr1->evaluate(fixture));
86+
87+
auto expr2 = make_expr(&test_fixture::some_const_string, &op::less_than, std::string("z"));
88+
assert(expr2->evaluate(fixture));
89+
90+
auto expr3 = make_expr(&test_fixture::some_const_string, &op::less_than, std::string("this IS 4 T3s7 $tRing "));
91+
assert(expr3->evaluate(fixture));
92+
93+
auto expr4 = make_expr(&test_fixture::some_const_string, &op::less_than, std::string("1234567890"));
94+
assert(!expr4->evaluate(fixture));
95+
96+
auto expr5 = make_expr(&test_fixture::some_const_string, &op::less_than, std::string("A"));
97+
assert(!expr5->evaluate(fixture));
2898

29-
fixture.some_string = "hey, world!";
30-
assert(!node->evaluate(fixture));
99+
auto expr6 = make_expr(&test_fixture::some_const_string, &op::less_than, std::string("this "));
100+
assert(!expr6->evaluate(fixture));
101+
102+
auto expr7 = make_expr(&test_fixture::some_const_string, &op::less_than, std::string("stuff"));
103+
assert(!expr7->evaluate(fixture));
104+
105+
auto expr8 = make_expr(&test_fixture::some_const_string, &op::less_than, std::string("abcdefghijklmnopqrstuvwxyz"));
106+
assert(!expr8->evaluate(fixture));
107+
108+
auto expr9 = make_expr(&test_fixture::some_const_string, &op::less_than, std::string("this IS 4 T3s7 $tRing "));
109+
assert(!expr9->evaluate(fixture));
110+
111+
auto expr10 = make_expr(&test_fixture::some_const_string, &op::less_than, fixture.some_const_string);
112+
assert(!expr10->evaluate(fixture));
113+
}
114+
115+
// test greater_than
116+
{
117+
auto expr1 = make_expr(&test_fixture::some_const_string, &op::greater_than, std::string("u"));
118+
assert(!expr1->evaluate(fixture));
119+
120+
auto expr2 = make_expr(&test_fixture::some_const_string, &op::greater_than, std::string("z"));
121+
assert(!expr2->evaluate(fixture));
122+
123+
auto expr3 = make_expr(&test_fixture::some_const_string, &op::greater_than, std::string("this IS 4 T3s7 $tRing "));
124+
assert(!expr3->evaluate(fixture));
125+
126+
auto expr4 = make_expr(&test_fixture::some_const_string, &op::greater_than, std::string("1234567890"));
127+
assert(expr4->evaluate(fixture));
128+
129+
auto expr5 = make_expr(&test_fixture::some_const_string, &op::greater_than, std::string("A"));
130+
assert(expr5->evaluate(fixture));
131+
132+
auto expr6 = make_expr(&test_fixture::some_const_string, &op::greater_than, std::string("this "));
133+
assert(expr6->evaluate(fixture));
134+
135+
auto expr7 = make_expr(&test_fixture::some_const_string, &op::greater_than, std::string("stuff"));
136+
assert(expr7->evaluate(fixture));
137+
138+
auto expr8 = make_expr(&test_fixture::some_const_string, &op::greater_than, std::string("abcdefghijklmnopqrstuvwxyz"));
139+
assert(expr8->evaluate(fixture));
140+
141+
auto expr9 = make_expr(&test_fixture::some_const_string, &op::greater_than, std::string("this IS 4 T3s7 $tRing "));
142+
assert(!expr9->evaluate(fixture));
143+
144+
auto expr10 = make_expr(&test_fixture::some_const_string, &op::greater_than, fixture.some_const_string);
145+
assert(!expr10->evaluate(fixture));
146+
}
147+
}
148+
149+
// test non-const string
150+
{
151+
fixture.some_string = "hello world!";
152+
153+
auto node = make_expr(&test_fixture::some_string, &op::equals, std::string("hello world!"));
154+
assert(node->evaluate(fixture));
155+
156+
fixture.some_string = "hey, world!";
157+
assert(!node->evaluate(fixture));
158+
}
31159
}
32160

33161
void test_char_ptr_evaluation() {

0 commit comments

Comments
 (0)