|
Boost-Commit : |
From: daniel_james_at_[hidden]
Date: 2008-07-13 15:29:33
Author: danieljames
Date: 2008-07-13 15:29:32 EDT (Sun, 13 Jul 2008)
New Revision: 47400
URL: http://svn.boost.org/trac/boost/changeset/47400
Log:
Add missing test files.
Added:
sandbox/move/libs/move/test/auto_ptr.cpp (contents, props changed)
sandbox/move/libs/move/test/swappable.cpp (contents, props changed)
Added: sandbox/move/libs/move/test/auto_ptr.cpp
==============================================================================
--- (empty file)
+++ sandbox/move/libs/move/test/auto_ptr.cpp 2008-07-13 15:29:32 EDT (Sun, 13 Jul 2008)
@@ -0,0 +1,186 @@
+// Copyright David Abrahams 2004. Distributed under the Boost
+// 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 <boost/move.hpp>
+#include <boost/assert.hpp>
+#include <boost/static_assert.hpp>
+#include <memory>
+#include "say.hpp"
+
+//
+// A noncopyable class to be stored in an auto_ptr.
+//
+
+class Noncopyable
+{
+ Noncopyable(Noncopyable const& rhs);
+ Noncopyable& operator=(Noncopyable const& rhs);
+ public: // "Ordinary" stuff
+ Noncopyable(int t = 0) : tag(t), resource(++cnt)
+ {
+ SAY("Noncopyable(" << tag << ") #" << resource);
+ }
+
+ ~Noncopyable()
+ {
+ release();
+ }
+
+ static int copies; // count the number of copies
+
+ int tag;
+
+ private: // helper functions
+
+ void release()
+ {
+ BOOST_ASSERT(resource <= cnt); // check for garbage
+ SAY("destroy(" << tag << ") #" << resource);
+ resource = 0;
+ }
+
+ private: // Data members
+ int resource;
+ static int cnt; // count the number of resources
+};
+
+int Noncopyable::cnt;
+int Noncopyable::copies;
+
+typedef std::auto_ptr<Noncopyable> Ptr;
+
+//
+// Some functions we can use to test the passing of Ys in and out of
+// functions.
+//
+Ptr source(int tag)
+{
+ return Ptr(new Noncopyable(tag));
+}
+
+Ptr const csource(int tag)
+{
+ return Ptr(new Noncopyable(tag));
+}
+
+void sink(Ptr)
+{
+ SAY("in rvalue sink");
+}
+
+void sink2(Ptr&)
+{
+ SAY("in non-const lvalue sink2");
+}
+
+void sink2(Ptr const&)
+{
+ SAY("in const lvalue sink2");
+}
+
+void sink3(Ptr&)
+{
+ SAY("in non-const lvalue sink3");
+}
+
+template <class T>
+void tsink(T)
+{
+ SAY("in templated rvalue sink");
+}
+
+int main()
+{
+ BOOST_STATIC_ASSERT((boost::is_same<boost::custom_move_tag,
+ boost::move_type<Ptr>::type>::value));
+
+ SAY(" ------ test 1, direct init from rvalue ------- ");
+ Ptr z2(source(2));
+
+ SAY(" ------ test 2, copy init from rvalue ------- ");
+ Ptr z4 = Ptr(new Noncopyable(4));
+
+ SAY(" ------ test 3, copy init from lvalue ------- ");
+ Ptr z5 = z4;
+ BOOST_ASSERT(z5->tag == 4 && !z4.get());
+
+ SAY(" ------ test 4, direct init from lvalue ------- ");
+ Ptr z6(z5);
+ BOOST_ASSERT(z6->tag == 4 && !z5.get());
+
+
+ SAY(" ------ test 5, construct const ------- ");
+ Ptr const z7;
+
+ //SAY(" ------ test 6, copy init from lvalue ------- ");
+ //Ptr::expect_copy();
+ //Ptr z8 = z7;
+
+ //SAY(" ------ test 7, direct init from lvalue ------- ");
+ //Ptr::expect_copy();
+ //Ptr z9(z7);
+
+ SAY(" ------ test 8, pass rvalue by-value ------- ");
+ sink(source(8));
+
+ //SAY(" ------ test 9, pass const rvalue by-value ------- ");
+ //sink(csource(9));
+
+ SAY(" ------ test 10, pass rvalue by overloaded reference ------- ");
+ sink2(source(10));
+
+ //SAY(" ------ test 11, pass const rvalue by overloaded reference ------- ");
+ //sink2(csource(11));
+
+ SAY(" ------ test 13, pass lvalue by-value ------- ");
+ sink(z5);
+
+ //SAY(" ------ test 14, pass const lvalue by-value ------- ");
+ //sink(z7);
+
+ SAY(" ------ test 15, pass lvalue by-reference ------- ");
+ Ptr z15(new Noncopyable(15));
+ sink2(z4);
+ sink2(z15);
+
+ //SAY(" ------ test 16, pass const lvalue by const reference ------- ");
+ //sink2(z7);
+
+ SAY(" ------ test 17, pass rvalue by value to template param ------- ");
+ tsink(source(17));
+
+ //SAY(" ------ test 18, direct initialize a const Ptr with an Ptr ------- ");
+ //typedef Ptr const YC;
+ //sink2(YC(Ptr()));
+
+ SAY(" ------ test 19, assign from non-const lvalue ------- ");
+ z4 = z5;
+ Ptr z19(new Noncopyable(19));
+ z4 = z19;
+ BOOST_ASSERT(z4->tag == 19 && !z19.get());
+
+ //SAY(" ------ test 20, assign from const lvalue ------- ");
+ //Ptr::expect_copy();
+ //z4 = z7;
+
+ SAY(" ------ test 21, assign from rvalue ------- ");
+ z4 = source(21);
+
+ SAY(" ------ test 22, explicit move direct init from movable lvalue ------- ");
+ BOOST_STATIC_ASSERT(boost::is_movable<Ptr>::value);
+ Ptr z10(boost::move(z2));
+
+ SAY(" ------ test 23, explicit move copy init from movable lvalue ------- ");
+ Ptr z23(new Noncopyable(23));
+ Ptr z11 = boost::move(z23);
+
+ SAY(" ------ test 24, move assign from movable lvalue ------- ");
+ Ptr z24(new Noncopyable(24));
+ z10 = boost::move(z24);
+ BOOST_ASSERT(z10.get() && z10->tag == 24 && !z24.get());
+
+ return 0;
+}
Added: sandbox/move/libs/move/test/swappable.cpp
==============================================================================
--- (empty file)
+++ sandbox/move/libs/move/test/swappable.cpp 2008-07-13 15:29:32 EDT (Sun, 13 Jul 2008)
@@ -0,0 +1,241 @@
+// Copyright David Abrahams 2004. Distributed under the Boost
+// 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 <boost/move.hpp>
+#include <boost/assert.hpp>
+#include <boost/static_assert.hpp>
+#include "say.hpp"
+
+//
+// A sample swappable class.
+//
+// Note that when it says 'move' here, it can mean that the class will be moved
+// by swapping.
+//
+// TODO: I need to distinguish between classes constructed just to be swapped into,
+// and value classes.
+
+class Swappable
+{
+ public: // "Ordinary" stuff
+ Swappable() : resource(++cnt)
+ {
+ SAY("Swappable() #" << resource);
+ }
+
+ Swappable(Swappable const& rhs) : resource(++cnt)
+ {
+ copy(rhs);
+ }
+
+ Swappable& operator=(Swappable const& rhs)
+ {
+ copy(rhs);
+ resource = rhs.resource;
+ return *this;
+ }
+
+ ~Swappable()
+ {
+ release();
+ }
+
+ 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; }
+
+ friend void swap(Swappable& x, Swappable& y) {
+ SAY("SWAP #" << x.resource << ", " << y.resource);
+ BOOST_ASSERT(move_expected);
+ std::swap(x.resource, y.resource);
+ }
+
+ private: // helper functions
+ void release()
+ {
+ BOOST_ASSERT(resource <= cnt); // check for garbage
+ if (!resource)
+ SAY("destroy empty");
+ else
+ SAY("destroy #" << resource);
+ resource = 0;
+ }
+
+ void copy(Swappable const& rhs)
+ {
+ BOOST_ASSERT(rhs.resource <= cnt); // check for garbage
+ SAY("copy #" << this->resource << " <- #" << rhs.resource);
+ if (!copy_expected)
+ {
+ SAY("***** SUBOPTIMAL COPY ******");
+ ++suboptimal_copies;
+ }
+ ++copies;
+ }
+
+ private: // Data members
+ int resource;
+
+ static int cnt; // count the number of resources
+ static bool copy_expected;
+ static bool move_expected;
+};
+
+int Swappable::cnt;
+int Swappable::copies;
+int Swappable::suboptimal_copies;
+bool Swappable::copy_expected;
+bool Swappable::move_expected;
+
+//
+// Some functions we can use to test the passing of Ys in and out of
+// functions.
+//
+Swappable source()
+{
+ return Swappable();
+}
+
+Swappable const csource()
+{
+ return Swappable();
+}
+
+void sink(Swappable)
+{
+ SAY("in rvalue sink");
+}
+
+void sink2(Swappable&)
+{
+ SAY("in non-const lvalue sink2");
+}
+
+void sink2(Swappable const&)
+{
+ SAY("in const lvalue sink2");
+}
+
+void sink3(Swappable&)
+{
+ SAY("in non-const lvalue sink3");
+}
+
+template <class T>
+void tsink(T)
+{
+ SAY("in templated rvalue sink");
+}
+
+int main()
+{
+ BOOST_STATIC_ASSERT((boost::is_same<boost::swap_tag,
+ boost::move_type<Swappable>::type>::value));
+
+ SAY(" ------ test 1, direct init from rvalue ------- ");
+ Swappable::expect_move();
+ Swappable z2(source());
+
+ SAY(" ------ test 2, copy init from rvalue ------- ");
+ Swappable::expect_move();
+ Swappable z4 = Swappable();
+
+ SAY(" ------ test 3, copy init from lvalue ------- ");
+ Swappable::expect_copy();
+ Swappable z5 = z4;
+
+ SAY(" ------ test 4, direct init from lvalue ------- ");
+ Swappable::expect_copy();
+ Swappable z6(z4);
+
+ SAY(" ------ test 5, construct const ------- ");
+ Swappable const z7;
+
+ SAY(" ------ test 6, copy init from lvalue ------- ");
+ Swappable::expect_copy();
+ Swappable z8 = z7;
+
+ SAY(" ------ test 7, direct init from lvalue ------- ");
+ Swappable::expect_copy();
+ Swappable z9(z7);
+
+ SAY(" ------ test 8, pass rvalue by-value ------- ");
+ Swappable::expect_move();
+ sink(source());
+
+ SAY(" ------ test 9, pass const rvalue by-value ------- ");
+ Swappable::expect_move();
+ sink(csource());
+
+ SAY(" ------ test 10, pass rvalue by overloaded reference ------- ");
+ Swappable::expect_move();
+ sink2(source());
+
+ SAY(" ------ test 11, pass const rvalue by overloaded reference ------- ");
+ Swappable::expect_move();
+ sink2(csource());
+
+ SAY(" ------ test 13, pass lvalue by-value ------- ");
+ Swappable::expect_copy();
+ sink(z5);
+
+ SAY(" ------ test 14, pass const lvalue by-value ------- ");
+ Swappable::expect_copy();
+ sink(z7);
+
+ SAY(" ------ test 15, pass lvalue by-reference ------- ");
+ Swappable::expect_copy();
+ sink2(z4);
+
+ SAY(" ------ test 16, pass const lvalue by const reference ------- ");
+ Swappable::expect_copy();
+ sink2(z7);
+
+ SAY(" ------ test 17, pass rvalue by value to template param ------- ");
+ Swappable::expect_move();
+ tsink(source());
+
+ SAY(" ------ test 18, direct initialize a const Swappable with an Swappable ------- ");
+ Swappable::expect_move();
+ typedef Swappable const YC;
+ sink2(YC(Swappable()));
+
+ SAY(" ------ test 19, assign from non-const lvalue ------- ");
+ Swappable::expect_copy();
+ z4 = z5;
+
+ SAY(" ------ test 20, assign from const lvalue ------- ");
+ Swappable::expect_copy();
+ z4 = z7;
+
+ SAY(" ------ test 21, assign from rvalue ------- ");
+ Swappable::expect_copy();
+ z4 = source();
+
+ SAY(" ------ test 22, explicit move direct init from movable lvalue ------- ");
+ BOOST_STATIC_ASSERT(boost::is_movable<Swappable>::value);
+ Swappable::expect_move();
+ Swappable z10(boost::move(z2));
+
+ SAY(" ------ test 23, explicit move copy init from movable lvalue ------- ");
+ Swappable::expect_move();
+ Swappable z11 = boost::move(z9);
+
+ SAY(" ------ test 24, move assign from movable lvalue ------- ");
+ Swappable::expect_move();
+ z10 = boost::move(z8);
+
+ SAY(" ------ test 25, request move construct from non-movable lvalue ------- ");
+ //BOOST_STATIC_ASSERT(!boost::is_movable<std::string>::value);
+ std::string s1("hello");
+ std::string s2(boost::move(s1));
+
+ SAY("----- done, with " << Swappable::suboptimal_copies << " suboptimal copies -----");
+
+ return Swappable::suboptimal_copies == 0 ? 0 : -1;
+}
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