Skip to content

Commit ae198d3

Browse files
committed
Add tests for variant array creation.
1 parent 96d1807 commit ae198d3

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

libraries/libfc/test/variant/test_variant.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,56 @@ BOOST_AUTO_TEST_CASE(bitset_test)
211211
check_variant_round_trip(fc::bitset("01110011010100101111001101100100100111111111111111111001"));
212212
}
213213

214+
BOOST_AUTO_TEST_CASE(array) {
215+
// check that variant arrays can be created or updated from:
216+
// - `std::initializer_list`
217+
// - `std::vector`
218+
// - `std::array`
219+
// --------------------------------------------------------------------------------------------
220+
auto check_variant_round_trip = []<class C>(const C& arr) {
221+
fc::variant m(arr);
222+
223+
std::vector<int> v;
224+
fc::from_variant(m, v);
225+
auto expected = std::vector<int>{arr.begin(), arr.end()};
226+
BOOST_TEST(v == expected);
227+
};
228+
229+
check_variant_round_trip(std::initializer_list<int>{});
230+
check_variant_round_trip(std::initializer_list<int>{1, 2, 3});
231+
232+
check_variant_round_trip(std::vector<int>{});
233+
check_variant_round_trip(std::vector<int>{1, 2, 3});
234+
235+
check_variant_round_trip(std::array<int, 0>{});
236+
check_variant_round_trip(std::array{1, 2, 3});
237+
238+
// check that mutable_variant_object arrays can be created or updated from:
239+
// - `std::initializer_list`
240+
// - `std::vector`
241+
// - `std::array`
242+
// --------------------------------------------------------------------------------------------
243+
auto check_variant_round_trip2 = []<class C>(const C& arr) {
244+
fc::mutable_variant_object mu("a", arr);
245+
246+
std::vector<int> v;
247+
fc::from_variant(mu["a"], v);
248+
auto expected = std::vector<int>{arr.begin(), arr.end()};
249+
BOOST_TEST(v == expected);
250+
251+
auto mu2 = fc::mutable_variant_object()("b", arr); // also test mutable_variant_object operator()
252+
fc::from_variant(mu2["b"], v);
253+
BOOST_TEST(v == expected);
254+
};
255+
256+
check_variant_round_trip2(std::initializer_list<int>{});
257+
check_variant_round_trip2(std::initializer_list<int>{1, 2, 3});
258+
259+
check_variant_round_trip2(std::vector<int>{});
260+
check_variant_round_trip2(std::vector<int>{1, 2, 3});
261+
262+
check_variant_round_trip2(std::array<int, 0>{});
263+
check_variant_round_trip2(std::array{1, 2, 3});
264+
}
265+
214266
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)