mirror of
https://github.com/boostorg/core.git
synced 2025-05-10 07:13:54 +00:00
66 lines
1022 B
C++
66 lines
1022 B
C++
/*
|
|
Copyright 2018 Glen Joseph Fernandes
|
|
(glenjofe@gmail.com)
|
|
|
|
Distributed under the Boost Software License, Version 1.0.
|
|
(http://www.boost.org/LICENSE_1_0.txt)
|
|
*/
|
|
#include <boost/core/exchange.hpp>
|
|
#include <boost/core/lightweight_test.hpp>
|
|
|
|
void test1()
|
|
{
|
|
int i = 1;
|
|
BOOST_TEST(boost::exchange(i, 2) == 1);
|
|
BOOST_TEST(i == 2);
|
|
}
|
|
|
|
class C1 {
|
|
public:
|
|
explicit C1(int i)
|
|
: i_(i) { }
|
|
int i() const {
|
|
return i_;
|
|
}
|
|
private:
|
|
int i_;
|
|
};
|
|
|
|
void test2()
|
|
{
|
|
C1 x(1);
|
|
BOOST_TEST(boost::exchange(x, C1(2)).i() == 1);
|
|
BOOST_TEST(x.i() == 2);
|
|
}
|
|
|
|
class C2 {
|
|
public:
|
|
explicit C2(int i)
|
|
: i_(i) { }
|
|
operator C1() const {
|
|
return C1(i_);
|
|
}
|
|
int i() const {
|
|
return i_;
|
|
}
|
|
private:
|
|
C2(const C2&);
|
|
C2& operator=(const C2&);
|
|
int i_;
|
|
};
|
|
|
|
void test3()
|
|
{
|
|
C1 x(1);
|
|
BOOST_TEST(boost::exchange(x, C2(2)).i() == 1);
|
|
BOOST_TEST(x.i() == 2);
|
|
}
|
|
|
|
int main()
|
|
{
|
|
test1();
|
|
test2();
|
|
test3();
|
|
return boost::report_errors();
|
|
}
|