|
| 1 | +/** |
| 2 | + * Copyright Quadrivium LLC |
| 3 | + * All Rights Reserved |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <gtest/gtest.h> |
| 8 | +#include <scale/scale.hpp> |
| 9 | + |
| 10 | +using scale::ByteArray; |
| 11 | +using scale::decode; |
| 12 | +using scale::encode; |
| 13 | +using scale::ScaleDecoderStream; |
| 14 | +using scale::ScaleEncoderStream; |
| 15 | + |
| 16 | +class StdVariantFixture |
| 17 | + : public testing::TestWithParam< |
| 18 | + std::pair<std::variant<uint8_t, uint32_t>, ByteArray>> { |
| 19 | + protected: |
| 20 | + ScaleEncoderStream s; |
| 21 | +}; |
| 22 | +namespace { |
| 23 | + std::pair<std::variant<uint8_t, uint32_t>, ByteArray> make_pair( |
| 24 | + std::variant<uint8_t, uint32_t> v, ByteArray m) { |
| 25 | + return {v, std::move(m)}; |
| 26 | + } |
| 27 | +} // namespace |
| 28 | + |
| 29 | +/** |
| 30 | + * @given variant value and byte array |
| 31 | + * @when value is scale-encoded |
| 32 | + * @then encoded bytes match predefined byte array |
| 33 | + */ |
| 34 | +TEST_P(StdVariantFixture, EncodeSuccessTest) { |
| 35 | + const auto &[value, match] = GetParam(); |
| 36 | + ASSERT_NO_THROW(s << value); |
| 37 | + ASSERT_EQ(s.to_vector(), match); |
| 38 | +} |
| 39 | + |
| 40 | +INSTANTIATE_TEST_SUITE_P(CompactTestCases, |
| 41 | + StdVariantFixture, |
| 42 | + ::testing::Values(make_pair(uint8_t(1), {0, 1}), |
| 43 | + make_pair(uint32_t(2), |
| 44 | + {1, 2, 0, 0, 0}))); |
| 45 | + |
| 46 | +/** |
| 47 | + * @given byte array of encoded variant of types uint8_t and uint32_t |
| 48 | + * containing uint8_t value |
| 49 | + * @when variant decoded from scale decoder stream |
| 50 | + * @then obtained varian has alternative type uint8_t and is equal to encoded |
| 51 | + * uint8_t value |
| 52 | + */ |
| 53 | +TEST(ScaleStdVariant, DecodeU8Success) { |
| 54 | + ByteArray match = {0, 1}; // uint8_t{1} |
| 55 | + ScaleDecoderStream s(match); |
| 56 | + std::variant<uint8_t, uint32_t> val{}; |
| 57 | + ASSERT_NO_THROW(s >> val); |
| 58 | + ASSERT_EQ(std::get<uint8_t>(val), 1); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * @given byte array of encoded variant of types uint8_t and uint32_t |
| 63 | + * containing uint32_t value |
| 64 | + * @when variant decoded from scale decoder stream |
| 65 | + * @then obtained varian has alternative type uint32_t and is equal to encoded |
| 66 | + * uint32_t value |
| 67 | + */ |
| 68 | +TEST(ScaleStdVariant, DecodeU32Success) { |
| 69 | + ByteArray match = {1, 1, 0, 0, 0}; // uint32_t{1} |
| 70 | + ScaleDecoderStream s(match); |
| 71 | + std::variant<uint8_t, uint32_t> val{}; |
| 72 | + ASSERT_NO_THROW(s >> val); |
| 73 | + ASSERT_EQ(std::get<uint32_t>(val), 1); |
| 74 | +} |
0 commit comments