Coverage Report

Created: 2020-06-26 05:44

/home/arjun/llvm-project/llvm/utils/unittest/googlemock/src/gmock-cardinalities.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2007, Google Inc.
2
// All rights reserved.
3
//
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
6
// met:
7
//
8
//     * Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
//     * Redistributions in binary form must reproduce the above
11
// copyright notice, this list of conditions and the following disclaimer
12
// in the documentation and/or other materials provided with the
13
// distribution.
14
//     * Neither the name of Google Inc. nor the names of its
15
// contributors may be used to endorse or promote products derived from
16
// this software without specific prior written permission.
17
//
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
//
30
// Author: wan@google.com (Zhanyong Wan)
31
32
// Google Mock - a framework for writing C++ mock classes.
33
//
34
// This file implements cardinalities.
35
36
#include "gmock/gmock-cardinalities.h"
37
38
#include <limits.h>
39
#include <ostream>  // NOLINT
40
#include <sstream>
41
#include <string>
42
#include "gmock/internal/gmock-internal-utils.h"
43
#include "gtest/gtest.h"
44
45
namespace testing {
46
47
namespace {
48
49
// Implements the Between(m, n) cardinality.
50
class BetweenCardinalityImpl : public CardinalityInterface {
51
 public:
52
  BetweenCardinalityImpl(int min, int max)
53
      : min_(min >= 0 ? min : 0),
54
0
        max_(max >= min_ ? max : min_) {
55
0
    std::stringstream ss;
56
0
    if (min < 0) {
57
0
      ss << "The invocation lower bound must be >= 0, "
58
0
         << "but is actually " << min << ".";
59
0
      internal::Expect(false, __FILE__, __LINE__, ss.str());
60
0
    } else if (max < 0) {
61
0
      ss << "The invocation upper bound must be >= 0, "
62
0
         << "but is actually " << max << ".";
63
0
      internal::Expect(false, __FILE__, __LINE__, ss.str());
64
0
    } else if (min > max) {
65
0
      ss << "The invocation upper bound (" << max
66
0
         << ") must be >= the invocation lower bound (" << min
67
0
         << ").";
68
0
      internal::Expect(false, __FILE__, __LINE__, ss.str());
69
0
    }
70
0
  }
71
72
  // Conservative estimate on the lower/upper bound of the number of
73
  // calls allowed.
74
0
  virtual int ConservativeLowerBound() const { return min_; }
75
0
  virtual int ConservativeUpperBound() const { return max_; }
76
77
0
  virtual bool IsSatisfiedByCallCount(int call_count) const {
78
0
    return min_ <= call_count && call_count <= max_;
79
0
  }
80
81
0
  virtual bool IsSaturatedByCallCount(int call_count) const {
82
0
    return call_count >= max_;
83
0
  }
84
85
  virtual void DescribeTo(::std::ostream* os) const;
86
87
 private:
88
  const int min_;
89
  const int max_;
90
91
  GTEST_DISALLOW_COPY_AND_ASSIGN_(BetweenCardinalityImpl);
92
};
93
94
// Formats "n times" in a human-friendly way.
95
0
inline internal::string FormatTimes(int n) {
96
0
  if (n == 1) {
97
0
    return "once";
98
0
  } else if (n == 2) {
99
0
    return "twice";
100
0
  } else {
101
0
    std::stringstream ss;
102
0
    ss << n << " times";
103
0
    return ss.str();
104
0
  }
105
0
}
106
107
// Describes the Between(m, n) cardinality in human-friendly text.
108
0
void BetweenCardinalityImpl::DescribeTo(::std::ostream* os) const {
109
0
  if (min_ == 0) {
110
0
    if (max_ == 0) {
111
0
      *os << "never called";
112
0
    } else if (max_ == INT_MAX) {
113
0
      *os << "called any number of times";
114
0
    } else {
115
0
      *os << "called at most " << FormatTimes(max_);
116
0
    }
117
0
  } else if (min_ == max_) {
118
0
    *os << "called " << FormatTimes(min_);
119
0
  } else if (max_ == INT_MAX) {
120
0
    *os << "called at least " << FormatTimes(min_);
121
0
  } else {
122
0
    // 0 < min_ < max_ < INT_MAX
123
0
    *os << "called between " << min_ << " and " << max_ << " times";
124
0
  }
125
0
}
126
127
}  // Unnamed namespace
128
129
// Describes the given call count to an ostream.
130
void Cardinality::DescribeActualCallCountTo(int actual_call_count,
131
0
                                            ::std::ostream* os) {
132
0
  if (actual_call_count > 0) {
133
0
    *os << "called " << FormatTimes(actual_call_count);
134
0
  } else {
135
0
    *os << "never called";
136
0
  }
137
0
}
138
139
// Creates a cardinality that allows at least n calls.
140
0
GTEST_API_ Cardinality AtLeast(int n) { return Between(n, INT_MAX); }
141
142
// Creates a cardinality that allows at most n calls.
143
0
GTEST_API_ Cardinality AtMost(int n) { return Between(0, n); }
144
145
// Creates a cardinality that allows any number of calls.
146
0
GTEST_API_ Cardinality AnyNumber() { return AtLeast(0); }
147
148
// Creates a cardinality that allows between min and max calls.
149
0
GTEST_API_ Cardinality Between(int min, int max) {
150
0
  return Cardinality(new BetweenCardinalityImpl(min, max));
151
0
}
152
153
// Creates a cardinality that allows exactly n calls.
154
0
GTEST_API_ Cardinality Exactly(int n) { return Between(n, n); }
155
156
}  // namespace testing