PageRenderTime 299ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/thirdparty/breakpad/third_party/protobuf/protobuf/gtest/test/gtest_pred_impl_unittest.cc

http://github.com/tomahawk-player/tomahawk
C++ | 2050 lines | 1449 code | 219 blank | 382 comment | 32 complexity | ce191bd144218e3af8586eedb56155e3 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. // Copyright 2006, 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. // This file is AUTOMATICALLY GENERATED on 10/02/2008 by command
  30. // 'gen_gtest_pred_impl.py 5'. DO NOT EDIT BY HAND!
  31. // Regression test for gtest_pred_impl.h
  32. //
  33. // This file is generated by a script and quite long. If you intend to
  34. // learn how Google Test works by reading its unit tests, read
  35. // gtest_unittest.cc instead.
  36. //
  37. // This is intended as a regression test for the Google Test predicate
  38. // assertions. We compile it as part of the gtest_unittest target
  39. // only to keep the implementation tidy and compact, as it is quite
  40. // involved to set up the stage for testing Google Test using Google
  41. // Test itself.
  42. //
  43. // Currently, gtest_unittest takes ~11 seconds to run in the testing
  44. // daemon. In the future, if it grows too large and needs much more
  45. // time to finish, we should consider separating this file into a
  46. // stand-alone regression test.
  47. #include <iostream>
  48. #include <gtest/gtest.h>
  49. #include <gtest/gtest-spi.h>
  50. // A user-defined data type.
  51. struct Bool {
  52. explicit Bool(int val) : value(val != 0) {}
  53. bool operator>(int n) const { return value > Bool(n).value; }
  54. Bool operator+(const Bool& rhs) const { return Bool(value + rhs.value); }
  55. bool operator==(const Bool& rhs) const { return value == rhs.value; }
  56. bool value;
  57. };
  58. // Enables Bool to be used in assertions.
  59. std::ostream& operator<<(std::ostream& os, const Bool& x) {
  60. return os << (x.value ? "true" : "false");
  61. }
  62. // Sample functions/functors for testing unary predicate assertions.
  63. // A unary predicate function.
  64. template <typename T1>
  65. bool PredFunction1(T1 v1) {
  66. return v1 > 0;
  67. }
  68. // The following two functions are needed to circumvent a bug in
  69. // gcc 2.95.3, which sometimes has problem with the above template
  70. // function.
  71. bool PredFunction1Int(int v1) {
  72. return v1 > 0;
  73. }
  74. bool PredFunction1Bool(Bool v1) {
  75. return v1 > 0;
  76. }
  77. // A unary predicate functor.
  78. struct PredFunctor1 {
  79. template <typename T1>
  80. bool operator()(const T1& v1) {
  81. return v1 > 0;
  82. }
  83. };
  84. // A unary predicate-formatter function.
  85. template <typename T1>
  86. testing::AssertionResult PredFormatFunction1(const char* e1,
  87. const T1& v1) {
  88. if (PredFunction1(v1))
  89. return testing::AssertionSuccess();
  90. testing::Message msg;
  91. msg << e1
  92. << " is expected to be positive, but evaluates to "
  93. << v1 << ".";
  94. return testing::AssertionFailure(msg);
  95. }
  96. // A unary predicate-formatter functor.
  97. struct PredFormatFunctor1 {
  98. template <typename T1>
  99. testing::AssertionResult operator()(const char* e1,
  100. const T1& v1) const {
  101. return PredFormatFunction1(e1, v1);
  102. }
  103. };
  104. // Tests for {EXPECT|ASSERT}_PRED_FORMAT1.
  105. class Predicate1Test : public testing::Test {
  106. protected:
  107. virtual void SetUp() {
  108. expected_to_finish_ = true;
  109. finished_ = false;
  110. n1_ = 0;
  111. }
  112. virtual void TearDown() {
  113. // Verifies that each of the predicate's arguments was evaluated
  114. // exactly once.
  115. EXPECT_EQ(1, n1_) <<
  116. "The predicate assertion didn't evaluate argument 2 "
  117. "exactly once.";
  118. // Verifies that the control flow in the test function is expected.
  119. if (expected_to_finish_ && !finished_) {
  120. FAIL() << "The predicate assertion unexpactedly aborted the test.";
  121. } else if (!expected_to_finish_ && finished_) {
  122. FAIL() << "The failed predicate assertion didn't abort the test "
  123. "as expected.";
  124. }
  125. }
  126. // true iff the test function is expected to run to finish.
  127. static bool expected_to_finish_;
  128. // true iff the test function did run to finish.
  129. static bool finished_;
  130. static int n1_;
  131. };
  132. bool Predicate1Test::expected_to_finish_;
  133. bool Predicate1Test::finished_;
  134. int Predicate1Test::n1_;
  135. typedef Predicate1Test EXPECT_PRED_FORMAT1Test;
  136. typedef Predicate1Test ASSERT_PRED_FORMAT1Test;
  137. typedef Predicate1Test EXPECT_PRED1Test;
  138. typedef Predicate1Test ASSERT_PRED1Test;
  139. // Tests a successful EXPECT_PRED1 where the
  140. // predicate-formatter is a function on a built-in type (int).
  141. TEST_F(EXPECT_PRED1Test, FunctionOnBuiltInTypeSuccess) {
  142. EXPECT_PRED1(PredFunction1Int,
  143. ++n1_);
  144. finished_ = true;
  145. }
  146. // Tests a successful EXPECT_PRED1 where the
  147. // predicate-formatter is a function on a user-defined type (Bool).
  148. TEST_F(EXPECT_PRED1Test, FunctionOnUserTypeSuccess) {
  149. EXPECT_PRED1(PredFunction1Bool,
  150. Bool(++n1_));
  151. finished_ = true;
  152. }
  153. // Tests a successful EXPECT_PRED1 where the
  154. // predicate-formatter is a functor on a built-in type (int).
  155. TEST_F(EXPECT_PRED1Test, FunctorOnBuiltInTypeSuccess) {
  156. EXPECT_PRED1(PredFunctor1(),
  157. ++n1_);
  158. finished_ = true;
  159. }
  160. // Tests a successful EXPECT_PRED1 where the
  161. // predicate-formatter is a functor on a user-defined type (Bool).
  162. TEST_F(EXPECT_PRED1Test, FunctorOnUserTypeSuccess) {
  163. EXPECT_PRED1(PredFunctor1(),
  164. Bool(++n1_));
  165. finished_ = true;
  166. }
  167. // Tests a failed EXPECT_PRED1 where the
  168. // predicate-formatter is a function on a built-in type (int).
  169. TEST_F(EXPECT_PRED1Test, FunctionOnBuiltInTypeFailure) {
  170. EXPECT_NONFATAL_FAILURE({ // NOLINT
  171. EXPECT_PRED1(PredFunction1Int,
  172. n1_++);
  173. finished_ = true;
  174. }, "");
  175. }
  176. // Tests a failed EXPECT_PRED1 where the
  177. // predicate-formatter is a function on a user-defined type (Bool).
  178. TEST_F(EXPECT_PRED1Test, FunctionOnUserTypeFailure) {
  179. EXPECT_NONFATAL_FAILURE({ // NOLINT
  180. EXPECT_PRED1(PredFunction1Bool,
  181. Bool(n1_++));
  182. finished_ = true;
  183. }, "");
  184. }
  185. // Tests a failed EXPECT_PRED1 where the
  186. // predicate-formatter is a functor on a built-in type (int).
  187. TEST_F(EXPECT_PRED1Test, FunctorOnBuiltInTypeFailure) {
  188. EXPECT_NONFATAL_FAILURE({ // NOLINT
  189. EXPECT_PRED1(PredFunctor1(),
  190. n1_++);
  191. finished_ = true;
  192. }, "");
  193. }
  194. // Tests a failed EXPECT_PRED1 where the
  195. // predicate-formatter is a functor on a user-defined type (Bool).
  196. TEST_F(EXPECT_PRED1Test, FunctorOnUserTypeFailure) {
  197. EXPECT_NONFATAL_FAILURE({ // NOLINT
  198. EXPECT_PRED1(PredFunctor1(),
  199. Bool(n1_++));
  200. finished_ = true;
  201. }, "");
  202. }
  203. // Tests a successful ASSERT_PRED1 where the
  204. // predicate-formatter is a function on a built-in type (int).
  205. TEST_F(ASSERT_PRED1Test, FunctionOnBuiltInTypeSuccess) {
  206. ASSERT_PRED1(PredFunction1Int,
  207. ++n1_);
  208. finished_ = true;
  209. }
  210. // Tests a successful ASSERT_PRED1 where the
  211. // predicate-formatter is a function on a user-defined type (Bool).
  212. TEST_F(ASSERT_PRED1Test, FunctionOnUserTypeSuccess) {
  213. ASSERT_PRED1(PredFunction1Bool,
  214. Bool(++n1_));
  215. finished_ = true;
  216. }
  217. // Tests a successful ASSERT_PRED1 where the
  218. // predicate-formatter is a functor on a built-in type (int).
  219. TEST_F(ASSERT_PRED1Test, FunctorOnBuiltInTypeSuccess) {
  220. ASSERT_PRED1(PredFunctor1(),
  221. ++n1_);
  222. finished_ = true;
  223. }
  224. // Tests a successful ASSERT_PRED1 where the
  225. // predicate-formatter is a functor on a user-defined type (Bool).
  226. TEST_F(ASSERT_PRED1Test, FunctorOnUserTypeSuccess) {
  227. ASSERT_PRED1(PredFunctor1(),
  228. Bool(++n1_));
  229. finished_ = true;
  230. }
  231. // Tests a failed ASSERT_PRED1 where the
  232. // predicate-formatter is a function on a built-in type (int).
  233. TEST_F(ASSERT_PRED1Test, FunctionOnBuiltInTypeFailure) {
  234. expected_to_finish_ = false;
  235. EXPECT_FATAL_FAILURE({ // NOLINT
  236. ASSERT_PRED1(PredFunction1Int,
  237. n1_++);
  238. finished_ = true;
  239. }, "");
  240. }
  241. // Tests a failed ASSERT_PRED1 where the
  242. // predicate-formatter is a function on a user-defined type (Bool).
  243. TEST_F(ASSERT_PRED1Test, FunctionOnUserTypeFailure) {
  244. expected_to_finish_ = false;
  245. EXPECT_FATAL_FAILURE({ // NOLINT
  246. ASSERT_PRED1(PredFunction1Bool,
  247. Bool(n1_++));
  248. finished_ = true;
  249. }, "");
  250. }
  251. // Tests a failed ASSERT_PRED1 where the
  252. // predicate-formatter is a functor on a built-in type (int).
  253. TEST_F(ASSERT_PRED1Test, FunctorOnBuiltInTypeFailure) {
  254. expected_to_finish_ = false;
  255. EXPECT_FATAL_FAILURE({ // NOLINT
  256. ASSERT_PRED1(PredFunctor1(),
  257. n1_++);
  258. finished_ = true;
  259. }, "");
  260. }
  261. // Tests a failed ASSERT_PRED1 where the
  262. // predicate-formatter is a functor on a user-defined type (Bool).
  263. TEST_F(ASSERT_PRED1Test, FunctorOnUserTypeFailure) {
  264. expected_to_finish_ = false;
  265. EXPECT_FATAL_FAILURE({ // NOLINT
  266. ASSERT_PRED1(PredFunctor1(),
  267. Bool(n1_++));
  268. finished_ = true;
  269. }, "");
  270. }
  271. // Tests a successful EXPECT_PRED_FORMAT1 where the
  272. // predicate-formatter is a function on a built-in type (int).
  273. TEST_F(EXPECT_PRED_FORMAT1Test, FunctionOnBuiltInTypeSuccess) {
  274. EXPECT_PRED_FORMAT1(PredFormatFunction1,
  275. ++n1_);
  276. finished_ = true;
  277. }
  278. // Tests a successful EXPECT_PRED_FORMAT1 where the
  279. // predicate-formatter is a function on a user-defined type (Bool).
  280. TEST_F(EXPECT_PRED_FORMAT1Test, FunctionOnUserTypeSuccess) {
  281. EXPECT_PRED_FORMAT1(PredFormatFunction1,
  282. Bool(++n1_));
  283. finished_ = true;
  284. }
  285. // Tests a successful EXPECT_PRED_FORMAT1 where the
  286. // predicate-formatter is a functor on a built-in type (int).
  287. TEST_F(EXPECT_PRED_FORMAT1Test, FunctorOnBuiltInTypeSuccess) {
  288. EXPECT_PRED_FORMAT1(PredFormatFunctor1(),
  289. ++n1_);
  290. finished_ = true;
  291. }
  292. // Tests a successful EXPECT_PRED_FORMAT1 where the
  293. // predicate-formatter is a functor on a user-defined type (Bool).
  294. TEST_F(EXPECT_PRED_FORMAT1Test, FunctorOnUserTypeSuccess) {
  295. EXPECT_PRED_FORMAT1(PredFormatFunctor1(),
  296. Bool(++n1_));
  297. finished_ = true;
  298. }
  299. // Tests a failed EXPECT_PRED_FORMAT1 where the
  300. // predicate-formatter is a function on a built-in type (int).
  301. TEST_F(EXPECT_PRED_FORMAT1Test, FunctionOnBuiltInTypeFailure) {
  302. EXPECT_NONFATAL_FAILURE({ // NOLINT
  303. EXPECT_PRED_FORMAT1(PredFormatFunction1,
  304. n1_++);
  305. finished_ = true;
  306. }, "");
  307. }
  308. // Tests a failed EXPECT_PRED_FORMAT1 where the
  309. // predicate-formatter is a function on a user-defined type (Bool).
  310. TEST_F(EXPECT_PRED_FORMAT1Test, FunctionOnUserTypeFailure) {
  311. EXPECT_NONFATAL_FAILURE({ // NOLINT
  312. EXPECT_PRED_FORMAT1(PredFormatFunction1,
  313. Bool(n1_++));
  314. finished_ = true;
  315. }, "");
  316. }
  317. // Tests a failed EXPECT_PRED_FORMAT1 where the
  318. // predicate-formatter is a functor on a built-in type (int).
  319. TEST_F(EXPECT_PRED_FORMAT1Test, FunctorOnBuiltInTypeFailure) {
  320. EXPECT_NONFATAL_FAILURE({ // NOLINT
  321. EXPECT_PRED_FORMAT1(PredFormatFunctor1(),
  322. n1_++);
  323. finished_ = true;
  324. }, "");
  325. }
  326. // Tests a failed EXPECT_PRED_FORMAT1 where the
  327. // predicate-formatter is a functor on a user-defined type (Bool).
  328. TEST_F(EXPECT_PRED_FORMAT1Test, FunctorOnUserTypeFailure) {
  329. EXPECT_NONFATAL_FAILURE({ // NOLINT
  330. EXPECT_PRED_FORMAT1(PredFormatFunctor1(),
  331. Bool(n1_++));
  332. finished_ = true;
  333. }, "");
  334. }
  335. // Tests a successful ASSERT_PRED_FORMAT1 where the
  336. // predicate-formatter is a function on a built-in type (int).
  337. TEST_F(ASSERT_PRED_FORMAT1Test, FunctionOnBuiltInTypeSuccess) {
  338. ASSERT_PRED_FORMAT1(PredFormatFunction1,
  339. ++n1_);
  340. finished_ = true;
  341. }
  342. // Tests a successful ASSERT_PRED_FORMAT1 where the
  343. // predicate-formatter is a function on a user-defined type (Bool).
  344. TEST_F(ASSERT_PRED_FORMAT1Test, FunctionOnUserTypeSuccess) {
  345. ASSERT_PRED_FORMAT1(PredFormatFunction1,
  346. Bool(++n1_));
  347. finished_ = true;
  348. }
  349. // Tests a successful ASSERT_PRED_FORMAT1 where the
  350. // predicate-formatter is a functor on a built-in type (int).
  351. TEST_F(ASSERT_PRED_FORMAT1Test, FunctorOnBuiltInTypeSuccess) {
  352. ASSERT_PRED_FORMAT1(PredFormatFunctor1(),
  353. ++n1_);
  354. finished_ = true;
  355. }
  356. // Tests a successful ASSERT_PRED_FORMAT1 where the
  357. // predicate-formatter is a functor on a user-defined type (Bool).
  358. TEST_F(ASSERT_PRED_FORMAT1Test, FunctorOnUserTypeSuccess) {
  359. ASSERT_PRED_FORMAT1(PredFormatFunctor1(),
  360. Bool(++n1_));
  361. finished_ = true;
  362. }
  363. // Tests a failed ASSERT_PRED_FORMAT1 where the
  364. // predicate-formatter is a function on a built-in type (int).
  365. TEST_F(ASSERT_PRED_FORMAT1Test, FunctionOnBuiltInTypeFailure) {
  366. expected_to_finish_ = false;
  367. EXPECT_FATAL_FAILURE({ // NOLINT
  368. ASSERT_PRED_FORMAT1(PredFormatFunction1,
  369. n1_++);
  370. finished_ = true;
  371. }, "");
  372. }
  373. // Tests a failed ASSERT_PRED_FORMAT1 where the
  374. // predicate-formatter is a function on a user-defined type (Bool).
  375. TEST_F(ASSERT_PRED_FORMAT1Test, FunctionOnUserTypeFailure) {
  376. expected_to_finish_ = false;
  377. EXPECT_FATAL_FAILURE({ // NOLINT
  378. ASSERT_PRED_FORMAT1(PredFormatFunction1,
  379. Bool(n1_++));
  380. finished_ = true;
  381. }, "");
  382. }
  383. // Tests a failed ASSERT_PRED_FORMAT1 where the
  384. // predicate-formatter is a functor on a built-in type (int).
  385. TEST_F(ASSERT_PRED_FORMAT1Test, FunctorOnBuiltInTypeFailure) {
  386. expected_to_finish_ = false;
  387. EXPECT_FATAL_FAILURE({ // NOLINT
  388. ASSERT_PRED_FORMAT1(PredFormatFunctor1(),
  389. n1_++);
  390. finished_ = true;
  391. }, "");
  392. }
  393. // Tests a failed ASSERT_PRED_FORMAT1 where the
  394. // predicate-formatter is a functor on a user-defined type (Bool).
  395. TEST_F(ASSERT_PRED_FORMAT1Test, FunctorOnUserTypeFailure) {
  396. expected_to_finish_ = false;
  397. EXPECT_FATAL_FAILURE({ // NOLINT
  398. ASSERT_PRED_FORMAT1(PredFormatFunctor1(),
  399. Bool(n1_++));
  400. finished_ = true;
  401. }, "");
  402. }
  403. // Sample functions/functors for testing binary predicate assertions.
  404. // A binary predicate function.
  405. template <typename T1, typename T2>
  406. bool PredFunction2(T1 v1, T2 v2) {
  407. return v1 + v2 > 0;
  408. }
  409. // The following two functions are needed to circumvent a bug in
  410. // gcc 2.95.3, which sometimes has problem with the above template
  411. // function.
  412. bool PredFunction2Int(int v1, int v2) {
  413. return v1 + v2 > 0;
  414. }
  415. bool PredFunction2Bool(Bool v1, Bool v2) {
  416. return v1 + v2 > 0;
  417. }
  418. // A binary predicate functor.
  419. struct PredFunctor2 {
  420. template <typename T1, typename T2>
  421. bool operator()(const T1& v1,
  422. const T2& v2) {
  423. return v1 + v2 > 0;
  424. }
  425. };
  426. // A binary predicate-formatter function.
  427. template <typename T1, typename T2>
  428. testing::AssertionResult PredFormatFunction2(const char* e1,
  429. const char* e2,
  430. const T1& v1,
  431. const T2& v2) {
  432. if (PredFunction2(v1, v2))
  433. return testing::AssertionSuccess();
  434. testing::Message msg;
  435. msg << e1 << " + " << e2
  436. << " is expected to be positive, but evaluates to "
  437. << v1 + v2 << ".";
  438. return testing::AssertionFailure(msg);
  439. }
  440. // A binary predicate-formatter functor.
  441. struct PredFormatFunctor2 {
  442. template <typename T1, typename T2>
  443. testing::AssertionResult operator()(const char* e1,
  444. const char* e2,
  445. const T1& v1,
  446. const T2& v2) const {
  447. return PredFormatFunction2(e1, e2, v1, v2);
  448. }
  449. };
  450. // Tests for {EXPECT|ASSERT}_PRED_FORMAT2.
  451. class Predicate2Test : public testing::Test {
  452. protected:
  453. virtual void SetUp() {
  454. expected_to_finish_ = true;
  455. finished_ = false;
  456. n1_ = n2_ = 0;
  457. }
  458. virtual void TearDown() {
  459. // Verifies that each of the predicate's arguments was evaluated
  460. // exactly once.
  461. EXPECT_EQ(1, n1_) <<
  462. "The predicate assertion didn't evaluate argument 2 "
  463. "exactly once.";
  464. EXPECT_EQ(1, n2_) <<
  465. "The predicate assertion didn't evaluate argument 3 "
  466. "exactly once.";
  467. // Verifies that the control flow in the test function is expected.
  468. if (expected_to_finish_ && !finished_) {
  469. FAIL() << "The predicate assertion unexpactedly aborted the test.";
  470. } else if (!expected_to_finish_ && finished_) {
  471. FAIL() << "The failed predicate assertion didn't abort the test "
  472. "as expected.";
  473. }
  474. }
  475. // true iff the test function is expected to run to finish.
  476. static bool expected_to_finish_;
  477. // true iff the test function did run to finish.
  478. static bool finished_;
  479. static int n1_;
  480. static int n2_;
  481. };
  482. bool Predicate2Test::expected_to_finish_;
  483. bool Predicate2Test::finished_;
  484. int Predicate2Test::n1_;
  485. int Predicate2Test::n2_;
  486. typedef Predicate2Test EXPECT_PRED_FORMAT2Test;
  487. typedef Predicate2Test ASSERT_PRED_FORMAT2Test;
  488. typedef Predicate2Test EXPECT_PRED2Test;
  489. typedef Predicate2Test ASSERT_PRED2Test;
  490. // Tests a successful EXPECT_PRED2 where the
  491. // predicate-formatter is a function on a built-in type (int).
  492. TEST_F(EXPECT_PRED2Test, FunctionOnBuiltInTypeSuccess) {
  493. EXPECT_PRED2(PredFunction2Int,
  494. ++n1_,
  495. ++n2_);
  496. finished_ = true;
  497. }
  498. // Tests a successful EXPECT_PRED2 where the
  499. // predicate-formatter is a function on a user-defined type (Bool).
  500. TEST_F(EXPECT_PRED2Test, FunctionOnUserTypeSuccess) {
  501. EXPECT_PRED2(PredFunction2Bool,
  502. Bool(++n1_),
  503. Bool(++n2_));
  504. finished_ = true;
  505. }
  506. // Tests a successful EXPECT_PRED2 where the
  507. // predicate-formatter is a functor on a built-in type (int).
  508. TEST_F(EXPECT_PRED2Test, FunctorOnBuiltInTypeSuccess) {
  509. EXPECT_PRED2(PredFunctor2(),
  510. ++n1_,
  511. ++n2_);
  512. finished_ = true;
  513. }
  514. // Tests a successful EXPECT_PRED2 where the
  515. // predicate-formatter is a functor on a user-defined type (Bool).
  516. TEST_F(EXPECT_PRED2Test, FunctorOnUserTypeSuccess) {
  517. EXPECT_PRED2(PredFunctor2(),
  518. Bool(++n1_),
  519. Bool(++n2_));
  520. finished_ = true;
  521. }
  522. // Tests a failed EXPECT_PRED2 where the
  523. // predicate-formatter is a function on a built-in type (int).
  524. TEST_F(EXPECT_PRED2Test, FunctionOnBuiltInTypeFailure) {
  525. EXPECT_NONFATAL_FAILURE({ // NOLINT
  526. EXPECT_PRED2(PredFunction2Int,
  527. n1_++,
  528. n2_++);
  529. finished_ = true;
  530. }, "");
  531. }
  532. // Tests a failed EXPECT_PRED2 where the
  533. // predicate-formatter is a function on a user-defined type (Bool).
  534. TEST_F(EXPECT_PRED2Test, FunctionOnUserTypeFailure) {
  535. EXPECT_NONFATAL_FAILURE({ // NOLINT
  536. EXPECT_PRED2(PredFunction2Bool,
  537. Bool(n1_++),
  538. Bool(n2_++));
  539. finished_ = true;
  540. }, "");
  541. }
  542. // Tests a failed EXPECT_PRED2 where the
  543. // predicate-formatter is a functor on a built-in type (int).
  544. TEST_F(EXPECT_PRED2Test, FunctorOnBuiltInTypeFailure) {
  545. EXPECT_NONFATAL_FAILURE({ // NOLINT
  546. EXPECT_PRED2(PredFunctor2(),
  547. n1_++,
  548. n2_++);
  549. finished_ = true;
  550. }, "");
  551. }
  552. // Tests a failed EXPECT_PRED2 where the
  553. // predicate-formatter is a functor on a user-defined type (Bool).
  554. TEST_F(EXPECT_PRED2Test, FunctorOnUserTypeFailure) {
  555. EXPECT_NONFATAL_FAILURE({ // NOLINT
  556. EXPECT_PRED2(PredFunctor2(),
  557. Bool(n1_++),
  558. Bool(n2_++));
  559. finished_ = true;
  560. }, "");
  561. }
  562. // Tests a successful ASSERT_PRED2 where the
  563. // predicate-formatter is a function on a built-in type (int).
  564. TEST_F(ASSERT_PRED2Test, FunctionOnBuiltInTypeSuccess) {
  565. ASSERT_PRED2(PredFunction2Int,
  566. ++n1_,
  567. ++n2_);
  568. finished_ = true;
  569. }
  570. // Tests a successful ASSERT_PRED2 where the
  571. // predicate-formatter is a function on a user-defined type (Bool).
  572. TEST_F(ASSERT_PRED2Test, FunctionOnUserTypeSuccess) {
  573. ASSERT_PRED2(PredFunction2Bool,
  574. Bool(++n1_),
  575. Bool(++n2_));
  576. finished_ = true;
  577. }
  578. // Tests a successful ASSERT_PRED2 where the
  579. // predicate-formatter is a functor on a built-in type (int).
  580. TEST_F(ASSERT_PRED2Test, FunctorOnBuiltInTypeSuccess) {
  581. ASSERT_PRED2(PredFunctor2(),
  582. ++n1_,
  583. ++n2_);
  584. finished_ = true;
  585. }
  586. // Tests a successful ASSERT_PRED2 where the
  587. // predicate-formatter is a functor on a user-defined type (Bool).
  588. TEST_F(ASSERT_PRED2Test, FunctorOnUserTypeSuccess) {
  589. ASSERT_PRED2(PredFunctor2(),
  590. Bool(++n1_),
  591. Bool(++n2_));
  592. finished_ = true;
  593. }
  594. // Tests a failed ASSERT_PRED2 where the
  595. // predicate-formatter is a function on a built-in type (int).
  596. TEST_F(ASSERT_PRED2Test, FunctionOnBuiltInTypeFailure) {
  597. expected_to_finish_ = false;
  598. EXPECT_FATAL_FAILURE({ // NOLINT
  599. ASSERT_PRED2(PredFunction2Int,
  600. n1_++,
  601. n2_++);
  602. finished_ = true;
  603. }, "");
  604. }
  605. // Tests a failed ASSERT_PRED2 where the
  606. // predicate-formatter is a function on a user-defined type (Bool).
  607. TEST_F(ASSERT_PRED2Test, FunctionOnUserTypeFailure) {
  608. expected_to_finish_ = false;
  609. EXPECT_FATAL_FAILURE({ // NOLINT
  610. ASSERT_PRED2(PredFunction2Bool,
  611. Bool(n1_++),
  612. Bool(n2_++));
  613. finished_ = true;
  614. }, "");
  615. }
  616. // Tests a failed ASSERT_PRED2 where the
  617. // predicate-formatter is a functor on a built-in type (int).
  618. TEST_F(ASSERT_PRED2Test, FunctorOnBuiltInTypeFailure) {
  619. expected_to_finish_ = false;
  620. EXPECT_FATAL_FAILURE({ // NOLINT
  621. ASSERT_PRED2(PredFunctor2(),
  622. n1_++,
  623. n2_++);
  624. finished_ = true;
  625. }, "");
  626. }
  627. // Tests a failed ASSERT_PRED2 where the
  628. // predicate-formatter is a functor on a user-defined type (Bool).
  629. TEST_F(ASSERT_PRED2Test, FunctorOnUserTypeFailure) {
  630. expected_to_finish_ = false;
  631. EXPECT_FATAL_FAILURE({ // NOLINT
  632. ASSERT_PRED2(PredFunctor2(),
  633. Bool(n1_++),
  634. Bool(n2_++));
  635. finished_ = true;
  636. }, "");
  637. }
  638. // Tests a successful EXPECT_PRED_FORMAT2 where the
  639. // predicate-formatter is a function on a built-in type (int).
  640. TEST_F(EXPECT_PRED_FORMAT2Test, FunctionOnBuiltInTypeSuccess) {
  641. EXPECT_PRED_FORMAT2(PredFormatFunction2,
  642. ++n1_,
  643. ++n2_);
  644. finished_ = true;
  645. }
  646. // Tests a successful EXPECT_PRED_FORMAT2 where the
  647. // predicate-formatter is a function on a user-defined type (Bool).
  648. TEST_F(EXPECT_PRED_FORMAT2Test, FunctionOnUserTypeSuccess) {
  649. EXPECT_PRED_FORMAT2(PredFormatFunction2,
  650. Bool(++n1_),
  651. Bool(++n2_));
  652. finished_ = true;
  653. }
  654. // Tests a successful EXPECT_PRED_FORMAT2 where the
  655. // predicate-formatter is a functor on a built-in type (int).
  656. TEST_F(EXPECT_PRED_FORMAT2Test, FunctorOnBuiltInTypeSuccess) {
  657. EXPECT_PRED_FORMAT2(PredFormatFunctor2(),
  658. ++n1_,
  659. ++n2_);
  660. finished_ = true;
  661. }
  662. // Tests a successful EXPECT_PRED_FORMAT2 where the
  663. // predicate-formatter is a functor on a user-defined type (Bool).
  664. TEST_F(EXPECT_PRED_FORMAT2Test, FunctorOnUserTypeSuccess) {
  665. EXPECT_PRED_FORMAT2(PredFormatFunctor2(),
  666. Bool(++n1_),
  667. Bool(++n2_));
  668. finished_ = true;
  669. }
  670. // Tests a failed EXPECT_PRED_FORMAT2 where the
  671. // predicate-formatter is a function on a built-in type (int).
  672. TEST_F(EXPECT_PRED_FORMAT2Test, FunctionOnBuiltInTypeFailure) {
  673. EXPECT_NONFATAL_FAILURE({ // NOLINT
  674. EXPECT_PRED_FORMAT2(PredFormatFunction2,
  675. n1_++,
  676. n2_++);
  677. finished_ = true;
  678. }, "");
  679. }
  680. // Tests a failed EXPECT_PRED_FORMAT2 where the
  681. // predicate-formatter is a function on a user-defined type (Bool).
  682. TEST_F(EXPECT_PRED_FORMAT2Test, FunctionOnUserTypeFailure) {
  683. EXPECT_NONFATAL_FAILURE({ // NOLINT
  684. EXPECT_PRED_FORMAT2(PredFormatFunction2,
  685. Bool(n1_++),
  686. Bool(n2_++));
  687. finished_ = true;
  688. }, "");
  689. }
  690. // Tests a failed EXPECT_PRED_FORMAT2 where the
  691. // predicate-formatter is a functor on a built-in type (int).
  692. TEST_F(EXPECT_PRED_FORMAT2Test, FunctorOnBuiltInTypeFailure) {
  693. EXPECT_NONFATAL_FAILURE({ // NOLINT
  694. EXPECT_PRED_FORMAT2(PredFormatFunctor2(),
  695. n1_++,
  696. n2_++);
  697. finished_ = true;
  698. }, "");
  699. }
  700. // Tests a failed EXPECT_PRED_FORMAT2 where the
  701. // predicate-formatter is a functor on a user-defined type (Bool).
  702. TEST_F(EXPECT_PRED_FORMAT2Test, FunctorOnUserTypeFailure) {
  703. EXPECT_NONFATAL_FAILURE({ // NOLINT
  704. EXPECT_PRED_FORMAT2(PredFormatFunctor2(),
  705. Bool(n1_++),
  706. Bool(n2_++));
  707. finished_ = true;
  708. }, "");
  709. }
  710. // Tests a successful ASSERT_PRED_FORMAT2 where the
  711. // predicate-formatter is a function on a built-in type (int).
  712. TEST_F(ASSERT_PRED_FORMAT2Test, FunctionOnBuiltInTypeSuccess) {
  713. ASSERT_PRED_FORMAT2(PredFormatFunction2,
  714. ++n1_,
  715. ++n2_);
  716. finished_ = true;
  717. }
  718. // Tests a successful ASSERT_PRED_FORMAT2 where the
  719. // predicate-formatter is a function on a user-defined type (Bool).
  720. TEST_F(ASSERT_PRED_FORMAT2Test, FunctionOnUserTypeSuccess) {
  721. ASSERT_PRED_FORMAT2(PredFormatFunction2,
  722. Bool(++n1_),
  723. Bool(++n2_));
  724. finished_ = true;
  725. }
  726. // Tests a successful ASSERT_PRED_FORMAT2 where the
  727. // predicate-formatter is a functor on a built-in type (int).
  728. TEST_F(ASSERT_PRED_FORMAT2Test, FunctorOnBuiltInTypeSuccess) {
  729. ASSERT_PRED_FORMAT2(PredFormatFunctor2(),
  730. ++n1_,
  731. ++n2_);
  732. finished_ = true;
  733. }
  734. // Tests a successful ASSERT_PRED_FORMAT2 where the
  735. // predicate-formatter is a functor on a user-defined type (Bool).
  736. TEST_F(ASSERT_PRED_FORMAT2Test, FunctorOnUserTypeSuccess) {
  737. ASSERT_PRED_FORMAT2(PredFormatFunctor2(),
  738. Bool(++n1_),
  739. Bool(++n2_));
  740. finished_ = true;
  741. }
  742. // Tests a failed ASSERT_PRED_FORMAT2 where the
  743. // predicate-formatter is a function on a built-in type (int).
  744. TEST_F(ASSERT_PRED_FORMAT2Test, FunctionOnBuiltInTypeFailure) {
  745. expected_to_finish_ = false;
  746. EXPECT_FATAL_FAILURE({ // NOLINT
  747. ASSERT_PRED_FORMAT2(PredFormatFunction2,
  748. n1_++,
  749. n2_++);
  750. finished_ = true;
  751. }, "");
  752. }
  753. // Tests a failed ASSERT_PRED_FORMAT2 where the
  754. // predicate-formatter is a function on a user-defined type (Bool).
  755. TEST_F(ASSERT_PRED_FORMAT2Test, FunctionOnUserTypeFailure) {
  756. expected_to_finish_ = false;
  757. EXPECT_FATAL_FAILURE({ // NOLINT
  758. ASSERT_PRED_FORMAT2(PredFormatFunction2,
  759. Bool(n1_++),
  760. Bool(n2_++));
  761. finished_ = true;
  762. }, "");
  763. }
  764. // Tests a failed ASSERT_PRED_FORMAT2 where the
  765. // predicate-formatter is a functor on a built-in type (int).
  766. TEST_F(ASSERT_PRED_FORMAT2Test, FunctorOnBuiltInTypeFailure) {
  767. expected_to_finish_ = false;
  768. EXPECT_FATAL_FAILURE({ // NOLINT
  769. ASSERT_PRED_FORMAT2(PredFormatFunctor2(),
  770. n1_++,
  771. n2_++);
  772. finished_ = true;
  773. }, "");
  774. }
  775. // Tests a failed ASSERT_PRED_FORMAT2 where the
  776. // predicate-formatter is a functor on a user-defined type (Bool).
  777. TEST_F(ASSERT_PRED_FORMAT2Test, FunctorOnUserTypeFailure) {
  778. expected_to_finish_ = false;
  779. EXPECT_FATAL_FAILURE({ // NOLINT
  780. ASSERT_PRED_FORMAT2(PredFormatFunctor2(),
  781. Bool(n1_++),
  782. Bool(n2_++));
  783. finished_ = true;
  784. }, "");
  785. }
  786. // Sample functions/functors for testing ternary predicate assertions.
  787. // A ternary predicate function.
  788. template <typename T1, typename T2, typename T3>
  789. bool PredFunction3(T1 v1, T2 v2, T3 v3) {
  790. return v1 + v2 + v3 > 0;
  791. }
  792. // The following two functions are needed to circumvent a bug in
  793. // gcc 2.95.3, which sometimes has problem with the above template
  794. // function.
  795. bool PredFunction3Int(int v1, int v2, int v3) {
  796. return v1 + v2 + v3 > 0;
  797. }
  798. bool PredFunction3Bool(Bool v1, Bool v2, Bool v3) {
  799. return v1 + v2 + v3 > 0;
  800. }
  801. // A ternary predicate functor.
  802. struct PredFunctor3 {
  803. template <typename T1, typename T2, typename T3>
  804. bool operator()(const T1& v1,
  805. const T2& v2,
  806. const T3& v3) {
  807. return v1 + v2 + v3 > 0;
  808. }
  809. };
  810. // A ternary predicate-formatter function.
  811. template <typename T1, typename T2, typename T3>
  812. testing::AssertionResult PredFormatFunction3(const char* e1,
  813. const char* e2,
  814. const char* e3,
  815. const T1& v1,
  816. const T2& v2,
  817. const T3& v3) {
  818. if (PredFunction3(v1, v2, v3))
  819. return testing::AssertionSuccess();
  820. testing::Message msg;
  821. msg << e1 << " + " << e2 << " + " << e3
  822. << " is expected to be positive, but evaluates to "
  823. << v1 + v2 + v3 << ".";
  824. return testing::AssertionFailure(msg);
  825. }
  826. // A ternary predicate-formatter functor.
  827. struct PredFormatFunctor3 {
  828. template <typename T1, typename T2, typename T3>
  829. testing::AssertionResult operator()(const char* e1,
  830. const char* e2,
  831. const char* e3,
  832. const T1& v1,
  833. const T2& v2,
  834. const T3& v3) const {
  835. return PredFormatFunction3(e1, e2, e3, v1, v2, v3);
  836. }
  837. };
  838. // Tests for {EXPECT|ASSERT}_PRED_FORMAT3.
  839. class Predicate3Test : public testing::Test {
  840. protected:
  841. virtual void SetUp() {
  842. expected_to_finish_ = true;
  843. finished_ = false;
  844. n1_ = n2_ = n3_ = 0;
  845. }
  846. virtual void TearDown() {
  847. // Verifies that each of the predicate's arguments was evaluated
  848. // exactly once.
  849. EXPECT_EQ(1, n1_) <<
  850. "The predicate assertion didn't evaluate argument 2 "
  851. "exactly once.";
  852. EXPECT_EQ(1, n2_) <<
  853. "The predicate assertion didn't evaluate argument 3 "
  854. "exactly once.";
  855. EXPECT_EQ(1, n3_) <<
  856. "The predicate assertion didn't evaluate argument 4 "
  857. "exactly once.";
  858. // Verifies that the control flow in the test function is expected.
  859. if (expected_to_finish_ && !finished_) {
  860. FAIL() << "The predicate assertion unexpactedly aborted the test.";
  861. } else if (!expected_to_finish_ && finished_) {
  862. FAIL() << "The failed predicate assertion didn't abort the test "
  863. "as expected.";
  864. }
  865. }
  866. // true iff the test function is expected to run to finish.
  867. static bool expected_to_finish_;
  868. // true iff the test function did run to finish.
  869. static bool finished_;
  870. static int n1_;
  871. static int n2_;
  872. static int n3_;
  873. };
  874. bool Predicate3Test::expected_to_finish_;
  875. bool Predicate3Test::finished_;
  876. int Predicate3Test::n1_;
  877. int Predicate3Test::n2_;
  878. int Predicate3Test::n3_;
  879. typedef Predicate3Test EXPECT_PRED_FORMAT3Test;
  880. typedef Predicate3Test ASSERT_PRED_FORMAT3Test;
  881. typedef Predicate3Test EXPECT_PRED3Test;
  882. typedef Predicate3Test ASSERT_PRED3Test;
  883. // Tests a successful EXPECT_PRED3 where the
  884. // predicate-formatter is a function on a built-in type (int).
  885. TEST_F(EXPECT_PRED3Test, FunctionOnBuiltInTypeSuccess) {
  886. EXPECT_PRED3(PredFunction3Int,
  887. ++n1_,
  888. ++n2_,
  889. ++n3_);
  890. finished_ = true;
  891. }
  892. // Tests a successful EXPECT_PRED3 where the
  893. // predicate-formatter is a function on a user-defined type (Bool).
  894. TEST_F(EXPECT_PRED3Test, FunctionOnUserTypeSuccess) {
  895. EXPECT_PRED3(PredFunction3Bool,
  896. Bool(++n1_),
  897. Bool(++n2_),
  898. Bool(++n3_));
  899. finished_ = true;
  900. }
  901. // Tests a successful EXPECT_PRED3 where the
  902. // predicate-formatter is a functor on a built-in type (int).
  903. TEST_F(EXPECT_PRED3Test, FunctorOnBuiltInTypeSuccess) {
  904. EXPECT_PRED3(PredFunctor3(),
  905. ++n1_,
  906. ++n2_,
  907. ++n3_);
  908. finished_ = true;
  909. }
  910. // Tests a successful EXPECT_PRED3 where the
  911. // predicate-formatter is a functor on a user-defined type (Bool).
  912. TEST_F(EXPECT_PRED3Test, FunctorOnUserTypeSuccess) {
  913. EXPECT_PRED3(PredFunctor3(),
  914. Bool(++n1_),
  915. Bool(++n2_),
  916. Bool(++n3_));
  917. finished_ = true;
  918. }
  919. // Tests a failed EXPECT_PRED3 where the
  920. // predicate-formatter is a function on a built-in type (int).
  921. TEST_F(EXPECT_PRED3Test, FunctionOnBuiltInTypeFailure) {
  922. EXPECT_NONFATAL_FAILURE({ // NOLINT
  923. EXPECT_PRED3(PredFunction3Int,
  924. n1_++,
  925. n2_++,
  926. n3_++);
  927. finished_ = true;
  928. }, "");
  929. }
  930. // Tests a failed EXPECT_PRED3 where the
  931. // predicate-formatter is a function on a user-defined type (Bool).
  932. TEST_F(EXPECT_PRED3Test, FunctionOnUserTypeFailure) {
  933. EXPECT_NONFATAL_FAILURE({ // NOLINT
  934. EXPECT_PRED3(PredFunction3Bool,
  935. Bool(n1_++),
  936. Bool(n2_++),
  937. Bool(n3_++));
  938. finished_ = true;
  939. }, "");
  940. }
  941. // Tests a failed EXPECT_PRED3 where the
  942. // predicate-formatter is a functor on a built-in type (int).
  943. TEST_F(EXPECT_PRED3Test, FunctorOnBuiltInTypeFailure) {
  944. EXPECT_NONFATAL_FAILURE({ // NOLINT
  945. EXPECT_PRED3(PredFunctor3(),
  946. n1_++,
  947. n2_++,
  948. n3_++);
  949. finished_ = true;
  950. }, "");
  951. }
  952. // Tests a failed EXPECT_PRED3 where the
  953. // predicate-formatter is a functor on a user-defined type (Bool).
  954. TEST_F(EXPECT_PRED3Test, FunctorOnUserTypeFailure) {
  955. EXPECT_NONFATAL_FAILURE({ // NOLINT
  956. EXPECT_PRED3(PredFunctor3(),
  957. Bool(n1_++),
  958. Bool(n2_++),
  959. Bool(n3_++));
  960. finished_ = true;
  961. }, "");
  962. }
  963. // Tests a successful ASSERT_PRED3 where the
  964. // predicate-formatter is a function on a built-in type (int).
  965. TEST_F(ASSERT_PRED3Test, FunctionOnBuiltInTypeSuccess) {
  966. ASSERT_PRED3(PredFunction3Int,
  967. ++n1_,
  968. ++n2_,
  969. ++n3_);
  970. finished_ = true;
  971. }
  972. // Tests a successful ASSERT_PRED3 where the
  973. // predicate-formatter is a function on a user-defined type (Bool).
  974. TEST_F(ASSERT_PRED3Test, FunctionOnUserTypeSuccess) {
  975. ASSERT_PRED3(PredFunction3Bool,
  976. Bool(++n1_),
  977. Bool(++n2_),
  978. Bool(++n3_));
  979. finished_ = true;
  980. }
  981. // Tests a successful ASSERT_PRED3 where the
  982. // predicate-formatter is a functor on a built-in type (int).
  983. TEST_F(ASSERT_PRED3Test, FunctorOnBuiltInTypeSuccess) {
  984. ASSERT_PRED3(PredFunctor3(),
  985. ++n1_,
  986. ++n2_,
  987. ++n3_);
  988. finished_ = true;
  989. }
  990. // Tests a successful ASSERT_PRED3 where the
  991. // predicate-formatter is a functor on a user-defined type (Bool).
  992. TEST_F(ASSERT_PRED3Test, FunctorOnUserTypeSuccess) {
  993. ASSERT_PRED3(PredFunctor3(),
  994. Bool(++n1_),
  995. Bool(++n2_),
  996. Bool(++n3_));
  997. finished_ = true;
  998. }
  999. // Tests a failed ASSERT_PRED3 where the
  1000. // predicate-formatter is a function on a built-in type (int).
  1001. TEST_F(ASSERT_PRED3Test, FunctionOnBuiltInTypeFailure) {
  1002. expected_to_finish_ = false;
  1003. EXPECT_FATAL_FAILURE({ // NOLINT
  1004. ASSERT_PRED3(PredFunction3Int,
  1005. n1_++,
  1006. n2_++,
  1007. n3_++);
  1008. finished_ = true;
  1009. }, "");
  1010. }
  1011. // Tests a failed ASSERT_PRED3 where the
  1012. // predicate-formatter is a function on a user-defined type (Bool).
  1013. TEST_F(ASSERT_PRED3Test, FunctionOnUserTypeFailure) {
  1014. expected_to_finish_ = false;
  1015. EXPECT_FATAL_FAILURE({ // NOLINT
  1016. ASSERT_PRED3(PredFunction3Bool,
  1017. Bool(n1_++),
  1018. Bool(n2_++),
  1019. Bool(n3_++));
  1020. finished_ = true;
  1021. }, "");
  1022. }
  1023. // Tests a failed ASSERT_PRED3 where the
  1024. // predicate-formatter is a functor on a built-in type (int).
  1025. TEST_F(ASSERT_PRED3Test, FunctorOnBuiltInTypeFailure) {
  1026. expected_to_finish_ = false;
  1027. EXPECT_FATAL_FAILURE({ // NOLINT
  1028. ASSERT_PRED3(PredFunctor3(),
  1029. n1_++,
  1030. n2_++,
  1031. n3_++);
  1032. finished_ = true;
  1033. }, "");
  1034. }
  1035. // Tests a failed ASSERT_PRED3 where the
  1036. // predicate-formatter is a functor on a user-defined type (Bool).
  1037. TEST_F(ASSERT_PRED3Test, FunctorOnUserTypeFailure) {
  1038. expected_to_finish_ = false;
  1039. EXPECT_FATAL_FAILURE({ // NOLINT
  1040. ASSERT_PRED3(PredFunctor3(),
  1041. Bool(n1_++),
  1042. Bool(n2_++),
  1043. Bool(n3_++));
  1044. finished_ = true;
  1045. }, "");
  1046. }
  1047. // Tests a successful EXPECT_PRED_FORMAT3 where the
  1048. // predicate-formatter is a function on a built-in type (int).
  1049. TEST_F(EXPECT_PRED_FORMAT3Test, FunctionOnBuiltInTypeSuccess) {
  1050. EXPECT_PRED_FORMAT3(PredFormatFunction3,
  1051. ++n1_,
  1052. ++n2_,
  1053. ++n3_);
  1054. finished_ = true;
  1055. }
  1056. // Tests a successful EXPECT_PRED_FORMAT3 where the
  1057. // predicate-formatter is a function on a user-defined type (Bool).
  1058. TEST_F(EXPECT_PRED_FORMAT3Test, FunctionOnUserTypeSuccess) {
  1059. EXPECT_PRED_FORMAT3(PredFormatFunction3,
  1060. Bool(++n1_),
  1061. Bool(++n2_),
  1062. Bool(++n3_));
  1063. finished_ = true;
  1064. }
  1065. // Tests a successful EXPECT_PRED_FORMAT3 where the
  1066. // predicate-formatter is a functor on a built-in type (int).
  1067. TEST_F(EXPECT_PRED_FORMAT3Test, FunctorOnBuiltInTypeSuccess) {
  1068. EXPECT_PRED_FORMAT3(PredFormatFunctor3(),
  1069. ++n1_,
  1070. ++n2_,
  1071. ++n3_);
  1072. finished_ = true;
  1073. }
  1074. // Tests a successful EXPECT_PRED_FORMAT3 where the
  1075. // predicate-formatter is a functor on a user-defined type (Bool).
  1076. TEST_F(EXPECT_PRED_FORMAT3Test, FunctorOnUserTypeSuccess) {
  1077. EXPECT_PRED_FORMAT3(PredFormatFunctor3(),
  1078. Bool(++n1_),
  1079. Bool(++n2_),
  1080. Bool(++n3_));
  1081. finished_ = true;
  1082. }
  1083. // Tests a failed EXPECT_PRED_FORMAT3 where the
  1084. // predicate-formatter is a function on a built-in type (int).
  1085. TEST_F(EXPECT_PRED_FORMAT3Test, FunctionOnBuiltInTypeFailure) {
  1086. EXPECT_NONFATAL_FAILURE({ // NOLINT
  1087. EXPECT_PRED_FORMAT3(PredFormatFunction3,
  1088. n1_++,
  1089. n2_++,
  1090. n3_++);
  1091. finished_ = true;
  1092. }, "");
  1093. }
  1094. // Tests a failed EXPECT_PRED_FORMAT3 where the
  1095. // predicate-formatter is a function on a user-defined type (Bool).
  1096. TEST_F(EXPECT_PRED_FORMAT3Test, FunctionOnUserTypeFailure) {
  1097. EXPECT_NONFATAL_FAILURE({ // NOLINT
  1098. EXPECT_PRED_FORMAT3(PredFormatFunction3,
  1099. Bool(n1_++),
  1100. Bool(n2_++),
  1101. Bool(n3_++));
  1102. finished_ = true;
  1103. }, "");
  1104. }
  1105. // Tests a failed EXPECT_PRED_FORMAT3 where the
  1106. // predicate-formatter is a functor on a built-in type (int).
  1107. TEST_F(EXPECT_PRED_FORMAT3Test, FunctorOnBuiltInTypeFailure) {
  1108. EXPECT_NONFATAL_FAILURE({ // NOLINT
  1109. EXPECT_PRED_FORMAT3(PredFormatFunctor3(),
  1110. n1_++,
  1111. n2_++,
  1112. n3_++);
  1113. finished_ = true;
  1114. }, "");
  1115. }
  1116. // Tests a failed EXPECT_PRED_FORMAT3 where the
  1117. // predicate-formatter is a functor on a user-defined type (Bool).
  1118. TEST_F(EXPECT_PRED_FORMAT3Test, FunctorOnUserTypeFailure) {
  1119. EXPECT_NONFATAL_FAILURE({ // NOLINT
  1120. EXPECT_PRED_FORMAT3(PredFormatFunctor3(),
  1121. Bool(n1_++),
  1122. Bool(n2_++),
  1123. Bool(n3_++));
  1124. finished_ = true;
  1125. }, "");
  1126. }
  1127. // Tests a successful ASSERT_PRED_FORMAT3 where the
  1128. // predicate-formatter is a function on a built-in type (int).
  1129. TEST_F(ASSERT_PRED_FORMAT3Test, FunctionOnBuiltInTypeSuccess) {
  1130. ASSERT_PRED_FORMAT3(PredFormatFunction3,
  1131. ++n1_,
  1132. ++n2_,
  1133. ++n3_);
  1134. finished_ = true;
  1135. }
  1136. // Tests a successful ASSERT_PRED_FORMAT3 where the
  1137. // predicate-formatter is a function on a user-defined type (Bool).
  1138. TEST_F(ASSERT_PRED_FORMAT3Test, FunctionOnUserTypeSuccess) {
  1139. ASSERT_PRED_FORMAT3(PredFormatFunction3,
  1140. Bool(++n1_),
  1141. Bool(++n2_),
  1142. Bool(++n3_));
  1143. finished_ = true;
  1144. }
  1145. // Tests a successful ASSERT_PRED_FORMAT3 where the
  1146. // predicate-formatter is a functor on a built-in type (int).
  1147. TEST_F(ASSERT_PRED_FORMAT3Test, FunctorOnBuiltInTypeSuccess) {
  1148. ASSERT_PRED_FORMAT3(PredFormatFunctor3(),
  1149. ++n1_,
  1150. ++n2_,
  1151. ++n3_);
  1152. finished_ = true;
  1153. }
  1154. // Tests a successful ASSERT_PRED_FORMAT3 where the
  1155. // predicate-formatter is a functor on a user-defined type (Bool).
  1156. TEST_F(ASSERT_PRED_FORMAT3Test, FunctorOnUserTypeSuccess) {
  1157. ASSERT_PRED_FORMAT3(PredFormatFunctor3(),
  1158. Bool(++n1_),
  1159. Bool(++n2_),
  1160. Bool(++n3_));
  1161. finished_ = true;
  1162. }
  1163. // Tests a failed ASSERT_PRED_FORMAT3 where the
  1164. // predicate-formatter is a function on a built-in type (int).
  1165. TEST_F(ASSERT_PRED_FORMAT3Test, FunctionOnBuiltInTypeFailure) {
  1166. expected_to_finish_ = false;
  1167. EXPECT_FATAL_FAILURE({ // NOLINT
  1168. ASSERT_PRED_FORMAT3(PredFormatFunction3,
  1169. n1_++,
  1170. n2_++,
  1171. n3_++);
  1172. finished_ = true;
  1173. }, "");
  1174. }
  1175. // Tests a failed ASSERT_PRED_FORMAT3 where the
  1176. // predicate-formatter is a function on a user-defined type (Bool).
  1177. TEST_F(ASSERT_PRED_FORMAT3Test, FunctionOnUserTypeFailure) {
  1178. expected_to_finish_ = false;
  1179. EXPECT_FATAL_FAILURE({ // NOLINT
  1180. ASSERT_PRED_FORMAT3(PredFormatFunction3,
  1181. Bool(n1_++),
  1182. Bool(n2_++),
  1183. Bool(n3_++));
  1184. finished_ = true;
  1185. }, "");
  1186. }
  1187. // Tests a failed ASSERT_PRED_FORMAT3 where the
  1188. // predicate-formatter is a functor on a built-in type (int).
  1189. TEST_F(ASSERT_PRED_FORMAT3Test, FunctorOnBuiltInTypeFailure) {
  1190. expected_to_finish_ = false;
  1191. EXPECT_FATAL_FAILURE({ // NOLINT
  1192. ASSERT_PRED_FORMAT3(PredFormatFunctor3(),
  1193. n1_++,
  1194. n2_++,
  1195. n3_++);
  1196. finished_ = true;
  1197. }, "");
  1198. }
  1199. // Tests a failed ASSERT_PRED_FORMAT3 where the
  1200. // predicate-formatter is a functor on a user-defined type (Bool).
  1201. TEST_F(ASSERT_PRED_FORMAT3Test, FunctorOnUserTypeFailure) {
  1202. expected_to_finish_ = false;
  1203. EXPECT_FATAL_FAILURE({ // NOLINT
  1204. ASSERT_PRED_FORMAT3(PredFormatFunctor3(),
  1205. Bool(n1_++),
  1206. Bool(n2_++),
  1207. Bool(n3_++));
  1208. finished_ = true;
  1209. }, "");
  1210. }
  1211. // Sample functions/functors for testing 4-ary predicate assertions.
  1212. // A 4-ary predicate function.
  1213. template <typename T1, typename T2, typename T3, typename T4>
  1214. bool PredFunction4(T1 v1, T2 v2, T3 v3, T4 v4) {
  1215. return v1 + v2 + v3 + v4 > 0;
  1216. }
  1217. // The following two functions are needed to circumvent a bug in
  1218. // gcc 2.95.3, which sometimes has problem with the above template
  1219. // function.
  1220. bool PredFunction4Int(int v1, int v2, int v3, int v4) {
  1221. return v1 + v2 + v3 + v4 > 0;
  1222. }
  1223. bool PredFunction4Bool(Bool v1, Bool v2, Bool v3, Bool v4) {
  1224. return v1 + v2 + v3 + v4 > 0;
  1225. }
  1226. // A 4-ary predicate functor.
  1227. struct PredFunctor4 {
  1228. template <typename T1, typename T2, typename T3, typename T4>
  1229. bool operator()(const T1& v1,
  1230. const T2& v2,
  1231. const T3& v3,
  1232. const T4& v4) {
  1233. return v1 + v2 + v3 + v4 > 0;
  1234. }
  1235. };
  1236. // A 4-ary predicate-formatter function.
  1237. template <typename T1, typename T2, typename T3, typename T4>
  1238. testing::AssertionResult PredFormatFunction4(const char* e1,
  1239. const char* e2,
  1240. const char* e3,
  1241. const char* e4,
  1242. const T1& v1,
  1243. const T2& v2,
  1244. const T3& v3,
  1245. const T4& v4) {
  1246. if (PredFunction4(v1, v2, v3, v4))
  1247. return testing::AssertionSuccess();
  1248. testing::Message msg;
  1249. msg << e1 << " + " << e2 << " + " << e3 << " + " << e4
  1250. << " is expected to be positive, but evaluates to "
  1251. << v1 + v2 + v3 + v4 << ".";
  1252. return testing::AssertionFailure(msg);
  1253. }
  1254. // A 4-ary predicate-formatter functor.
  1255. struct PredFormatFunctor4 {
  1256. template <typename T1, typename T2, typename T3, typename T4>
  1257. testing::AssertionResult operator()(const char* e1,
  1258. const char* e2,
  1259. const char* e3,
  1260. const char* e4,
  1261. const T1& v1,
  1262. const T2& v2,
  1263. const T3& v3,
  1264. const T4& v4) const {
  1265. return PredFormatFunction4(e1, e2, e3, e4, v1, v2, v3, v4);
  1266. }
  1267. };
  1268. // Tests for {EXPECT|ASSERT}_PRED_FORMAT4.
  1269. class Predicate4Test : public testing::Test {
  1270. protected:
  1271. virtual void SetUp() {
  1272. expected_to_finish_ = true;
  1273. finished_ = false;
  1274. n1_ = n2_ = n3_ = n4_ = 0;
  1275. }
  1276. virtual void TearDown() {
  1277. // Verifies that each of the predicate's arguments was evaluated
  1278. // exactly once.
  1279. EXPECT_EQ(1, n1_) <<
  1280. "The predicate assertion didn't evaluate argument 2 "
  1281. "exactly once.";
  1282. EXPECT_EQ(1, n2_) <<
  1283. "The predicate assertion didn't evaluate argument 3 "
  1284. "exactly once.";
  1285. EXPECT_EQ(1, n3_) <<
  1286. "The predicate assertion didn't evaluate argument 4 "
  1287. "exactly once.";
  1288. EXPECT_EQ(1, n4_) <<
  1289. "The predicate assertion didn't evaluate argument 5 "
  1290. "exactly once.";
  1291. // Verifies that the control flow in the test function is expected.
  1292. if (expected_to_finish_ && !finished_) {
  1293. FAIL() << "The predicate assertion unexpactedly aborted the test.";
  1294. } else if (!expected_to_finish_ && finished_) {
  1295. FAIL() << "The failed predicate assertion didn't abort the test "
  1296. "as expected.";
  1297. }
  1298. }
  1299. // true iff the test function is expected to run to finish.
  1300. static bool expected_to_finish_;
  1301. // true iff the test function did run to finish.
  1302. static bool finished_;
  1303. static int n1_;
  1304. static int n2_;
  1305. static int n3_;
  1306. static int n4_;
  1307. };
  1308. bool Predicate4Test::expected_to_finish_;
  1309. bool Predicate4Test::finished_;
  1310. int Predicate4Test::n1_;
  1311. int Predicate4Test::n2_;
  1312. int Predicate4Test::n3_;
  1313. int Predicate4Test::n4_;
  1314. typedef Predicate4Test EXPECT_PRED_FORMAT4Test;
  1315. typedef Predicate4Test ASSERT_PRED_FORMAT4Test;
  1316. typedef Predicate4Test EXPECT_PRED4Test;
  1317. typedef Predicate4Test ASSERT_PRED4Test;
  1318. // Tests a successful EXPECT_PRED4 where the
  1319. // predicate-formatter is a function on a built-in type (int).
  1320. TEST_F(EXPECT_PRED4Test, FunctionOnBuiltInTypeSuccess) {
  1321. EXPECT_PRED4(PredFunction4Int,
  1322. ++n1_,
  1323. ++n2_,
  1324. ++n3_,
  1325. ++n4_);
  1326. finished_ = true;
  1327. }
  1328. // Tests a successful EXPECT_PRED4 where the
  1329. // predicate-formatter is a function on a user-defined type (Bool).
  1330. TEST_F(EXPECT_PRED4Test, FunctionOnUserTypeSuccess) {
  1331. EXPECT_PRED4(PredFunction4Bool,
  1332. Bool(++n1_),
  1333. Bool(++n2_),
  1334. Bool(++n3_),
  1335. Bool(++n4_));
  1336. finished_ = true;
  1337. }
  1338. // Tests a successful EXPECT_PRED4 where the
  1339. // predicate-formatter is a functor on a built-in type (int).
  1340. TEST_F(EXPECT_PRED4Test, FunctorOnBuiltInTypeSuccess) {
  1341. EXPECT_PRED4(PredFunctor4(),
  1342. ++n1_,
  1343. ++n2_,
  1344. ++n3_,
  1345. ++n4_);
  1346. finished_ = true;
  1347. }
  1348. // Tests a successful EXPECT_PRED4 where the
  1349. // predicate-formatter is a functor on a user-defined type (Bool).
  1350. TEST_F(EXPECT_PRED4Test, FunctorOnUserTypeSuccess) {
  1351. EXPECT_PRED4(PredFunctor4(),
  1352. Bool(++n1_),
  1353. Bool(++n2_),
  1354. Bool(++n3_),
  1355. Bool(++n4_));
  1356. finished_ = true;
  1357. }
  1358. // Tests a failed EXPECT_PRED4 where the
  1359. // predicate-formatter is a function on a built-in type (int).
  1360. TEST_F(EXPECT_PRED4Test, FunctionOnBuiltInTypeFailure) {
  1361. EXPECT_NONFATAL_FAILURE({ // NOLINT
  1362. EXPECT_PRED4(PredFunction4Int,
  1363. n1_++,
  1364. n2_++,
  1365. n3_++,
  1366. n4_++);
  1367. finished_ = true;
  1368. }, "");
  1369. }
  1370. // Tests a failed EXPECT_PRED4 where the
  1371. // predicate-formatter is a function on a user-defined type (Bool).
  1372. TEST_F(EXPECT_PRED4Test, FunctionOnUserTypeFailure) {
  1373. EXPECT_NONFATAL_FAILURE({ // NOLINT
  1374. EXPECT_PRED4(PredFunction4Bool,
  1375. Bool(n1_++),
  1376. Bool(n2_++),
  1377. Bool(n3_++),
  1378. Bool(n4_++));
  1379. finished_ = true;
  1380. }, "");
  1381. }
  1382. // Tests a failed EXPECT_PRED4 where the
  1383. // predicate-formatter is a functor on a built-in type (int).
  1384. TEST_F(EXPECT_PRED4Test, FunctorOnBuiltInTypeFailure) {
  1385. EXPECT_NONFATAL_FAILURE({ // NOLINT
  1386. EXPECT_PRED4(PredFunctor4(),
  1387. n1_++,
  1388. n2_++,
  1389. n3_++,
  1390. n4_++);
  1391. finished_ = true;
  1392. }, "");
  1393. }
  1394. // Tests a failed EXPECT_PRED4 where the
  1395. // predicate-formatter is a functor on a user-defined type (Bool).
  1396. TEST_F(EXPECT_PRED4Test, FunctorOnUserTypeFailure) {
  1397. EXPECT_NONFATAL_FAILURE({ // NOLINT
  1398. EXPECT_PRED4(PredFunctor4

Large files files are truncated, but you can click here to view the full file