1 /* 2 * Copyright (C) 2009-2020 Codership Oy <info@codership.com> 3 */ 4 5 #ifndef GCOMM_CHECK_TEMPL_HPP 6 #define GCOMM_CHECK_TEMPL_HPP 7 8 #include "gcomm/types.hpp" 9 #include "gcomm/transport.hpp" 10 #include <check.h> 11 12 #include <deque> 13 #include <algorithm> 14 15 namespace gcomm 16 { 17 18 19 template<class T> check_serialization(const T & c,const size_t expected_size,const T & default_c)20 void check_serialization(const T& c, const size_t expected_size, 21 const T& default_c) 22 { 23 24 ck_assert_msg(c.serial_size() == expected_size, 25 "size = %lu expected = %lu", 26 c.serial_size(), expected_size); 27 gu::byte_t* buf = new gu::byte_t[expected_size + 7]; 28 size_t ret; 29 // Check that what is written gets also read 30 try 31 { 32 (void)c.serialize(buf, expected_size, 1); 33 std::ostringstream os; 34 os << c; 35 ck_abort_msg("exception not thrown for %s", os.str().c_str()); 36 } 37 catch (gu::Exception& e) 38 { 39 // OK 40 } 41 ck_assert(c.serialize(buf, expected_size, 0) == expected_size); 42 43 T c2(default_c); 44 45 // Commented out. This test happened to work because default 46 // protocol version for messages was zero and if the second 47 // byte of the buffer contained something else, exception was 48 // thrown. Now that the version can be different from zero, 49 // the outcome of this check depends on message structure. 50 // try 51 // { 52 // size_t res(c2.unserialize(buf, expected_size, 1)); 53 // std::ostringstream os; 54 // os << c; 55 // ck_abort_msg("exception not thrown for %s, result %zu expected %zu", 56 // os.str().c_str(), res, expected_size); 57 // } 58 // catch (gu::Exception& e) 59 // { 60 // // OK 61 // } 62 63 ret = c2.unserialize(buf, expected_size, 0); 64 ck_assert_msg(ret == expected_size, 65 "expected %zu ret %zu", expected_size, ret); 66 if ((c == c2) == false) 67 { 68 log_warn << "\n\t" << c << " !=\n\t" << c2; 69 } 70 ck_assert(c == c2); 71 72 // Check that read/write return offset properly 73 74 ck_assert(c.serialize(buf, expected_size + 7, 5) == expected_size + 5); 75 ck_assert(c2.unserialize(buf, expected_size + 7, 5) == expected_size + 5); 76 77 ck_assert(c == c2); 78 79 delete[] buf; 80 } 81 82 83 84 85 86 87 } // namespace gcomm 88 89 #endif // CHECK_TEMPL_HPP 90