#define BOOST_TEST_MODULE RawDataReader #include #include #include #include "helpers.h" #include #include #include using namespace MyGrate; using Bytes = std::vector; template using BytesTo = std::tuple; BOOST_DATA_TEST_CASE(read_packedinteger, boost::unit_test::data::make>({ {{0x00, 0xb2, 0x2, 0}, 0}, {{0x01, 0xb2, 0x2, 0}, 1}, {{0xfa, 0xb2, 0x2, 0}, 0xfa}, {{0xfc, 0xb2, 0x2, 0}, 0x02b2}, {{0xfd, 0xb2, 0x2, 0}, 690}, {{0xfe, 0xb2, 0x2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 690}, }), bytes, out) { RawDataReader rdr {bytes.data(), bytes.size()}; BOOST_CHECK_EQUAL((rdr.readValue()), out); } BOOST_DATA_TEST_CASE(invalid_packed_ints, boost::unit_test::data::make({ {0xFF, 0x78, 0x38, 0x1a, 0x0b}, {0xFB, 0x78, 0x38, 0x1a, 0x0b}, }), bytes) { RawDataReader rdr {bytes.data(), bytes.size()}; BOOST_CHECK_THROW(rdr.readValue(), std::domain_error); } BOOST_DATA_TEST_CASE(read_overflow, boost::unit_test::data::make({ {0x00, 0xFB, 0x12, 0x00}, }), bytes) { RawDataReader rdr {bytes.data(), bytes.size()}; rdr.discard(1); BOOST_CHECK_EQUAL(rdr.readValue(), 0x12FB); BOOST_CHECK_THROW(rdr.readValue(), std::range_error); } /* BOOST_DATA_TEST_CASE(read_overflow_string, boost::unit_test::data::make({ {0x04, 'a', 'b', 'c'}, }), bytes) { RawDataReader rdr {bytes.data(), bytes.size()}; // BOOST_CHECK_THROW(rdr.readValue>(), std::range_error); } BOOST_DATA_TEST_CASE(read_datetime2, boost::unit_test::data::make>({ {{0x99, 0x78, 0x38, 0x1a, 0x0b}, make_tm(2006, 2, 28, 1, 40, 11)}, }), bytes, exp) { RawDataReader rdr {bytes.data(), bytes.size()}; (void)exp; // BOOST_CHECK_EQUAL((rdr.readValue()), out); } BOOST_DATA_TEST_CASE(read_varchars1, boost::unit_test::data::make>({ {{0x00}, ""}, {{0x01, 'A'}, "A"}, {{0x03, 'a', 'b', 'c'}, "abc"}, {{0x10, '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}, "0123456789abcdef"}, }), bytes, exp) { RawDataReader rdr {bytes.data(), bytes.size()}; (void)exp; // BOOST_CHECK_EQUAL((rdr.readValue>()), exp); } BOOST_DATA_TEST_CASE(read_varchars2, boost::unit_test::data::make>({ {{0x00, 0x00}, ""}, {{0x01, 0x00, 'A'}, "A"}, {{0x03, 0x00, 'a', 'b', 'c'}, "abc"}, {{0x10, 0x00, '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}, "0123456789abcdef"}, }), bytes, exp) { RawDataReader rdr {bytes.data(), bytes.size()}; (void)exp; // BOOST_CHECK_EQUAL((rdr.readValue>()), exp); } */ BOOST_DATA_TEST_CASE(read_bytes, boost::unit_test::data::make({ {}, {0x01}, {0x00, 0x01, 0x02}, {0xFF, 0xFE, 0xFD}, {0xFF, 0xFE, 0xFD, 0x00, 0x01, 0x02}, }), bytes) { RawDataReader rdr {bytes.data(), bytes.size()}; const auto out {rdr.viewValue(bytes.size())}; BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), bytes.begin(), bytes.end()); } BOOST_DATA_TEST_CASE(read_bytes_overflow, boost::unit_test::data::make({ {}, {0x01}, {0x00, 0x01, 0x02}, {0xFF, 0xFE, 0xFD}, {0xFF, 0xFE, 0xFD, 0x00, 0x01, 0x02}, }), bytes) { RawDataReader rdr {bytes.data(), bytes.size()}; BOOST_CHECK_THROW(rdr.viewValue(bytes.size() + 1), std::range_error); } BOOST_DATA_TEST_CASE(read_field_type, boost::unit_test::data::make>({ {{0x00}, MYSQL_TYPE_DECIMAL}, {{0x04}, MYSQL_TYPE_FLOAT}, {{0xFF}, MYSQL_TYPE_GEOMETRY}, }), bytes, exp) { RawDataReader rdr {bytes.data(), bytes.size()}; BOOST_CHECK_EQUAL(rdr.readValue(1), exp); } BOOST_AUTO_TEST_CASE(rdr_from_MARIADB_STRING) { char buf[5] = "test"; MARIADB_STRING str {buf, strlen(buf)}; RawDataReader rdr {str}; BOOST_CHECK_EQUAL(rdr.viewValue(4), "test"); } using SimpleTypes = boost::mpl::list; BOOST_AUTO_TEST_CASE_TEMPLATE(rdr_read_simple, T, SimpleTypes) { RawDataReader rdr {"don't care, some bytes", 20}; rdr.readValue(); }