mirror of
https://github.com/Rogiel/PacketBuffer
synced 2025-12-06 00:13:04 +00:00
Initial commit
This commit is contained in:
397
tests/Packer.cpp
Normal file
397
tests/Packer.cpp
Normal file
@@ -0,0 +1,397 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Rogiel Sulzbach
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of Rogiel Sulzbach nor the names of contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <catch.hpp>
|
||||
#include <sstream>
|
||||
|
||||
#include <PacketBuffer/PacketBuffer.h>
|
||||
|
||||
std::string string_to_hex(const std::string& input) {
|
||||
static const char* const lut = "0123456789ABCDEF";
|
||||
size_t len = input.length();
|
||||
|
||||
std::string output;
|
||||
output.reserve(2 * len);
|
||||
for(size_t i = 0; i < len; ++i) {
|
||||
const unsigned char c = input[i];
|
||||
output.push_back(lut[c >> 4]);
|
||||
output.push_back(lut[c & 15]);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
TEST_CASE("Packer", "[packer]") {
|
||||
|
||||
std::stringstream ss;
|
||||
|
||||
SECTION("little endian") {
|
||||
PacketBuffer::Packer<std::ostream, boost::endian::order::little> packer(ss);
|
||||
|
||||
SECTION("uint8_t") {
|
||||
SECTION("min") {
|
||||
packer.pack(std::numeric_limits<uint8_t>::min());
|
||||
CHECK(string_to_hex(ss.str()) == "00");
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
packer.pack(uint8_t(1));
|
||||
CHECK(string_to_hex(ss.str()) == "01");
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
packer.pack(std::numeric_limits<uint8_t>::max());
|
||||
CHECK(string_to_hex(ss.str()) == "FF");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("int8_t") {
|
||||
SECTION("min") {
|
||||
packer.pack(std::numeric_limits<int8_t>::min());
|
||||
CHECK(string_to_hex(ss.str()) == "80");
|
||||
}
|
||||
|
||||
SECTION("zero") {
|
||||
packer.pack(int8_t(0));
|
||||
CHECK(string_to_hex(ss.str()) == "00");
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
packer.pack(int8_t(1));
|
||||
CHECK(string_to_hex(ss.str()) == "01");
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
packer.pack(std::numeric_limits<int8_t>::max());
|
||||
CHECK(string_to_hex(ss.str()) == "7F");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("uint16_t") {
|
||||
SECTION("min") {
|
||||
packer.pack(std::numeric_limits<uint16_t>::min());
|
||||
CHECK(string_to_hex(ss.str()) == "0000");
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
packer.pack(uint16_t(1));
|
||||
CHECK(string_to_hex(ss.str()) == "0100");
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
packer.pack(std::numeric_limits<uint16_t>::max());
|
||||
CHECK(string_to_hex(ss.str()) == "FFFF");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("int16_t") {
|
||||
SECTION("min") {
|
||||
packer.pack(std::numeric_limits<int16_t>::min());
|
||||
CHECK(string_to_hex(ss.str()) == "0080");
|
||||
}
|
||||
|
||||
SECTION("zero") {
|
||||
packer.pack(int16_t(0));
|
||||
CHECK(string_to_hex(ss.str()) == "0000");
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
packer.pack(int16_t(1));
|
||||
CHECK(string_to_hex(ss.str()) == "0100");
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
packer.pack(std::numeric_limits<int16_t>::max());
|
||||
CHECK(string_to_hex(ss.str()) == "FF7F");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("uint32_t") {
|
||||
SECTION("min") {
|
||||
packer.pack(std::numeric_limits<uint32_t>::min());
|
||||
CHECK(string_to_hex(ss.str()) == "00000000");
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
packer.pack(uint32_t(1));
|
||||
CHECK(string_to_hex(ss.str()) == "01000000");
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
packer.pack(std::numeric_limits<uint32_t>::max());
|
||||
CHECK(string_to_hex(ss.str()) == "FFFFFFFF");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("int32_t") {
|
||||
SECTION("min") {
|
||||
packer.pack(std::numeric_limits<int32_t>::min());
|
||||
CHECK(string_to_hex(ss.str()) == "00000080");
|
||||
}
|
||||
|
||||
SECTION("zero") {
|
||||
packer.pack(int32_t(0));
|
||||
CHECK(string_to_hex(ss.str()) == "00000000");
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
packer.pack(int32_t(1));
|
||||
CHECK(string_to_hex(ss.str()) == "01000000");
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
packer.pack(std::numeric_limits<int32_t>::max());
|
||||
CHECK(string_to_hex(ss.str()) == "FFFFFF7F");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("uint64_t") {
|
||||
SECTION("min") {
|
||||
packer.pack(std::numeric_limits<uint64_t>::min());
|
||||
CHECK(string_to_hex(ss.str()) == "0000000000000000");
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
packer.pack(uint64_t(1));
|
||||
CHECK(string_to_hex(ss.str()) == "0100000000000000");
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
packer.pack(std::numeric_limits<uint64_t>::max());
|
||||
CHECK(string_to_hex(ss.str()) == "FFFFFFFFFFFFFFFF");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("int64_t") {
|
||||
SECTION("min") {
|
||||
packer.pack(std::numeric_limits<int64_t>::min());
|
||||
CHECK(string_to_hex(ss.str()) == "0000000000000080");
|
||||
}
|
||||
|
||||
SECTION("zero") {
|
||||
packer.pack(int64_t(0));
|
||||
CHECK(string_to_hex(ss.str()) == "0000000000000000");
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
packer.pack(int64_t(1));
|
||||
CHECK(string_to_hex(ss.str()) == "0100000000000000");
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
packer.pack(std::numeric_limits<int64_t>::max());
|
||||
CHECK(string_to_hex(ss.str()) == "FFFFFFFFFFFFFF7F");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("bool") {
|
||||
SECTION("true") {
|
||||
packer.pack(true);
|
||||
CHECK(string_to_hex(ss.str()) == "01");
|
||||
}
|
||||
|
||||
SECTION("false") {
|
||||
packer.pack(false);
|
||||
CHECK(string_to_hex(ss.str()) == "00");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("big endian") {
|
||||
PacketBuffer::Packer<std::ostream, boost::endian::order::big> packer(ss);
|
||||
|
||||
SECTION("uint8_t") {
|
||||
SECTION("min") {
|
||||
packer.pack(std::numeric_limits<uint8_t>::min());
|
||||
CHECK(string_to_hex(ss.str()) == "00");
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
packer.pack(uint8_t(1));
|
||||
CHECK(string_to_hex(ss.str()) == "01");
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
packer.pack(std::numeric_limits<uint8_t>::max());
|
||||
CHECK(string_to_hex(ss.str()) == "FF");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("int8_t") {
|
||||
SECTION("min") {
|
||||
packer.pack(std::numeric_limits<int8_t>::min());
|
||||
CHECK(string_to_hex(ss.str()) == "80");
|
||||
}
|
||||
|
||||
SECTION("zero") {
|
||||
packer.pack(int8_t(0));
|
||||
CHECK(string_to_hex(ss.str()) == "00");
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
packer.pack(int8_t(1));
|
||||
CHECK(string_to_hex(ss.str()) == "01");
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
packer.pack(std::numeric_limits<int8_t>::max());
|
||||
CHECK(string_to_hex(ss.str()) == "7F");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("uint16_t") {
|
||||
SECTION("min") {
|
||||
packer.pack(std::numeric_limits<uint16_t>::min());
|
||||
CHECK(string_to_hex(ss.str()) == "0000");
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
packer.pack(uint16_t(1));
|
||||
CHECK(string_to_hex(ss.str()) == "0001");
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
packer.pack(std::numeric_limits<uint16_t>::max());
|
||||
CHECK(string_to_hex(ss.str()) == "FFFF");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("int16_t") {
|
||||
SECTION("min") {
|
||||
packer.pack(std::numeric_limits<int16_t>::min());
|
||||
CHECK(string_to_hex(ss.str()) == "8000");
|
||||
}
|
||||
|
||||
SECTION("zero") {
|
||||
packer.pack(int16_t(0));
|
||||
CHECK(string_to_hex(ss.str()) == "0000");
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
packer.pack(int16_t(1));
|
||||
CHECK(string_to_hex(ss.str()) == "0001");
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
packer.pack(std::numeric_limits<int16_t>::max());
|
||||
CHECK(string_to_hex(ss.str()) == "7FFF");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("uint32_t") {
|
||||
SECTION("min") {
|
||||
packer.pack(std::numeric_limits<uint32_t>::min());
|
||||
CHECK(string_to_hex(ss.str()) == "00000000");
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
packer.pack(uint32_t(1));
|
||||
CHECK(string_to_hex(ss.str()) == "00000001");
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
packer.pack(std::numeric_limits<uint32_t>::max());
|
||||
CHECK(string_to_hex(ss.str()) == "FFFFFFFF");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("int32_t") {
|
||||
SECTION("min") {
|
||||
packer.pack(std::numeric_limits<int32_t>::min());
|
||||
CHECK(string_to_hex(ss.str()) == "80000000");
|
||||
}
|
||||
|
||||
SECTION("zero") {
|
||||
packer.pack(int32_t(0));
|
||||
CHECK(string_to_hex(ss.str()) == "00000000");
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
packer.pack(int32_t(1));
|
||||
CHECK(string_to_hex(ss.str()) == "00000001");
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
packer.pack(std::numeric_limits<int32_t>::max());
|
||||
CHECK(string_to_hex(ss.str()) == "7FFFFFFF");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("uint64_t") {
|
||||
SECTION("min") {
|
||||
packer.pack(std::numeric_limits<uint64_t>::min());
|
||||
CHECK(string_to_hex(ss.str()) == "0000000000000000");
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
packer.pack(uint64_t(1));
|
||||
CHECK(string_to_hex(ss.str()) == "0000000000000001");
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
packer.pack(std::numeric_limits<uint64_t>::max());
|
||||
CHECK(string_to_hex(ss.str()) == "FFFFFFFFFFFFFFFF");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("int64_t") {
|
||||
SECTION("min") {
|
||||
packer.pack(std::numeric_limits<int64_t>::min());
|
||||
CHECK(string_to_hex(ss.str()) == "8000000000000000");
|
||||
}
|
||||
|
||||
SECTION("zero") {
|
||||
packer.pack(int64_t(0));
|
||||
CHECK(string_to_hex(ss.str()) == "0000000000000000");
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
packer.pack(int64_t(1));
|
||||
CHECK(string_to_hex(ss.str()) == "0000000000000001");
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
packer.pack(std::numeric_limits<int64_t>::max());
|
||||
CHECK(string_to_hex(ss.str()) == "7FFFFFFFFFFFFFFF");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("bool") {
|
||||
SECTION("true") {
|
||||
packer.pack(true);
|
||||
CHECK(string_to_hex(ss.str()) == "01");
|
||||
}
|
||||
|
||||
SECTION("false") {
|
||||
packer.pack(false);
|
||||
CHECK(string_to_hex(ss.str()) == "00");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
113
tests/Serializer/Std/Array.cpp
Normal file
113
tests/Serializer/Std/Array.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Rogiel Sulzbach
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of Rogiel Sulzbach nor the names of contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <catch.hpp>
|
||||
#include <sstream>
|
||||
|
||||
#include <PacketBuffer/PacketBuffer.h>
|
||||
|
||||
namespace {
|
||||
std::string string_to_hex(const std::string& input) {
|
||||
static const char* const lut = "0123456789ABCDEF";
|
||||
size_t len = input.length();
|
||||
|
||||
std::string output;
|
||||
output.reserve(2 * len);
|
||||
for(size_t i = 0; i < len; ++i) {
|
||||
const unsigned char c = input[i];
|
||||
output.push_back(lut[c >> 4]);
|
||||
output.push_back(lut[c & 15]);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Serializer/Std/Array", "[serializer][std][array]") {
|
||||
|
||||
std::stringstream ss;
|
||||
PacketBuffer::Packer<std::ostream> packer(ss);
|
||||
PacketBuffer::Unpacker<std::istream> unpacker(ss);
|
||||
|
||||
SECTION("empty array") {
|
||||
SECTION("should be correctly packed") {
|
||||
std::array<uint8_t, 0> array;
|
||||
packer.pack(array);
|
||||
|
||||
CHECK(string_to_hex(ss.str()) == "");
|
||||
|
||||
SECTION("and should unpack back") {
|
||||
std::array<uint8_t, 0> unpacked;
|
||||
unpacker.unpack(unpacked);
|
||||
|
||||
CHECK(unpacked.size() == 0);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("should be correctly packed") {
|
||||
std::array<uint8_t, 2> array = {100, 200};
|
||||
packer.pack(array);
|
||||
|
||||
CHECK(string_to_hex(ss.str()) == "64C8");
|
||||
|
||||
SECTION("and should unpack back") {
|
||||
std::array<uint8_t, 2> unpacked;
|
||||
unpacker.unpack(unpacked);
|
||||
|
||||
REQUIRE(unpacked.size() == 2);
|
||||
CHECK(unpacked == array);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("Serializer/Std/CArray", "[serializer][std][c-array]") {
|
||||
std::stringstream ss;
|
||||
PacketBuffer::Packer<std::ostream> packer(ss);
|
||||
PacketBuffer::Unpacker<std::istream> unpacker(ss);
|
||||
|
||||
SECTION("should be correctly packed") {
|
||||
uint8_t array[4] = {1, 2, 3, 4};
|
||||
packer.pack(array);
|
||||
|
||||
CHECK(string_to_hex(ss.str()) == "01020304");
|
||||
|
||||
SECTION("and should unpack back") {
|
||||
uint8_t unpacked[4];
|
||||
unpacker.unpack(unpacked);
|
||||
|
||||
CHECK(unpacked[0] == array[0]);
|
||||
CHECK(unpacked[1] == array[1]);
|
||||
CHECK(unpacked[2] == array[2]);
|
||||
CHECK(unpacked[3] == array[3]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
109
tests/Serializer/Std/String.cpp
Normal file
109
tests/Serializer/Std/String.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Rogiel Sulzbach
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of Rogiel Sulzbach nor the names of contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <catch.hpp>
|
||||
#include <sstream>
|
||||
|
||||
#include <PacketBuffer/PacketBuffer.h>
|
||||
|
||||
namespace {
|
||||
std::string string_to_hex(const std::string& input) {
|
||||
static const char* const lut = "0123456789ABCDEF";
|
||||
size_t len = input.length();
|
||||
|
||||
std::string output;
|
||||
output.reserve(2 * len);
|
||||
for(size_t i = 0; i < len; ++i) {
|
||||
const unsigned char c = input[i];
|
||||
output.push_back(lut[c >> 4]);
|
||||
output.push_back(lut[c & 15]);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Serializer/Std/String", "[serializer][std][string]") {
|
||||
|
||||
std::stringstream ss;
|
||||
PacketBuffer::Packer<std::ostream> packer(ss);
|
||||
PacketBuffer::Unpacker<std::istream> unpacker(ss);
|
||||
|
||||
SECTION("empty string") {
|
||||
SECTION("should be correctly packed") {
|
||||
std::string string = "";
|
||||
packer.pack(string);
|
||||
|
||||
CHECK(string_to_hex(ss.str()) == "0000000000000000");
|
||||
|
||||
SECTION("and should unpack back") {
|
||||
std::string unpacked;
|
||||
unpacker.unpack(unpacked);
|
||||
|
||||
CHECK(unpacked.size() == 0);
|
||||
CHECK(unpacked == string);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("should be correctly packed") {
|
||||
std::string string = "Hello Testing World";
|
||||
packer.pack(string);
|
||||
|
||||
CHECK(string_to_hex(ss.str()) == "130000000000000048656C6C6F2054657374696E6720576F726C64");
|
||||
|
||||
SECTION("and should unpack back") {
|
||||
std::string unpacked;
|
||||
unpacker.unpack(unpacked);
|
||||
|
||||
REQUIRE(unpacked.size() == 19);
|
||||
CHECK(unpacked == string);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("with NULL character") {
|
||||
SECTION("should be correctly packed") {
|
||||
std::string string = "Hey";
|
||||
string.resize(4);
|
||||
string[3] = 0x00;
|
||||
packer.pack(string);
|
||||
|
||||
CHECK(string_to_hex(ss.str()) == "040000000000000048657900");
|
||||
|
||||
SECTION("and should unpack back") {
|
||||
std::string unpacked;
|
||||
unpacker.unpack(unpacked);
|
||||
|
||||
REQUIRE(unpacked.size() == 4);
|
||||
CHECK(unpacked == string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
413
tests/Unpacker.cpp
Normal file
413
tests/Unpacker.cpp
Normal file
@@ -0,0 +1,413 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Rogiel Sulzbach
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of Rogiel Sulzbach nor the names of contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <catch.hpp>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <PacketBuffer/PacketBuffer.h>
|
||||
|
||||
std::string hex_to_string(const std::string& input) {
|
||||
static const char* const lut = "0123456789ABCDEF";
|
||||
size_t len = input.length();
|
||||
if(len & 1) throw std::invalid_argument("odd length");
|
||||
|
||||
std::string output;
|
||||
output.reserve(len / 2);
|
||||
for(size_t i = 0; i < len; i += 2) {
|
||||
char a = input[i];
|
||||
const char* p = std::lower_bound(lut, lut + 16, a);
|
||||
if(*p != a) throw std::invalid_argument("not a hex digit");
|
||||
|
||||
char b = input[i + 1];
|
||||
const char* q = std::lower_bound(lut, lut + 16, b);
|
||||
if(*q != b) throw std::invalid_argument("not a hex digit");
|
||||
|
||||
output.push_back(((p - lut) << 4) | (q - lut));
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
TEST_CASE("Unpacker", "[unpacker]") {
|
||||
|
||||
std::stringstream ss;
|
||||
|
||||
SECTION("little endian") {
|
||||
PacketBuffer::Unpacker<std::istream, boost::endian::order::little> unpacker(ss);
|
||||
|
||||
SECTION("uint8_t") {
|
||||
SECTION("min") {
|
||||
ss.str(hex_to_string("00"));
|
||||
CHECK(unpacker.unpack<uint8_t>() == 0);
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
ss.str(hex_to_string("01"));
|
||||
CHECK(unpacker.unpack<uint8_t>() == 1);
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
ss.str(hex_to_string("FF"));
|
||||
CHECK(unpacker.unpack<uint8_t>() == 255);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("int8_t") {
|
||||
SECTION("min") {
|
||||
ss.str(hex_to_string("80"));
|
||||
CHECK(unpacker.unpack<int8_t>() == -128);
|
||||
}
|
||||
|
||||
SECTION("zero") {
|
||||
ss.str(hex_to_string("00"));
|
||||
CHECK(unpacker.unpack<int8_t>() == 0);
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
ss.str(hex_to_string("01"));
|
||||
CHECK(unpacker.unpack<int8_t>() == 1);
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
ss.str(hex_to_string("7F"));
|
||||
CHECK(unpacker.unpack<int8_t>() == 127);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("uint16_t") {
|
||||
SECTION("min") {
|
||||
ss.str(hex_to_string("0000"));
|
||||
CHECK(unpacker.unpack<uint16_t>() == 0);
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
ss.str(hex_to_string("0100"));
|
||||
CHECK(unpacker.unpack<uint16_t>() == 1);
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
ss.str(hex_to_string("FFFF"));
|
||||
CHECK(unpacker.unpack<uint16_t>() == 65535);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("int16_t") {
|
||||
SECTION("min") {
|
||||
ss.str(hex_to_string("0080"));
|
||||
CHECK(unpacker.unpack<int16_t>() == -32768);
|
||||
}
|
||||
|
||||
SECTION("zero") {
|
||||
ss.str(hex_to_string("0000"));
|
||||
CHECK(unpacker.unpack<int16_t>() == 0);
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
ss.str(hex_to_string("0100"));
|
||||
CHECK(unpacker.unpack<int16_t>() == 1);
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
ss.str(hex_to_string("FF7F"));
|
||||
CHECK(unpacker.unpack<int16_t>() == 32767);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("uint32_t") {
|
||||
SECTION("min") {
|
||||
ss.str(hex_to_string("00000000"));
|
||||
CHECK(unpacker.unpack<uint32_t>() == 0);
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
ss.str(hex_to_string("01000000"));
|
||||
CHECK(unpacker.unpack<uint32_t>() == 1);
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
ss.str(hex_to_string("FFFFFFFF"));
|
||||
CHECK(unpacker.unpack<uint32_t>() == 4294967295);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("int32_t") {
|
||||
SECTION("min") {
|
||||
ss.str(hex_to_string("00000080"));
|
||||
CHECK(unpacker.unpack<int32_t>() == -2147483648);
|
||||
}
|
||||
|
||||
SECTION("zero") {
|
||||
ss.str(hex_to_string("00000000"));
|
||||
CHECK(unpacker.unpack<int32_t>() == 0);
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
ss.str(hex_to_string("01000000"));
|
||||
CHECK(unpacker.unpack<int32_t>() == 1);
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
ss.str(hex_to_string("FFFFFF7F"));
|
||||
CHECK(unpacker.unpack<int32_t>() == 2147483647);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("uint64_t") {
|
||||
SECTION("min") {
|
||||
ss.str(hex_to_string("0000000000000000"));
|
||||
CHECK(unpacker.unpack<uint64_t>() == 0);
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
ss.str(hex_to_string("0100000000000000"));
|
||||
CHECK(unpacker.unpack<uint64_t>() == 1);
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
ss.str(hex_to_string("FFFFFFFFFFFFFFFF"));
|
||||
CHECK(unpacker.unpack<uint64_t>() == 18446744073709551615UL);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("int64_t") {
|
||||
SECTION("min") {
|
||||
ss.str(hex_to_string("0000000000000080"));
|
||||
CHECK(unpacker.unpack<int64_t>() == -9223372036854775808L);
|
||||
}
|
||||
|
||||
SECTION("zero") {
|
||||
ss.str(hex_to_string("0000000000000000"));
|
||||
CHECK(unpacker.unpack<int64_t>() == 0);
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
ss.str(hex_to_string("0100000000000000"));
|
||||
CHECK(unpacker.unpack<int64_t>() == 1);
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
ss.str(hex_to_string("FFFFFFFFFFFFFF7F"));
|
||||
CHECK(unpacker.unpack<int64_t>() == 9223372036854775807L);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("bool") {
|
||||
SECTION("true") {
|
||||
ss.str(hex_to_string("01"));
|
||||
CHECK(unpacker.unpack<bool>() == true);
|
||||
}
|
||||
|
||||
SECTION("false") {
|
||||
ss.str(hex_to_string("00"));
|
||||
CHECK(unpacker.unpack<bool>() == false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("big endian") {
|
||||
PacketBuffer::Unpacker<std::istream, boost::endian::order::big> unpacker(ss);
|
||||
|
||||
SECTION("uint8_t") {
|
||||
SECTION("min") {
|
||||
ss.str(hex_to_string("00"));
|
||||
CHECK(unpacker.unpack<uint8_t>() == 0);
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
ss.str(hex_to_string("01"));
|
||||
CHECK(unpacker.unpack<uint8_t>() == 1);
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
ss.str(hex_to_string("FF"));
|
||||
CHECK(unpacker.unpack<uint8_t>() == 255);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("int8_t") {
|
||||
SECTION("min") {
|
||||
ss.str(hex_to_string("80"));
|
||||
CHECK(unpacker.unpack<int8_t>() == -128);
|
||||
}
|
||||
|
||||
SECTION("zero") {
|
||||
ss.str(hex_to_string("00"));
|
||||
CHECK(unpacker.unpack<int8_t>() == 0);
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
ss.str(hex_to_string("01"));
|
||||
CHECK(unpacker.unpack<int8_t>() == 1);
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
ss.str(hex_to_string("7F"));
|
||||
CHECK(unpacker.unpack<int8_t>() == 127);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("uint16_t") {
|
||||
SECTION("min") {
|
||||
ss.str(hex_to_string("0000"));
|
||||
CHECK(unpacker.unpack<uint16_t>() == 0);
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
ss.str(hex_to_string("0001"));
|
||||
CHECK(unpacker.unpack<uint16_t>() == 1);
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
ss.str(hex_to_string("FFFF"));
|
||||
CHECK(unpacker.unpack<uint16_t>() == 65535);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("int16_t") {
|
||||
SECTION("min") {
|
||||
ss.str(hex_to_string("8000"));
|
||||
CHECK(unpacker.unpack<int16_t>() == -32768);
|
||||
}
|
||||
|
||||
SECTION("zero") {
|
||||
ss.str(hex_to_string("0000"));
|
||||
CHECK(unpacker.unpack<int16_t>() == 0);
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
ss.str(hex_to_string("0001"));
|
||||
CHECK(unpacker.unpack<int16_t>() == 1);
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
ss.str(hex_to_string("7FFF"));
|
||||
CHECK(unpacker.unpack<int16_t>() == 32767);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("uint32_t") {
|
||||
SECTION("min") {
|
||||
ss.str(hex_to_string("00000000"));
|
||||
CHECK(unpacker.unpack<uint32_t>() == 0);
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
ss.str(hex_to_string("00000001"));
|
||||
CHECK(unpacker.unpack<uint32_t>() == 1);
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
ss.str(hex_to_string("FFFFFFFF"));
|
||||
CHECK(unpacker.unpack<uint32_t>() == 4294967295);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("int32_t") {
|
||||
SECTION("min") {
|
||||
ss.str(hex_to_string("80000000"));
|
||||
CHECK(unpacker.unpack<int32_t>() == -2147483648);
|
||||
}
|
||||
|
||||
SECTION("zero") {
|
||||
ss.str(hex_to_string("00000000"));
|
||||
CHECK(unpacker.unpack<int32_t>() == 0);
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
ss.str(hex_to_string("00000001"));
|
||||
CHECK(unpacker.unpack<int32_t>() == 1);
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
ss.str(hex_to_string("7FFFFFFF"));
|
||||
CHECK(unpacker.unpack<int32_t>() == 2147483647);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("uint64_t") {
|
||||
SECTION("min") {
|
||||
ss.str(hex_to_string("0000000000000000"));
|
||||
CHECK(unpacker.unpack<uint64_t>() == 0);
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
ss.str(hex_to_string("0000000000000001"));
|
||||
CHECK(unpacker.unpack<uint64_t>() == 1);
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
ss.str(hex_to_string("FFFFFFFFFFFFFFFF"));
|
||||
CHECK(unpacker.unpack<uint64_t>() == 18446744073709551615UL);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("int64_t") {
|
||||
SECTION("min") {
|
||||
ss.str(hex_to_string("8000000000000000"));
|
||||
CHECK(unpacker.unpack<int64_t>() == -9223372036854775808L);
|
||||
}
|
||||
|
||||
SECTION("zero") {
|
||||
ss.str(hex_to_string("0000000000000000"));
|
||||
CHECK(unpacker.unpack<int64_t>() == 0);
|
||||
}
|
||||
|
||||
SECTION("one") {
|
||||
ss.str(hex_to_string("0000000000000001"));
|
||||
CHECK(unpacker.unpack<int64_t>() == 1);
|
||||
}
|
||||
|
||||
SECTION("max") {
|
||||
ss.str(hex_to_string("7FFFFFFFFFFFFFFF"));
|
||||
CHECK(unpacker.unpack<int64_t>() == 9223372036854775807L);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("bool") {
|
||||
SECTION("true") {
|
||||
ss.str(hex_to_string("01"));
|
||||
CHECK(unpacker.unpack<bool>() == true);
|
||||
}
|
||||
|
||||
SECTION("false") {
|
||||
ss.str(hex_to_string("00"));
|
||||
CHECK(unpacker.unpack<bool>() == false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("should throw an error on overflow") {
|
||||
PacketBuffer::Unpacker<std::istream> unpacker(ss);
|
||||
|
||||
// REQUIRE_THROWS(unpacker.unpack<uint32_t>());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
32
tests/main.cpp
Normal file
32
tests/main.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) 2017, Rogiel Sulzbach
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of Rogiel Sulzbach nor the names of contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#define CATCH_CONFIG_MAIN
|
||||
|
||||
#include "catch.hpp"
|
||||
Reference in New Issue
Block a user