39
39
#include < nil/crypto3/multiprecision/cpp_int.hpp>
40
40
#include < nil/crypto3/multiprecision/number.hpp>
41
41
42
+ // #include <nil/crypto3/marshalling/algorithms/pack.hpp>
43
+ #include < nil/marshalling/algorithms/pack.hpp>
44
+ #include < nil/marshalling/algorithms/unpack.hpp>
42
45
#include < nil/crypto3/marshalling/types/integral.hpp>
43
46
44
47
template <class T >
@@ -102,20 +105,16 @@ void test_round_trip_fixed_precision_big_endian(T val) {
102
105
103
106
export_bits (val, cv.begin () + begin_index, units_bits, true );
104
107
105
- auto read_iter = cv.begin ();
106
- nil::marshalling::status_type status = test_val.read (read_iter, cv.size () * units_bits);
107
- BOOST_CHECK (status == nil::marshalling::status_type::success);
108
+ nil::marshalling::status_type status;
109
+ T test_val1 = nil::marshalling::pack<nil::marshalling::option::big_endian, T>(cv, status);
108
110
109
- BOOST_CHECK (val == test_val.value ());
111
+ BOOST_CHECK (val == test_val1);
112
+ BOOST_CHECK (status == nil::marshalling::status_type::success);
110
113
111
- std::vector<unit_type> test_val_byteblob;
112
- test_val_byteblob.resize (cv.size ());
113
- auto write_iter = test_val_byteblob.begin ();
114
+ std::vector<unit_type> test_cv = nil::marshalling::unpack<nil::marshalling::option::big_endian, unit_type>(val, status);
114
115
115
- status = test_val. write (write_iter, test_val_byteblob. size () * units_bits );
116
+ BOOST_CHECK ( std::equal (test_cv. begin (), test_cv. end (), cv. begin ()) );
116
117
BOOST_CHECK (status == nil::marshalling::status_type::success);
117
-
118
- BOOST_CHECK (cv == test_val_byteblob);
119
118
}
120
119
121
120
template <class T >
@@ -133,24 +132,23 @@ void test_round_trip_fixed_precision_little_endian(T val) {
133
132
export_bits (val, std::back_inserter (cv), units_bits, false );
134
133
cv.resize (unitblob_size, 0x00 );
135
134
136
- auto read_iter = cv.begin ();
137
- nil::marshalling::status_type status = test_val.read (read_iter, cv.size () * units_bits);
138
- BOOST_CHECK (status == nil::marshalling::status_type::success);
135
+ nil::marshalling::status_type status;
136
+ T test_val1 = nil::marshalling::pack<nil::marshalling::option::little_endian, T>(cv, status);
139
137
140
- BOOST_CHECK (val == test_val.value ());
138
+ BOOST_CHECK (val == test_val1);
139
+ BOOST_CHECK (status == nil::marshalling::status_type::success);
141
140
142
- std::vector<unit_type> test_val_byteblob;
143
- test_val_byteblob.resize (cv.size ());
144
- auto write_iter = test_val_byteblob.begin ();
141
+ std::vector<unit_type> test_cv = nil::marshalling::unpack<nil::marshalling::option::little_endian, unit_type>(val, status);
145
142
146
- status = test_val. write (write_iter, test_val_byteblob. size () * units_bits );
143
+ BOOST_CHECK ( std::equal (test_cv. begin (), test_cv. end (), cv. begin ()) );
147
144
BOOST_CHECK (status == nil::marshalling::status_type::success);
148
-
149
- BOOST_CHECK (cv == test_val_byteblob);
150
145
}
151
146
152
147
template <class T >
153
148
void test_round_trip_fixed_precision () {
149
+
150
+ static_assert (nil::marshalling::is_compatible<T>::value);
151
+
154
152
std::cout << std::hex;
155
153
std::cerr << std::hex;
156
154
for (unsigned i = 0 ; i < 1000 ; ++i) {
@@ -160,72 +158,40 @@ void test_round_trip_fixed_precision() {
160
158
}
161
159
}
162
160
163
- template <class T >
164
- void test_round_trip_non_fixed_precision_big_endian (T val) {
161
+ template <typename TEndianness, class T >
162
+ void test_round_trip_non_fixed_precision (T val) {
165
163
using namespace nil ::crypto3::marshalling;
166
164
167
165
std::size_t units_bits = 8 ;
168
166
using unit_type = unsigned char ;
169
- using integral_type = types::integral<nil::marshalling::field_type<nil::marshalling::option::big_endian>, T>;
170
-
171
- integral_type test_val;
172
167
173
168
std::vector<unit_type> cv;
174
- export_bits (val, std::back_inserter (cv), units_bits, true );
175
-
176
- auto read_iter = cv.begin ();
177
- nil::marshalling::status_type status = test_val.read (read_iter, cv.size () * units_bits);
178
- BOOST_CHECK (status == nil::marshalling::status_type::success);
179
-
180
- BOOST_CHECK (val == test_val.value ());
181
-
182
- std::vector<unit_type> test_val_byteblob;
183
- test_val_byteblob.resize (cv.size ());
184
- auto write_iter = test_val_byteblob.begin ();
185
-
186
- status = test_val.write (write_iter, test_val_byteblob.size () * units_bits);
187
- BOOST_CHECK (status == nil::marshalling::status_type::success);
188
-
189
- BOOST_CHECK (cv == test_val_byteblob);
190
- }
169
+ export_bits (val, std::back_inserter (cv), units_bits,
170
+ std::is_same<TEndianness, nil::marshalling::option::big_endian>::value?true :false );
191
171
192
- template <class T >
193
- void test_round_trip_non_fixed_precision_little_endian (T val) {
194
- using namespace nil ::crypto3::marshalling;
195
-
196
- std::size_t units_bits = 8 ;
197
- using unit_type = unsigned char ;
198
- using integral_type = types::integral<nil::marshalling::field_type<nil::marshalling::option::little_endian>, T>;
172
+ nil::marshalling::status_type status;
173
+ T test_val1 = nil::marshalling::pack<TEndianness, T>(cv, status);
199
174
200
- integral_type test_val;
201
-
202
- std::vector<unsigned char > cv;
203
- export_bits (val, std::back_inserter (cv), units_bits, false );
204
-
205
- auto read_iter = cv.begin ();
206
- nil::marshalling::status_type status = test_val.read (read_iter, cv.size () * units_bits);
175
+ BOOST_CHECK (val == test_val1);
207
176
BOOST_CHECK (status == nil::marshalling::status_type::success);
208
177
209
- BOOST_CHECK (val == test_val. value () );
178
+ std::vector<unit_type> test_cv = nil::marshalling::unpack<TEndianness, unit_type>(val, status );
210
179
211
- std::vector<unsigned char > test_val_byteblob;
212
- test_val_byteblob.resize (cv.size ());
213
- auto write_iter = test_val_byteblob.begin ();
214
-
215
- status = test_val.write (write_iter, test_val_byteblob.size () * units_bits);
180
+ BOOST_CHECK (std::equal (test_cv.begin (), test_cv.end (), cv.begin ()));
216
181
BOOST_CHECK (status == nil::marshalling::status_type::success);
217
-
218
- BOOST_CHECK (cv == test_val_byteblob);
219
182
}
220
183
221
184
template <class T >
222
185
void test_round_trip_non_fixed_precision () {
186
+
187
+ static_assert (nil::marshalling::is_compatible<T>::value);
188
+
223
189
std::cout << std::hex;
224
190
std::cerr << std::hex;
225
191
for (unsigned i = 0 ; i < 1000 ; ++i) {
226
192
T val = generate_random<T>();
227
- test_round_trip_non_fixed_precision_big_endian (val);
228
- test_round_trip_non_fixed_precision_little_endian (val);
193
+ test_round_trip_non_fixed_precision<nil::marshalling::option::big_endian> (val);
194
+ test_round_trip_non_fixed_precision<nil::marshalling::option::little_endian> (val);
229
195
}
230
196
}
231
197
0 commit comments