]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
b32b8144 | 9 | #include <boost/detail/lightweight_test.hpp> |
7c673cae FG |
10 | |
11 | namespace test { | |
b32b8144 FG |
12 | struct object_count |
13 | { | |
14 | int instances; | |
15 | int constructions; | |
16 | ||
17 | object_count() : instances(0), constructions(0) {} | |
18 | void reset() { *this = object_count(); } | |
19 | ||
20 | void construct() | |
21 | { | |
22 | ++instances; | |
23 | ++constructions; | |
24 | } | |
25 | ||
26 | void destruct() | |
27 | { | |
28 | if (instances == 0) { | |
29 | BOOST_ERROR("Unbalanced constructions."); | |
30 | } else { | |
31 | --instances; | |
32 | } | |
7c673cae | 33 | } |
b32b8144 FG |
34 | |
35 | bool operator==(object_count const& x) const | |
36 | { | |
37 | return instances == x.instances && constructions == x.constructions; | |
38 | } | |
39 | ||
40 | bool operator!=(object_count const& x) const { return !(*this == x); } | |
41 | ||
42 | friend std::ostream& operator<<(std::ostream& out, object_count const& c) | |
7c673cae | 43 | { |
b32b8144 FG |
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 | { | |
61 | counted_object() { global_object_count.construct(); } | |
62 | counted_object(counted_object const&) { global_object_count.construct(); } | |
63 | ~counted_object() { global_object_count.destruct(); } | |
64 | }; | |
65 | ||
66 | struct check_instances | |
67 | { | |
68 | int instances_; | |
69 | int constructions_; | |
70 | ||
71 | check_instances() | |
72 | : instances_(global_object_count.instances), | |
73 | constructions_(global_object_count.constructions) | |
74 | { | |
75 | } | |
76 | ~check_instances() | |
77 | { | |
78 | BOOST_TEST(global_object_count.instances == instances_); | |
79 | } | |
80 | ||
81 | int instances() const { return global_object_count.instances - instances_; } | |
82 | int constructions() const | |
83 | { | |
84 | return global_object_count.constructions - constructions_; | |
85 | } | |
86 | }; | |
7c673cae FG |
87 | } |
88 | ||
89 | #endif |