forked from Harrm/scale-codec
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathscale_std_variant_test.cpp
More file actions
96 lines (83 loc) · 2.92 KB
/
Copy pathscale_std_variant_test.cpp
File metadata and controls
96 lines (83 loc) · 2.92 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#include <gtest/gtest.h>
#include <qtils/test/outcome.hpp>
#include <scale/scale.hpp>
using scale::ByteArray;
using scale::get_variant;
using scale::impl::memory::decode;
using scale::impl::memory::encode;
using Encoder = scale::backend::ToBytes<>;
using Decoder = scale::backend::FromBytes<>;
using Testee = std::variant<uint8_t, uint32_t>;
class StdVariantFixture
: public testing::TestWithParam<std::pair<Testee, ByteArray>> {};
namespace {
std::pair<Testee, ByteArray> make_pair(Testee variant, ByteArray bytes) {
return {variant, std::move(bytes)};
}
} // namespace
/**
* @given variant value and byte array
* @when value is scale-encoded
* @then encoded bytes match predefined byte array
*/
TEST_P(StdVariantFixture, EncodeSuccess) {
const auto &[value, bytes] = GetParam();
ASSERT_OUTCOME_SUCCESS(encoded, encode(value));
ASSERT_EQ(encoded, bytes);
}
/**
* @given variant value and byte array
* @when value is scale-encoded
* @then encoded bytes match predefined byte array
*/
TEST_P(StdVariantFixture, DecodeSuccess) {
const auto &[value, bytes] = GetParam();
ASSERT_OUTCOME_SUCCESS(decoded, decode<Testee>(bytes));
ASSERT_EQ(decoded, value);
}
INSTANTIATE_TEST_SUITE_P(CompactTestCases,
StdVariantFixture,
::testing::Values(make_pair(uint8_t(1), {0, 1}),
make_pair(uint32_t(2),
{1, 2, 0, 0, 0})));
/**
* @given byte array of encoded variant of types uint8_t and uint32_t
* containing uint8_t value
* @when variant decoded from scale decoder stream
* @then obtained varian has alternative type uint8_t and is equal to encoded
* uint8_t value
*/
TEST(ScaleStdVariant, DecodeU8Success) {
ByteArray bytes = {0, 1}; // uint8_t{1}
ASSERT_OUTCOME_SUCCESS(decoded, decode<Testee>(bytes));
ASSERT_EQ(scale::get_variant<uint8_t>(decoded), 1);
}
/**
* @given byte array of encoded variant of types uint8_t and uint32_t
* containing uint32_t value
* @when variant decoded from scale decoder stream
* @then obtained varian has alternative type uint32_t and is equal to encoded
* uint32_t value
*/
TEST(ScaleStdVariant, DecodeU32Success) {
ByteArray bytes = {1, 1, 0, 0, 0}; // uint32_t{1}
ASSERT_OUTCOME_SUCCESS(decoded, decode<Testee>(bytes));
ASSERT_EQ(scale::get_variant<uint32_t>(decoded), 1);
}
/**
* @given byte array of encoded variant of types uint8_t and uint32_t
* containing uint32_t value
* @when variant decoded from scale decoder stream
* @then obtained varian has alternative type uint32_t and is equal to encoded
* uint32_t value
*/
TEST(ScaleStdVariant, DecodeWrongVariantIndex) {
ByteArray bytes = {2, 0, 0, 0, 0}; // index out of bound
ASSERT_OUTCOME_ERROR(decode<Testee>(bytes),
scale::DecodeError::WRONG_TYPE_INDEX);
}