]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
1 | // Copyright (c) 2016-2020 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/ops.hpp> | |
7 | #include <boost/pfr/io.hpp> | |
8 | ||
9 | #include <iostream> | |
10 | #include <typeinfo> | |
11 | #include <tuple> | |
12 | #include <sstream> | |
13 | #include <set> | |
14 | #include <string> | |
15 | ||
16 | #include <boost/core/lightweight_test.hpp> | |
17 | ||
18 | #ifdef __clang__ | |
19 | # pragma clang diagnostic ignored "-Wmissing-braces" | |
20 | #endif | |
21 | ||
22 | unsigned test_union_counter = 0; | |
23 | ||
24 | union test_union { | |
25 | int i; | |
26 | float f; | |
27 | }; | |
28 | ||
29 | inline bool operator< (test_union l, test_union r) noexcept { ++test_union_counter; return l.i < r.i; } | |
30 | inline bool operator<=(test_union l, test_union r) noexcept { ++test_union_counter; return l.i <= r.i; } | |
31 | inline bool operator> (test_union l, test_union r) noexcept { ++test_union_counter; return l.i > r.i; } | |
32 | inline bool operator>=(test_union l, test_union r) noexcept { ++test_union_counter; return l.i >= r.i; } | |
33 | inline bool operator==(test_union l, test_union r) noexcept { ++test_union_counter; return l.i == r.i; } | |
34 | inline bool operator!=(test_union l, test_union r) noexcept { ++test_union_counter; return l.i != r.i; } | |
35 | inline std::ostream& operator<<(std::ostream& os, test_union src) { ++test_union_counter; return os << src.i; } | |
36 | inline std::istream& operator>>(std::istream& is, test_union& src) { ++test_union_counter; return is >> src.i; } | |
37 | ||
38 | ||
39 | template <class T> | |
40 | void test_comparable_struct() { | |
41 | using namespace boost::pfr; | |
42 | T s1 {0, 1, false, 6,7,8,9,10,11}; | |
43 | T s2 = s1; | |
44 | T s3 {0, 1, false, 6,7,8,9,10,11111}; | |
45 | BOOST_TEST(eq(s1, s2)); | |
46 | BOOST_TEST(le(s1, s2)); | |
47 | BOOST_TEST(ge(s1, s2)); | |
48 | BOOST_TEST(!ne(s1, s2)); | |
49 | BOOST_TEST(!eq(s1, s3)); | |
50 | BOOST_TEST(ne(s1, s3)); | |
51 | BOOST_TEST(lt(s1, s3)); | |
52 | BOOST_TEST(gt(s3, s2)); | |
53 | BOOST_TEST(le(s1, s3)); | |
54 | BOOST_TEST(ge(s3, s2)); | |
55 | ||
56 | std::cout << boost::pfr::io(s1); | |
57 | ||
58 | T s4; | |
59 | std::stringstream ss; | |
60 | ss.exceptions ( std::ios::failbit); | |
61 | ss << boost::pfr::io(s1); | |
62 | ss >> boost::pfr::io(s4); | |
63 | std::cout << boost::pfr::io(s4); | |
64 | BOOST_TEST(eq(s1, s4)); | |
65 | } | |
66 | ||
67 | void test_empty_struct() { | |
68 | struct empty {}; | |
69 | std::cout << boost::pfr::io(empty{}); | |
70 | BOOST_TEST(boost::pfr::eq(empty{}, empty{})); | |
71 | } | |
72 | ||
73 | void test_implicit_conversions() { | |
74 | std::stringstream ss; | |
75 | ss << boost::pfr::io(std::true_type{}); | |
76 | BOOST_TEST_EQ(ss.str(), "1"); // Does not break implicit conversion | |
77 | } | |
78 | ||
79 | ||
80 | namespace foo { | |
81 | struct comparable_struct { | |
82 | int i; short s; bool bl; int a,b,c,d,e,f; | |
83 | }; | |
84 | } | |
85 | ||
86 | int main() { | |
87 | test_comparable_struct<foo::comparable_struct>(); | |
88 | ||
89 | struct local_comparable_struct { | |
90 | int i; short s; bool bl; int a,b,c,d,e,f; | |
91 | }; | |
92 | test_comparable_struct<local_comparable_struct>(); | |
93 | ||
94 | struct local_comparable_struct_with_union { | |
95 | int i; short s; bool bl; int a,b,c,d,e; test_union u; | |
96 | }; | |
97 | test_comparable_struct<local_comparable_struct_with_union>(); | |
98 | ||
99 | // Making sure that test_union overloaded operations were called. | |
100 | BOOST_TEST_EQ(test_union_counter, 17); | |
101 | ||
102 | test_empty_struct(); | |
103 | test_implicit_conversions(); | |
104 | ||
105 | return boost::report_errors(); | |
106 | } | |
107 | ||
108 |