From a17c85b0e1a39fa6eb3c5ebb8e86b72011df5bcd Mon Sep 17 00:00:00 2001 From: apathy Date: Fri, 6 Jan 2017 15:50:20 -0600 Subject: [PATCH] Moved tests to dedicated dir --- src/stationapi/CMakeLists.txt | 7 -- tests/CMakeLists.txt | 5 +- tests/stationapi/Serialization_Tests.cpp | 99 ++++++++++++++++++++++++ tests/stationapi/StringUtils_Tests.cpp | 43 ++++++++++ 4 files changed, 146 insertions(+), 8 deletions(-) create mode 100644 tests/stationapi/Serialization_Tests.cpp create mode 100644 tests/stationapi/StringUtils_Tests.cpp diff --git a/src/stationapi/CMakeLists.txt b/src/stationapi/CMakeLists.txt index 6b93bbc..361ba1e 100644 --- a/src/stationapi/CMakeLists.txt +++ b/src/stationapi/CMakeLists.txt @@ -17,10 +17,3 @@ target_include_directories(stationapi PUBLIC ${PROJECT_SOURCE_DIR}/externals/udplibrary ${Boost_INCLUDE_DIRS} ${SQLite3_INCLUDE_DIR}) - -add_executable(stationapi_tests - Serialization_Tests.cpp - StringUtils_Tests.cpp) - -target_link_libraries(stationapi_tests - stationapi) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cdf669d..16d3794 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,7 +2,10 @@ include_directories(${PROJECT_SOURCE_DIR}/externals/catch ${PROJECT_SOURCE_DIR}/src) add_executable(stationapi_tests - main.cpp) + main.cpp + + stationapi/Serialization_Tests.cpp + stationapi/StringUtils_Tests.cpp) target_link_libraries(stationapi_tests stationapi) diff --git a/tests/stationapi/Serialization_Tests.cpp b/tests/stationapi/Serialization_Tests.cpp new file mode 100644 index 0000000..4433b86 --- /dev/null +++ b/tests/stationapi/Serialization_Tests.cpp @@ -0,0 +1,99 @@ + +#include "catch.hpp" + +#include "Serialization.hpp" + +SCENARIO("integer serialization", "[serialization]") { + GIVEN("an initialized 32bit signed integer and a binary stream") { + int32_t signedInt = -8; + std::stringstream bs(std::ios_base::out | std::ios_base::in | std::ios_base::binary); + + WHEN("the stream has the integer written to it") { + write(bs, signedInt); + + THEN("the serialized output size is increased by 4") { + REQUIRE(bs.str().length() == 4); + } + + AND_THEN("the serialized output is little-endian") { + auto str = bs.str(); + REQUIRE(str[0] == (char)0xF8); + REQUIRE(str[1] == (char)0xFF); + REQUIRE(str[2] == (char)0xFF); + REQUIRE(str[3] == (char)0xFF); + } + } + } + + GIVEN("a binary stream containing a serialized 32bit signed integer") { + int32_t signedInt = -8; + std::stringstream bs(std::ios_base::out | std::ios_base::in | std::ios_base::binary); + write(bs, signedInt); + + WHEN("the stream has the integer read from it") { + auto testInt = read(bs); + + THEN("the value read is the value expected") { + REQUIRE(testInt == signedInt); + } + } + + AND_WHEN("the stream is read into an existing integer variable") { + int32_t testInt = 0; + read(bs, testInt); + + THEN("the value read is the value expected") { + REQUIRE(testInt == signedInt); + } + } + } +} + +SCENARIO("string serialization", "[serialization]") { + GIVEN("an initialized ascii string and a binary stream") { + std::string asciiStr = "Some string value"; + std::stringstream bs(std::ios_base::out | std::ios_base::in | std::ios_base::binary); + + WHEN("the stream has an ascii string written to it") { + write(bs, asciiStr); + + THEN("the serialized output size is increased by the length of the string, plus a uint16_t string length") { + REQUIRE(bs.str().length() == asciiStr.length() + sizeof(uint16_t)); + } + + AND_THEN("the output contains a uint16_t string length, followed by the ascii string") { + auto length = peekAt(bs, 0); + REQUIRE(length == asciiStr.length()); + + auto str = bs.str(); + + REQUIRE(str[2] == asciiStr[0]); + REQUIRE(str[18] == asciiStr[16]); + } + } + } + + GIVEN("a binary string containing a serialized ascii string") { + std::string asciiStr = "Some string value"; + std::stringstream bs(std::ios_base::out | std::ios_base::in | std::ios_base::binary); + write(bs, asciiStr); + + WHEN("the stream has the string read from it") { + auto testStr = read(bs); + + THEN("the value re ad is the value expected") { + REQUIRE(testStr.compare(asciiStr) == 0); + } + } + + AND_WHEN("the stream is read into an existing string variable") { + std::string testStr; + read(bs, testStr); + + THEN("the value read is the value expected") { + REQUIRE(testStr.compare(asciiStr) == 0); + } + } + } +} + diff --git a/tests/stationapi/StringUtils_Tests.cpp b/tests/stationapi/StringUtils_Tests.cpp new file mode 100644 index 0000000..722435b --- /dev/null +++ b/tests/stationapi/StringUtils_Tests.cpp @@ -0,0 +1,43 @@ + +#include "catch.hpp" + +#include "StringUtils.hpp" + +SCENARIO("string widths can be converted to and from 8 and 16 bits", "[strings]") { + GIVEN("a narrow string initialized with text") { + std::string narrowStr = "Some string text here."; + + REQUIRE(narrowStr.length() == 22); + + WHEN("the width is converted to wide") { + auto wideStr = ToWideString(narrowStr); + + THEN("the length of the new string is the same as the narrow string") { + REQUIRE(wideStr.length() == narrowStr.length()); + } + + AND_THEN("the contents of the new string is equivalent to the narrow string") { + REQUIRE(wideStr.compare(u"Some string text here.") == 0); + } + } + + } + + GIVEN("a wide string initialized with text") { + std::u16string wideStr = u"Some string text here."; + + REQUIRE(wideStr.length() == 22); + + WHEN("the width is converted to narrow") { + auto narrowStr = FromWideString(wideStr); + + THEN("the length of the new string is the same as the wide string") { + REQUIRE(narrowStr.length() == wideStr.length()); + } + + AND_THEN("the contents of the new string is equivalent to the wide string") { + REQUIRE(narrowStr.compare("Some string text here.") == 0); + } + } + } +}