• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2016-2021 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>
test_write_read(const T & value)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>
to_string_test(const T & value,const char * ethalon)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>
test_type(const T & value,const char * ethalon)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 
41 struct with_operator{};
operator ==(with_operator,with_operator)42 inline bool operator==(with_operator, with_operator) {
43     return true;
44 }
operator <<(std::ostream & os,with_operator)45 std::ostream& operator<<(std::ostream& os, with_operator) {
46     return os << "{with_operator}";
47 }
operator >>(std::istream & is,with_operator &)48 std::istream& operator>>(std::istream& is, with_operator&) {
49     std::string s;
50     is >> s;
51     return is;
52 }
53 
main()54 int main() {
55     struct test1 {
56         int f0;
57         int f1;
58         char f2;
59         int f3;
60         short f4;
61     };
62     test_type(test1{1, 2, '3', 4, 5}, "{1, 2, 3, 4, 5}");
63     test_type(test1{199, 299, '9', 499, 599}, "{199, 299, 9, 499, 599}");
64 
65     struct test2 {
66         with_operator f0;
67         with_operator f1;
68         with_operator f2;
69         with_operator f3;
70         with_operator f4;
71     };
72     test_type(test2{}, "{{with_operator}, {with_operator}, {with_operator}, {with_operator}, {with_operator}}");
73 
74     struct test3 {
75         int f0;
76         int f1;
77         char f2;
78         int f3;
79         with_operator f4;
80     };
81 
82     test_type(
83         test3{1, 2, '3', 4, {}},
84         "{1, 2, 3, 4, {with_operator}}"
85     );
86 
87     return boost::report_errors();
88 }
89 
90 
91