Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Sequence_Unit_Tests / testing_counters.hpp
blob32e7ab4eb41efdbb387ed9b3a805ff180e3a1b76
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"
14 #include "ace/Copy_Disabled.h"
16 #include <iostream>
18 /**
19 * @brief Used to count how many times a function gets called. The
20 * unit test should create one instance per function.
22 class call_counter
24 public:
25 inline call_counter()
26 : count_(0)
27 , failure_countdown_(0)
30 inline long current_count() const
32 return count_;
35 inline void failure_countdown(long countdown)
37 failure_countdown_ = countdown;
40 inline void operator()()
42 ++count_;
43 if (--failure_countdown_ == 0)
45 throw testing_exception();
49 private:
50 long count_;
51 long failure_countdown_;
54 /**
55 * @brief Used to detect if a testing_counter is "called" the right
56 * number of times.
58 class expected_calls
59 : private ACE_Copy_Disabled
61 public:
62 inline expected_calls(call_counter const & counter)
63 : current_count_(counter.current_count())
64 , previous_count_(counter.current_count())
65 , counter_(counter)
66 { }
68 inline bool expect(long n)
70 reset();
71 return (previous_count_ + n == current_count_);
74 inline void reset()
76 previous_count_ = current_count_;
77 current_count_ = counter_.current_count();
80 inline long current_count() const
82 return current_count_;
85 inline long previous_count() const
87 return previous_count_;
90 private:
91 long current_count_;
92 long previous_count_;
93 call_counter const & counter_;
96 inline std::ostream & operator<<(std::ostream & os, expected_calls const & x)
98 return os << "current=" << x.current_count()
99 << ",previous=" << x.previous_count();
102 #endif // guard_testing_counters_hpp