summaryrefslogtreecommitdiff
path: root/TAO/tests/Sequence_Iterators/testing_counters.hpp
blob: e5f20bb8ee12be12c5b28e2cfd5040c0c5d2fcfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef guard_testing_counters_hpp
#define guard_testing_counters_hpp
/**
 * @file
 *
 * @brief Some unit tests need to count how many times a function is
 * called.   Here we implement some simple helper classes for that
 * purpose.
 *
 * $Id$
 *
 * @author Carlos O'Ryan
 */

#include "testing_exception.hpp"

#include <boost/utility.hpp>

#include <iostream>

/**
 * @brief Used to count how many times a function gets called.  The
 *        unit test should create one instance per function.
 */
class call_counter
{
public:
  inline call_counter()
    : count_(0)
    , failure_countdown_(0)
  {}

  inline long current_count() const
  {
    return count_;
  }

  inline void failure_countdown(long countdown)
  {
    failure_countdown_ = countdown;
  }

  inline void operator()()
  {
    ++count_;
    if (--failure_countdown_ == 0)
    {
      throw testing_exception();
    }
  }

private:
  long count_;
  long failure_countdown_;
};

/**
 * @brief Used to detect if a testing_counter is "called" the right
 *        number of times.
 */
class expected_calls
  : private boost::noncopyable
{
public:
  inline expected_calls(call_counter const & counter)
    : current_count_(counter.current_count())
    , previous_count_(counter.current_count())
    , counter_(counter)
  { }

  inline bool expect(long n)
  {
    reset();
    return (previous_count_ + n == current_count_);
  }

  inline void reset()
  {
    previous_count_ = current_count_;
    current_count_ = counter_.current_count();
  }

  inline long current_count() const
  {
    return current_count_;
  }

  inline long previous_count() const
  {
    return previous_count_;
  }

private:
  long current_count_;
  long previous_count_;
  call_counter const & counter_;
};

inline std::ostream & operator<<(std::ostream & os, expected_calls const & x)
{
  return os << "current=" << x.current_count()
            << ",previous=" << x.previous_count();
    
}

#endif // guard_testing_counters_hpp