From 19f7cd133401aec6498f77a2047f18652786feb8 Mon Sep 17 00:00:00 2001 From: Joshua Scott Date: Fri, 30 Mar 2018 21:12:54 +0100 Subject: [PATCH] test: Test Record serialization/deserialization and NOXFER fields --- test/CMakeLists.txt | 2 + test/samples/dml.bin | Bin 0 -> 50 bytes test/src/unit-dml.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 test/samples/dml.bin diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 80846cf..1eaf2d4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,6 +2,8 @@ set(CATCH_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/third_party) add_library(Catch INTERFACE) target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR}) +file(COPY "samples" DESTINATION "${PROJECT_BINARY_DIR}/test") + file(GLOB files "src/unit-*.cpp") foreach (file ${files}) get_filename_component(file_basename ${file} NAME_WE) diff --git a/test/samples/dml.bin b/test/samples/dml.bin new file mode 100644 index 0000000000000000000000000000000000000000..b764ea766faa2c082847ff0c1eae6b129a616cb8 GIT binary patch literal 50 ucmZ3rYBvzxJp;ik3?Z(;AwZJBl_3~Nrlm;M3FDj2c= literal 0 HcmV?d00001 diff --git a/test/src/unit-dml.cpp b/test/src/unit-dml.cpp index e6e840d..53ca691 100644 --- a/test/src/unit-dml.cpp +++ b/test/src/unit-dml.cpp @@ -1,6 +1,7 @@ #define CATCH_CONFIG_MAIN #include #include +#include using namespace ki::dml; @@ -146,6 +147,13 @@ TEST_CASE("Field Serialization", "[dml]") REQUIRE(ss.str() == "\xFF\xEE\xDD\xCC\xBB\xAA\x99\x88"); } + SECTION("Non-transferable Fields") + { + record->add_field("TestNOXFER", false)->set_value("Hello, world!"); + record->write_to(ss); + REQUIRE(ss.str() == ""); + } + delete record; } @@ -269,3 +277,78 @@ TEST_CASE("Field Deserialization", "[dml]") delete record; } + +TEST_CASE("Record Serialization", "[dml]") +{ + auto *record = new Record(); + std::stringstream ss; + + record->add_field("TestByt")->set_value(0xAA); + record->add_field("TestUByt")->set_value(0xAA); + record->add_field("TestShrt")->set_value(0xAABB); + record->add_field("TestUShrt")->set_value(0xAABB); + record->add_field("TestInt")->set_value(0xAABBCCDD); + record->add_field("TestUInt")->set_value(0xAABBCCDD); + record->add_field("TestStr")->set_value("TEST"); + record->add_field("TestWStr")->set_value(u"TEST"); + record->add_field("TestFlt")->set_value(152.4f); + record->add_field("TestDbl")->set_value(152.4); + record->add_field("TestGid")->set_value(0x8899AABBCCDDEEFF); + record->add_field("TestNOXFER", false)->set_value(0xAA); + record->write_to(ss); + + std::ifstream ifs("samples/dml.bin", std::ios::binary|std::ios::ate); + if (ifs.is_open()) + { + size_t size = ifs.tellg(); + char *buffer = new char[size]; + ifs.seekg(0, std::ios::beg); + ifs.read(buffer, size); + ifs.close(); + + REQUIRE(strcmp(buffer, ss.str().c_str()) == 0); + delete[] buffer; + } + else + FAIL(); + + delete record; +} + +TEST_CASE("Record Deserialization", "[dml]") +{ + auto *record = new Record(); + std::ifstream ifs("samples/dml.bin", std::ios::binary); + if (!ifs.is_open()) + FAIL(); + + auto *test_byt = record->add_field("TestByt"); + auto *test_ubyt = record->add_field("TestUByt"); + auto *test_shrt = record->add_field("TestShrt"); + auto *test_ushrt = record->add_field("TestUShrt"); + auto *test_int = record->add_field("TestInt"); + auto *test_uint = record->add_field("TestUInt"); + auto *test_str = record->add_field("TestStr"); + auto *test_wstr = record->add_field("TestWStr"); + auto *test_flt = record->add_field("TestFlt"); + auto *test_dbl = record->add_field("TestDbl"); + auto *test_gid = record->add_field("TestGid"); + auto *test_noxfer = record->add_field("TestNOXFER", false); + record->read_from(ifs); + ifs.close(); + + REQUIRE((UBYT)test_byt->get_value() == 0xAA); + REQUIRE((UBYT)test_ubyt->get_value() == 0xAA); + REQUIRE((USHRT)test_shrt->get_value() == 0xAABB); + REQUIRE(test_ushrt->get_value() == 0xAABB); + REQUIRE(test_int->get_value() == 0xAABBCCDD); + REQUIRE(test_uint->get_value() == 0xAABBCCDD); + REQUIRE(test_str->get_value() == "TEST"); + REQUIRE(test_wstr->get_value() == u"TEST"); + REQUIRE(test_flt->get_value() == 152.4f); + REQUIRE(test_dbl->get_value() == 152.4); + REQUIRE(test_gid->get_value() == 0x8899AABBCCDDEEFF); + REQUIRE(test_noxfer->get_value() == 0x0); + + delete record; +}