More tests update
[ACE_TAO.git] / TAO / tests / Sequence_Iterators / testing_counters.hpp
blobfd190f2ec47ef9bf76d758f99a38f3bd6ded5566
1 #ifndef guard_testing_counters_hpp
2 #define guard_testing_counters_hpp
3 /**
4 * @file
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
8 * purpose.
10 * @author Carlos O'Ryan
13 #include "testing_exception.hpp"
15 #include <boost/utility.hpp>
17 #include <iostream>
19 /**
20 * @brief Used to count how many times a function gets called. The
21 * unit test should create one instance per function.
23 class call_counter
25 public:
26 inline call_counter()
27 : count_(0)
28 , failure_countdown_(0)
31 inline long current_count() const
33 return count_;
36 inline void failure_countdown(long countdown)
38 failure_countdown_ = countdown;
41 inline void operator()()
43 ++count_;
44 if (--failure_countdown_ == 0)
46 throw testing_exception();
50 private:
51 long count_;
52 long failure_countdown_;
55 /**
56 * @brief Used to detect if a testing_counter is "called" the right
57 * number of times.
59 class expected_calls
60 : private boost::noncopyable
62 public:
63 inline expected_calls(call_counter const & counter)
64 : current_count_(counter.current_count())
65 , previous_count_(counter.current_count())
66 , counter_(counter)
67 { }
69 inline bool expect(long n)
71 reset();
72 return (previous_count_ + n == current_count_);
75 inline void reset()
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_;
91 private:
92 long current_count_;
93 long 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