File: binarywriter.cpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (63 lines) | stat: -rw-r--r-- 2,263 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "format.hpp"

#include <components/serialization/binarywriter.hpp>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <array>
#include <cstdint>
#include <vector>

namespace
{
    using namespace testing;
    using namespace Serialization;
    using namespace 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);
    }
}