Skip to content

Instantly share code, notes, and snippets.

@sean-parent
Last active December 23, 2015 21:09
Show Gist options
  • Save sean-parent/6694092 to your computer and use it in GitHub Desktop.
Save sean-parent/6694092 to your computer and use it in GitHub Desktop.
Snippet from asl to test regular and movable semantics.
namespace adobe {
// Precondition: x != T()
template <typename T>
void test_regular(const T& x)
{
assert(x != T());
T y = x; // Copy-construct
BOOST_CHECK(x == y);
T z; // Default-construcct
BOOST_CHECK(z != x);
z = y; // Assignment
BOOST_CHECK(x == z);
T w;
swap(y, w);
BOOST_CHECK(x == w && x != y && y == T());
}
/*
Precondition: x != T(), remote_address(T) is defined.
*/
template <typename T>
void test_movable(const T& x)
{
assert(x != T());
assert(is_movable<T>::value);
test_regular(x);
T y = x;
// move construction (and RVO)
const void* addr = remote_address(y);
T z = adobe::move(y);
BOOST_CHECK(y == T());
BOOST_CHECK(z == x);
BOOST_CHECK(remote_address(z) == addr);
// move assignment
y = adobe::move(z);
BOOST_CHECK(z == T());
BOOST_CHECK(y == x);
BOOST_CHECK(remote_address(y) == addr);
}
const void* remote_address(const string_t& x)
{
assert(!x.empty());
return x.begin();
}
} // namespace adobe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment