mirror of
https://github.com/OpenMW/openmw.git
synced 2025-01-23 09:23:52 +00:00
a58f1a94e3
To construct serializer from given entities: * Data source/destination - any value that has to be serialized/deserialized, usually already existing type. * Format - functional object to define high level serialization logic to define specific format and data schema. Like order of fields, allocation. * Visitor - functional object to define low level serialization logic to operator on given data part. * BinaryWriter - copies given value into provided buffer. * BinaryReader - copies value into given destination from provided buffer. * SizeAccumulator - calculates required buffer size for given data.
57 lines
2.2 KiB
C++
57 lines
2.2 KiB
C++
#include "format.hpp"
|
|
|
|
#include <components/detournavigator/serialization/binarywriter.hpp>
|
|
|
|
#include <gtest/gtest.h>
|
|
#include <gmock/gmock.h>
|
|
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
using namespace testing;
|
|
using namespace DetourNavigator::Serialization;
|
|
using namespace DetourNavigator::SerializationTesting;
|
|
|
|
TEST(DetourNavigatorSerializationBinaryWriterTest, shouldWriteArithmeticTypeValue)
|
|
{
|
|
std::vector<std::byte> result(4);
|
|
BinaryWriter binaryWriter(result.data(), result.data() + result.size());
|
|
const TestFormat<Mode::Write> format;
|
|
binaryWriter(format, std::uint32_t(42));
|
|
EXPECT_THAT(result, ElementsAre(std::byte(42), std::byte(0), std::byte(0), std::byte(0)));
|
|
}
|
|
|
|
TEST(DetourNavigatorSerializationBinaryWriterTest, shouldWriteArithmeticTypeRangeValue)
|
|
{
|
|
std::vector<std::byte> result(8);
|
|
BinaryWriter binaryWriter(result.data(), result.data() + result.size());
|
|
std::vector<std::uint32_t> values({42, 13});
|
|
const TestFormat<Mode::Write> format;
|
|
binaryWriter(format, values.data(), values.size());
|
|
constexpr std::array<std::byte, 8> expected {
|
|
std::byte(42), std::byte(0), std::byte(0), std::byte(0),
|
|
std::byte(13), std::byte(0), std::byte(0), std::byte(0),
|
|
};
|
|
EXPECT_THAT(result, ElementsAreArray(expected));
|
|
}
|
|
|
|
TEST(DetourNavigatorSerializationBinaryWriterTest, forNotEnoughSpaceForArithmeticTypeShouldThrowException)
|
|
{
|
|
std::vector<std::byte> result(3);
|
|
BinaryWriter binaryWriter(result.data(), result.data() + result.size());
|
|
const TestFormat<Mode::Write> format;
|
|
EXPECT_THROW(binaryWriter(format, std::uint32_t(42)), std::runtime_error);
|
|
}
|
|
|
|
TEST(DetourNavigatorSerializationBinaryWriterTest, forNotEnoughSpaceForArithmeticTypeRangeShouldThrowException)
|
|
{
|
|
std::vector<std::byte> result(7);
|
|
BinaryWriter binaryWriter(result.data(), result.data() + result.size());
|
|
std::vector<std::uint32_t> values({42, 13});
|
|
const TestFormat<Mode::Write> format;
|
|
EXPECT_THROW(binaryWriter(format, values.data(), values.size()), std::runtime_error);
|
|
}
|
|
}
|