11#include < attwoodn/expression_tree.hpp>
22#include " test_utils.hpp"
3+ #include < functional>
34#include < cassert>
45
56using namespace attwoodn ::expression_tree;
@@ -276,5 +277,48 @@ void test_copied_expression_tree() {
276277}
277278
278279void 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}
0 commit comments