Matchers.tests.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. * Created by Phil on 21/02/2017.
  3. * Copyright 2017 Two Blue Cubes Ltd. All rights reserved.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #include "catch.hpp"
  9. #include <sstream>
  10. #include <algorithm>
  11. #ifdef __clang__
  12. #pragma clang diagnostic push
  13. #pragma clang diagnostic ignored "-Wweak-vtables"
  14. #pragma clang diagnostic ignored "-Wpadded"
  15. #endif
  16. namespace { namespace MatchersTests {
  17. #ifndef CATCH_CONFIG_DISABLE_MATCHERS
  18. #ifndef MATCHERS_TEST_HELPERS_INCLUDED // Don't compile this more than once per TU
  19. #define MATCHERS_TEST_HELPERS_INCLUDED
  20. inline const char *testStringForMatching() {
  21. return "this string contains 'abc' as a substring";
  22. }
  23. inline const char *testStringForMatching2() {
  24. return "some completely different text that contains one common word";
  25. }
  26. inline bool alwaysTrue(int) { return true; }
  27. inline bool alwaysFalse(int) { return false; }
  28. #ifdef _MSC_VER
  29. #pragma warning(disable:4702) // Unreachable code -- MSVC 19 (VS 2015) sees right through the indirection
  30. #endif
  31. #include <exception>
  32. struct SpecialException : std::exception {
  33. SpecialException(int i_) : i(i_) {}
  34. char const* what() const noexcept override {
  35. return "SpecialException::what";
  36. }
  37. int i;
  38. };
  39. struct DerivedException : std::exception {
  40. char const* what() const noexcept override {
  41. return "DerivedException::what";
  42. }
  43. };
  44. void doesNotThrow() {}
  45. [[noreturn]]
  46. void throwsSpecialException(int i) {
  47. throw SpecialException{i};
  48. }
  49. [[noreturn]]
  50. void throwsAsInt(int i) {
  51. throw i;
  52. }
  53. [[noreturn]]
  54. void throwsDerivedException() {
  55. throw DerivedException{};
  56. }
  57. class ExceptionMatcher : public Catch::MatcherBase<SpecialException> {
  58. int m_expected;
  59. public:
  60. ExceptionMatcher(int i) : m_expected(i) {}
  61. bool match(SpecialException const &se) const override {
  62. return se.i == m_expected;
  63. }
  64. std::string describe() const override {
  65. std::ostringstream ss;
  66. ss << "special exception has value of " << m_expected;
  67. return ss.str();
  68. }
  69. };
  70. #endif
  71. using namespace Catch::Matchers;
  72. #ifdef __DJGPP__
  73. float nextafter(float from, float to)
  74. {
  75. return ::nextafterf(from, to);
  76. }
  77. double nextafter(double from, double to)
  78. {
  79. return ::nextafter(from, to);
  80. }
  81. #else
  82. using std::nextafter;
  83. #endif
  84. TEST_CASE("String matchers", "[matchers]") {
  85. REQUIRE_THAT(testStringForMatching(), Contains("string"));
  86. REQUIRE_THAT(testStringForMatching(), Contains("string", Catch::CaseSensitive::No));
  87. CHECK_THAT(testStringForMatching(), Contains("abc"));
  88. CHECK_THAT(testStringForMatching(), Contains("aBC", Catch::CaseSensitive::No));
  89. CHECK_THAT(testStringForMatching(), StartsWith("this"));
  90. CHECK_THAT(testStringForMatching(), StartsWith("THIS", Catch::CaseSensitive::No));
  91. CHECK_THAT(testStringForMatching(), EndsWith("substring"));
  92. CHECK_THAT(testStringForMatching(), EndsWith(" SuBsTrInG", Catch::CaseSensitive::No));
  93. }
  94. TEST_CASE("Contains string matcher", "[.][failing][matchers]") {
  95. CHECK_THAT(testStringForMatching(), Contains("not there", Catch::CaseSensitive::No));
  96. CHECK_THAT(testStringForMatching(), Contains("STRING"));
  97. }
  98. TEST_CASE("StartsWith string matcher", "[.][failing][matchers]") {
  99. CHECK_THAT(testStringForMatching(), StartsWith("This String"));
  100. CHECK_THAT(testStringForMatching(), StartsWith("string", Catch::CaseSensitive::No));
  101. }
  102. TEST_CASE("EndsWith string matcher", "[.][failing][matchers]") {
  103. CHECK_THAT(testStringForMatching(), EndsWith("Substring"));
  104. CHECK_THAT(testStringForMatching(), EndsWith("this", Catch::CaseSensitive::No));
  105. }
  106. TEST_CASE("Equals string matcher", "[.][failing][matchers]") {
  107. CHECK_THAT(testStringForMatching(), Equals("this string contains 'ABC' as a substring"));
  108. CHECK_THAT(testStringForMatching(), Equals("something else", Catch::CaseSensitive::No));
  109. }
  110. TEST_CASE("Equals", "[matchers]") {
  111. CHECK_THAT(testStringForMatching(), Equals("this string contains 'abc' as a substring"));
  112. CHECK_THAT(testStringForMatching(),
  113. Equals("this string contains 'ABC' as a substring", Catch::CaseSensitive::No));
  114. }
  115. // <regex> does not work in libstdc++ 4.8, so we have to enable these tests only when they
  116. // are expected to pass and cannot have them in baselines
  117. TEST_CASE("Regex string matcher -- libstdc++-4.8 workaround", "[matchers][approvals]") {
  118. // This is fiiiine
  119. // Taken from an answer at
  120. // https://stackoverflow.com/questions/12530406/is-gcc-4-8-or-earlier-buggy-about-regular-expressions
  121. #if (!defined(__GNUC__)) || \
  122. (__cplusplus >= 201103L && \
  123. (!defined(__GLIBCXX__) || (__cplusplus >= 201402L) || \
  124. (defined(_GLIBCXX_REGEX_DFS_QUANTIFIERS_LIMIT) || \
  125. defined(_GLIBCXX_REGEX_STATE_LIMIT) || \
  126. (defined(_GLIBCXX_RELEASE) && \
  127. _GLIBCXX_RELEASE > 4))))
  128. // DJGPP meets the above condition but <regex> does not work properly anyway
  129. #ifndef __DJGPP__
  130. REQUIRE_THAT(testStringForMatching(), Matches("this string contains 'abc' as a substring"));
  131. REQUIRE_THAT(testStringForMatching(),
  132. Matches("this string CONTAINS 'abc' as a substring", Catch::CaseSensitive::No));
  133. REQUIRE_THAT(testStringForMatching(), Matches("^this string contains 'abc' as a substring$"));
  134. REQUIRE_THAT(testStringForMatching(), Matches("^.* 'abc' .*$"));
  135. REQUIRE_THAT(testStringForMatching(), Matches("^.* 'ABC' .*$", Catch::CaseSensitive::No));
  136. #endif
  137. #endif
  138. REQUIRE_THAT(testStringForMatching2(), !Matches("this string contains 'abc' as a substring"));
  139. }
  140. TEST_CASE("Regex string matcher", "[matchers][.failing]") {
  141. CHECK_THAT(testStringForMatching(), Matches("this STRING contains 'abc' as a substring"));
  142. CHECK_THAT(testStringForMatching(), Matches("contains 'abc' as a substring"));
  143. CHECK_THAT(testStringForMatching(), Matches("this string contains 'abc' as a"));
  144. }
  145. TEST_CASE("Matchers can be (AllOf) composed with the && operator", "[matchers][operators][operator&&]") {
  146. CHECK_THAT(testStringForMatching(),
  147. Contains("string") &&
  148. Contains("abc") &&
  149. Contains("substring") &&
  150. Contains("contains"));
  151. }
  152. TEST_CASE("Matchers can be (AnyOf) composed with the || operator", "[matchers][operators][operator||]") {
  153. CHECK_THAT(testStringForMatching(), Contains("string") || Contains("different") || Contains("random"));
  154. CHECK_THAT(testStringForMatching2(), Contains("string") || Contains("different") || Contains("random"));
  155. }
  156. TEST_CASE("Matchers can be composed with both && and ||", "[matchers][operators][operator||][operator&&]") {
  157. CHECK_THAT(testStringForMatching(), (Contains("string") || Contains("different")) && Contains("substring"));
  158. }
  159. TEST_CASE("Matchers can be composed with both && and || - failing",
  160. "[matchers][operators][operator||][operator&&][.failing]") {
  161. CHECK_THAT(testStringForMatching(), (Contains("string") || Contains("different")) && Contains("random"));
  162. }
  163. TEST_CASE("Matchers can be negated (Not) with the ! operator", "[matchers][operators][not]") {
  164. CHECK_THAT(testStringForMatching(), !Contains("different"));
  165. }
  166. TEST_CASE("Matchers can be negated (Not) with the ! operator - failing",
  167. "[matchers][operators][not][.failing]") {
  168. CHECK_THAT(testStringForMatching(), !Contains("substring"));
  169. }
  170. template<typename T>
  171. struct CustomAllocator : private std::allocator<T>
  172. {
  173. using size_type = size_t;
  174. using difference_type = ptrdiff_t;
  175. using pointer = T*;
  176. using const_pointer = const T*;
  177. using reference = T&;
  178. using const_reference = const T&;
  179. using value_type = T;
  180. template<typename U>
  181. struct rebind
  182. { using other = CustomAllocator<U>; };
  183. using propagate_on_container_move_assignment = std::true_type;
  184. using is_always_equal = std::true_type;
  185. CustomAllocator() = default;
  186. CustomAllocator(const CustomAllocator& other)
  187. : std::allocator<T>(other) { }
  188. template<typename U>
  189. CustomAllocator(const CustomAllocator<U>&) { }
  190. ~CustomAllocator() = default;
  191. using std::allocator<T>::address;
  192. using std::allocator<T>::allocate;
  193. using std::allocator<T>::construct;
  194. using std::allocator<T>::deallocate;
  195. using std::allocator<T>::max_size;
  196. using std::allocator<T>::destroy;
  197. };
  198. TEST_CASE("Vector matchers", "[matchers][vector]") {
  199. std::vector<int> v;
  200. v.push_back(1);
  201. v.push_back(2);
  202. v.push_back(3);
  203. std::vector<int> v2;
  204. v2.push_back(1);
  205. v2.push_back(2);
  206. std::vector<double> v3;
  207. v3.push_back(1);
  208. v3.push_back(2);
  209. v3.push_back(3);
  210. std::vector<double> v4;
  211. v4.push_back(1 + 1e-8);
  212. v4.push_back(2 + 1e-8);
  213. v4.push_back(3 + 1e-8);
  214. std::vector<int, CustomAllocator<int>> v5;
  215. v5.push_back(1);
  216. v5.push_back(2);
  217. v5.push_back(3);
  218. std::vector<int, CustomAllocator<int>> v6;
  219. v6.push_back(1);
  220. v6.push_back(2);
  221. std::vector<int> empty;
  222. SECTION("Contains (element)") {
  223. CHECK_THAT(v, VectorContains(1));
  224. CHECK_THAT(v, VectorContains(2));
  225. CHECK_THAT(v5, (VectorContains<int, CustomAllocator<int>>(2)));
  226. }
  227. SECTION("Contains (vector)") {
  228. CHECK_THAT(v, Contains(v2));
  229. CHECK_THAT(v, Contains<int>({ 1, 2 }));
  230. CHECK_THAT(v5, (Contains<int, std::allocator<int>, CustomAllocator<int>>(v2)));
  231. v2.push_back(3); // now exactly matches
  232. CHECK_THAT(v, Contains(v2));
  233. CHECK_THAT(v, Contains(empty));
  234. CHECK_THAT(empty, Contains(empty));
  235. CHECK_THAT(v5, (Contains<int, std::allocator<int>, CustomAllocator<int>>(v2)));
  236. CHECK_THAT(v5, Contains(v6));
  237. }
  238. SECTION("Contains (element), composed") {
  239. CHECK_THAT(v, VectorContains(1) && VectorContains(2));
  240. }
  241. SECTION("Equals") {
  242. // Same vector
  243. CHECK_THAT(v, Equals(v));
  244. CHECK_THAT(empty, Equals(empty));
  245. // Different vector with same elements
  246. CHECK_THAT(v, Equals<int>({ 1, 2, 3 }));
  247. v2.push_back(3);
  248. CHECK_THAT(v, Equals(v2));
  249. CHECK_THAT(v5, (Equals<int, std::allocator<int>, CustomAllocator<int>>(v2)));
  250. v6.push_back(3);
  251. CHECK_THAT(v5, Equals(v6));
  252. }
  253. SECTION("UnorderedEquals") {
  254. CHECK_THAT(v, UnorderedEquals(v));
  255. CHECK_THAT(v, UnorderedEquals<int>({ 3, 2, 1 }));
  256. CHECK_THAT(empty, UnorderedEquals(empty));
  257. auto permuted = v;
  258. std::next_permutation(begin(permuted), end(permuted));
  259. REQUIRE_THAT(permuted, UnorderedEquals(v));
  260. std::reverse(begin(permuted), end(permuted));
  261. REQUIRE_THAT(permuted, UnorderedEquals(v));
  262. CHECK_THAT(v5, (UnorderedEquals<int, std::allocator<int>, CustomAllocator<int>>(permuted)));
  263. auto v5_permuted = v5;
  264. std::next_permutation(begin(v5_permuted), end(v5_permuted));
  265. CHECK_THAT(v5_permuted, UnorderedEquals(v5));
  266. }
  267. }
  268. TEST_CASE("Vector matchers that fail", "[matchers][vector][.][failing]") {
  269. std::vector<int> v;
  270. v.push_back(1);
  271. v.push_back(2);
  272. v.push_back(3);
  273. std::vector<int> v2;
  274. v2.push_back(1);
  275. v2.push_back(2);
  276. std::vector<double> v3;
  277. v3.push_back(1);
  278. v3.push_back(2);
  279. v3.push_back(3);
  280. std::vector<double> v4;
  281. v4.push_back(1.1);
  282. v4.push_back(2.1);
  283. v4.push_back(3.1);
  284. std::vector<int> empty;
  285. SECTION("Contains (element)") {
  286. CHECK_THAT(v, VectorContains(-1));
  287. CHECK_THAT(empty, VectorContains(1));
  288. }
  289. SECTION("Contains (vector)") {
  290. CHECK_THAT(empty, Contains(v));
  291. v2.push_back(4);
  292. CHECK_THAT(v, Contains(v2));
  293. }
  294. SECTION("Equals") {
  295. CHECK_THAT(v, Equals(v2));
  296. CHECK_THAT(v2, Equals(v));
  297. CHECK_THAT(empty, Equals(v));
  298. CHECK_THAT(v, Equals(empty));
  299. }
  300. SECTION("UnorderedEquals") {
  301. CHECK_THAT(v, UnorderedEquals(empty));
  302. CHECK_THAT(empty, UnorderedEquals(v));
  303. auto permuted = v;
  304. std::next_permutation(begin(permuted), end(permuted));
  305. permuted.pop_back();
  306. CHECK_THAT(permuted, UnorderedEquals(v));
  307. std::reverse(begin(permuted), end(permuted));
  308. CHECK_THAT(permuted, UnorderedEquals(v));
  309. }
  310. }
  311. TEST_CASE("Exception matchers that succeed", "[matchers][exceptions][!throws]") {
  312. CHECK_THROWS_MATCHES(throwsSpecialException(1), SpecialException, ExceptionMatcher{1});
  313. REQUIRE_THROWS_MATCHES(throwsSpecialException(2), SpecialException, ExceptionMatcher{2});
  314. }
  315. TEST_CASE("Exception matchers that fail", "[matchers][exceptions][!throws][.failing]") {
  316. SECTION("No exception") {
  317. CHECK_THROWS_MATCHES(doesNotThrow(), SpecialException, ExceptionMatcher{1});
  318. REQUIRE_THROWS_MATCHES(doesNotThrow(), SpecialException, ExceptionMatcher{1});
  319. }
  320. SECTION("Type mismatch") {
  321. CHECK_THROWS_MATCHES(throwsAsInt(1), SpecialException, ExceptionMatcher{1});
  322. REQUIRE_THROWS_MATCHES(throwsAsInt(1), SpecialException, ExceptionMatcher{1});
  323. }
  324. SECTION("Contents are wrong") {
  325. CHECK_THROWS_MATCHES(throwsSpecialException(3), SpecialException, ExceptionMatcher{1});
  326. REQUIRE_THROWS_MATCHES(throwsSpecialException(4), SpecialException, ExceptionMatcher{1});
  327. }
  328. }
  329. TEST_CASE("Floating point matchers: float", "[matchers][floating-point]") {
  330. SECTION("Relative") {
  331. REQUIRE_THAT(10.f, WithinRel(11.1f, 0.1f));
  332. REQUIRE_THAT(10.f, !WithinRel(11.2f, 0.1f));
  333. REQUIRE_THAT( 1.f, !WithinRel(0.f, 0.99f));
  334. REQUIRE_THAT(-0.f, WithinRel(0.f));
  335. SECTION("Some subnormal values") {
  336. auto v1 = std::numeric_limits<float>::min();
  337. auto v2 = v1;
  338. for (int i = 0; i < 5; ++i) {
  339. v2 = std::nextafter(v1, 0.f);
  340. }
  341. REQUIRE_THAT(v1, WithinRel(v2));
  342. }
  343. }
  344. SECTION("Margin") {
  345. REQUIRE_THAT(1.f, WithinAbs(1.f, 0));
  346. REQUIRE_THAT(0.f, WithinAbs(1.f, 1));
  347. REQUIRE_THAT(0.f, !WithinAbs(1.f, 0.99f));
  348. REQUIRE_THAT(0.f, !WithinAbs(1.f, 0.99f));
  349. REQUIRE_THAT(0.f, WithinAbs(-0.f, 0));
  350. REQUIRE_THAT(11.f, !WithinAbs(10.f, 0.5f));
  351. REQUIRE_THAT(10.f, !WithinAbs(11.f, 0.5f));
  352. REQUIRE_THAT(-10.f, WithinAbs(-10.f, 0.5f));
  353. REQUIRE_THAT(-10.f, WithinAbs(-9.6f, 0.5f));
  354. }
  355. SECTION("ULPs") {
  356. REQUIRE_THAT(1.f, WithinULP(1.f, 0));
  357. REQUIRE_THAT(nextafter(1.f, 2.f), WithinULP(1.f, 1));
  358. REQUIRE_THAT(0.f, WithinULP(nextafter(0.f, 1.f), 1));
  359. REQUIRE_THAT(1.f, WithinULP(nextafter(1.f, 0.f), 1));
  360. REQUIRE_THAT(1.f, !WithinULP(nextafter(1.f, 2.f), 0));
  361. REQUIRE_THAT(1.f, WithinULP(1.f, 0));
  362. REQUIRE_THAT(-0.f, WithinULP(0.f, 0));
  363. }
  364. SECTION("Composed") {
  365. REQUIRE_THAT(1.f, WithinAbs(1.f, 0.5) || WithinULP(1.f, 1));
  366. REQUIRE_THAT(1.f, WithinAbs(2.f, 0.5) || WithinULP(1.f, 0));
  367. REQUIRE_THAT(0.0001f, WithinAbs(0.f, 0.001f) || WithinRel(0.f, 0.1f));
  368. }
  369. SECTION("Constructor validation") {
  370. REQUIRE_NOTHROW(WithinAbs(1.f, 0.f));
  371. REQUIRE_THROWS_AS(WithinAbs(1.f, -1.f), std::domain_error);
  372. REQUIRE_NOTHROW(WithinULP(1.f, 0));
  373. REQUIRE_THROWS_AS(WithinULP(1.f, static_cast<uint64_t>(-1)), std::domain_error);
  374. REQUIRE_NOTHROW(WithinRel(1.f, 0.f));
  375. REQUIRE_THROWS_AS(WithinRel(1.f, -0.2f), std::domain_error);
  376. REQUIRE_THROWS_AS(WithinRel(1.f, 1.f), std::domain_error);
  377. }
  378. }
  379. TEST_CASE("Floating point matchers: double", "[matchers][floating-point]") {
  380. SECTION("Relative") {
  381. REQUIRE_THAT(10., WithinRel(11.1, 0.1));
  382. REQUIRE_THAT(10., !WithinRel(11.2, 0.1));
  383. REQUIRE_THAT(1., !WithinRel(0., 0.99));
  384. REQUIRE_THAT(-0., WithinRel(0.));
  385. SECTION("Some subnormal values") {
  386. auto v1 = std::numeric_limits<double>::min();
  387. auto v2 = v1;
  388. for (int i = 0; i < 5; ++i) {
  389. v2 = std::nextafter(v1, 0);
  390. }
  391. REQUIRE_THAT(v1, WithinRel(v2));
  392. }
  393. }
  394. SECTION("Margin") {
  395. REQUIRE_THAT(1., WithinAbs(1., 0));
  396. REQUIRE_THAT(0., WithinAbs(1., 1));
  397. REQUIRE_THAT(0., !WithinAbs(1., 0.99));
  398. REQUIRE_THAT(0., !WithinAbs(1., 0.99));
  399. REQUIRE_THAT(11., !WithinAbs(10., 0.5));
  400. REQUIRE_THAT(10., !WithinAbs(11., 0.5));
  401. REQUIRE_THAT(-10., WithinAbs(-10., 0.5));
  402. REQUIRE_THAT(-10., WithinAbs(-9.6, 0.5));
  403. }
  404. SECTION("ULPs") {
  405. REQUIRE_THAT(1., WithinULP(1., 0));
  406. REQUIRE_THAT(nextafter(1., 2.), WithinULP(1., 1));
  407. REQUIRE_THAT(0., WithinULP(nextafter(0., 1.), 1));
  408. REQUIRE_THAT(1., WithinULP(nextafter(1., 0.), 1));
  409. REQUIRE_THAT(1., !WithinULP(nextafter(1., 2.), 0));
  410. REQUIRE_THAT(1., WithinULP(1., 0));
  411. REQUIRE_THAT(-0., WithinULP(0., 0));
  412. }
  413. SECTION("Composed") {
  414. REQUIRE_THAT(1., WithinAbs(1., 0.5) || WithinULP(2., 1));
  415. REQUIRE_THAT(1., WithinAbs(2., 0.5) || WithinULP(1., 0));
  416. REQUIRE_THAT(0.0001, WithinAbs(0., 0.001) || WithinRel(0., 0.1));
  417. }
  418. SECTION("Constructor validation") {
  419. REQUIRE_NOTHROW(WithinAbs(1., 0.));
  420. REQUIRE_THROWS_AS(WithinAbs(1., -1.), std::domain_error);
  421. REQUIRE_NOTHROW(WithinULP(1., 0));
  422. REQUIRE_NOTHROW(WithinRel(1., 0.));
  423. REQUIRE_THROWS_AS(WithinRel(1., -0.2), std::domain_error);
  424. REQUIRE_THROWS_AS(WithinRel(1., 1.), std::domain_error);
  425. }
  426. }
  427. TEST_CASE("Floating point matchers that are problematic in approvals", "[approvals][matchers][floating-point]") {
  428. REQUIRE_THAT(NAN, !WithinAbs(NAN, 0));
  429. REQUIRE_THAT(NAN, !(WithinAbs(NAN, 100) || WithinULP(NAN, 123)));
  430. REQUIRE_THAT(NAN, !WithinULP(NAN, 123));
  431. REQUIRE_THAT(INFINITY, WithinRel(INFINITY));
  432. REQUIRE_THAT(-INFINITY, !WithinRel(INFINITY));
  433. REQUIRE_THAT(1., !WithinRel(INFINITY));
  434. REQUIRE_THAT(INFINITY, !WithinRel(1.));
  435. REQUIRE_THAT(NAN, !WithinRel(NAN));
  436. REQUIRE_THAT(1., !WithinRel(NAN));
  437. REQUIRE_THAT(NAN, !WithinRel(1.));
  438. }
  439. TEST_CASE("Arbitrary predicate matcher", "[matchers][generic]") {
  440. SECTION("Function pointer") {
  441. REQUIRE_THAT(1, Predicate<int>(alwaysTrue, "always true"));
  442. REQUIRE_THAT(1, !Predicate<int>(alwaysFalse, "always false"));
  443. }
  444. SECTION("Lambdas + different type") {
  445. REQUIRE_THAT("Hello olleH",
  446. Predicate<std::string>(
  447. [] (std::string const& str) -> bool { return str.front() == str.back(); },
  448. "First and last character should be equal")
  449. );
  450. REQUIRE_THAT("This wouldn't pass",
  451. !Predicate<std::string>(
  452. [] (std::string const& str) -> bool { return str.front() == str.back(); }
  453. )
  454. );
  455. }
  456. }
  457. TEST_CASE("Regression test #1", "[matchers][vector]") {
  458. // At some point, UnorderedEqualsMatcher skipped
  459. // mismatched prefixed before doing the comparison itself
  460. std::vector<char> actual = { 'a', 'b' };
  461. std::vector<char> expected = { 'c', 'b' };
  462. CHECK_THAT(actual, !UnorderedEquals(expected));
  463. }
  464. TEST_CASE("Predicate matcher can accept const char*", "[matchers][compilation]") {
  465. REQUIRE_THAT("foo", Predicate<const char*>([] (const char* const&) { return true; }));
  466. }
  467. TEST_CASE("Vector Approx matcher", "[matchers][approx][vector]") {
  468. using Catch::Matchers::Approx;
  469. SECTION("Empty vector is roughly equal to an empty vector") {
  470. std::vector<double> empty;
  471. REQUIRE_THAT(empty, Approx(empty));
  472. }
  473. SECTION("Vectors with elements") {
  474. std::vector<double> v1({1., 2., 3.});
  475. SECTION("A vector is approx equal to itself") {
  476. REQUIRE_THAT(v1, Approx(v1));
  477. REQUIRE_THAT(v1, Approx<double>({ 1., 2., 3. }));
  478. }
  479. std::vector<double> v2({1.5, 2.5, 3.5});
  480. SECTION("Different length") {
  481. auto temp(v1);
  482. temp.push_back(4);
  483. REQUIRE_THAT(v1, !Approx(temp));
  484. }
  485. SECTION("Same length, different elements") {
  486. REQUIRE_THAT(v1, !Approx(v2));
  487. REQUIRE_THAT(v1, Approx(v2).margin(0.5));
  488. REQUIRE_THAT(v1, Approx(v2).epsilon(0.5));
  489. REQUIRE_THAT(v1, Approx(v2).epsilon(0.1).scale(500));
  490. }
  491. }
  492. }
  493. TEST_CASE("Vector Approx matcher -- failing", "[matchers][approx][vector][.failing]") {
  494. using Catch::Matchers::Approx;
  495. SECTION("Empty and non empty vectors are not approx equal") {
  496. std::vector<double> empty, t1({1, 2});
  497. CHECK_THAT(empty, Approx(t1));
  498. }
  499. SECTION("Just different vectors") {
  500. std::vector<double> v1({2., 4., 6.}), v2({1., 3., 5.});
  501. CHECK_THAT(v1, Approx(v2));
  502. }
  503. }
  504. TEST_CASE("Exceptions matchers", "[matchers][exceptions][!throws]") {
  505. REQUIRE_THROWS_MATCHES(throwsDerivedException(), DerivedException, Message("DerivedException::what"));
  506. REQUIRE_THROWS_MATCHES(throwsDerivedException(), DerivedException, !Message("derivedexception::what"));
  507. REQUIRE_THROWS_MATCHES(throwsSpecialException(2), SpecialException, !Message("DerivedException::what"));
  508. REQUIRE_THROWS_MATCHES(throwsSpecialException(2), SpecialException, Message("SpecialException::what"));
  509. }
  510. TEST_CASE("Composed matchers are distinct", "[matchers][composed]") {
  511. auto m1 = Contains("string");
  512. auto m2 = Contains("random");
  513. auto composed1 = m1 || m2;
  514. auto m3 = Contains("different");
  515. auto composed2 = composed1 || m3;
  516. REQUIRE_THAT(testStringForMatching2(), !composed1);
  517. REQUIRE_THAT(testStringForMatching2(), composed2);
  518. }
  519. struct CheckedTestingMatcher : Catch::MatcherBase<int> {
  520. mutable bool matchCalled = false;
  521. bool matchSucceeds = false;
  522. bool match(int const&) const override {
  523. matchCalled = true;
  524. return matchSucceeds;
  525. }
  526. std::string describe() const override {
  527. return "CheckedTestingMatcher set to " + (matchSucceeds ? std::string("succeed") : std::string("fail"));
  528. }
  529. };
  530. TEST_CASE("Composed matchers shortcircuit", "[matchers][composed]") {
  531. // Check that if first returns false, second is not touched
  532. CheckedTestingMatcher first, second;
  533. SECTION("&&") {
  534. first.matchSucceeds = false;
  535. // This assertion doesn't actually test anything, we just
  536. // want the composed matcher's `match` being called.
  537. CHECK_THAT(1, !(first && second));
  538. // These two assertions are the important ones
  539. REQUIRE(first.matchCalled);
  540. REQUIRE(!second.matchCalled);
  541. }
  542. // Check that if first returns true, second is not touched
  543. SECTION("||") {
  544. first.matchSucceeds = true;
  545. // This assertion doesn't actually test anything, we just
  546. // want the composed matcher's `match` being called.
  547. CHECK_THAT(1, first || second);
  548. // These two assertions are the important ones
  549. REQUIRE(first.matchCalled);
  550. REQUIRE(!second.matchCalled);
  551. }
  552. }
  553. } } // namespace MatchersTests
  554. #endif // CATCH_CONFIG_DISABLE_MATCHERS
  555. #ifdef __clang__
  556. #pragma clang diagnostic pop
  557. #endif