mirror of
https://github.com/boostorg/core.git
synced 2025-11-28 21:30:09 +01:00
Implement boost::exchange
This commit is contained in:
65
test/exchange_test.cpp
Normal file
65
test/exchange_test.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
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();
|
||||
}
|
||||
Reference in New Issue
Block a user