|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2015 NUClear Contributors |
| 5 | + * |
| 6 | + * This file is part of the NUClear codebase. |
| 7 | + * See https://github.com/Fastcode/NUClear for further info. |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 10 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 11 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | + * permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 15 | + * Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 18 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 20 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +#include <catch2/catch_test_macros.hpp> |
| 24 | +#include <cstdint> |
| 25 | +#include <type_traits> |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +#include "util/serialise/Serialise.hpp" |
| 29 | + |
| 30 | +namespace { |
| 31 | + |
| 32 | +/** |
| 33 | + * A type that is trivially copyable but carries its own wire format. |
| 34 | + * |
| 35 | + * A code generator's message class is the motivating case: it is a plain struct of scalars, so it is trivially |
| 36 | + * copyable, yet it has an encoding and a type identity of its own that a byte copy would bypass. |
| 37 | + */ |
| 38 | +struct Encoded { |
| 39 | + uint16_t value{0}; |
| 40 | +}; |
| 41 | + |
| 42 | +/// Marks the family of types that carry their own encoding, as a generator would for everything it emits |
| 43 | +template <typename T> |
| 44 | +struct is_encoded : std::is_same<T, Encoded> {}; |
| 45 | + |
| 46 | +/// The identity these types define for themselves, which is not their demangled C++ name |
| 47 | +constexpr uint64_t ENCODED_HASH = 0x0123456789ABCDEFULL; |
| 48 | + |
| 49 | +/// The wire format these types define for themselves, which is not their object representation |
| 50 | +constexpr uint8_t ENCODED_PREFIX = 0xAB; |
| 51 | + |
| 52 | +} // namespace |
| 53 | + |
| 54 | +namespace NUClear { |
| 55 | +namespace util { |
| 56 | + namespace serialise { |
| 57 | + |
| 58 | + /** |
| 59 | + * Opt the family out of the byte copy. |
| 60 | + * |
| 61 | + * Without this the partial specialisation below is ambiguous with the trivially copyable one: neither is more |
| 62 | + * specialised than the other, so the two are equally good matches and the instantiation is ill-formed. A full |
| 63 | + * specialisation would win outright, but that is one specialisation per type rather than one for everything a |
| 64 | + * generator emits. |
| 65 | + */ |
| 66 | + template <typename T> |
| 67 | + struct serialise_trivially<T, std::enable_if_t<is_encoded<T>::value>> : std::false_type {}; |
| 68 | + |
| 69 | + template <typename T> |
| 70 | + struct Serialise<T, std::enable_if_t<is_encoded<T>::value, T>> { |
| 71 | + static std::vector<uint8_t> serialise(const T& in) { |
| 72 | + return {ENCODED_PREFIX, static_cast<uint8_t>(in.value >> 8), static_cast<uint8_t>(in.value & 0xFF)}; |
| 73 | + } |
| 74 | + |
| 75 | + static T deserialise(const std::vector<uint8_t>& in) { |
| 76 | + if (in.size() != 3 || in[0] != ENCODED_PREFIX) { |
| 77 | + throw std::length_error("Not an encoded value"); |
| 78 | + } |
| 79 | + return T{static_cast<uint16_t>((uint16_t(in[1]) << 8) | in[2])}; |
| 80 | + } |
| 81 | + |
| 82 | + static uint64_t hash() { |
| 83 | + return ENCODED_HASH; |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + } // namespace serialise |
| 88 | +} // namespace util |
| 89 | +} // namespace NUClear |
| 90 | + |
| 91 | +SCENARIO("A trivially copyable type can provide its own serialisation", "[util][serialise][trivially]") { |
| 92 | + |
| 93 | + GIVEN("a trivially copyable type that has opted out of the byte copy") { |
| 94 | + STATIC_REQUIRE(std::is_trivially_copyable<Encoded>::value); |
| 95 | + STATIC_REQUIRE_FALSE(NUClear::util::serialise::serialise_trivially<Encoded>::value); |
| 96 | + |
| 97 | + const Encoded in{0x1234}; |
| 98 | + |
| 99 | + WHEN("it is serialised") { |
| 100 | + const auto serialised = NUClear::util::serialise::Serialise<Encoded>::serialise(in); |
| 101 | + |
| 102 | + THEN("its own wire format is used rather than its object representation") { |
| 103 | + REQUIRE(serialised == std::vector<uint8_t>{ENCODED_PREFIX, 0x12, 0x34}); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + WHEN("it is round tripped") { |
| 108 | + const auto serialised = NUClear::util::serialise::Serialise<Encoded>::serialise(in); |
| 109 | + const auto deserialised = NUClear::util::serialise::Serialise<Encoded>::deserialise(serialised); |
| 110 | + |
| 111 | + THEN("the deserialised value is the same as the input") { |
| 112 | + REQUIRE(deserialised.value == in.value); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + WHEN("its hash is taken") { |
| 117 | + THEN("it is the identity the type defines rather than one derived from its C++ name") { |
| 118 | + REQUIRE(NUClear::util::serialise::Serialise<Encoded>::hash() == ENCODED_HASH); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + GIVEN("a trivially copyable type that has not opted out") { |
| 124 | + STATIC_REQUIRE(NUClear::util::serialise::serialise_trivially<uint32_t>::value); |
| 125 | + |
| 126 | + const uint32_t in = 0xCAFEFECA; // Mirrored so that endianess doesn't matter for the test |
| 127 | + |
| 128 | + WHEN("it is serialised") { |
| 129 | + const auto serialised = NUClear::util::serialise::Serialise<uint32_t>::serialise(in); |
| 130 | + |
| 131 | + THEN("it still uses the byte copy") { |
| 132 | + REQUIRE(serialised.size() == sizeof(uint32_t)); |
| 133 | + REQUIRE(serialised == std::vector<uint8_t>{0xCA, 0xFE, 0xFE, 0xCA}); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments