1
+
2
+ /* *
3
+ * Copyright Quadrivium LLC
4
+ * All Rights Reserved
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ */
7
+
8
+ /* *
9
+ * @brief Unit tests for encoding and decoding tagged types using SCALE.
10
+ *
11
+ * This file contains tests for encoding and decoding tagged types,
12
+ * ensuring that serialization and deserialization work as expected.
13
+ */
14
+
15
+ #include < gtest/gtest.h>
16
+
17
+ #include < qtils/test/outcome.hpp>
18
+ #include < qtils/tagged.hpp>
19
+
20
+ #include < scale/scale.hpp>
21
+
22
+ using scale::ByteArray;
23
+ using scale::impl::memory::decode;
24
+ using scale::impl::memory::encode;
25
+
26
+ using TaggedString = qtils::Tagged<std::string, class String >;
27
+ using TaggedInteger = qtils::Tagged<uint32_t , class Integer >;
28
+
29
+ /* *
30
+ * @brief Tests encoding of a tagged string.
31
+ *
32
+ * @given A string and its tagged equivalent.
33
+ * @when Encoding is applied.
34
+ * @then The serialized value of the tagged string matches the original string.
35
+ */
36
+ TEST (Tagged, StringEncode) {
37
+ std::string string = " hello world" ;
38
+ TaggedString tagged = " hello world" ;
39
+
40
+ ASSERT_OUTCOME_SUCCESS (encoded_original, encode (string));
41
+ ASSERT_OUTCOME_SUCCESS (encoded_tagged, encode (tagged));
42
+
43
+ ASSERT_EQ (encoded_tagged, encoded_original);
44
+ }
45
+
46
+ /* *
47
+ * @brief Tests decoding of a tagged string.
48
+ *
49
+ * @given A byte sequence containing an encoded string.
50
+ * @when Decoding is applied.
51
+ * @then The decoded value matches the original string.
52
+ */
53
+ TEST (Tagged, StringDecode) {
54
+ std::string original = " hello world" ;
55
+ ASSERT_OUTCOME_SUCCESS (encoded, encode (original));
56
+
57
+ ASSERT_OUTCOME_SUCCESS (decoded, decode<TaggedString>(encoded));
58
+
59
+ ASSERT_EQ (untagged (decoded), original);
60
+ }
61
+
62
+ /* *
63
+ * @brief Tests encoding and decoding of a tagged integer.
64
+ *
65
+ * @given A tagged integer.
66
+ * @when The integer is encoded and then decoded.
67
+ * @then The decoded value matches the original integer.
68
+ */
69
+ TEST (Tagged, StringEncodeAndDecode) {
70
+ TaggedString original = " hello world" ;
71
+
72
+ ASSERT_OUTCOME_SUCCESS (encoded, encode (original));
73
+ ASSERT_OUTCOME_SUCCESS (decoded, decode<TaggedString>(encoded));
74
+ ASSERT_EQ (decoded, original);
75
+ }
76
+
77
+ TEST (Tagged, IntegerEncode) {
78
+ uint32_t integer = 123456789 ;
79
+ TaggedInteger tagged (123456789 );
80
+
81
+ ASSERT_OUTCOME_SUCCESS (encoded_original, encode (integer));
82
+ ASSERT_OUTCOME_SUCCESS (encoded_tagged, encode (tagged));
83
+
84
+ ASSERT_EQ (encoded_tagged, encoded_original);
85
+ }
86
+
87
+ /* *
88
+ * @brief Tests decoding of a tagged integer.
89
+ *
90
+ * @given A byte sequence containing an encoded integer.
91
+ * @when Decoding is applied.
92
+ * @then The decoded value matches the original integer.
93
+ */
94
+ TEST (Tagged, IntegerDecode) {
95
+ uint32_t original = 123456789 ;
96
+ ASSERT_OUTCOME_SUCCESS (encoded, encode (original));
97
+
98
+ ASSERT_OUTCOME_SUCCESS (decoded, decode<TaggedInteger>(encoded));
99
+
100
+ ASSERT_EQ (untagged (decoded), original);
101
+ }
0 commit comments