|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r50489 - sandbox/move/libs/move/test
From: daniel_james_at_[hidden]
Date: 2009-01-05 18:10:10
Author: danieljames
Date: 2009-01-05 18:10:09 EST (Mon, 05 Jan 2009)
New Revision: 50489
URL: http://svn.boost.org/trac/boost/changeset/50489
Log:
Starting on generic move tests.
Added:
sandbox/move/libs/move/test/move_boost_move.cpp (contents, props changed)
Text files modified:
sandbox/move/libs/move/test/Jamfile.v2 | 1
sandbox/move/libs/move/test/auto_ptr.cpp | 17 +++++++-
sandbox/move/libs/move/test/generic_tests.hpp | 31 ++++++++++++---
sandbox/move/libs/move/test/move.cpp | 46 +++++++++++-----------
sandbox/move/libs/move/test/noncopyable.cpp | 78 +++++++++++++++++++++------------------
sandbox/move/libs/move/test/x.hpp | 30 +++++----------
sandbox/move/libs/move/test/y.hpp | 30 ++++-----------
7 files changed, 122 insertions(+), 111 deletions(-)
Modified: sandbox/move/libs/move/test/Jamfile.v2
==============================================================================
--- sandbox/move/libs/move/test/Jamfile.v2 (original)
+++ sandbox/move/libs/move/test/Jamfile.v2 2009-01-05 18:10:09 EST (Mon, 05 Jan 2009)
@@ -17,6 +17,7 @@
[ run move_test.cpp ]
[ run no_sfinae_test.cpp ]
[ run has_swap_overload.cpp ]
+ [ run move_boost_move.cpp ]
;
test-suite "move-fail" :
Modified: sandbox/move/libs/move/test/auto_ptr.cpp
==============================================================================
--- sandbox/move/libs/move/test/auto_ptr.cpp (original)
+++ sandbox/move/libs/move/test/auto_ptr.cpp 2009-01-05 18:10:09 EST (Mon, 05 Jan 2009)
@@ -9,6 +9,7 @@
#include <boost/static_assert.hpp>
#include <memory>
#include "say.hpp"
+#include "./generic_tests.hpp"
//
// A noncopyable class to be stored in an auto_ptr.
@@ -32,6 +33,7 @@
static int copies; // count the number of copies
int tag;
+ int resource;
private: // helper functions
@@ -43,7 +45,6 @@
}
private: // Data members
- int resource;
static int cnt; // count the number of resources
};
@@ -52,6 +53,14 @@
typedef std::auto_ptr<Noncopyable> Ptr;
+struct extract_auto_ptr_value {
+ int operator()(Ptr const& x) {
+ return x->resource;
+ }
+};
+
+typedef test::generic_tests<Ptr, extract_auto_ptr_value> generic_ptr_tests;
+
//
// Some functions we can use to test the passing of Ys in and out of
// functions.
@@ -87,13 +96,15 @@
}
template <class T>
-void tsink(T)
+void template_sink(T)
{
SAY("in templated rvalue sink");
}
int main()
{
+ generic_ptr_tests::move_tests();
+
BOOST_STATIC_ASSERT((boost::is_same<boost::custom_move_tag,
boost::move_type<Ptr>::type>::value));
@@ -150,7 +161,7 @@
//sink2(z7);
SAY(" ------ test 17, pass rvalue by value to template param ------- ");
- tsink(source(17));
+ template_sink(source(17));
//SAY(" ------ test 18, direct initialize a const Ptr with an Ptr ------- ");
//typedef Ptr const YC;
Modified: sandbox/move/libs/move/test/generic_tests.hpp
==============================================================================
--- sandbox/move/libs/move/test/generic_tests.hpp (original)
+++ sandbox/move/libs/move/test/generic_tests.hpp 2009-01-05 18:10:09 EST (Mon, 05 Jan 2009)
@@ -6,10 +6,23 @@
#if !defined(BOOST_MOVE_TEST_GENERIC_TESTS_HEADER)
#define BOOST_MOVE_TEST_GENERIC_TESTS_HEADER
-#include <boost/move.hpp>
+#include <boost/move/move.hpp>
#include "say.hpp"
namespace test {
+ bool copy_allowed = false;
+ int suboptimal_copies = 0;
+
+ void copied() {
+ if(!copy_allowed) {
+ SAY("***** SUBOPTIMAL COPY ******");
+ ++suboptimal_copies;
+ }
+ }
+
+ void moved() {
+ }
+
template <class T>
void tsink(T)
{
@@ -17,6 +30,13 @@
}
template <class X>
+ struct default_extract_value {
+ int operator()(X const& x) {
+ return x.value();
+ }
+ };
+
+ template <class X, class ExtractValue = default_extract_value<X> >
struct generic_tests {
//
@@ -57,7 +77,7 @@
static void move_tests()
{
SAY("****** Move Tests *****");
- X::expect_move();
+ copy_allowed = false;
SAY(" ------ test 1, direct init from rvalue ------- ");
X z2(source());
@@ -71,8 +91,8 @@
SAY(" ------ test 3, pass rvalue by-value ------- ");
sink(source());
- // TODO: This is failing for my noncopyable type. I'm not sure if
- // this is right or wrong.
+ // TODO: This is failing for my noncopyable type.
+ // Maybe it should be moved into a separate test.
//SAY(" ------ test 4, pass const rvalue by-value ------- ");
//sink(csource());
@@ -105,8 +125,7 @@
static void copy_tests()
{
SAY("****** Copy Tests *****");
-
- X::expect_copy();
+ copy_allowed = false;
SAY(" ------ test 3, copy init from lvalue ------- ");
X z4;
Modified: sandbox/move/libs/move/test/move.cpp
==============================================================================
--- sandbox/move/libs/move/test/move.cpp (original)
+++ sandbox/move/libs/move/test/move.cpp 2009-01-05 18:10:09 EST (Mon, 05 Jan 2009)
@@ -51,96 +51,96 @@
int main()
{
SAY(" ------ test 1, direct init from rvalue ------- ");
- X::expect_move();
+ //X::expect_move();
X z2(source());
SAY(" ------ test 2, copy init from rvalue ------- ");
- X::expect_move();
+ //X::expect_move();
X z4 = X();
SAY(" ------ test 3, copy init from lvalue ------- ");
- X::expect_copy();
+ //X::expect_copy();
X z5 = z4;
SAY(" ------ test 4, direct init from lvalue ------- ");
- X::expect_copy();
+ //X::expect_copy();
X z6(z4);
SAY(" ------ test 5, construct const ------- ");
X const z7;
SAY(" ------ test 6, copy init from lvalue ------- ");
- X::expect_copy();
+ //X::expect_copy();
X z8 = z7;
SAY(" ------ test 7, direct init from lvalue ------- ");
- X::expect_copy();
+ //X::expect_copy();
X z9(z7);
SAY(" ------ test 8, pass rvalue by-value ------- ");
- X::expect_move();
+ //X::expect_move();
sink(source());
SAY(" ------ test 9, pass const rvalue by-value ------- ");
- X::expect_move();
+ //X::expect_move();
sink(csource());
SAY(" ------ test 10, pass rvalue by overloaded reference ------- ");
- X::expect_move();
+ //X::expect_move();
sink2(source());
SAY(" ------ test 11, pass const rvalue by overloaded reference ------- ");
- X::expect_move();
+ //X::expect_move();
sink2(csource());
SAY(" ------ test 13, pass lvalue by-value ------- ");
- X::expect_copy();
+ //X::expect_copy();
sink(z5);
SAY(" ------ test 14, pass const lvalue by-value ------- ");
- X::expect_copy();
+ //X::expect_copy();
sink(z7);
SAY(" ------ test 15, pass lvalue by-reference ------- ");
- X::expect_copy();
+ //X::expect_copy();
sink2(z4);
SAY(" ------ test 16, pass const lvalue by const reference ------- ");
- X::expect_copy();
+ //X::expect_copy();
sink2(z7);
SAY(" ------ test 17, pass rvalue by value to template param ------- ");
- X::expect_move();
+ //X::expect_move();
tsink(source());
SAY(" ------ test 18, direct initialize a const X with an X ------- ");
- X::expect_move();
+ //X::expect_move();
typedef X const XC;
sink2(XC(X()));
SAY(" ------ test 19, assign from non-const lvalue ------- ");
- X::expect_copy();
+ //X::expect_copy();
z4 = z5;
SAY(" ------ test 20, assign from const lvalue ------- ");
- X::expect_copy();
+ //X::expect_copy();
z4 = z7;
SAY(" ------ test 21, assign from rvalue ------- ");
- X::expect_move();
+ //X::expect_move();
z4 = source();
SAY(" ------ test 22, explicit move direct init from movable lvalue ------- ");
BOOST_STATIC_ASSERT(boost::is_movable<X>::value);
- X::expect_move();
+ //X::expect_move();
X z10(boost::move(z2));
SAY(" ------ test 23, explicit move copy init from movable lvalue ------- ");
- X::expect_move();
+ //X::expect_move();
X z11 = boost::move(z9);
SAY(" ------ test 24, move assign from movable lvalue ------- ");
- X::expect_move();
+ //X::expect_move();
z10 = boost::move(z8);
SAY(" ------ test 25, request move construct from non-movable lvalue ------- ");
@@ -148,6 +148,6 @@
std::string s1("hello");
std::string s2(boost::move(s1));
- SAY("----- done, with " << X::suboptimal_copies << " suboptimal copies -----");
+ SAY("----- done, with " << test::suboptimal_copies << " suboptimal copies -----");
return 0;
}
Added: sandbox/move/libs/move/test/move_boost_move.cpp
==============================================================================
--- (empty file)
+++ sandbox/move/libs/move/test/move_boost_move.cpp 2009-01-05 18:10:09 EST (Mon, 05 Jan 2009)
@@ -0,0 +1,16 @@
+// Copyright David Abrahams 2004.
+// Copyright Daniel James 2008.
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#define DEBUG_MOVE
+#include "x.hpp"
+#include "y.hpp"
+
+#include <boost/static_assert.hpp>
+#include "./generic_tests.hpp"
+
+int main() {
+ test::generic_tests<X>::copy_and_move_tests();
+ test::generic_tests<Y>::move_tests();
+}
Modified: sandbox/move/libs/move/test/noncopyable.cpp
==============================================================================
--- sandbox/move/libs/move/test/noncopyable.cpp (original)
+++ sandbox/move/libs/move/test/noncopyable.cpp 2009-01-05 18:10:09 EST (Mon, 05 Jan 2009)
@@ -8,55 +8,59 @@
#include <boost/static_assert.hpp>
#include "say.hpp"
#include "y.hpp"
+#include "./generic_tests.hpp"
+
//
// Some functions we can use to test the passing of Ys in and out of
// functions.
//
-Y source()
+Y y_source()
{
return Y();
}
-Y const csource()
+Y const y_csource()
{
return Y();
}
-void sink(Y)
+void y_sink(Y)
{
SAY("in rvalue sink");
}
-void sink2(Y&)
+void y_sink2(Y&)
{
SAY("in non-const lvalue sink2");
}
-void sink2(Y const&)
+void y_sink2(Y const&)
{
SAY("in const lvalue sink2");
}
-void sink3(Y&)
+void y_sink3(Y&)
{
SAY("in non-const lvalue sink3");
}
template <class T>
-void tsink(T)
+void y_tsink(T)
{
SAY("in templated rvalue sink");
}
int main()
{
- SAY(" ------ test 1, direct init from rvalue ------- ");
- Y::expect_move();
- Y z2(source());
+ test::generic_tests<Y>::move_tests();
+
+ // SAY(" ------ test 1, direct init from rvalue ------- ");
+ //Y::expect_move();
+ Y z2(y_source());
- SAY(" ------ test 2, copy init from rvalue ------- ");
- Y::expect_move();
+ // SAY(" ------ test 2, copy init from rvalue ------- ");
+ //Y::expect_move();
Y z4 = Y();
//SAY(" ------ test 3, copy init from lvalue ------- ");
@@ -67,7 +71,7 @@
//Y::expect_copy();
//Y z6(z4);
- SAY(" ------ test 5, construct const ------- ");
+ // SAY(" ------ test 5, construct const ------- ");
Y const z7;
//SAY(" ------ test 6, copy init from lvalue ------- ");
@@ -80,13 +84,13 @@
//Y z9(z7);
Y z9;
- SAY(" ------ test 8, pass rvalue by-value ------- ");
- Y::expect_move();
- sink(source());
-
- SAY(" ------ test 9, pass const rvalue by-value ------- ");
- Y::expect_move();
- sink(csource());
+ //SAY(" ------ test 8, pass rvalue by-value ------- ");
+ //Y::expect_move();
+ //y_sink(y_source());
+
+ //SAY(" ------ test 9, pass const rvalue by-value ------- ");
+ //Y::expect_move();
+ //y_sink(y_csource());
// http://lists.boost.org/Archives/boost/2008/06/138884.php
//
@@ -97,36 +101,36 @@
// TODO: Look this up properly.
//SAY(" ------ test 10, pass rvalue by overloaded reference ------- ");
//Y::expect_move();
- //sink2(source());
+ //y_sink2(y_source());
//SAY(" ------ test 11, pass const rvalue by overloaded reference ------- ");
//Y::expect_move();
- //sink2(csource());
+ //y_sink2(y_csource());
//SAY(" ------ test 13, pass lvalue by-value ------- ");
//Y::expect_copy();
- //sink(z5);
+ //y_sink(z5);
//SAY(" ------ test 14, pass const lvalue by-value ------- ");
//Y::expect_copy();
- //sink(z7);
+ //y_sink(z7);
SAY(" ------ test 15, pass lvalue by-reference ------- ");
- Y::expect_copy();
- sink2(z4);
+ //Y::expect_copy();
+ y_sink2(z4);
SAY(" ------ test 16, pass const lvalue by const reference ------- ");
- Y::expect_copy();
- sink2(z7);
+ //Y::expect_copy();
+ y_sink2(z7);
SAY(" ------ test 17, pass rvalue by value to template param ------- ");
- Y::expect_move();
- tsink(source());
+ //Y::expect_move();
+ y_tsink(y_source());
//SAY(" ------ test 18, direct initialize a const Y with an Y ------- ");
//Y::expect_move();
//typedef Y const YC;
- //sink2(YC(Y()));
+ //y_sink2(YC(Y()));
//SAY(" ------ test 19, assign from non-const lvalue ------- ");
//Y::expect_copy();
@@ -137,22 +141,22 @@
//z4 = z7;
SAY(" ------ test 21, assign from rvalue ------- ");
- Y::expect_move();
- z4 = source();
+ //Y::expect_move();
+ z4 = y_source();
SAY(" ------ test 22, explicit move direct init from movable lvalue ------- ");
BOOST_STATIC_ASSERT(boost::is_movable<Y>::value);
- Y::expect_move();
+ //Y::expect_move();
Y z10(boost::move(z2));
SAY(" ------ test 23, explicit move copy init from movable lvalue ------- ");
- Y::expect_move();
+ //Y::expect_move();
Y z11 = boost::move(z9);
SAY(" ------ test 24, move assign from movable lvalue ------- ");
- Y::expect_move();
+ //Y::expect_move();
z10 = boost::move(z8);
- SAY("----- done, with " << Y::suboptimal_copies << " suboptimal copies -----");
+ SAY("----- done, with " << test::suboptimal_copies << " suboptimal copies -----");
return 0;
}
Modified: sandbox/move/libs/move/test/x.hpp
==============================================================================
--- sandbox/move/libs/move/test/x.hpp (original)
+++ sandbox/move/libs/move/test/x.hpp 2009-01-05 18:10:09 EST (Mon, 05 Jan 2009)
@@ -7,6 +7,7 @@
# include <boost/move/move.hpp>
# include <boost/assert.hpp>
# include "say.hpp"
+# include "./generic_tests.hpp"
//
// A sample movable class.
@@ -38,7 +39,7 @@
{
BOOST_ASSERT(rhs.source.resource <= cnt); // check for garbage
SAY("MOVE #" << resource);
- BOOST_ASSERT(move_expected);
+ test::moved();
rhs.source.resource = 0;
BOOST_ASSERT(resource);
}
@@ -50,19 +51,17 @@
release();
resource = rhs.resource;
SAY("MOVE #" << resource);
- BOOST_ASSERT(move_expected);
+ test::moved();
rhs.resource = 0;
BOOST_ASSERT(resource);
return *this;
}
static int copies; // count the number of copies
- static int suboptimal_copies; // count the number of copies that should've been avoidable
-
- static void expect_copy() { copy_expected = true; move_expected = false; }
- static void expect_move() { copy_expected = false; move_expected = true; }
operator boost::move_from<X>() { return *this; }
+
+ int value() const { return resource; }
private: // helper functions
void release()
@@ -79,15 +78,7 @@
{
BOOST_ASSERT(rhs.resource <= cnt); // check for garbage
SAY("copy #" << this->resource << " <- #" << rhs.resource);
- if (!copy_expected)
- {
- SAY("***** SUBOPTIMAL COPY ******");
- ++suboptimal_copies;
- }
- else
- {
- move_expected = true; // OK to move now
- }
+ test::copied();
++copies;
}
@@ -95,14 +86,13 @@
int resource;
static int cnt; // count the number of resources
- static bool copy_expected;
- static bool move_expected;
};
int X::cnt;
int X::copies;
-int X::suboptimal_copies;
-bool X::copy_expected;
-bool X::move_expected;
+
+struct X_source {
+ X operator()() { return X(); }
+};
#endif // X_DWA2004410_HPP
Modified: sandbox/move/libs/move/test/y.hpp
==============================================================================
--- sandbox/move/libs/move/test/y.hpp (original)
+++ sandbox/move/libs/move/test/y.hpp 2009-01-05 18:10:09 EST (Mon, 05 Jan 2009)
@@ -8,6 +8,7 @@
# include <boost/move/move.hpp>
# include <boost/assert.hpp>
# include "say.hpp"
+# include "./generic_tests.hpp"
//
// A sample movable class.
@@ -33,7 +34,7 @@
{
BOOST_ASSERT(rhs.source.resource <= cnt); // check for garbage
SAY("MOVE #" << resource);
- BOOST_ASSERT(move_expected);
+ test::moved();
rhs.source.resource = 0;
BOOST_ASSERT(resource);
}
@@ -45,7 +46,7 @@
release();
resource = rhs.resource;
SAY("MOVE #" << resource);
- BOOST_ASSERT(move_expected);
+ test::moved();
rhs.resource = 0;
BOOST_ASSERT(resource);
return *this;
@@ -55,13 +56,11 @@
{
return boost::move_from<Y>(*this);
}
-
+
+ int value() const { return resource; }
+
static int copies; // count the number of copies
- static int suboptimal_copies; // count the number of copies that should've been avoidable
-
- static void expect_copy() { copy_expected = true; move_expected = false; }
- static void expect_move() { copy_expected = false; move_expected = true; }
-
+
private: // helper functions
void release()
{
@@ -77,15 +76,7 @@
{
BOOST_ASSERT(rhs.resource <= cnt); // check for garbage
SAY("copy #" << this->resource << " <- #" << rhs.resource);
- if (!copy_expected)
- {
- SAY("***** SUBOPTIMAL COPY ******");
- ++suboptimal_copies;
- }
- else
- {
- move_expected = true; // OK to move now
- }
+ test::copied();
++copies;
}
@@ -93,14 +84,9 @@
int resource;
static int cnt; // count the number of resources
- static bool copy_expected;
- static bool move_expected;
};
int Y::cnt;
int Y::copies;
-int Y::suboptimal_copies;
-bool Y::copy_expected;
-bool Y::move_expected;
#endif // Y_DWA2004410_HPP
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk