| 1 | // Copyright (c) 2016-2024 Antony Polukhin |
| 2 | // |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #include <boost/pfr/io.hpp> |
| 7 | |
| 8 | #include <sstream> |
| 9 | #include <string> |
| 10 | |
| 11 | #include <boost/core/lightweight_test.hpp> |
| 12 | |
| 13 | template <class T> |
| 14 | void test_write_read(const T& value) { |
| 15 | T result; |
| 16 | std::stringstream ss; |
| 17 | ss << boost::pfr::io(value); |
| 18 | ss >> boost::pfr::io(result); |
| 19 | BOOST_TEST_EQ(value.f0, result.f0); |
| 20 | BOOST_TEST_EQ(value.f1, result.f1); |
| 21 | BOOST_TEST_EQ(value.f2, result.f2); |
| 22 | BOOST_TEST_EQ(value.f3, result.f3); |
| 23 | BOOST_TEST_EQ(value.f4, result.f4); |
| 24 | } |
| 25 | |
| 26 | template <class T> |
| 27 | void to_string_test(const T& value, const char* ethalon) { |
| 28 | std::stringstream ss; |
| 29 | ss << boost::pfr::io(value); |
| 30 | BOOST_TEST_EQ(ss.str(), ethalon); |
| 31 | } |
| 32 | |
| 33 | template <class T> |
| 34 | void test_type(const T& value, const char* ethalon) { |
| 35 | test_write_read(value); |
| 36 | to_string_test(value, ethalon); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | int main() { |
| 41 | struct test4 { |
| 42 | int f0; |
| 43 | std::string f1; |
| 44 | char f2; |
| 45 | int f3; |
| 46 | std::string f4; |
| 47 | }; |
| 48 | test_type( |
| 49 | value: test4{.f0: 1, .f1: {"my o my" }, .f2: '3', .f3: 4, .f4: {"hello there!" } }, |
| 50 | ethalon: "{1, \"my o my\", 3, 4, \"hello there!\"}" |
| 51 | ); |
| 52 | |
| 53 | #if 0 |
| 54 | // TODO: |
| 55 | std::string f1_referenced{"my O my" }; |
| 56 | std::string f4_referenced{"Hello There!" }; |
| 57 | struct test5 { |
| 58 | int f0; |
| 59 | const std::string& f1; |
| 60 | char f2; |
| 61 | int f3; |
| 62 | const std::string& f4; |
| 63 | }; |
| 64 | to_string_test( |
| 65 | test5{1, f1_referenced, '3', 4, f4_referenced }, |
| 66 | "{1, \"my o my\", 3, 4, \"hello there!\"}" |
| 67 | ); |
| 68 | #endif |
| 69 | |
| 70 | return boost::report_errors(); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | |