| 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 |
// Google Mock - a framework for writing C++ mock classes. |
| 31 |
// |
| 32 |
// This file tests some commonly used argument matchers. |
| 33 |
|
| 34 |
#include <cmath> |
| 35 |
#include <limits> |
| 36 |
#include <memory> |
| 37 |
#include <ostream> |
| 38 |
#include <string> |
| 39 |
|
| 40 |
#include "gmock/gmock.h" |
| 41 |
#include "test/gmock-matchers_test.h" |
| 42 |
#include "gtest/gtest.h" |
| 43 |
|
| 44 |
// Silence warning C4244: 'initializing': conversion from 'int' to 'short', |
| 45 |
// possible loss of data and C4100, unreferenced local parameter |
| 46 |
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244 4100) |
| 47 |
|
| 48 |
namespace testing { |
| 49 |
namespace gmock_matchers_test { |
| 50 |
namespace { |
| 51 |
|
| 52 |
typedef ::std::tuple<long, int> Tuple2; // NOLINT |
| 53 |
|
| 54 |
// Tests that Eq() matches a 2-tuple where the first field == the |
| 55 |
// second field. |
| 56 |
TEST(Eq2Test, MatchesEqualArguments) { |
| 57 |
Matcher<const Tuple2&> m = Eq(); |
| 58 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
| 59 |
EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
| 60 |
} |
| 61 |
|
| 62 |
// Tests that Eq() describes itself properly. |
| 63 |
TEST(Eq2Test, CanDescribeSelf) { |
| 64 |
Matcher<const Tuple2&> m = Eq(); |
| 65 |
EXPECT_EQ("are an equal pair", Describe(m)); |
| 66 |
} |
| 67 |
|
| 68 |
// Tests that Ge() matches a 2-tuple where the first field >= the |
| 69 |
// second field. |
| 70 |
TEST(Ge2Test, MatchesGreaterThanOrEqualArguments) { |
| 71 |
Matcher<const Tuple2&> m = Ge(); |
| 72 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
| 73 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
| 74 |
EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
| 75 |
} |
| 76 |
|
| 77 |
// Tests that Ge() describes itself properly. |
| 78 |
TEST(Ge2Test, CanDescribeSelf) { |
| 79 |
Matcher<const Tuple2&> m = Ge(); |
| 80 |
EXPECT_EQ("are a pair where the first >= the second", Describe(m)); |
| 81 |
} |
| 82 |
|
| 83 |
// Tests that Gt() matches a 2-tuple where the first field > the |
| 84 |
// second field. |
| 85 |
TEST(Gt2Test, MatchesGreaterThanArguments) { |
| 86 |
Matcher<const Tuple2&> m = Gt(); |
| 87 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
| 88 |
EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
| 89 |
EXPECT_FALSE(m.Matches(Tuple2(5L, 6))); |
| 90 |
} |
| 91 |
|
| 92 |
// Tests that Gt() describes itself properly. |
| 93 |
TEST(Gt2Test, CanDescribeSelf) { |
| 94 |
Matcher<const Tuple2&> m = Gt(); |
| 95 |
EXPECT_EQ("are a pair where the first > the second", Describe(m)); |
| 96 |
} |
| 97 |
|
| 98 |
// Tests that Le() matches a 2-tuple where the first field <= the |
| 99 |
// second field. |
| 100 |
TEST(Le2Test, MatchesLessThanOrEqualArguments) { |
| 101 |
Matcher<const Tuple2&> m = Le(); |
| 102 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
| 103 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 5))); |
| 104 |
EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); |
| 105 |
} |
| 106 |
|
| 107 |
// Tests that Le() describes itself properly. |
| 108 |
TEST(Le2Test, CanDescribeSelf) { |
| 109 |
Matcher<const Tuple2&> m = Le(); |
| 110 |
EXPECT_EQ("are a pair where the first <= the second", Describe(m)); |
| 111 |
} |
| 112 |
|
| 113 |
// Tests that Lt() matches a 2-tuple where the first field < the |
| 114 |
// second field. |
| 115 |
TEST(Lt2Test, MatchesLessThanArguments) { |
| 116 |
Matcher<const Tuple2&> m = Lt(); |
| 117 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
| 118 |
EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
| 119 |
EXPECT_FALSE(m.Matches(Tuple2(5L, 4))); |
| 120 |
} |
| 121 |
|
| 122 |
// Tests that Lt() describes itself properly. |
| 123 |
TEST(Lt2Test, CanDescribeSelf) { |
| 124 |
Matcher<const Tuple2&> m = Lt(); |
| 125 |
EXPECT_EQ("are a pair where the first < the second", Describe(m)); |
| 126 |
} |
| 127 |
|
| 128 |
// Tests that Ne() matches a 2-tuple where the first field != the |
| 129 |
// second field. |
| 130 |
TEST(Ne2Test, MatchesUnequalArguments) { |
| 131 |
Matcher<const Tuple2&> m = Ne(); |
| 132 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 6))); |
| 133 |
EXPECT_TRUE(m.Matches(Tuple2(5L, 4))); |
| 134 |
EXPECT_FALSE(m.Matches(Tuple2(5L, 5))); |
| 135 |
} |
| 136 |
|
| 137 |
// Tests that Ne() describes itself properly. |
| 138 |
TEST(Ne2Test, CanDescribeSelf) { |
| 139 |
Matcher<const Tuple2&> m = Ne(); |
| 140 |
EXPECT_EQ("are an unequal pair", Describe(m)); |
| 141 |
} |
| 142 |
|
| 143 |
TEST(PairMatchBaseTest, WorksWithMoveOnly) { |
| 144 |
using Pointers = std::tuple<std::unique_ptr<int>, std::unique_ptr<int>>; |
| 145 |
Matcher<Pointers> matcher = Eq(); |
| 146 |
Pointers pointers; |
| 147 |
// Tested values don't matter; the point is that matcher does not copy the |
| 148 |
// matched values. |
| 149 |
EXPECT_TRUE(matcher.Matches(pointers)); |
| 150 |
} |
| 151 |
|
| 152 |
// Tests that IsNan() matches a NaN, with float. |
| 153 |
TEST(IsNan, FloatMatchesNan) { |
| 154 |
float quiet_nan = std::numeric_limits<float>::quiet_NaN(); |
| 155 |
float other_nan = std::nanf("1"); |
| 156 |
float real_value = 1.0f; |
| 157 |
|
| 158 |
Matcher<float> m = IsNan(); |
| 159 |
EXPECT_TRUE(m.Matches(quiet_nan)); |
| 160 |
EXPECT_TRUE(m.Matches(other_nan)); |
| 161 |
EXPECT_FALSE(m.Matches(real_value)); |
| 162 |
|
| 163 |
Matcher<float&> m_ref = IsNan(); |
| 164 |
EXPECT_TRUE(m_ref.Matches(quiet_nan)); |
| 165 |
EXPECT_TRUE(m_ref.Matches(other_nan)); |
| 166 |
EXPECT_FALSE(m_ref.Matches(real_value)); |
| 167 |
|
| 168 |
Matcher<const float&> m_cref = IsNan(); |
| 169 |
EXPECT_TRUE(m_cref.Matches(quiet_nan)); |
| 170 |
EXPECT_TRUE(m_cref.Matches(other_nan)); |
| 171 |
EXPECT_FALSE(m_cref.Matches(real_value)); |
| 172 |
} |
| 173 |
|
| 174 |
// Tests that IsNan() matches a NaN, with double. |
| 175 |
TEST(IsNan, DoubleMatchesNan) { |
| 176 |
double quiet_nan = std::numeric_limits<double>::quiet_NaN(); |
| 177 |
double other_nan = std::nan("1"); |
| 178 |
double real_value = 1.0; |
| 179 |
|
| 180 |
Matcher<double> m = IsNan(); |
| 181 |
EXPECT_TRUE(m.Matches(quiet_nan)); |
| 182 |
EXPECT_TRUE(m.Matches(other_nan)); |
| 183 |
EXPECT_FALSE(m.Matches(real_value)); |
| 184 |
|
| 185 |
Matcher<double&> m_ref = IsNan(); |
| 186 |
EXPECT_TRUE(m_ref.Matches(quiet_nan)); |
| 187 |
EXPECT_TRUE(m_ref.Matches(other_nan)); |
| 188 |
EXPECT_FALSE(m_ref.Matches(real_value)); |
| 189 |
|
| 190 |
Matcher<const double&> m_cref = IsNan(); |
| 191 |
EXPECT_TRUE(m_cref.Matches(quiet_nan)); |
| 192 |
EXPECT_TRUE(m_cref.Matches(other_nan)); |
| 193 |
EXPECT_FALSE(m_cref.Matches(real_value)); |
| 194 |
} |
| 195 |
|
| 196 |
// Tests that IsNan() matches a NaN, with long double. |
| 197 |
TEST(IsNan, LongDoubleMatchesNan) { |
| 198 |
long double quiet_nan = std::numeric_limits<long double>::quiet_NaN(); |
| 199 |
long double other_nan = std::nan("1"); |
| 200 |
long double real_value = 1.0; |
| 201 |
|
| 202 |
Matcher<long double> m = IsNan(); |
| 203 |
EXPECT_TRUE(m.Matches(quiet_nan)); |
| 204 |
EXPECT_TRUE(m.Matches(other_nan)); |
| 205 |
EXPECT_FALSE(m.Matches(real_value)); |
| 206 |
|
| 207 |
Matcher<long double&> m_ref = IsNan(); |
| 208 |
EXPECT_TRUE(m_ref.Matches(quiet_nan)); |
| 209 |
EXPECT_TRUE(m_ref.Matches(other_nan)); |
| 210 |
EXPECT_FALSE(m_ref.Matches(real_value)); |
| 211 |
|
| 212 |
Matcher<const long double&> m_cref = IsNan(); |
| 213 |
EXPECT_TRUE(m_cref.Matches(quiet_nan)); |
| 214 |
EXPECT_TRUE(m_cref.Matches(other_nan)); |
| 215 |
EXPECT_FALSE(m_cref.Matches(real_value)); |
| 216 |
} |
| 217 |
|
| 218 |
// Tests that IsNan() works with Not. |
| 219 |
TEST(IsNan, NotMatchesNan) { |
| 220 |
Matcher<float> mf = Not(IsNan()); |
| 221 |
EXPECT_FALSE(mf.Matches(std::numeric_limits<float>::quiet_NaN())); |
| 222 |
EXPECT_FALSE(mf.Matches(std::nanf("1"))); |
| 223 |
EXPECT_TRUE(mf.Matches(1.0)); |
| 224 |
|
| 225 |
Matcher<double> md = Not(IsNan()); |
| 226 |
EXPECT_FALSE(md.Matches(std::numeric_limits<double>::quiet_NaN())); |
| 227 |
EXPECT_FALSE(md.Matches(std::nan("1"))); |
| 228 |
EXPECT_TRUE(md.Matches(1.0)); |
| 229 |
|
| 230 |
Matcher<long double> mld = Not(IsNan()); |
| 231 |
EXPECT_FALSE(mld.Matches(std::numeric_limits<long double>::quiet_NaN())); |
| 232 |
EXPECT_FALSE(mld.Matches(std::nanl("1"))); |
| 233 |
EXPECT_TRUE(mld.Matches(1.0)); |
| 234 |
} |
| 235 |
|
| 236 |
// Tests that IsNan() can describe itself. |
| 237 |
TEST(IsNan, CanDescribeSelf) { |
| 238 |
Matcher<float> mf = IsNan(); |
| 239 |
EXPECT_EQ("is NaN", Describe(mf)); |
| 240 |
|
| 241 |
Matcher<double> md = IsNan(); |
| 242 |
EXPECT_EQ("is NaN", Describe(md)); |
| 243 |
|
| 244 |
Matcher<long double> mld = IsNan(); |
| 245 |
EXPECT_EQ("is NaN", Describe(mld)); |
| 246 |
} |
| 247 |
|
| 248 |
// Tests that IsNan() can describe itself with Not. |
| 249 |
TEST(IsNan, CanDescribeSelfWithNot) { |
| 250 |
Matcher<float> mf = Not(IsNan()); |
| 251 |
EXPECT_EQ("isn't NaN", Describe(mf)); |
| 252 |
|
| 253 |
Matcher<double> md = Not(IsNan()); |
| 254 |
EXPECT_EQ("isn't NaN", Describe(md)); |
| 255 |
|
| 256 |
Matcher<long double> mld = Not(IsNan()); |
| 257 |
EXPECT_EQ("isn't NaN", Describe(mld)); |
| 258 |
} |
| 259 |
|
| 260 |
// Tests that FloatEq() matches a 2-tuple where |
| 261 |
// FloatEq(first field) matches the second field. |
| 262 |
TEST(FloatEq2Test, MatchesEqualArguments) { |
| 263 |
typedef ::std::tuple<float, float> Tpl; |
| 264 |
Matcher<const Tpl&> m = FloatEq(); |
| 265 |
EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); |
| 266 |
EXPECT_TRUE(m.Matches(Tpl(0.3f, 0.1f + 0.1f + 0.1f))); |
| 267 |
EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f))); |
| 268 |
} |
| 269 |
|
| 270 |
// Tests that FloatEq() describes itself properly. |
| 271 |
TEST(FloatEq2Test, CanDescribeSelf) { |
| 272 |
Matcher<const ::std::tuple<float, float>&> m = FloatEq(); |
| 273 |
EXPECT_EQ("are an almost-equal pair", Describe(m)); |
| 274 |
} |
| 275 |
|
| 276 |
// Tests that NanSensitiveFloatEq() matches a 2-tuple where |
| 277 |
// NanSensitiveFloatEq(first field) matches the second field. |
| 278 |
TEST(NanSensitiveFloatEqTest, MatchesEqualArgumentsWithNaN) { |
| 279 |
typedef ::std::tuple<float, float> Tpl; |
| 280 |
Matcher<const Tpl&> m = NanSensitiveFloatEq(); |
| 281 |
EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); |
| 282 |
EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), |
| 283 |
std::numeric_limits<float>::quiet_NaN()))); |
| 284 |
EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f))); |
| 285 |
EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN()))); |
| 286 |
EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f))); |
| 287 |
} |
| 288 |
|
| 289 |
// Tests that NanSensitiveFloatEq() describes itself properly. |
| 290 |
TEST(NanSensitiveFloatEqTest, CanDescribeSelfWithNaNs) { |
| 291 |
Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatEq(); |
| 292 |
EXPECT_EQ("are an almost-equal pair", Describe(m)); |
| 293 |
} |
| 294 |
|
| 295 |
// Tests that DoubleEq() matches a 2-tuple where |
| 296 |
// DoubleEq(first field) matches the second field. |
| 297 |
TEST(DoubleEq2Test, MatchesEqualArguments) { |
| 298 |
typedef ::std::tuple<double, double> Tpl; |
| 299 |
Matcher<const Tpl&> m = DoubleEq(); |
| 300 |
EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0))); |
| 301 |
EXPECT_TRUE(m.Matches(Tpl(0.3, 0.1 + 0.1 + 0.1))); |
| 302 |
EXPECT_FALSE(m.Matches(Tpl(1.1, 1.0))); |
| 303 |
} |
| 304 |
|
| 305 |
// Tests that DoubleEq() describes itself properly. |
| 306 |
TEST(DoubleEq2Test, CanDescribeSelf) { |
| 307 |
Matcher<const ::std::tuple<double, double>&> m = DoubleEq(); |
| 308 |
EXPECT_EQ("are an almost-equal pair", Describe(m)); |
| 309 |
} |
| 310 |
|
| 311 |
// Tests that NanSensitiveDoubleEq() matches a 2-tuple where |
| 312 |
// NanSensitiveDoubleEq(first field) matches the second field. |
| 313 |
TEST(NanSensitiveDoubleEqTest, MatchesEqualArgumentsWithNaN) { |
| 314 |
typedef ::std::tuple<double, double> Tpl; |
| 315 |
Matcher<const Tpl&> m = NanSensitiveDoubleEq(); |
| 316 |
EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); |
| 317 |
EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), |
| 318 |
std::numeric_limits<double>::quiet_NaN()))); |
| 319 |
EXPECT_FALSE(m.Matches(Tpl(1.1f, 1.0f))); |
| 320 |
EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN()))); |
| 321 |
EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f))); |
| 322 |
} |
| 323 |
|
| 324 |
// Tests that DoubleEq() describes itself properly. |
| 325 |
TEST(NanSensitiveDoubleEqTest, CanDescribeSelfWithNaNs) { |
| 326 |
Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleEq(); |
| 327 |
EXPECT_EQ("are an almost-equal pair", Describe(m)); |
| 328 |
} |
| 329 |
|
| 330 |
// Tests that FloatEq() matches a 2-tuple where |
| 331 |
// FloatNear(first field, max_abs_error) matches the second field. |
| 332 |
TEST(FloatNear2Test, MatchesEqualArguments) { |
| 333 |
typedef ::std::tuple<float, float> Tpl; |
| 334 |
Matcher<const Tpl&> m = FloatNear(0.5f); |
| 335 |
EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); |
| 336 |
EXPECT_TRUE(m.Matches(Tpl(1.3f, 1.0f))); |
| 337 |
EXPECT_FALSE(m.Matches(Tpl(1.8f, 1.0f))); |
| 338 |
} |
| 339 |
|
| 340 |
// Tests that FloatNear() describes itself properly. |
| 341 |
TEST(FloatNear2Test, CanDescribeSelf) { |
| 342 |
Matcher<const ::std::tuple<float, float>&> m = FloatNear(0.5f); |
| 343 |
EXPECT_EQ("are an almost-equal pair", Describe(m)); |
| 344 |
} |
| 345 |
|
| 346 |
// Tests that NanSensitiveFloatNear() matches a 2-tuple where |
| 347 |
// NanSensitiveFloatNear(first field) matches the second field. |
| 348 |
TEST(NanSensitiveFloatNearTest, MatchesNearbyArgumentsWithNaN) { |
| 349 |
typedef ::std::tuple<float, float> Tpl; |
| 350 |
Matcher<const Tpl&> m = NanSensitiveFloatNear(0.5f); |
| 351 |
EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); |
| 352 |
EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f))); |
| 353 |
EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), |
| 354 |
std::numeric_limits<float>::quiet_NaN()))); |
| 355 |
EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f))); |
| 356 |
EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<float>::quiet_NaN()))); |
| 357 |
EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<float>::quiet_NaN(), 1.0f))); |
| 358 |
} |
| 359 |
|
| 360 |
// Tests that NanSensitiveFloatNear() describes itself properly. |
| 361 |
TEST(NanSensitiveFloatNearTest, CanDescribeSelfWithNaNs) { |
| 362 |
Matcher<const ::std::tuple<float, float>&> m = NanSensitiveFloatNear(0.5f); |
| 363 |
EXPECT_EQ("are an almost-equal pair", Describe(m)); |
| 364 |
} |
| 365 |
|
| 366 |
// Tests that FloatEq() matches a 2-tuple where |
| 367 |
// DoubleNear(first field, max_abs_error) matches the second field. |
| 368 |
TEST(DoubleNear2Test, MatchesEqualArguments) { |
| 369 |
typedef ::std::tuple<double, double> Tpl; |
| 370 |
Matcher<const Tpl&> m = DoubleNear(0.5); |
| 371 |
EXPECT_TRUE(m.Matches(Tpl(1.0, 1.0))); |
| 372 |
EXPECT_TRUE(m.Matches(Tpl(1.3, 1.0))); |
| 373 |
EXPECT_FALSE(m.Matches(Tpl(1.8, 1.0))); |
| 374 |
} |
| 375 |
|
| 376 |
// Tests that DoubleNear() describes itself properly. |
| 377 |
TEST(DoubleNear2Test, CanDescribeSelf) { |
| 378 |
Matcher<const ::std::tuple<double, double>&> m = DoubleNear(0.5); |
| 379 |
EXPECT_EQ("are an almost-equal pair", Describe(m)); |
| 380 |
} |
| 381 |
|
| 382 |
// Tests that NanSensitiveDoubleNear() matches a 2-tuple where |
| 383 |
// NanSensitiveDoubleNear(first field) matches the second field. |
| 384 |
TEST(NanSensitiveDoubleNearTest, MatchesNearbyArgumentsWithNaN) { |
| 385 |
typedef ::std::tuple<double, double> Tpl; |
| 386 |
Matcher<const Tpl&> m = NanSensitiveDoubleNear(0.5f); |
| 387 |
EXPECT_TRUE(m.Matches(Tpl(1.0f, 1.0f))); |
| 388 |
EXPECT_TRUE(m.Matches(Tpl(1.1f, 1.0f))); |
| 389 |
EXPECT_TRUE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), |
| 390 |
std::numeric_limits<double>::quiet_NaN()))); |
| 391 |
EXPECT_FALSE(m.Matches(Tpl(1.6f, 1.0f))); |
| 392 |
EXPECT_FALSE(m.Matches(Tpl(1.0f, std::numeric_limits<double>::quiet_NaN()))); |
| 393 |
EXPECT_FALSE(m.Matches(Tpl(std::numeric_limits<double>::quiet_NaN(), 1.0f))); |
| 394 |
} |
| 395 |
|
| 396 |
// Tests that NanSensitiveDoubleNear() describes itself properly. |
| 397 |
TEST(NanSensitiveDoubleNearTest, CanDescribeSelfWithNaNs) { |
| 398 |
Matcher<const ::std::tuple<double, double>&> m = NanSensitiveDoubleNear(0.5f); |
| 399 |
EXPECT_EQ("are an almost-equal pair", Describe(m)); |
| 400 |
} |
| 401 |
|
| 402 |
// Tests that DistanceFrom() can describe itself properly. |
| 403 |
TEST(DistanceFrom, CanDescribeSelf) { |
| 404 |
Matcher<double> m = DistanceFrom(1.5, Lt(0.1)); |
| 405 |
EXPECT_EQ(Describe(m), "is < 0.1 away from 1.5"); |
| 406 |
|
| 407 |
m = DistanceFrom(2.5, Gt(0.2)); |
| 408 |
EXPECT_EQ(Describe(m), "is > 0.2 away from 2.5"); |
| 409 |
} |
| 410 |
|
| 411 |
// Tests that DistanceFrom() can explain match failure. |
| 412 |
TEST(DistanceFrom, CanExplainMatchFailure) { |
| 413 |
Matcher<double> m = DistanceFrom(1.5, Lt(0.1)); |
| 414 |
EXPECT_EQ(Explain(m, 2.0), "which is 0.5 away from 1.5"); |
| 415 |
} |
| 416 |
|
| 417 |
// Tests that DistanceFrom() matches a double that is within the given range of |
| 418 |
// the given value. |
| 419 |
TEST(DistanceFrom, MatchesDoubleWithinRange) { |
| 420 |
const Matcher<double> m = DistanceFrom(0.5, Le(0.1)); |
| 421 |
EXPECT_TRUE(m.Matches(0.45)); |
| 422 |
EXPECT_TRUE(m.Matches(0.5)); |
| 423 |
EXPECT_TRUE(m.Matches(0.55)); |
| 424 |
EXPECT_FALSE(m.Matches(0.39)); |
| 425 |
EXPECT_FALSE(m.Matches(0.61)); |
| 426 |
} |
| 427 |
|
| 428 |
// Tests that DistanceFrom() matches a double reference that is within the given |
| 429 |
// range of the given value. |
| 430 |
TEST(DistanceFrom, MatchesDoubleRefWithinRange) { |
| 431 |
const Matcher<const double&> m = DistanceFrom(0.5, Le(0.1)); |
| 432 |
EXPECT_TRUE(m.Matches(0.45)); |
| 433 |
EXPECT_TRUE(m.Matches(0.5)); |
| 434 |
EXPECT_TRUE(m.Matches(0.55)); |
| 435 |
EXPECT_FALSE(m.Matches(0.39)); |
| 436 |
EXPECT_FALSE(m.Matches(0.61)); |
| 437 |
} |
| 438 |
|
| 439 |
// Tests that DistanceFrom() can be implicitly converted to a matcher depending |
| 440 |
// on the type of the argument. |
| 441 |
TEST(DistanceFrom, CanBeImplicitlyConvertedToMatcher) { |
| 442 |
EXPECT_THAT(0.58, DistanceFrom(0.5, Le(0.1))); |
| 443 |
EXPECT_THAT(0.2, Not(DistanceFrom(0.5, Le(0.1)))); |
| 444 |
|
| 445 |
EXPECT_THAT(0.58f, DistanceFrom(0.5f, Le(0.1f))); |
| 446 |
EXPECT_THAT(0.7f, Not(DistanceFrom(0.5f, Le(0.1f)))); |
| 447 |
} |
| 448 |
|
| 449 |
// Tests that DistanceFrom() can be used on compatible types (i.e. not |
| 450 |
// everything has to be of the same type). |
| 451 |
TEST(DistanceFrom, CanBeUsedOnCompatibleTypes) { |
| 452 |
EXPECT_THAT(0.58, DistanceFrom(0.5, Le(0.1f))); |
| 453 |
EXPECT_THAT(0.2, Not(DistanceFrom(0.5, Le(0.1f)))); |
| 454 |
|
| 455 |
EXPECT_THAT(0.58, DistanceFrom(0.5f, Le(0.1))); |
| 456 |
EXPECT_THAT(0.2, Not(DistanceFrom(0.5f, Le(0.1)))); |
| 457 |
|
| 458 |
EXPECT_THAT(0.58, DistanceFrom(0.5f, Le(0.1f))); |
| 459 |
EXPECT_THAT(0.2, Not(DistanceFrom(0.5f, Le(0.1f)))); |
| 460 |
|
| 461 |
EXPECT_THAT(0.58f, DistanceFrom(0.5, Le(0.1))); |
| 462 |
EXPECT_THAT(0.2f, Not(DistanceFrom(0.5, Le(0.1)))); |
| 463 |
|
| 464 |
EXPECT_THAT(0.58f, DistanceFrom(0.5, Le(0.1f))); |
| 465 |
EXPECT_THAT(0.2f, Not(DistanceFrom(0.5, Le(0.1f)))); |
| 466 |
|
| 467 |
EXPECT_THAT(0.58f, DistanceFrom(0.5f, Le(0.1))); |
| 468 |
EXPECT_THAT(0.2f, Not(DistanceFrom(0.5f, Le(0.1)))); |
| 469 |
} |
| 470 |
|
| 471 |
// A 2-dimensional point. For testing using DistanceFrom() with a custom type |
| 472 |
// that doesn't have a built-in distance function. |
| 473 |
class Point { |
| 474 |
public: |
| 475 |
Point(double x, double y) : x_(x), y_(y) {} |
| 476 |
double x() const { return x_; } |
| 477 |
double y() const { return y_; } |
| 478 |
|
| 479 |
private: |
| 480 |
double x_; |
| 481 |
double y_; |
| 482 |
}; |
| 483 |
|
| 484 |
// Returns the distance between two points. |
| 485 |
double PointDistance(const Point& lhs, const Point& rhs) { |
| 486 |
return std::sqrt(std::pow(lhs.x() - rhs.x(), 2) + |
| 487 |
std::pow(lhs.y() - rhs.y(), 2)); |
| 488 |
} |
| 489 |
|
| 490 |
// Tests that DistanceFrom() can be used on a type with a custom distance |
| 491 |
// function. |
| 492 |
TEST(DistanceFrom, CanBeUsedOnTypeWithCustomDistanceFunction) { |
| 493 |
const Matcher<Point> m = |
| 494 |
DistanceFrom(Point(0.5, 0.5), PointDistance, Le(0.1)); |
| 495 |
EXPECT_THAT(Point(0.45, 0.45), m); |
| 496 |
EXPECT_THAT(Point(0.2, 0.45), Not(m)); |
| 497 |
} |
| 498 |
|
| 499 |
// A wrapper around a double value. For testing using DistanceFrom() with a |
| 500 |
// custom type that has neither a built-in distance function nor a built-in |
| 501 |
// distance comparator. |
| 502 |
class Double { |
| 503 |
public: |
| 504 |
explicit Double(double value) : value_(value) {} |
| 505 |
Double(const Double& other) = default; |
| 506 |
double value() const { return value_; } |
| 507 |
|
| 508 |
// Defines how to print a Double value. We don't use the AbslStringify API |
| 509 |
// because googletest doesn't require absl yet. |
| 510 |
friend void PrintTo(const Double& value, std::ostream* os) { |
| 511 |
*os << "Double(" << value.value() << ")"; |
| 512 |
} |
| 513 |
|
| 514 |
private: |
| 515 |
double value_; |
| 516 |
}; |
| 517 |
|
| 518 |
// Returns the distance between two Double values. |
| 519 |
Double DoubleDistance(Double lhs, Double rhs) { |
| 520 |
return Double(std::abs(lhs.value() - rhs.value())); |
| 521 |
} |
| 522 |
|
| 523 |
MATCHER_P(DoubleLe, rhs, (negation ? "is > " : "is <= ") + PrintToString(rhs)) { |
| 524 |
return arg.value() <= rhs.value(); |
| 525 |
} |
| 526 |
|
| 527 |
// Tests that DistanceFrom() can describe itself properly for a type with a |
| 528 |
// custom printer. |
| 529 |
TEST(DistanceFrom, CanDescribeWithCustomPrinter) { |
| 530 |
const Matcher<Double> m = |
| 531 |
DistanceFrom(Double(0.5), DoubleDistance, DoubleLe(Double(0.1))); |
| 532 |
EXPECT_EQ(Describe(m), "is <= Double(0.1) away from Double(0.5)"); |
| 533 |
EXPECT_EQ(DescribeNegation(m), "is > Double(0.1) away from Double(0.5)"); |
| 534 |
} |
| 535 |
|
| 536 |
// Tests that DistanceFrom() can be used with a custom distance function and |
| 537 |
// comparator. |
| 538 |
TEST(DistanceFrom, CanCustomizeDistanceAndComparator) { |
| 539 |
const Matcher<Double> m = |
| 540 |
DistanceFrom(Double(0.5), DoubleDistance, DoubleLe(Double(0.1))); |
| 541 |
EXPECT_TRUE(m.Matches(Double(0.45))); |
| 542 |
EXPECT_TRUE(m.Matches(Double(0.5))); |
| 543 |
EXPECT_FALSE(m.Matches(Double(0.39))); |
| 544 |
EXPECT_FALSE(m.Matches(Double(0.61))); |
| 545 |
} |
| 546 |
|
| 547 |
// For testing using DistanceFrom() with a type that supports both - and abs. |
| 548 |
class Float { |
| 549 |
public: |
| 550 |
explicit Float(float value) : value_(value) {} |
| 551 |
Float(const Float& other) = default; |
| 552 |
float value() const { return value_; } |
| 553 |
|
| 554 |
private: |
| 555 |
float value_ = 0.0f; |
| 556 |
}; |
| 557 |
|
| 558 |
// Returns the difference between two Float values. This must be defined in the |
| 559 |
// same namespace as Float. |
| 560 |
Float operator-(const Float& lhs, const Float& rhs) { |
| 561 |
return Float(lhs.value() - rhs.value()); |
| 562 |
} |
| 563 |
|
| 564 |
// Returns the absolute value of a Float value. This must be defined in the |
| 565 |
// same namespace as Float. |
| 566 |
Float abs(Float value) { return Float(std::abs(value.value())); } |
| 567 |
|
| 568 |
// Returns true if and only if the first Float value is less than the second |
| 569 |
// Float value. This must be defined in the same namespace as Float. |
| 570 |
bool operator<(const Float& lhs, const Float& rhs) { |
| 571 |
return lhs.value() < rhs.value(); |
| 572 |
} |
| 573 |
|
| 574 |
// Tests that DistanceFrom() can be used with a type that supports both - and |
| 575 |
// abs. |
| 576 |
TEST(DistanceFrom, CanBeUsedWithTypeThatSupportsBothMinusAndAbs) { |
| 577 |
const Matcher<Float> m = DistanceFrom(Float(0.5f), Lt(Float(0.1f))); |
| 578 |
EXPECT_TRUE(m.Matches(Float(0.45f))); |
| 579 |
EXPECT_TRUE(m.Matches(Float(0.55f))); |
| 580 |
EXPECT_FALSE(m.Matches(Float(0.39f))); |
| 581 |
EXPECT_FALSE(m.Matches(Float(0.61f))); |
| 582 |
} |
| 583 |
|
| 584 |
// Tests that Not(m) matches any value that doesn't match m. |
| 585 |
TEST(NotTest, NegatesMatcher) { |
| 586 |
Matcher<int> m; |
| 587 |
m = Not(Eq(2)); |
| 588 |
EXPECT_TRUE(m.Matches(3)); |
| 589 |
EXPECT_FALSE(m.Matches(2)); |
| 590 |
} |
| 591 |
|
| 592 |
// Tests that Not(m) describes itself properly. |
| 593 |
TEST(NotTest, CanDescribeSelf) { |
| 594 |
Matcher<int> m = Not(Eq(5)); |
| 595 |
EXPECT_EQ("isn't equal to 5", Describe(m)); |
| 596 |
} |
| 597 |
|
| 598 |
// Tests that monomorphic matchers are safely cast by the Not matcher. |
| 599 |
TEST(NotTest, NotMatcherSafelyCastsMonomorphicMatchers) { |
| 600 |
// greater_than_5 is a monomorphic matcher. |
| 601 |
Matcher<int> greater_than_5 = Gt(5); |
| 602 |
|
| 603 |
Matcher<const int&> m = Not(greater_than_5); |
| 604 |
Matcher<int&> m2 = Not(greater_than_5); |
| 605 |
Matcher<int&> m3 = Not(m); |
| 606 |
} |
| 607 |
|
| 608 |
// Helper to allow easy testing of AllOf matchers with num parameters. |
| 609 |
void AllOfMatches(int num, const Matcher<int>& m) { |
| 610 |
SCOPED_TRACE(Describe(m)); |
| 611 |
EXPECT_TRUE(m.Matches(0)); |
| 612 |
for (int i = 1; i <= num; ++i) { |
| 613 |
EXPECT_FALSE(m.Matches(i)); |
| 614 |
} |
| 615 |
EXPECT_TRUE(m.Matches(num + 1)); |
| 616 |
} |
| 617 |
|
| 618 |
INSTANTIATE_GTEST_MATCHER_TEST_P(AllOfTest); |
| 619 |
|
| 620 |
// Tests that AllOf(m1, ..., mn) matches any value that matches all of |
| 621 |
// the given matchers. |
| 622 |
TEST(AllOfTest, MatchesWhenAllMatch) { |
| 623 |
Matcher<int> m; |
| 624 |
m = AllOf(Le(2), Ge(1)); |
| 625 |
EXPECT_TRUE(m.Matches(1)); |
| 626 |
EXPECT_TRUE(m.Matches(2)); |
| 627 |
EXPECT_FALSE(m.Matches(0)); |
| 628 |
EXPECT_FALSE(m.Matches(3)); |
| 629 |
|
| 630 |
m = AllOf(Gt(0), Ne(1), Ne(2)); |
| 631 |
EXPECT_TRUE(m.Matches(3)); |
| 632 |
EXPECT_FALSE(m.Matches(2)); |
| 633 |
EXPECT_FALSE(m.Matches(1)); |
| 634 |
EXPECT_FALSE(m.Matches(0)); |
| 635 |
|
| 636 |
m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); |
| 637 |
EXPECT_TRUE(m.Matches(4)); |
| 638 |
EXPECT_FALSE(m.Matches(3)); |
| 639 |
EXPECT_FALSE(m.Matches(2)); |
| 640 |
EXPECT_FALSE(m.Matches(1)); |
| 641 |
EXPECT_FALSE(m.Matches(0)); |
| 642 |
|
| 643 |
m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
| 644 |
EXPECT_TRUE(m.Matches(0)); |
| 645 |
EXPECT_TRUE(m.Matches(1)); |
| 646 |
EXPECT_FALSE(m.Matches(3)); |
| 647 |
|
| 648 |
// The following tests for varying number of sub-matchers. Due to the way |
| 649 |
// the sub-matchers are handled it is enough to test every sub-matcher once |
| 650 |
// with sub-matchers using the same matcher type. Varying matcher types are |
| 651 |
// checked for above. |
| 652 |
AllOfMatches(2, AllOf(Ne(1), Ne(2))); |
| 653 |
AllOfMatches(3, AllOf(Ne(1), Ne(2), Ne(3))); |
| 654 |
AllOfMatches(4, AllOf(Ne(1), Ne(2), Ne(3), Ne(4))); |
| 655 |
AllOfMatches(5, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5))); |
| 656 |
AllOfMatches(6, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6))); |
| 657 |
AllOfMatches(7, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7))); |
| 658 |
AllOfMatches(8, |
| 659 |
AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8))); |
| 660 |
AllOfMatches( |
| 661 |
9, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9))); |
| 662 |
AllOfMatches(10, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), |
| 663 |
Ne(9), Ne(10))); |
| 664 |
AllOfMatches( |
| 665 |
50, AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9), |
| 666 |
Ne(10), Ne(11), Ne(12), Ne(13), Ne(14), Ne(15), Ne(16), Ne(17), |
| 667 |
Ne(18), Ne(19), Ne(20), Ne(21), Ne(22), Ne(23), Ne(24), Ne(25), |
| 668 |
Ne(26), Ne(27), Ne(28), Ne(29), Ne(30), Ne(31), Ne(32), Ne(33), |
| 669 |
Ne(34), Ne(35), Ne(36), Ne(37), Ne(38), Ne(39), Ne(40), Ne(41), |
| 670 |
Ne(42), Ne(43), Ne(44), Ne(45), Ne(46), Ne(47), Ne(48), Ne(49), |
| 671 |
Ne(50))); |
| 672 |
} |
| 673 |
|
| 674 |
// Tests that AllOf(m1, ..., mn) describes itself properly. |
| 675 |
TEST(AllOfTest, CanDescribeSelf) { |
| 676 |
Matcher<int> m; |
| 677 |
m = AllOf(Le(2), Ge(1)); |
| 678 |
EXPECT_EQ("(is <= 2) and (is >= 1)", Describe(m)); |
| 679 |
|
| 680 |
m = AllOf(Gt(0), Ne(1), Ne(2)); |
| 681 |
std::string expected_descr1 = |
| 682 |
"(is > 0) and (isn't equal to 1) and (isn't equal to 2)"; |
| 683 |
EXPECT_EQ(expected_descr1, Describe(m)); |
| 684 |
|
| 685 |
m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); |
| 686 |
std::string expected_descr2 = |
| 687 |
"(is > 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't equal " |
| 688 |
"to 3)"; |
| 689 |
EXPECT_EQ(expected_descr2, Describe(m)); |
| 690 |
|
| 691 |
m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
| 692 |
std::string expected_descr3 = |
| 693 |
"(is >= 0) and (is < 10) and (isn't equal to 3) and (isn't equal to 5) " |
| 694 |
"and (isn't equal to 7)"; |
| 695 |
EXPECT_EQ(expected_descr3, Describe(m)); |
| 696 |
} |
| 697 |
|
| 698 |
// Tests that AllOf(m1, ..., mn) describes its negation properly. |
| 699 |
TEST(AllOfTest, CanDescribeNegation) { |
| 700 |
Matcher<int> m; |
| 701 |
m = AllOf(Le(2), Ge(1)); |
| 702 |
std::string expected_descr4 = "(isn't <= 2) or (isn't >= 1)"; |
| 703 |
EXPECT_EQ(expected_descr4, DescribeNegation(m)); |
| 704 |
|
| 705 |
m = AllOf(Gt(0), Ne(1), Ne(2)); |
| 706 |
std::string expected_descr5 = |
| 707 |
"(isn't > 0) or (is equal to 1) or (is equal to 2)"; |
| 708 |
EXPECT_EQ(expected_descr5, DescribeNegation(m)); |
| 709 |
|
| 710 |
m = AllOf(Gt(0), Ne(1), Ne(2), Ne(3)); |
| 711 |
std::string expected_descr6 = |
| 712 |
"(isn't > 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)"; |
| 713 |
EXPECT_EQ(expected_descr6, DescribeNegation(m)); |
| 714 |
|
| 715 |
m = AllOf(Ge(0), Lt(10), Ne(3), Ne(5), Ne(7)); |
| 716 |
std::string expected_desr7 = |
| 717 |
"(isn't >= 0) or (isn't < 10) or (is equal to 3) or (is equal to 5) or " |
| 718 |
"(is equal to 7)"; |
| 719 |
EXPECT_EQ(expected_desr7, DescribeNegation(m)); |
| 720 |
|
| 721 |
m = AllOf(Ne(1), Ne(2), Ne(3), Ne(4), Ne(5), Ne(6), Ne(7), Ne(8), Ne(9), |
| 722 |
Ne(10), Ne(11)); |
| 723 |
AllOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); |
| 724 |
EXPECT_THAT(Describe(m), EndsWith("and (isn't equal to 11)")); |
| 725 |
AllOfMatches(11, m); |
| 726 |
} |
| 727 |
|
| 728 |
// Tests that monomorphic matchers are safely cast by the AllOf matcher. |
| 729 |
TEST(AllOfTest, AllOfMatcherSafelyCastsMonomorphicMatchers) { |
| 730 |
// greater_than_5 and less_than_10 are monomorphic matchers. |
| 731 |
Matcher<int> greater_than_5 = Gt(5); |
| 732 |
Matcher<int> less_than_10 = Lt(10); |
| 733 |
|
| 734 |
Matcher<const int&> m = AllOf(greater_than_5, less_than_10); |
| 735 |
Matcher<int&> m2 = AllOf(greater_than_5, less_than_10); |
| 736 |
Matcher<int&> m3 = AllOf(greater_than_5, m2); |
| 737 |
|
| 738 |
// Tests that BothOf works when composing itself. |
| 739 |
Matcher<const int&> m4 = AllOf(greater_than_5, less_than_10, less_than_10); |
| 740 |
Matcher<int&> m5 = AllOf(greater_than_5, less_than_10, less_than_10); |
| 741 |
} |
| 742 |
|
| 743 |
TEST_P(AllOfTestP, ExplainsResult) { |
| 744 |
Matcher<int> m; |
| 745 |
|
| 746 |
// Successful match. Both matchers need to explain. The second |
| 747 |
// matcher doesn't give an explanation, so the matcher description is used. |
| 748 |
m = AllOf(GreaterThan(10), Lt(30)); |
| 749 |
EXPECT_EQ("which is 15 more than 10, and is < 30", Explain(m, 25)); |
| 750 |
|
| 751 |
// Successful match. Both matchers need to explain. |
| 752 |
m = AllOf(GreaterThan(10), GreaterThan(20)); |
| 753 |
EXPECT_EQ("which is 20 more than 10, and which is 10 more than 20", |
| 754 |
Explain(m, 30)); |
| 755 |
|
| 756 |
// Successful match. All matchers need to explain. The second |
| 757 |
// matcher doesn't given an explanation. |
| 758 |
m = AllOf(GreaterThan(10), Lt(30), GreaterThan(20)); |
| 759 |
EXPECT_EQ( |
| 760 |
"which is 15 more than 10, and is < 30, and which is 5 more than 20", |
| 761 |
Explain(m, 25)); |
| 762 |
|
| 763 |
// Successful match. All matchers need to explain. |
| 764 |
m = AllOf(GreaterThan(10), GreaterThan(20), GreaterThan(30)); |
| 765 |
EXPECT_EQ( |
| 766 |
"which is 30 more than 10, and which is 20 more than 20, " |
| 767 |
"and which is 10 more than 30", |
| 768 |
Explain(m, 40)); |
| 769 |
|
| 770 |
// Failed match. The first matcher, which failed, needs to |
| 771 |
// explain. |
| 772 |
m = AllOf(GreaterThan(10), GreaterThan(20)); |
| 773 |
EXPECT_EQ("which is 5 less than 10", Explain(m, 5)); |
| 774 |
|
| 775 |
// Failed match. The second matcher, which failed, needs to |
| 776 |
// explain. Since it doesn't given an explanation, the matcher text is |
| 777 |
// printed. |
| 778 |
m = AllOf(GreaterThan(10), Lt(30)); |
| 779 |
EXPECT_EQ("which doesn't match (is < 30)", Explain(m, 40)); |
| 780 |
|
| 781 |
// Failed match. The second matcher, which failed, needs to |
| 782 |
// explain. |
| 783 |
m = AllOf(GreaterThan(10), GreaterThan(20)); |
| 784 |
EXPECT_EQ("which is 5 less than 20", Explain(m, 15)); |
| 785 |
} |
| 786 |
|
| 787 |
// Helper to allow easy testing of AnyOf matchers with num parameters. |
| 788 |
static void AnyOfMatches(int num, const Matcher<int>& m) { |
| 789 |
SCOPED_TRACE(Describe(m)); |
| 790 |
EXPECT_FALSE(m.Matches(0)); |
| 791 |
for (int i = 1; i <= num; ++i) { |
| 792 |
EXPECT_TRUE(m.Matches(i)); |
| 793 |
} |
| 794 |
EXPECT_FALSE(m.Matches(num + 1)); |
| 795 |
} |
| 796 |
|
| 797 |
static void AnyOfStringMatches(int num, const Matcher<std::string>& m) { |
| 798 |
SCOPED_TRACE(Describe(m)); |
| 799 |
EXPECT_FALSE(m.Matches(std::to_string(0))); |
| 800 |
|
| 801 |
for (int i = 1; i <= num; ++i) { |
| 802 |
EXPECT_TRUE(m.Matches(std::to_string(i))); |
| 803 |
} |
| 804 |
EXPECT_FALSE(m.Matches(std::to_string(num + 1))); |
| 805 |
} |
| 806 |
|
| 807 |
INSTANTIATE_GTEST_MATCHER_TEST_P(AnyOfTest); |
| 808 |
|
| 809 |
// Tests that AnyOf(m1, ..., mn) matches any value that matches at |
| 810 |
// least one of the given matchers. |
| 811 |
TEST(AnyOfTest, MatchesWhenAnyMatches) { |
| 812 |
Matcher<int> m; |
| 813 |
m = AnyOf(Le(1), Ge(3)); |
| 814 |
EXPECT_TRUE(m.Matches(1)); |
| 815 |
EXPECT_TRUE(m.Matches(4)); |
| 816 |
EXPECT_FALSE(m.Matches(2)); |
| 817 |
|
| 818 |
m = AnyOf(Lt(0), Eq(1), Eq(2)); |
| 819 |
EXPECT_TRUE(m.Matches(-1)); |
| 820 |
EXPECT_TRUE(m.Matches(1)); |
| 821 |
EXPECT_TRUE(m.Matches(2)); |
| 822 |
EXPECT_FALSE(m.Matches(0)); |
| 823 |
|
| 824 |
m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); |
| 825 |
EXPECT_TRUE(m.Matches(-1)); |
| 826 |
EXPECT_TRUE(m.Matches(1)); |
| 827 |
EXPECT_TRUE(m.Matches(2)); |
| 828 |
EXPECT_TRUE(m.Matches(3)); |
| 829 |
EXPECT_FALSE(m.Matches(0)); |
| 830 |
|
| 831 |
m = AnyOf(Le(0), Gt(10), 3, 5, 7); |
| 832 |
EXPECT_TRUE(m.Matches(0)); |
| 833 |
EXPECT_TRUE(m.Matches(11)); |
| 834 |
EXPECT_TRUE(m.Matches(3)); |
| 835 |
EXPECT_FALSE(m.Matches(2)); |
| 836 |
|
| 837 |
// The following tests for varying number of sub-matchers. Due to the way |
| 838 |
// the sub-matchers are handled it is enough to test every sub-matcher once |
| 839 |
// with sub-matchers using the same matcher type. Varying matcher types are |
| 840 |
// checked for above. |
| 841 |
AnyOfMatches(2, AnyOf(1, 2)); |
| 842 |
AnyOfMatches(3, AnyOf(1, 2, 3)); |
| 843 |
AnyOfMatches(4, AnyOf(1, 2, 3, 4)); |
| 844 |
AnyOfMatches(5, AnyOf(1, 2, 3, 4, 5)); |
| 845 |
AnyOfMatches(6, AnyOf(1, 2, 3, 4, 5, 6)); |
| 846 |
AnyOfMatches(7, AnyOf(1, 2, 3, 4, 5, 6, 7)); |
| 847 |
AnyOfMatches(8, AnyOf(1, 2, 3, 4, 5, 6, 7, 8)); |
| 848 |
AnyOfMatches(9, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9)); |
| 849 |
AnyOfMatches(10, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); |
| 850 |
} |
| 851 |
|
| 852 |
// Tests the variadic version of the AnyOfMatcher. |
| 853 |
TEST(AnyOfTest, VariadicMatchesWhenAnyMatches) { |
| 854 |
// Also make sure AnyOf is defined in the right namespace and does not depend |
| 855 |
// on ADL. |
| 856 |
Matcher<int> m = ::testing::AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); |
| 857 |
|
| 858 |
EXPECT_THAT(Describe(m), EndsWith("or (is equal to 11)")); |
| 859 |
AnyOfMatches(11, m); |
| 860 |
AnyOfMatches(50, AnyOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, |
| 861 |
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 862 |
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, |
| 863 |
45, 46, 47, 48, 49, 50)); |
| 864 |
AnyOfStringMatches( |
| 865 |
50, AnyOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", |
| 866 |
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", |
| 867 |
"23", "24", "25", "26", "27", "28", "29", "30", "31", "32", |
| 868 |
"33", "34", "35", "36", "37", "38", "39", "40", "41", "42", |
| 869 |
"43", "44", "45", "46", "47", "48", "49", "50")); |
| 870 |
} |
| 871 |
|
| 872 |
TEST(ConditionalTest, MatchesFirstIfCondition) { |
| 873 |
Matcher<std::string> eq_red = Eq("red"); |
| 874 |
Matcher<std::string> ne_red = Ne("red"); |
| 875 |
Matcher<std::string> m = Conditional(true, eq_red, ne_red); |
| 876 |
EXPECT_TRUE(m.Matches("red")); |
| 877 |
EXPECT_FALSE(m.Matches("green")); |
| 878 |
|
| 879 |
StringMatchResultListener listener; |
| 880 |
StringMatchResultListener expected; |
| 881 |
EXPECT_FALSE(m.MatchAndExplain("green", &listener)); |
| 882 |
EXPECT_FALSE(eq_red.MatchAndExplain("green", &expected)); |
| 883 |
EXPECT_THAT(listener.str(), Eq(expected.str())); |
| 884 |
} |
| 885 |
|
| 886 |
TEST(ConditionalTest, MatchesSecondIfCondition) { |
| 887 |
Matcher<std::string> eq_red = Eq("red"); |
| 888 |
Matcher<std::string> ne_red = Ne("red"); |
| 889 |
Matcher<std::string> m = Conditional(false, eq_red, ne_red); |
| 890 |
EXPECT_FALSE(m.Matches("red")); |
| 891 |
EXPECT_TRUE(m.Matches("green")); |
| 892 |
|
| 893 |
StringMatchResultListener listener; |
| 894 |
StringMatchResultListener expected; |
| 895 |
EXPECT_FALSE(m.MatchAndExplain("red", &listener)); |
| 896 |
EXPECT_FALSE(ne_red.MatchAndExplain("red", &expected)); |
| 897 |
EXPECT_THAT(listener.str(), Eq(expected.str())); |
| 898 |
} |
| 899 |
|
| 900 |
// Tests that AnyOf(m1, ..., mn) describes itself properly. |
| 901 |
TEST(AnyOfTest, CanDescribeSelf) { |
| 902 |
Matcher<int> m; |
| 903 |
m = AnyOf(Le(1), Ge(3)); |
| 904 |
|
| 905 |
EXPECT_EQ("(is <= 1) or (is >= 3)", Describe(m)); |
| 906 |
|
| 907 |
m = AnyOf(Lt(0), Eq(1), Eq(2)); |
| 908 |
EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2)", Describe(m)); |
| 909 |
|
| 910 |
m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); |
| 911 |
EXPECT_EQ("(is < 0) or (is equal to 1) or (is equal to 2) or (is equal to 3)", |
| 912 |
Describe(m)); |
| 913 |
|
| 914 |
m = AnyOf(Le(0), Gt(10), 3, 5, 7); |
| 915 |
EXPECT_EQ( |
| 916 |
"(is <= 0) or (is > 10) or (is equal to 3) or (is equal to 5) or (is " |
| 917 |
"equal to 7)", |
| 918 |
Describe(m)); |
| 919 |
} |
| 920 |
|
| 921 |
// Tests that AnyOf(m1, ..., mn) describes its negation properly. |
| 922 |
TEST(AnyOfTest, CanDescribeNegation) { |
| 923 |
Matcher<int> m; |
| 924 |
m = AnyOf(Le(1), Ge(3)); |
| 925 |
EXPECT_EQ("(isn't <= 1) and (isn't >= 3)", DescribeNegation(m)); |
| 926 |
|
| 927 |
m = AnyOf(Lt(0), Eq(1), Eq(2)); |
| 928 |
EXPECT_EQ("(isn't < 0) and (isn't equal to 1) and (isn't equal to 2)", |
| 929 |
DescribeNegation(m)); |
| 930 |
|
| 931 |
m = AnyOf(Lt(0), Eq(1), Eq(2), Eq(3)); |
| 932 |
EXPECT_EQ( |
| 933 |
"(isn't < 0) and (isn't equal to 1) and (isn't equal to 2) and (isn't " |
| 934 |
"equal to 3)", |
| 935 |
DescribeNegation(m)); |
| 936 |
|
| 937 |
m = AnyOf(Le(0), Gt(10), 3, 5, 7); |
| 938 |
EXPECT_EQ( |
| 939 |
"(isn't <= 0) and (isn't > 10) and (isn't equal to 3) and (isn't equal " |
| 940 |
"to 5) and (isn't equal to 7)", |
| 941 |
DescribeNegation(m)); |
| 942 |
} |
| 943 |
|
| 944 |
// Tests that monomorphic matchers are safely cast by the AnyOf matcher. |
| 945 |
TEST(AnyOfTest, AnyOfMatcherSafelyCastsMonomorphicMatchers) { |
| 946 |
// greater_than_5 and less_than_10 are monomorphic matchers. |
| 947 |
Matcher<int> greater_than_5 = Gt(5); |
| 948 |
Matcher<int> less_than_10 = Lt(10); |
| 949 |
|
| 950 |
Matcher<const int&> m = AnyOf(greater_than_5, less_than_10); |
| 951 |
Matcher<int&> m2 = AnyOf(greater_than_5, less_than_10); |
| 952 |
Matcher<int&> m3 = AnyOf(greater_than_5, m2); |
| 953 |
|
| 954 |
// Tests that EitherOf works when composing itself. |
| 955 |
Matcher<const int&> m4 = AnyOf(greater_than_5, less_than_10, less_than_10); |
| 956 |
Matcher<int&> m5 = AnyOf(greater_than_5, less_than_10, less_than_10); |
| 957 |
} |
| 958 |
|
| 959 |
TEST_P(AnyOfTestP, ExplainsResult) { |
| 960 |
Matcher<int> m; |
| 961 |
|
| 962 |
// Failed match. The second matcher have no explanation (description is used). |
| 963 |
m = AnyOf(GreaterThan(10), Lt(0)); |
| 964 |
EXPECT_EQ("which is 5 less than 10, and isn't < 0", Explain(m, 5)); |
| 965 |
|
| 966 |
// Failed match. Both matchers have explanations. |
| 967 |
m = AnyOf(GreaterThan(10), GreaterThan(20)); |
| 968 |
EXPECT_EQ("which is 5 less than 10, and which is 15 less than 20", |
| 969 |
Explain(m, 5)); |
| 970 |
|
| 971 |
// Failed match. The middle matcher have no explanation. |
| 972 |
m = AnyOf(GreaterThan(10), Gt(20), GreaterThan(30)); |
| 973 |
EXPECT_EQ( |
| 974 |
"which is 5 less than 10, and isn't > 20, and which is 25 less than 30", |
| 975 |
Explain(m, 5)); |
| 976 |
|
| 977 |
// Failed match. All three matchers have explanations. |
| 978 |
m = AnyOf(GreaterThan(10), GreaterThan(20), GreaterThan(30)); |
| 979 |
EXPECT_EQ( |
| 980 |
"which is 5 less than 10, and which is 15 less than 20, " |
| 981 |
"and which is 25 less than 30", |
| 982 |
Explain(m, 5)); |
| 983 |
|
| 984 |
// Successful match. The first macher succeeded and has explanation. |
| 985 |
m = AnyOf(GreaterThan(10), GreaterThan(20)); |
| 986 |
EXPECT_EQ("which is 5 more than 10", Explain(m, 15)); |
| 987 |
|
| 988 |
// Successful match. The second matcher succeeded and has explanation. |
| 989 |
m = AnyOf(GreaterThan(30), GreaterThan(20)); |
| 990 |
EXPECT_EQ("which is 5 more than 20", Explain(m, 25)); |
| 991 |
|
| 992 |
// Successful match. The first matcher succeeded and has no explanation. |
| 993 |
m = AnyOf(Gt(10), Lt(20)); |
| 994 |
EXPECT_EQ("which matches (is > 10)", Explain(m, 15)); |
| 995 |
|
| 996 |
// Successful match. The second matcher succeeded and has no explanation. |
| 997 |
m = AnyOf(Gt(30), Gt(20)); |
| 998 |
EXPECT_EQ("which matches (is > 20)", Explain(m, 25)); |
| 999 |
} |
| 1000 |
|
| 1001 |
// The following predicate function and predicate functor are for |
| 1002 |
// testing the Truly(predicate) matcher. |
| 1003 |
|
| 1004 |
// Returns non-zero if the input is positive. Note that the return |
| 1005 |
// type of this function is not bool. It's OK as Truly() accepts any |
| 1006 |
// unary function or functor whose return type can be implicitly |
| 1007 |
// converted to bool. |
| 1008 |
int IsPositive(double x) { return x > 0 ? 1 : 0; } |
| 1009 |
|
| 1010 |
// This functor returns true if the input is greater than the given |
| 1011 |
// number. |
| 1012 |
class IsGreaterThan { |
| 1013 |
public: |
| 1014 |
explicit IsGreaterThan(int threshold) : threshold_(threshold) {} |
| 1015 |
|
| 1016 |
bool operator()(int n) const { return n > threshold_; } |
| 1017 |
|
| 1018 |
private: |
| 1019 |
int threshold_; |
| 1020 |
}; |
| 1021 |
|
| 1022 |
// For testing Truly(). |
| 1023 |
const int foo = 0; |
| 1024 |
|
| 1025 |
// This predicate returns true if and only if the argument references foo and |
| 1026 |
// has a zero value. |
| 1027 |
bool ReferencesFooAndIsZero(const int& n) { return (&n == &foo) && (n == 0); } |
| 1028 |
|
| 1029 |
// Tests that Truly(predicate) matches what satisfies the given |
| 1030 |
// predicate. |
| 1031 |
TEST(TrulyTest, MatchesWhatSatisfiesThePredicate) { |
| 1032 |
Matcher<double> m = Truly(IsPositive); |
| 1033 |
EXPECT_TRUE(m.Matches(2.0)); |
| 1034 |
EXPECT_FALSE(m.Matches(-1.5)); |
| 1035 |
} |
| 1036 |
|
| 1037 |
// Tests that Truly(predicate_functor) works too. |
| 1038 |
TEST(TrulyTest, CanBeUsedWithFunctor) { |
| 1039 |
Matcher<int> m = Truly(IsGreaterThan(5)); |
| 1040 |
EXPECT_TRUE(m.Matches(6)); |
| 1041 |
EXPECT_FALSE(m.Matches(4)); |
| 1042 |
} |
| 1043 |
|
| 1044 |
// A class that can be implicitly converted to bool. |
| 1045 |
class ConvertibleToBool { |
| 1046 |
public: |
| 1047 |
explicit ConvertibleToBool(int number) : number_(number) {} |
| 1048 |
operator bool() const { return number_ != 0; } |
| 1049 |
|
| 1050 |
private: |
| 1051 |
int number_; |
| 1052 |
}; |
| 1053 |
|
| 1054 |
ConvertibleToBool IsNotZero(int number) { return ConvertibleToBool(number); } |
| 1055 |
|
| 1056 |
// Tests that the predicate used in Truly() may return a class that's |
| 1057 |
// implicitly convertible to bool, even when the class has no |
| 1058 |
// operator!(). |
| 1059 |
TEST(TrulyTest, PredicateCanReturnAClassConvertibleToBool) { |
| 1060 |
Matcher<int> m = Truly(IsNotZero); |
| 1061 |
EXPECT_TRUE(m.Matches(1)); |
| 1062 |
EXPECT_FALSE(m.Matches(0)); |
| 1063 |
} |
| 1064 |
|
| 1065 |
// Tests that Truly(predicate) can describe itself properly. |
| 1066 |
TEST(TrulyTest, CanDescribeSelf) { |
| 1067 |
Matcher<double> m = Truly(IsPositive); |
| 1068 |
EXPECT_EQ("satisfies the given predicate", Describe(m)); |
| 1069 |
} |
| 1070 |
|
| 1071 |
// Tests that Truly(predicate) works when the matcher takes its |
| 1072 |
// argument by reference. |
| 1073 |
TEST(TrulyTest, WorksForByRefArguments) { |
| 1074 |
Matcher<const int&> m = Truly(ReferencesFooAndIsZero); |
| 1075 |
EXPECT_TRUE(m.Matches(foo)); |
| 1076 |
int n = 0; |
| 1077 |
EXPECT_FALSE(m.Matches(n)); |
| 1078 |
} |
| 1079 |
|
| 1080 |
// Tests that Truly(predicate) provides a helpful reason when it fails. |
| 1081 |
TEST(TrulyTest, ExplainsFailures) { |
| 1082 |
StringMatchResultListener listener; |
| 1083 |
EXPECT_FALSE(ExplainMatchResult(Truly(IsPositive), -1, &listener)); |
| 1084 |
EXPECT_EQ(listener.str(), "didn't satisfy the given predicate"); |
| 1085 |
} |
| 1086 |
|
| 1087 |
// Tests that Matches(m) is a predicate satisfied by whatever that |
| 1088 |
// matches matcher m. |
| 1089 |
TEST(MatchesTest, IsSatisfiedByWhatMatchesTheMatcher) { |
| 1090 |
EXPECT_TRUE(Matches(Ge(0))(1)); |
| 1091 |
EXPECT_FALSE(Matches(Eq('a'))('b')); |
| 1092 |
} |
| 1093 |
|
| 1094 |
// Tests that Matches(m) works when the matcher takes its argument by |
| 1095 |
// reference. |
| 1096 |
TEST(MatchesTest, WorksOnByRefArguments) { |
| 1097 |
int m = 0, n = 0; |
| 1098 |
EXPECT_TRUE(Matches(AllOf(Ref(n), Eq(0)))(n)); |
| 1099 |
EXPECT_FALSE(Matches(Ref(m))(n)); |
| 1100 |
} |
| 1101 |
|
| 1102 |
// Tests that a Matcher on non-reference type can be used in |
| 1103 |
// Matches(). |
| 1104 |
TEST(MatchesTest, WorksWithMatcherOnNonRefType) { |
| 1105 |
Matcher<int> eq5 = Eq(5); |
| 1106 |
EXPECT_TRUE(Matches(eq5)(5)); |
| 1107 |
EXPECT_FALSE(Matches(eq5)(2)); |
| 1108 |
} |
| 1109 |
|
| 1110 |
// Tests Value(value, matcher). Since Value() is a simple wrapper for |
| 1111 |
// Matches(), which has been tested already, we don't spend a lot of |
| 1112 |
// effort on testing Value(). |
| 1113 |
TEST(ValueTest, WorksWithPolymorphicMatcher) { |
| 1114 |
EXPECT_TRUE(Value("hi", StartsWith("h"))); |
| 1115 |
EXPECT_FALSE(Value(5, Gt(10))); |
| 1116 |
} |
| 1117 |
|
| 1118 |
TEST(ValueTest, WorksWithMonomorphicMatcher) { |
| 1119 |
const Matcher<int> is_zero = Eq(0); |
| 1120 |
EXPECT_TRUE(Value(0, is_zero)); |
| 1121 |
EXPECT_FALSE(Value('a', is_zero)); |
| 1122 |
|
| 1123 |
int n = 0; |
| 1124 |
const Matcher<const int&> ref_n = Ref(n); |
| 1125 |
EXPECT_TRUE(Value(n, ref_n)); |
| 1126 |
EXPECT_FALSE(Value(1, ref_n)); |
| 1127 |
} |
| 1128 |
|
| 1129 |
TEST(AllArgsTest, WorksForTuple) { |
| 1130 |
EXPECT_THAT(std::make_tuple(1, 2L), AllArgs(Lt())); |
| 1131 |
EXPECT_THAT(std::make_tuple(2L, 1), Not(AllArgs(Lt()))); |
| 1132 |
} |
| 1133 |
|
| 1134 |
TEST(AllArgsTest, WorksForNonTuple) { |
| 1135 |
EXPECT_THAT(42, AllArgs(Gt(0))); |
| 1136 |
EXPECT_THAT('a', Not(AllArgs(Eq('b')))); |
| 1137 |
} |
| 1138 |
|
| 1139 |
class AllArgsHelper { |
| 1140 |
public: |
| 1141 |
AllArgsHelper() = default; |
| 1142 |
|
| 1143 |
MOCK_METHOD2(Helper, int(char x, int y)); |
| 1144 |
|
| 1145 |
private: |
| 1146 |
AllArgsHelper(const AllArgsHelper&) = delete; |
| 1147 |
AllArgsHelper& operator=(const AllArgsHelper&) = delete; |
| 1148 |
}; |
| 1149 |
|
| 1150 |
TEST(AllArgsTest, WorksInWithClause) { |
| 1151 |
AllArgsHelper helper; |
| 1152 |
ON_CALL(helper, Helper(_, _)).With(AllArgs(Lt())).WillByDefault(Return(1)); |
| 1153 |
EXPECT_CALL(helper, Helper(_, _)); |
| 1154 |
EXPECT_CALL(helper, Helper(_, _)).With(AllArgs(Gt())).WillOnce(Return(2)); |
| 1155 |
|
| 1156 |
EXPECT_EQ(1, helper.Helper('\1', 2)); |
| 1157 |
EXPECT_EQ(2, helper.Helper('a', 1)); |
| 1158 |
} |
| 1159 |
|
| 1160 |
class OptionalMatchersHelper { |
| 1161 |
public: |
| 1162 |
OptionalMatchersHelper() = default; |
| 1163 |
|
| 1164 |
MOCK_METHOD0(NoArgs, int()); |
| 1165 |
|
| 1166 |
MOCK_METHOD1(OneArg, int(int y)); |
| 1167 |
|
| 1168 |
MOCK_METHOD2(TwoArgs, int(char x, int y)); |
| 1169 |
|
| 1170 |
MOCK_METHOD1(Overloaded, int(char x)); |
| 1171 |
MOCK_METHOD2(Overloaded, int(char x, int y)); |
| 1172 |
|
| 1173 |
private: |
| 1174 |
OptionalMatchersHelper(const OptionalMatchersHelper&) = delete; |
| 1175 |
OptionalMatchersHelper& operator=(const OptionalMatchersHelper&) = delete; |
| 1176 |
}; |
| 1177 |
|
| 1178 |
TEST(AllArgsTest, WorksWithoutMatchers) { |
| 1179 |
OptionalMatchersHelper helper; |
| 1180 |
|
| 1181 |
ON_CALL(helper, NoArgs).WillByDefault(Return(10)); |
| 1182 |
ON_CALL(helper, OneArg).WillByDefault(Return(20)); |
| 1183 |
ON_CALL(helper, TwoArgs).WillByDefault(Return(30)); |
| 1184 |
|
| 1185 |
EXPECT_EQ(10, helper.NoArgs()); |
| 1186 |
EXPECT_EQ(20, helper.OneArg(1)); |
| 1187 |
EXPECT_EQ(30, helper.TwoArgs('\1', 2)); |
| 1188 |
|
| 1189 |
EXPECT_CALL(helper, NoArgs).Times(1); |
| 1190 |
EXPECT_CALL(helper, OneArg).WillOnce(Return(100)); |
| 1191 |
EXPECT_CALL(helper, OneArg(17)).WillOnce(Return(200)); |
| 1192 |
EXPECT_CALL(helper, TwoArgs).Times(0); |
| 1193 |
|
| 1194 |
EXPECT_EQ(10, helper.NoArgs()); |
| 1195 |
EXPECT_EQ(100, helper.OneArg(1)); |
| 1196 |
EXPECT_EQ(200, helper.OneArg(17)); |
| 1197 |
} |
| 1198 |
|
| 1199 |
// Tests floating-point matchers. |
| 1200 |
template <typename RawType> |
| 1201 |
class FloatingPointTest : public testing::Test { |
| 1202 |
protected: |
| 1203 |
typedef testing::internal::FloatingPoint<RawType> Floating; |
| 1204 |
typedef typename Floating::Bits Bits; |
| 1205 |
|
| 1206 |
FloatingPointTest() |
| 1207 |
: max_ulps_(Floating::kMaxUlps), |
| 1208 |
zero_bits_(Floating(0).bits()), |
| 1209 |
one_bits_(Floating(1).bits()), |
| 1210 |
infinity_bits_(Floating(Floating::Infinity()).bits()), |
| 1211 |
close_to_positive_zero_( |
| 1212 |
Floating::ReinterpretBits(zero_bits_ + max_ulps_ / 2)), |
| 1213 |
close_to_negative_zero_( |
| 1214 |
-Floating::ReinterpretBits(zero_bits_ + max_ulps_ - max_ulps_ / 2)), |
| 1215 |
further_from_negative_zero_(-Floating::ReinterpretBits( |
| 1216 |
zero_bits_ + max_ulps_ + 1 - max_ulps_ / 2)), |
| 1217 |
close_to_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_)), |
| 1218 |
further_from_one_(Floating::ReinterpretBits(one_bits_ + max_ulps_ + 1)), |
| 1219 |
infinity_(Floating::Infinity()), |
| 1220 |
close_to_infinity_( |
| 1221 |
Floating::ReinterpretBits(infinity_bits_ - max_ulps_)), |
| 1222 |
further_from_infinity_( |
| 1223 |
Floating::ReinterpretBits(infinity_bits_ - max_ulps_ - 1)), |
| 1224 |
max_(std::numeric_limits<RawType>::max()), |
| 1225 |
nan1_(Floating::ReinterpretBits(Floating::kExponentBitMask | 1)), |
| 1226 |
nan2_(Floating::ReinterpretBits(Floating::kExponentBitMask | 200)) {} |
| 1227 |
|
| 1228 |
void TestSize() { EXPECT_EQ(sizeof(RawType), sizeof(Bits)); } |
| 1229 |
|
| 1230 |
// A battery of tests for FloatingEqMatcher::Matches. |
| 1231 |
// matcher_maker is a pointer to a function which creates a FloatingEqMatcher. |
| 1232 |
void TestMatches( |
| 1233 |
testing::internal::FloatingEqMatcher<RawType> (*matcher_maker)(RawType)) { |
| 1234 |
Matcher<RawType> m1 = matcher_maker(0.0); |
| 1235 |
EXPECT_TRUE(m1.Matches(-0.0)); |
| 1236 |
EXPECT_TRUE(m1.Matches(close_to_positive_zero_)); |
| 1237 |
EXPECT_TRUE(m1.Matches(close_to_negative_zero_)); |
| 1238 |
EXPECT_FALSE(m1.Matches(1.0)); |
| 1239 |
|
| 1240 |
Matcher<RawType> m2 = matcher_maker(close_to_positive_zero_); |
| 1241 |
EXPECT_FALSE(m2.Matches(further_from_negative_zero_)); |
| 1242 |
|
| 1243 |
Matcher<RawType> m3 = matcher_maker(1.0); |
| 1244 |
EXPECT_TRUE(m3.Matches(close_to_one_)); |
| 1245 |
EXPECT_FALSE(m3.Matches(further_from_one_)); |
| 1246 |
|
| 1247 |
// Test commutativity: matcher_maker(0.0).Matches(1.0) was tested above. |
| 1248 |
EXPECT_FALSE(m3.Matches(0.0)); |
| 1249 |
|
| 1250 |
Matcher<RawType> m4 = matcher_maker(-infinity_); |
| 1251 |
EXPECT_TRUE(m4.Matches(-close_to_infinity_)); |
| 1252 |
|
| 1253 |
Matcher<RawType> m5 = matcher_maker(infinity_); |
| 1254 |
EXPECT_TRUE(m5.Matches(close_to_infinity_)); |
| 1255 |
|
| 1256 |
// This is interesting as the representations of infinity_ and nan1_ |
| 1257 |
// are only 1 DLP apart. |
| 1258 |
EXPECT_FALSE(m5.Matches(nan1_)); |
| 1259 |
|
| 1260 |
// matcher_maker can produce a Matcher<const RawType&>, which is needed in |
| 1261 |
// some cases. |
| 1262 |
Matcher<const RawType&> m6 = matcher_maker(0.0); |
| 1263 |
EXPECT_TRUE(m6.Matches(-0.0)); |
| 1264 |
EXPECT_TRUE(m6.Matches(close_to_positive_zero_)); |
| 1265 |
EXPECT_FALSE(m6.Matches(1.0)); |
| 1266 |
|
| 1267 |
// matcher_maker can produce a Matcher<RawType&>, which is needed in some |
| 1268 |
// cases. |
| 1269 |
Matcher<RawType&> m7 = matcher_maker(0.0); |
| 1270 |
RawType x = 0.0; |
| 1271 |
EXPECT_TRUE(m7.Matches(x)); |
| 1272 |
x = 0.01f; |
| 1273 |
EXPECT_FALSE(m7.Matches(x)); |
| 1274 |
} |
| 1275 |
|
| 1276 |
// Pre-calculated numbers to be used by the tests. |
| 1277 |
|
| 1278 |
const Bits max_ulps_; |
| 1279 |
|
| 1280 |
const Bits zero_bits_; // The bits that represent 0.0. |
| 1281 |
const Bits one_bits_; // The bits that represent 1.0. |
| 1282 |
const Bits infinity_bits_; // The bits that represent +infinity. |
| 1283 |
|
| 1284 |
// Some numbers close to 0.0. |
| 1285 |
const RawType close_to_positive_zero_; |
| 1286 |
const RawType close_to_negative_zero_; |
| 1287 |
const RawType further_from_negative_zero_; |
| 1288 |
|
| 1289 |
// Some numbers close to 1.0. |
| 1290 |
const RawType close_to_one_; |
| 1291 |
const RawType further_from_one_; |
| 1292 |
|
| 1293 |
// Some numbers close to +infinity. |
| 1294 |
const RawType infinity_; |
| 1295 |
const RawType close_to_infinity_; |
| 1296 |
const RawType further_from_infinity_; |
| 1297 |
|
| 1298 |
// Maximum representable value that's not infinity. |
| 1299 |
const RawType max_; |
| 1300 |
|
| 1301 |
// Some NaNs. |
| 1302 |
const RawType nan1_; |
| 1303 |
const RawType nan2_; |
| 1304 |
}; |
| 1305 |
|
| 1306 |
// Tests floating-point matchers with fixed epsilons. |
| 1307 |
template <typename RawType> |
| 1308 |
class FloatingPointNearTest : public FloatingPointTest<RawType> { |
| 1309 |
protected: |
| 1310 |
typedef FloatingPointTest<RawType> ParentType; |
| 1311 |
|
| 1312 |
// A battery of tests for FloatingEqMatcher::Matches with a fixed epsilon. |
| 1313 |
// matcher_maker is a pointer to a function which creates a FloatingEqMatcher. |
| 1314 |
void TestNearMatches(testing::internal::FloatingEqMatcher<RawType> ( |
| 1315 |
*matcher_maker)(RawType, RawType)) { |
| 1316 |
Matcher<RawType> m1 = matcher_maker(0.0, 0.0); |
| 1317 |
EXPECT_TRUE(m1.Matches(0.0)); |
| 1318 |
EXPECT_TRUE(m1.Matches(-0.0)); |
| 1319 |
EXPECT_FALSE(m1.Matches(ParentType::close_to_positive_zero_)); |
| 1320 |
EXPECT_FALSE(m1.Matches(ParentType::close_to_negative_zero_)); |
| 1321 |
EXPECT_FALSE(m1.Matches(1.0)); |
| 1322 |
|
| 1323 |
Matcher<RawType> m2 = matcher_maker(0.0, 1.0); |
| 1324 |
EXPECT_TRUE(m2.Matches(0.0)); |
| 1325 |
EXPECT_TRUE(m2.Matches(-0.0)); |
| 1326 |
EXPECT_TRUE(m2.Matches(1.0)); |
| 1327 |
EXPECT_TRUE(m2.Matches(-1.0)); |
| 1328 |
EXPECT_FALSE(m2.Matches(ParentType::close_to_one_)); |
| 1329 |
EXPECT_FALSE(m2.Matches(-ParentType::close_to_one_)); |
| 1330 |
|
| 1331 |
// Check that inf matches inf, regardless of the of the specified max |
| 1332 |
// absolute error. |
| 1333 |
Matcher<RawType> m3 = matcher_maker(ParentType::infinity_, 0.0); |
| 1334 |
EXPECT_TRUE(m3.Matches(ParentType::infinity_)); |
| 1335 |
EXPECT_FALSE(m3.Matches(ParentType::close_to_infinity_)); |
| 1336 |
EXPECT_FALSE(m3.Matches(-ParentType::infinity_)); |
| 1337 |
|
| 1338 |
Matcher<RawType> m4 = matcher_maker(-ParentType::infinity_, 0.0); |
| 1339 |
EXPECT_TRUE(m4.Matches(-ParentType::infinity_)); |
| 1340 |
EXPECT_FALSE(m4.Matches(-ParentType::close_to_infinity_)); |
| 1341 |
EXPECT_FALSE(m4.Matches(ParentType::infinity_)); |
| 1342 |
|
| 1343 |
// Test various overflow scenarios. |
| 1344 |
Matcher<RawType> m5 = matcher_maker(ParentType::max_, ParentType::max_); |
| 1345 |
EXPECT_TRUE(m5.Matches(ParentType::max_)); |
| 1346 |
EXPECT_FALSE(m5.Matches(-ParentType::max_)); |
| 1347 |
|
| 1348 |
Matcher<RawType> m6 = matcher_maker(-ParentType::max_, ParentType::max_); |
| 1349 |
EXPECT_FALSE(m6.Matches(ParentType::max_)); |
| 1350 |
EXPECT_TRUE(m6.Matches(-ParentType::max_)); |
| 1351 |
|
| 1352 |
Matcher<RawType> m7 = matcher_maker(ParentType::max_, 0); |
| 1353 |
EXPECT_TRUE(m7.Matches(ParentType::max_)); |
| 1354 |
EXPECT_FALSE(m7.Matches(-ParentType::max_)); |
| 1355 |
|
| 1356 |
Matcher<RawType> m8 = matcher_maker(-ParentType::max_, 0); |
| 1357 |
EXPECT_FALSE(m8.Matches(ParentType::max_)); |
| 1358 |
EXPECT_TRUE(m8.Matches(-ParentType::max_)); |
| 1359 |
|
| 1360 |
// The difference between max() and -max() normally overflows to infinity, |
| 1361 |
// but it should still match if the max_abs_error is also infinity. |
| 1362 |
Matcher<RawType> m9 = |
| 1363 |
matcher_maker(ParentType::max_, ParentType::infinity_); |
| 1364 |
EXPECT_TRUE(m8.Matches(-ParentType::max_)); |
| 1365 |
|
| 1366 |
// matcher_maker can produce a Matcher<const RawType&>, which is needed in |
| 1367 |
// some cases. |
| 1368 |
Matcher<const RawType&> m10 = matcher_maker(0.0, 1.0); |
| 1369 |
EXPECT_TRUE(m10.Matches(-0.0)); |
| 1370 |
EXPECT_TRUE(m10.Matches(ParentType::close_to_positive_zero_)); |
| 1371 |
EXPECT_FALSE(m10.Matches(ParentType::close_to_one_)); |
| 1372 |
|
| 1373 |
// matcher_maker can produce a Matcher<RawType&>, which is needed in some |
| 1374 |
// cases. |
| 1375 |
Matcher<RawType&> m11 = matcher_maker(0.0, 1.0); |
| 1376 |
RawType x = 0.0; |
| 1377 |
EXPECT_TRUE(m11.Matches(x)); |
| 1378 |
x = 1.0f; |
| 1379 |
EXPECT_TRUE(m11.Matches(x)); |
| 1380 |
x = -1.0f; |
| 1381 |
EXPECT_TRUE(m11.Matches(x)); |
| 1382 |
x = 1.1f; |
| 1383 |
EXPECT_FALSE(m11.Matches(x)); |
| 1384 |
x = -1.1f; |
| 1385 |
EXPECT_FALSE(m11.Matches(x)); |
| 1386 |
} |
| 1387 |
}; |
| 1388 |
|
| 1389 |
// Instantiate FloatingPointTest for testing floats. |
| 1390 |
typedef FloatingPointTest<float> FloatTest; |
| 1391 |
|
| 1392 |
TEST_F(FloatTest, FloatEqApproximatelyMatchesFloats) { TestMatches(&FloatEq); } |
| 1393 |
|
| 1394 |
TEST_F(FloatTest, NanSensitiveFloatEqApproximatelyMatchesFloats) { |
| 1395 |
TestMatches(&NanSensitiveFloatEq); |
| 1396 |
} |
| 1397 |
|
| 1398 |
TEST_F(FloatTest, FloatEqCannotMatchNaN) { |
| 1399 |
// FloatEq never matches NaN. |
| 1400 |
Matcher<float> m = FloatEq(nan1_); |
| 1401 |
EXPECT_FALSE(m.Matches(nan1_)); |
| 1402 |
EXPECT_FALSE(m.Matches(nan2_)); |
| 1403 |
EXPECT_FALSE(m.Matches(1.0)); |
| 1404 |
} |
| 1405 |
|
| 1406 |
TEST_F(FloatTest, NanSensitiveFloatEqCanMatchNaN) { |
| 1407 |
// NanSensitiveFloatEq will match NaN. |
| 1408 |
Matcher<float> m = NanSensitiveFloatEq(nan1_); |
| 1409 |
EXPECT_TRUE(m.Matches(nan1_)); |
| 1410 |
EXPECT_TRUE(m.Matches(nan2_)); |
| 1411 |
EXPECT_FALSE(m.Matches(1.0)); |
| 1412 |
} |
| 1413 |
|
| 1414 |
TEST_F(FloatTest, FloatEqCanDescribeSelf) { |
| 1415 |
Matcher<float> m1 = FloatEq(2.0f); |
| 1416 |
EXPECT_EQ("is approximately 2", Describe(m1)); |
| 1417 |
EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
| 1418 |
|
| 1419 |
Matcher<float> m2 = FloatEq(0.5f); |
| 1420 |
EXPECT_EQ("is approximately 0.5", Describe(m2)); |
| 1421 |
EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
| 1422 |
|
| 1423 |
Matcher<float> m3 = FloatEq(nan1_); |
| 1424 |
EXPECT_EQ("never matches", Describe(m3)); |
| 1425 |
EXPECT_EQ("is anything", DescribeNegation(m3)); |
| 1426 |
} |
| 1427 |
|
| 1428 |
TEST_F(FloatTest, NanSensitiveFloatEqCanDescribeSelf) { |
| 1429 |
Matcher<float> m1 = NanSensitiveFloatEq(2.0f); |
| 1430 |
EXPECT_EQ("is approximately 2", Describe(m1)); |
| 1431 |
EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
| 1432 |
|
| 1433 |
Matcher<float> m2 = NanSensitiveFloatEq(0.5f); |
| 1434 |
EXPECT_EQ("is approximately 0.5", Describe(m2)); |
| 1435 |
EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
| 1436 |
|
| 1437 |
Matcher<float> m3 = NanSensitiveFloatEq(nan1_); |
| 1438 |
EXPECT_EQ("is NaN", Describe(m3)); |
| 1439 |
EXPECT_EQ("isn't NaN", DescribeNegation(m3)); |
| 1440 |
} |
| 1441 |
|
| 1442 |
// Instantiate FloatingPointTest for testing floats with a user-specified |
| 1443 |
// max absolute error. |
| 1444 |
typedef FloatingPointNearTest<float> FloatNearTest; |
| 1445 |
|
| 1446 |
TEST_F(FloatNearTest, FloatNearMatches) { TestNearMatches(&FloatNear); } |
| 1447 |
|
| 1448 |
TEST_F(FloatNearTest, NanSensitiveFloatNearApproximatelyMatchesFloats) { |
| 1449 |
TestNearMatches(&NanSensitiveFloatNear); |
| 1450 |
} |
| 1451 |
|
| 1452 |
TEST_F(FloatNearTest, FloatNearCanDescribeSelf) { |
| 1453 |
Matcher<float> m1 = FloatNear(2.0f, 0.5f); |
| 1454 |
EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1)); |
| 1455 |
EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)", |
| 1456 |
DescribeNegation(m1)); |
| 1457 |
|
| 1458 |
Matcher<float> m2 = FloatNear(0.5f, 0.5f); |
| 1459 |
EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2)); |
| 1460 |
EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)", |
| 1461 |
DescribeNegation(m2)); |
| 1462 |
|
| 1463 |
Matcher<float> m3 = FloatNear(nan1_, 0.0); |
| 1464 |
EXPECT_EQ("never matches", Describe(m3)); |
| 1465 |
EXPECT_EQ("is anything", DescribeNegation(m3)); |
| 1466 |
} |
| 1467 |
|
| 1468 |
TEST_F(FloatNearTest, NanSensitiveFloatNearCanDescribeSelf) { |
| 1469 |
Matcher<float> m1 = NanSensitiveFloatNear(2.0f, 0.5f); |
| 1470 |
EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1)); |
| 1471 |
EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)", |
| 1472 |
DescribeNegation(m1)); |
| 1473 |
|
| 1474 |
Matcher<float> m2 = NanSensitiveFloatNear(0.5f, 0.5f); |
| 1475 |
EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2)); |
| 1476 |
EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)", |
| 1477 |
DescribeNegation(m2)); |
| 1478 |
|
| 1479 |
Matcher<float> m3 = NanSensitiveFloatNear(nan1_, 0.1f); |
| 1480 |
EXPECT_EQ("is NaN", Describe(m3)); |
| 1481 |
EXPECT_EQ("isn't NaN", DescribeNegation(m3)); |
| 1482 |
} |
| 1483 |
|
| 1484 |
TEST_F(FloatNearTest, FloatNearCannotMatchNaN) { |
| 1485 |
// FloatNear never matches NaN. |
| 1486 |
Matcher<float> m = FloatNear(ParentType::nan1_, 0.1f); |
| 1487 |
EXPECT_FALSE(m.Matches(nan1_)); |
| 1488 |
EXPECT_FALSE(m.Matches(nan2_)); |
| 1489 |
EXPECT_FALSE(m.Matches(1.0)); |
| 1490 |
} |
| 1491 |
|
| 1492 |
TEST_F(FloatNearTest, NanSensitiveFloatNearCanMatchNaN) { |
| 1493 |
// NanSensitiveFloatNear will match NaN. |
| 1494 |
Matcher<float> m = NanSensitiveFloatNear(nan1_, 0.1f); |
| 1495 |
EXPECT_TRUE(m.Matches(nan1_)); |
| 1496 |
EXPECT_TRUE(m.Matches(nan2_)); |
| 1497 |
EXPECT_FALSE(m.Matches(1.0)); |
| 1498 |
} |
| 1499 |
|
| 1500 |
// Instantiate FloatingPointTest for testing doubles. |
| 1501 |
typedef FloatingPointTest<double> DoubleTest; |
| 1502 |
|
| 1503 |
TEST_F(DoubleTest, DoubleEqApproximatelyMatchesDoubles) { |
| 1504 |
TestMatches(&DoubleEq); |
| 1505 |
} |
| 1506 |
|
| 1507 |
TEST_F(DoubleTest, NanSensitiveDoubleEqApproximatelyMatchesDoubles) { |
| 1508 |
TestMatches(&NanSensitiveDoubleEq); |
| 1509 |
} |
| 1510 |
|
| 1511 |
TEST_F(DoubleTest, DoubleEqCannotMatchNaN) { |
| 1512 |
// DoubleEq never matches NaN. |
| 1513 |
Matcher<double> m = DoubleEq(nan1_); |
| 1514 |
EXPECT_FALSE(m.Matches(nan1_)); |
| 1515 |
EXPECT_FALSE(m.Matches(nan2_)); |
| 1516 |
EXPECT_FALSE(m.Matches(1.0)); |
| 1517 |
} |
| 1518 |
|
| 1519 |
TEST_F(DoubleTest, NanSensitiveDoubleEqCanMatchNaN) { |
| 1520 |
// NanSensitiveDoubleEq will match NaN. |
| 1521 |
Matcher<double> m = NanSensitiveDoubleEq(nan1_); |
| 1522 |
EXPECT_TRUE(m.Matches(nan1_)); |
| 1523 |
EXPECT_TRUE(m.Matches(nan2_)); |
| 1524 |
EXPECT_FALSE(m.Matches(1.0)); |
| 1525 |
} |
| 1526 |
|
| 1527 |
TEST_F(DoubleTest, DoubleEqCanDescribeSelf) { |
| 1528 |
Matcher<double> m1 = DoubleEq(2.0); |
| 1529 |
EXPECT_EQ("is approximately 2", Describe(m1)); |
| 1530 |
EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
| 1531 |
|
| 1532 |
Matcher<double> m2 = DoubleEq(0.5); |
| 1533 |
EXPECT_EQ("is approximately 0.5", Describe(m2)); |
| 1534 |
EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
| 1535 |
|
| 1536 |
Matcher<double> m3 = DoubleEq(nan1_); |
| 1537 |
EXPECT_EQ("never matches", Describe(m3)); |
| 1538 |
EXPECT_EQ("is anything", DescribeNegation(m3)); |
| 1539 |
} |
| 1540 |
|
| 1541 |
TEST_F(DoubleTest, NanSensitiveDoubleEqCanDescribeSelf) { |
| 1542 |
Matcher<double> m1 = NanSensitiveDoubleEq(2.0); |
| 1543 |
EXPECT_EQ("is approximately 2", Describe(m1)); |
| 1544 |
EXPECT_EQ("isn't approximately 2", DescribeNegation(m1)); |
| 1545 |
|
| 1546 |
Matcher<double> m2 = NanSensitiveDoubleEq(0.5); |
| 1547 |
EXPECT_EQ("is approximately 0.5", Describe(m2)); |
| 1548 |
EXPECT_EQ("isn't approximately 0.5", DescribeNegation(m2)); |
| 1549 |
|
| 1550 |
Matcher<double> m3 = NanSensitiveDoubleEq(nan1_); |
| 1551 |
EXPECT_EQ("is NaN", Describe(m3)); |
| 1552 |
EXPECT_EQ("isn't NaN", DescribeNegation(m3)); |
| 1553 |
} |
| 1554 |
|
| 1555 |
// Instantiate FloatingPointTest for testing floats with a user-specified |
| 1556 |
// max absolute error. |
| 1557 |
typedef FloatingPointNearTest<double> DoubleNearTest; |
| 1558 |
|
| 1559 |
TEST_F(DoubleNearTest, DoubleNearMatches) { TestNearMatches(&DoubleNear); } |
| 1560 |
|
| 1561 |
TEST_F(DoubleNearTest, NanSensitiveDoubleNearApproximatelyMatchesDoubles) { |
| 1562 |
TestNearMatches(&NanSensitiveDoubleNear); |
| 1563 |
} |
| 1564 |
|
| 1565 |
TEST_F(DoubleNearTest, DoubleNearCanDescribeSelf) { |
| 1566 |
Matcher<double> m1 = DoubleNear(2.0, 0.5); |
| 1567 |
EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1)); |
| 1568 |
EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)", |
| 1569 |
DescribeNegation(m1)); |
| 1570 |
|
| 1571 |
Matcher<double> m2 = DoubleNear(0.5, 0.5); |
| 1572 |
EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2)); |
| 1573 |
EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)", |
| 1574 |
DescribeNegation(m2)); |
| 1575 |
|
| 1576 |
Matcher<double> m3 = DoubleNear(nan1_, 0.0); |
| 1577 |
EXPECT_EQ("never matches", Describe(m3)); |
| 1578 |
EXPECT_EQ("is anything", DescribeNegation(m3)); |
| 1579 |
} |
| 1580 |
|
| 1581 |
TEST_F(DoubleNearTest, ExplainsResultWhenMatchFails) { |
| 1582 |
EXPECT_EQ("", Explain(DoubleNear(2.0, 0.1), 2.05)); |
| 1583 |
EXPECT_EQ("which is 0.2 from 2", Explain(DoubleNear(2.0, 0.1), 2.2)); |
| 1584 |
EXPECT_EQ("which is -0.3 from 2", Explain(DoubleNear(2.0, 0.1), 1.7)); |
| 1585 |
|
| 1586 |
const std::string explanation = |
| 1587 |
Explain(DoubleNear(2.1, 1e-10), 2.1 + 1.2e-10); |
| 1588 |
// Different C++ implementations may print floating-point numbers |
| 1589 |
// slightly differently. |
| 1590 |
EXPECT_TRUE(explanation == "which is 1.2e-10 from 2.1" || // GCC |
| 1591 |
explanation == "which is 1.2e-010 from 2.1") // MSVC |
| 1592 |
<< " where explanation is \"" << explanation << "\"."; |
| 1593 |
} |
| 1594 |
|
| 1595 |
TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanDescribeSelf) { |
| 1596 |
Matcher<double> m1 = NanSensitiveDoubleNear(2.0, 0.5); |
| 1597 |
EXPECT_EQ("is approximately 2 (absolute error <= 0.5)", Describe(m1)); |
| 1598 |
EXPECT_EQ("isn't approximately 2 (absolute error > 0.5)", |
| 1599 |
DescribeNegation(m1)); |
| 1600 |
|
| 1601 |
Matcher<double> m2 = NanSensitiveDoubleNear(0.5, 0.5); |
| 1602 |
EXPECT_EQ("is approximately 0.5 (absolute error <= 0.5)", Describe(m2)); |
| 1603 |
EXPECT_EQ("isn't approximately 0.5 (absolute error > 0.5)", |
| 1604 |
DescribeNegation(m2)); |
| 1605 |
|
| 1606 |
Matcher<double> m3 = NanSensitiveDoubleNear(nan1_, 0.1); |
| 1607 |
EXPECT_EQ("is NaN", Describe(m3)); |
| 1608 |
EXPECT_EQ("isn't NaN", DescribeNegation(m3)); |
| 1609 |
} |
| 1610 |
|
| 1611 |
TEST_F(DoubleNearTest, DoubleNearCannotMatchNaN) { |
| 1612 |
// DoubleNear never matches NaN. |
| 1613 |
Matcher<double> m = DoubleNear(ParentType::nan1_, 0.1); |
| 1614 |
EXPECT_FALSE(m.Matches(nan1_)); |
| 1615 |
EXPECT_FALSE(m.Matches(nan2_)); |
| 1616 |
EXPECT_FALSE(m.Matches(1.0)); |
| 1617 |
} |
| 1618 |
|
| 1619 |
TEST_F(DoubleNearTest, NanSensitiveDoubleNearCanMatchNaN) { |
| 1620 |
// NanSensitiveDoubleNear will match NaN. |
| 1621 |
Matcher<double> m = NanSensitiveDoubleNear(nan1_, 0.1); |
| 1622 |
EXPECT_TRUE(m.Matches(nan1_)); |
| 1623 |
EXPECT_TRUE(m.Matches(nan2_)); |
| 1624 |
EXPECT_FALSE(m.Matches(1.0)); |
| 1625 |
} |
| 1626 |
|
| 1627 |
TEST(NotTest, WorksOnMoveOnlyType) { |
| 1628 |
std::unique_ptr<int> p(new int(3)); |
| 1629 |
EXPECT_THAT(p, Pointee(Eq(3))); |
| 1630 |
EXPECT_THAT(p, Not(Pointee(Eq(2)))); |
| 1631 |
} |
| 1632 |
|
| 1633 |
TEST(AllOfTest, HugeMatcher) { |
| 1634 |
// Verify that using AllOf with many arguments doesn't cause |
| 1635 |
// the compiler to exceed template instantiation depth limit. |
| 1636 |
EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _, |
| 1637 |
testing::AllOf(_, _, _, _, _, _, _, _, _, _))); |
| 1638 |
} |
| 1639 |
|
| 1640 |
TEST(AnyOfTest, HugeMatcher) { |
| 1641 |
// Verify that using AnyOf with many arguments doesn't cause |
| 1642 |
// the compiler to exceed template instantiation depth limit. |
| 1643 |
EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _, |
| 1644 |
testing::AnyOf(_, _, _, _, _, _, _, _, _, _))); |
| 1645 |
} |
| 1646 |
|
| 1647 |
namespace adl_test { |
| 1648 |
|
| 1649 |
// Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf |
| 1650 |
// don't issue unqualified recursive calls. If they do, the argument dependent |
| 1651 |
// name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found |
| 1652 |
// as a candidate and the compilation will break due to an ambiguous overload. |
| 1653 |
|
| 1654 |
// The matcher must be in the same namespace as AllOf/AnyOf to make argument |
| 1655 |
// dependent lookup find those. |
| 1656 |
MATCHER(M, "") { |
| 1657 |
(void)arg; |
| 1658 |
return true; |
| 1659 |
} |
| 1660 |
|
| 1661 |
template <typename T1, typename T2> |
| 1662 |
bool AllOf(const T1& /*t1*/, const T2& /*t2*/) { |
| 1663 |
return true; |
| 1664 |
} |
| 1665 |
|
| 1666 |
TEST(AllOfTest, DoesNotCallAllOfUnqualified) { |
| 1667 |
EXPECT_THAT(42, |
| 1668 |
testing::AllOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M())); |
| 1669 |
} |
| 1670 |
|
| 1671 |
template <typename T1, typename T2> |
| 1672 |
bool AnyOf(const T1&, const T2&) { |
| 1673 |
return true; |
| 1674 |
} |
| 1675 |
|
| 1676 |
TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) { |
| 1677 |
EXPECT_THAT(42, |
| 1678 |
testing::AnyOf(M(), M(), M(), M(), M(), M(), M(), M(), M(), M())); |
| 1679 |
} |
| 1680 |
|
| 1681 |
} // namespace adl_test |
| 1682 |
|
| 1683 |
TEST(AllOfTest, WorksOnMoveOnlyType) { |
| 1684 |
std::unique_ptr<int> p(new int(3)); |
| 1685 |
EXPECT_THAT(p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5)))); |
| 1686 |
EXPECT_THAT(p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3))))); |
| 1687 |
} |
| 1688 |
|
| 1689 |
TEST(AnyOfTest, WorksOnMoveOnlyType) { |
| 1690 |
std::unique_ptr<int> p(new int(3)); |
| 1691 |
EXPECT_THAT(p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5)))); |
| 1692 |
EXPECT_THAT(p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5))))); |
| 1693 |
} |
| 1694 |
|
| 1695 |
} // namespace |
| 1696 |
} // namespace gmock_matchers_test |
| 1697 |
} // namespace testing |
| 1698 |
|
| 1699 |
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4244 4100 |