Boost logo

Boost-Commit :

From: daniel_james_at_[hidden]
Date: 2008-05-11 09:14:31


Author: danieljames
Date: 2008-05-11 09:14:31 EDT (Sun, 11 May 2008)
New Revision: 45280
URL: http://svn.boost.org/trac/boost/changeset/45280

Log:
Instead of comparing potentially dangling pointers in move_tests check if any objects have been constructed.
Added:
   branches/unordered/trunk/libs/unordered/test/helpers/count.hpp (contents, props changed)
Text files modified:
   branches/unordered/trunk/libs/unordered/test/objects/test.hpp | 5 +++--
   branches/unordered/trunk/libs/unordered/test/unordered/move_tests.cpp | 34 +++++++++++++++++-----------------
   2 files changed, 20 insertions(+), 19 deletions(-)

Added: branches/unordered/trunk/libs/unordered/test/helpers/count.hpp
==============================================================================
--- (empty file)
+++ branches/unordered/trunk/libs/unordered/test/helpers/count.hpp 2008-05-11 09:14:31 EDT (Sun, 11 May 2008)
@@ -0,0 +1,61 @@
+
+// Copyright 2008 Daniel James.
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or move at http://www.boost.org/LICENSE_1_0.txt)
+
+#if !defined(BOOST_UNORDERED_TEST_HELPERS_COUNT_HEAD)
+#define BOOST_UNORDERED_TEST_HELPERS_COUNT_HEAD
+
+namespace test {
+ struct object_count {
+ int instances;
+ int constructions;
+
+ object_count() : instances(0), constructions(0) {}
+ void reset() { *this = object_count(); }
+
+ void construct() {
+ ++instances;
+ ++constructions;
+ }
+
+ void destruct() {
+ if(instances == 0) {
+ BOOST_ERROR("Unbalanced constructions.");
+ }
+ else {
+ --instances;
+ }
+ }
+
+ bool operator==(object_count const& x) const {
+ return instances == x.instances &&
+ constructions == x.constructions;
+ }
+
+ bool operator!=(object_count const& x) const {
+ return !(*this == x);
+ }
+ };
+
+ template <class T>
+ struct counted_object
+ {
+ static object_count count_;
+
+ counted_object() { count_.construct(); }
+ counted_object(counted_object const&) { count_.construct(); }
+ ~counted_object() { count_.destruct(); }
+ };
+
+ template <class T> object_count counted_object<T>::count_;
+
+ struct globally_counted_object
+ : counted_object<globally_counted_object> {};
+
+ namespace {
+ object_count& global_object_count = globally_counted_object::count_;
+ }
+}
+
+#endif

Modified: branches/unordered/trunk/libs/unordered/test/objects/test.hpp
==============================================================================
--- branches/unordered/trunk/libs/unordered/test/objects/test.hpp (original)
+++ branches/unordered/trunk/libs/unordered/test/objects/test.hpp 2008-05-11 09:14:31 EDT (Sun, 11 May 2008)
@@ -11,7 +11,8 @@
 #include <cstddef>
 #include <iostream>
 #include "../helpers/fwd.hpp"
-#include "memory.hpp"
+#include "../helpers/count.hpp"
+#include "./memory.hpp"
 #include <map>
 
 namespace test
@@ -25,7 +26,7 @@
     template <class T> class allocator;
     object generate(object const*);
 
- class object
+ class object : globally_counted_object
     {
         friend class hash;
         friend class equal_to;

Modified: branches/unordered/trunk/libs/unordered/test/unordered/move_tests.cpp
==============================================================================
--- branches/unordered/trunk/libs/unordered/test/unordered/move_tests.cpp (original)
+++ branches/unordered/trunk/libs/unordered/test/unordered/move_tests.cpp 2008-05-11 09:14:31 EDT (Sun, 11 May 2008)
@@ -23,15 +23,15 @@
 
     template<class T>
     T create(test::random_values<T> const& v,
- BOOST_DEDUCED_TYPENAME T::value_type const*& first) {
+ test::object_count& count) {
         T x(v.begin(), v.end());
- first = &*x.begin();
+ count = test::global_object_count;
         return x;
     }
 
     template<class T>
     T create(test::random_values<T> const& v,
- BOOST_DEDUCED_TYPENAME T::value_type const*& first,
+ test::object_count& count,
             BOOST_DEDUCED_TYPENAME T::hasher hf,
             BOOST_DEDUCED_TYPENAME T::key_equal eq,
             BOOST_DEDUCED_TYPENAME T::allocator_type al,
@@ -39,7 +39,7 @@
         T x(0, hf, eq, al);
         x.max_load_factor(mlf);
         x.insert(v.begin(), v.end());
- first = &*x.begin();
+ count = test::global_object_count;
         return x;
     }
 
@@ -62,9 +62,9 @@
 
         {
             test::random_values<T> v(1000);
- BOOST_DEDUCED_TYPENAME T::value_type const* first = 0;
- T y(create(v, first));
- BOOST_CHECK(first == &*y.begin());
+ test::object_count count;
+ T y(create(v, count));
+ BOOST_CHECK(count == test::global_object_count);
             test::check_container(y, v);
             test::check_equivalent_keys(y);
         }
@@ -75,10 +75,10 @@
     {
         {
             test::random_values<T> v(500);
- BOOST_DEDUCED_TYPENAME T::value_type const* first = 0;
+ test::object_count count;
             T y;
- y = create(v, first);
- BOOST_CHECK(first == &*y.begin());
+ y = create(v, count);
+ BOOST_CHECK(count == test::global_object_count);
             test::check_container(y, v);
             test::check_equivalent_keys(y);
         }
@@ -95,12 +95,12 @@
         BOOST_DEDUCED_TYPENAME T::allocator_type al(1);
         BOOST_DEDUCED_TYPENAME T::allocator_type al2(2);
 
- BOOST_DEDUCED_TYPENAME T::value_type const* first;
+ test::object_count count;
 
         {
             test::random_values<T> v(500);
- T y(create(v, first, hf, eq, al, 0.5));
- BOOST_CHECK(first == &*y.begin());
+ T y(create(v, count, hf, eq, al, 0.5));
+ BOOST_CHECK(count == test::global_object_count);
             test::check_container(y, v);
             BOOST_CHECK(test::equivalent(y.hash_function(), hf));
             BOOST_CHECK(test::equivalent(y.key_eq(), eq));
@@ -112,8 +112,8 @@
         {
             // TODO: To do this correctly requires the fancy new allocator stuff.
             test::random_values<T> v(500);
- T y(create(v, first, hf, eq, al, 2.0), al2);
- BOOST_CHECK(first != &*y.begin());
+ T y(create(v, count, hf, eq, al, 2.0), al2);
+ BOOST_CHECK(count != test::global_object_count);
             test::check_container(y, v);
             BOOST_CHECK(test::equivalent(y.hash_function(), hf));
             BOOST_CHECK(test::equivalent(y.key_eq(), eq));
@@ -124,8 +124,8 @@
 
         {
             test::random_values<T> v(25);
- T y(create(v, first, hf, eq, al, 1.0), al);
- BOOST_CHECK(first == &*y.begin());
+ T y(create(v, count, hf, eq, al, 1.0), al);
+ BOOST_CHECK(count == test::global_object_count);
             test::check_container(y, v);
             BOOST_CHECK(test::equivalent(y.hash_function(), hf));
             BOOST_CHECK(test::equivalent(y.key_eq(), eq));


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