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 0000000..b764ea7 Binary files /dev/null and b/test/samples/dml.bin differ 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; +}