1 #ifndef guard_testing_counters_hpp
2 #define guard_testing_counters_hpp
6 * @brief Some unit tests need to count how many times a function is
7 * called. Here we implement some simple helper classes for that
10 * @author Carlos O'Ryan
13 #include "testing_exception.hpp"
15 #include <boost/utility.hpp>
20 * @brief Used to count how many times a function gets called. The
21 * unit test should create one instance per function.
28 , failure_countdown_(0)
31 inline long current_count() const
36 inline void failure_countdown(long countdown
)
38 failure_countdown_
= countdown
;
41 inline void operator()()
44 if (--failure_countdown_
== 0)
46 throw testing_exception();
52 long failure_countdown_
;
56 * @brief Used to detect if a testing_counter is "called" the right
60 : private boost::noncopyable
63 inline expected_calls(call_counter
const & counter
)
64 : current_count_(counter
.current_count())
65 , previous_count_(counter
.current_count())
69 inline bool expect(long n
)
72 return (previous_count_
+ n
== current_count_
);
77 previous_count_
= current_count_
;
78 current_count_
= counter_
.current_count();
81 inline long current_count() const
83 return current_count_
;
86 inline long previous_count() const
88 return previous_count_
;
94 call_counter
const & counter_
;
97 inline std::ostream
& operator<<(std::ostream
& os
, expected_calls
const & x
)
99 return os
<< "current=" << x
.current_count()
100 << ",previous=" << x
.previous_count();
104 #endif // guard_testing_counters_hpp