1 /*
2 Copyright 2018 Glen Joseph Fernandes
3 (glenjofe@gmail.com)
4 
5 Distributed under the Boost Software License, Version 1.0.
6 (http://www.boost.org/LICENSE_1_0.txt)
7 */
8 #include <boost/core/empty_value.hpp>
9 #include <boost/core/lightweight_test.hpp>
10 
11 struct empty {
valueempty12     int value() const {
13         return 1;
14     }
valueempty15     int value() {
16         return 2;
17     }
18 };
19 
20 class type {
21 public:
type(int count)22     explicit type(int count)
23         : value_(count) { }
value() const24     int value() const {
25         return value_ + 1;
26     }
value()27     int value() {
28         return value_ + 2;
29     }
30 private:
31     int value_;
32 };
33 
test_int()34 void test_int()
35 {
36     const boost::empty_value<int> v1(boost::empty_init_t(), 7);
37     BOOST_TEST(v1.get() == 7);
38     boost::empty_value<int> v2 = boost::empty_init_t();
39     BOOST_TEST(v2.get() == 0);
40     v2 = v1;
41     BOOST_TEST(v2.get() == 7);
42     v2.get() = 8;
43     BOOST_TEST(v2.get() == 8);
44 }
45 
test_empty()46 void test_empty()
47 {
48     const boost::empty_value<empty> v1 = boost::empty_init_t();
49     BOOST_TEST(v1.get().value() == 1);
50     boost::empty_value<empty> v2;
51     BOOST_TEST(v2.get().value() == 2);
52 }
53 
test_type()54 void test_type()
55 {
56     const boost::empty_value<type> v1(boost::empty_init_t(), 2);
57     BOOST_TEST(v1.get().value() == 3);
58     boost::empty_value<type> v2(boost::empty_init_t(), 3);
59     BOOST_TEST(v2.get().value() == 5);
60     v2 = v1;
61     BOOST_TEST(v2.get().value() == 4);
62     v2.get() = type(4);
63     BOOST_TEST(v2.get().value() == 6);
64 }
65 
main()66 int main()
67 {
68     test_int();
69     test_empty();
70     test_type();
71     return boost::report_errors();
72 }
73