Moved tests to dedicated dir

This commit is contained in:
apathy
2017-01-06 15:50:20 -06:00
parent e0fb362be0
commit a17c85b0e1
4 changed files with 146 additions and 8 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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<int32_t>(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<uint16_t>(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<std::string>(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);
}
}
}
}

View File

@@ -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);
}
}
}
}