Skip to content

Commit 2537a19

Browse files
committed
adds test for user-defined operator
1 parent 0216ed4 commit 2537a19

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

tests/expression_tree.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <attwoodn/expression_tree.hpp>
22
#include "test_utils.hpp"
3+
#include <functional>
34
#include <cassert>
45

56
using namespace attwoodn::expression_tree;
@@ -276,5 +277,48 @@ void test_copied_expression_tree() {
276277
}
277278

278279
void test_user_defined_operator() {
280+
auto is_small_packet_payload = [](const packet_payload& incoming, const packet_payload&) -> bool {
281+
if(incoming.error_code == 0 && incoming.checksum_ok && incoming.payload_size() <= 10) {
282+
return true;
283+
}
284+
return false;
285+
};
286+
287+
// only accept small, non-errored data packets from Jim.
288+
// evaluate packet contents using the user-defined lambda operator defined above
289+
expression_tree<data_packet> expr {
290+
make_expr(&data_packet::sender_name, op::equals, std::string("Jim"))
291+
->AND(make_expr(&data_packet::payload, is_small_packet_payload, packet_payload()))
292+
};
293+
294+
data_packet incoming_packet;
295+
296+
// Jim sends a small, non-errored data packet
297+
incoming_packet.sender_name = "Jim";
298+
incoming_packet.payload.checksum_ok = true;
299+
incoming_packet.payload.data = "hello!";
300+
incoming_packet.payload.error_code = 0;
301+
assert(expr.evaluate(incoming_packet)); // passes evaluation
302+
303+
// Pam sends the same packet payload
304+
incoming_packet.sender_name = "Pam";
305+
assert(!expr.evaluate(incoming_packet)); // fails evaluation. No messages from Pam are accepted (sorry Pam)
306+
307+
// Jim sends a packet with a bad checksum
308+
incoming_packet.sender_name = "Jim";
309+
incoming_packet.payload.checksum_ok = false;
310+
assert(!expr.evaluate(incoming_packet)); // fails evaluation. The packet was from Jim, but the checksum was bad
311+
312+
// Jim sends a packet whose payload is too big
313+
incoming_packet.payload.checksum_ok = true;
314+
incoming_packet.payload.data = "Boy do I have a long story for you - so I was talking to Pam ...";
315+
assert(!expr.evaluate(incoming_packet)); // fails evaluation. The packet's payload was too big. Give me the TLDR next time, Jim
316+
317+
// Jim sends a small, rude packet
318+
incoming_packet.payload.data = "Dwight sux";
319+
assert(expr.evaluate(incoming_packet)); // passes evaluation. The packet's payload was the right size this time
279320

321+
// Jim sends a packet has an error code
322+
incoming_packet.payload.error_code = 404;
323+
assert(!expr.evaluate(incoming_packet)); // fails evaluation. The packet's payload had an error code
280324
}

tests/test_utils.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ struct test_fixture {
1818
};
1919

2020
struct packet_payload {
21-
const uint16_t error_code;
21+
uint16_t error_code;
2222
std::string data;
23+
bool checksum_ok;
2324

2425
uint64_t payload_size() const {
2526
return data.size();
2627
}
2728
};
2829

2930
class data_packet {
30-
const uint32_t sender_id;
31-
packet_payload payload;
31+
public:
32+
std::string sender_name;
33+
packet_payload payload;
3234
};

0 commit comments

Comments
 (0)