1 2 // Copyright 2008-2009 Daniel James. 3 // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 // file LICENSE_1_0.txt or move at http://www.boost.org/LICENSE_1_0.txt) 5 6 #if !defined(BOOST_UNORDERED_TEST_HELPERS_COUNT_HEAD) 7 #define BOOST_UNORDERED_TEST_HELPERS_COUNT_HEAD 8 9 #include <boost/core/lightweight_test.hpp> 10 11 namespace test { 12 struct object_count 13 { 14 int instances; 15 int constructions; 16 object_counttest::object_count17 object_count() : instances(0), constructions(0) {} resettest::object_count18 void reset() { *this = object_count(); } 19 constructtest::object_count20 void construct() 21 { 22 ++instances; 23 ++constructions; 24 } 25 destructtest::object_count26 void destruct() 27 { 28 if (instances == 0) { 29 BOOST_ERROR("Unbalanced constructions."); 30 } else { 31 --instances; 32 } 33 } 34 operator ==test::object_count35 bool operator==(object_count const& x) const 36 { 37 return instances == x.instances && constructions == x.constructions; 38 } 39 operator !=test::object_count40 bool operator!=(object_count const& x) const { return !(*this == x); } 41 operator <<(std::ostream & out,object_count const & c)42 friend std::ostream& operator<<(std::ostream& out, object_count const& c) 43 { 44 out << "[instances: " << c.instances 45 << ", constructions: " << c.constructions << "]"; 46 return out; 47 } 48 }; 49 50 // This won't be a problem as I'm only using a single compile unit 51 // in each test (this is actually require by the minimal test 52 // framework). 53 // 54 // boostinspect:nounnamed 55 namespace { 56 object_count global_object_count; 57 } 58 59 struct counted_object 60 { counted_objecttest::counted_object61 counted_object() { global_object_count.construct(); } counted_objecttest::counted_object62 counted_object(counted_object const&) { global_object_count.construct(); } ~counted_objecttest::counted_object63 ~counted_object() { global_object_count.destruct(); } 64 }; 65 66 struct check_instances 67 { 68 int instances_; 69 int constructions_; 70 check_instancestest::check_instances71 check_instances() 72 : instances_(global_object_count.instances), 73 constructions_(global_object_count.constructions) 74 { 75 } ~check_instancestest::check_instances76 ~check_instances() 77 { 78 BOOST_TEST(global_object_count.instances == instances_); 79 } 80 instancestest::check_instances81 int instances() const { return global_object_count.instances - instances_; } constructionstest::check_instances82 int constructions() const 83 { 84 return global_object_count.constructions - constructions_; 85 } 86 }; 87 } 88 89 #endif 90