forked from Harrm/scale-codec
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathscale_tune_test.cpp
More file actions
125 lines (108 loc) · 3.1 KB
/
Copy pathscale_tune_test.cpp
File metadata and controls
125 lines (108 loc) · 3.1 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#include <gtest/gtest.h>
#include <qtils/empty.hpp>
#include <qtils/test/outcome.hpp>
#include <scale/definitions.hpp>
#include <scale/scale.hpp>
#ifndef CUSTOM_CONFIG_ENABLED
#error \
"This file should not be compiled, because custom config support is not enabed"
#endif
using scale::as_compact;
using scale::ByteArray;
using scale::Compact;
using scale::ConstSpanOfBytes;
using scale::Decoder;
using scale::Encoder;
using scale::backend::FromBytes;
using scale::backend::ToBytes;
using scale::impl::memory::decode;
using scale::impl::memory::encode;
template <typename T, typename... Configs>
outcome::result<std::vector<uint8_t>> encode_with_config(T &&value,
Configs &&...configs) {
std::vector<uint8_t> out;
ToBytes encoder(out, std::forward<Configs>(configs)...);
try {
encode(std::forward<T>(value), encoder);
} catch (std::system_error &e) {
return outcome::failure(e.code());
}
return std::move(out);
}
template <typename T, typename... Configs>
outcome::result<T> decode_with_config(ConstSpanOfBytes bytes,
Configs &&...configs) {
FromBytes decoder(bytes, std::forward<Configs>(configs)...);
T value;
try {
decode(value, decoder);
} catch (std::system_error &e) {
return outcome::failure(e.code());
}
return std::move(value);
}
struct MulConfig {
uint8_t multi;
};
struct AddConfig {
uint8_t add;
};
struct Object : private qtils::Empty {
std::vector<uint8_t> buff;
bool operator==(const Object &) const = default;
friend void encode(const Object &obj, Encoder &encoder) {
auto mul = encoder.template getConfig<MulConfig>().multi;
auto add = encoder.template getConfig<AddConfig>().add;
encode(as_compact(obj.buff.size()), encoder);
for (uint8_t i : obj.buff) {
uint8_t x = i * mul + add;
encoder.put(x);
}
}
friend void decode(Object &obj, Decoder &decoder) {
auto mul = decoder.getConfig<MulConfig>().multi;
auto add = decoder.getConfig<AddConfig>().add;
size_t size;
decode(as_compact(size), decoder);
obj.buff.resize(size);
for (uint8_t &i : obj.buff) {
uint8_t x = decoder.take();
i = (x - add) / mul;
}
}
private:
int x = 0;
};
TEST(CustomConfig, SunnyDayScenario) {
MulConfig mul_two{2};
MulConfig mul_three{3};
AddConfig add_six{6};
AddConfig add_twelve{12};
// Encoding
Object object;
object.buff = {3, 6, 9};
// X * 2 + 12
ASSERT_OUTCOME_SUCCESS( //
encoded,
encode_with_config(object, mul_two, add_twelve));
// Check encoding
{
ASSERT_OUTCOME_SUCCESS(decoded, decode<std::vector<uint8_t>>(encoded));
std::vector<uint8_t> expected = {18, 24, 30};
ASSERT_EQ(decoded, expected) << "Incorrect encoded";
}
// Decode and check it
{
ASSERT_OUTCOME_SUCCESS( //
decoded,
decode_with_config<Object>(encoded, mul_three, add_six));
Object expected;
expected.buff = {4, 6, 8};
ASSERT_EQ(decoded, expected) << "Incorrect decoded";
}
}