• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/exchange.hpp>
9 #include <boost/core/lightweight_test.hpp>
10 
test1()11 void test1()
12 {
13     int i = 1;
14     BOOST_TEST(boost::exchange(i, 2) == 1);
15     BOOST_TEST(i == 2);
16 }
17 
18 class C1 {
19 public:
C1(int i)20     explicit C1(int i)
21         : i_(i) { }
i() const22     int i() const {
23         return i_;
24     }
25 private:
26     int i_;
27 };
28 
test2()29 void test2()
30 {
31     C1 x(1);
32     BOOST_TEST(boost::exchange(x, C1(2)).i() == 1);
33     BOOST_TEST(x.i() == 2);
34 }
35 
36 class C2 {
37 public:
C2(int i)38     explicit C2(int i)
39         : i_(i) { }
operator C1() const40     operator C1() const {
41         return C1(i_);
42     }
i() const43     int i() const {
44         return i_;
45     }
46 private:
47     int i_;
48 };
49 
test3()50 void test3()
51 {
52     C1 x(1);
53     BOOST_TEST(boost::exchange(x, C2(2)).i() == 1);
54     BOOST_TEST(x.i() == 2);
55 }
56 
57 class C3 {
58 public:
C3(int i)59     explicit C3(int i)
60         : i_(i) { }
C3(const C3 & c)61     C3(const C3& c)
62         : i_(c.i_) { }
operator =(const C1 & c)63     C3& operator=(const C1& c) {
64         i_ = c.i();
65         return *this;
66     }
i() const67     int i() const {
68         return i_;
69     }
70 private:
71     C3& operator=(const C3&);
72     int i_;
73 };
74 
test4()75 void test4()
76 {
77     C3 x(1);
78     BOOST_TEST(boost::exchange(x, C1(2)).i() == 1);
79     BOOST_TEST(x.i() == 2);
80 }
81 
main()82 int main()
83 {
84     test1();
85     test2();
86     test3();
87     test4();
88     return boost::report_errors();
89 }
90