Matchers.tests.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #include <catch2/catch_test_macros.hpp>
  7. #include <catch2/catch_template_test_macros.hpp>
  8. #include <catch2/matchers/catch_matchers_exception.hpp>
  9. #include <catch2/matchers/catch_matchers_floating_point.hpp>
  10. #include <catch2/matchers/catch_matchers_predicate.hpp>
  11. #include <catch2/matchers/catch_matchers_string.hpp>
  12. #include <catch2/matchers/catch_matchers_vector.hpp>
  13. #include <catch2/matchers/catch_matchers_templated.hpp>
  14. #include <algorithm>
  15. #include <exception>
  16. #include <cmath>
  17. #include <list>
  18. #include <sstream>
  19. #ifdef __clang__
  20. # pragma clang diagnostic push
  21. # pragma clang diagnostic ignored "-Wweak-vtables"
  22. # pragma clang diagnostic ignored "-Wpadded"
  23. #endif
  24. namespace {
  25. static const char* testStringForMatching() {
  26. return "this string contains 'abc' as a substring";
  27. }
  28. static const char* testStringForMatching2() {
  29. return "some completely different text that contains one common word";
  30. }
  31. static bool alwaysTrue( int ) { return true; }
  32. static bool alwaysFalse( int ) { return false; }
  33. #ifdef _MSC_VER
  34. # pragma warning( disable : 4702 ) // Unreachable code -- MSVC 19 (VS 2015)
  35. // sees right through the indirection
  36. #endif
  37. struct SpecialException : std::exception {
  38. SpecialException( int i_ ): i( i_ ) {}
  39. char const* what() const noexcept override {
  40. return "SpecialException::what";
  41. }
  42. int i;
  43. };
  44. struct DerivedException : std::exception {
  45. char const* what() const noexcept override {
  46. return "DerivedException::what";
  47. }
  48. };
  49. static void doesNotThrow() {}
  50. [[noreturn]] static void throwsSpecialException( int i ) {
  51. throw SpecialException{ i };
  52. }
  53. [[noreturn]] static void throwsAsInt( int i ) { throw i; }
  54. [[noreturn]] static void throwsDerivedException() {
  55. throw DerivedException{};
  56. }
  57. class ExceptionMatcher
  58. : public Catch::Matchers::MatcherBase<SpecialException> {
  59. int m_expected;
  60. public:
  61. ExceptionMatcher( int i ): m_expected( i ) {}
  62. bool match( SpecialException const& se ) const override {
  63. return se.i == m_expected;
  64. }
  65. std::string describe() const override {
  66. std::ostringstream ss;
  67. ss << "special exception has value of " << m_expected;
  68. return ss.str();
  69. }
  70. };
  71. using namespace Catch::Matchers;
  72. #ifdef __DJGPP__
  73. static float nextafter( float from, float to ) {
  74. return ::nextafterf( from, to );
  75. }
  76. static double nextafter( double from, double to ) {
  77. return ::nextafter( from, to );
  78. }
  79. #else
  80. using std::nextafter;
  81. #endif
  82. } // end unnamed namespace
  83. TEST_CASE( "String matchers", "[matchers]" ) {
  84. REQUIRE_THAT( testStringForMatching(), ContainsSubstring( "string" ) );
  85. REQUIRE_THAT( testStringForMatching(),
  86. ContainsSubstring( "string", Catch::CaseSensitive::No ) );
  87. CHECK_THAT( testStringForMatching(), ContainsSubstring( "abc" ) );
  88. CHECK_THAT( testStringForMatching(),
  89. ContainsSubstring( "aBC", Catch::CaseSensitive::No ) );
  90. CHECK_THAT( testStringForMatching(), StartsWith( "this" ) );
  91. CHECK_THAT( testStringForMatching(),
  92. StartsWith( "THIS", Catch::CaseSensitive::No ) );
  93. CHECK_THAT( testStringForMatching(), EndsWith( "substring" ) );
  94. CHECK_THAT( testStringForMatching(),
  95. EndsWith( " SuBsTrInG", Catch::CaseSensitive::No ) );
  96. }
  97. TEST_CASE( "Contains string matcher", "[.][failing][matchers]" ) {
  98. CHECK_THAT( testStringForMatching(),
  99. ContainsSubstring( "not there", Catch::CaseSensitive::No ) );
  100. CHECK_THAT( testStringForMatching(), ContainsSubstring( "STRING" ) );
  101. }
  102. TEST_CASE( "StartsWith string matcher", "[.][failing][matchers]" ) {
  103. CHECK_THAT( testStringForMatching(), StartsWith( "This String" ) );
  104. CHECK_THAT( testStringForMatching(),
  105. StartsWith( "string", Catch::CaseSensitive::No ) );
  106. }
  107. TEST_CASE( "EndsWith string matcher", "[.][failing][matchers]" ) {
  108. CHECK_THAT( testStringForMatching(), EndsWith( "Substring" ) );
  109. CHECK_THAT( testStringForMatching(),
  110. EndsWith( "this", Catch::CaseSensitive::No ) );
  111. }
  112. TEST_CASE( "Equals string matcher", "[.][failing][matchers]" ) {
  113. CHECK_THAT( testStringForMatching(),
  114. Equals( "this string contains 'ABC' as a substring" ) );
  115. CHECK_THAT( testStringForMatching(),
  116. Equals( "something else", Catch::CaseSensitive::No ) );
  117. }
  118. TEST_CASE( "Equals", "[matchers]" ) {
  119. CHECK_THAT( testStringForMatching(),
  120. Equals( "this string contains 'abc' as a substring" ) );
  121. CHECK_THAT( testStringForMatching(),
  122. Equals( "this string contains 'ABC' as a substring",
  123. Catch::CaseSensitive::No ) );
  124. }
  125. TEST_CASE( "Regex string matcher -- libstdc++-4.8 workaround",
  126. "[matchers][approvals]" ) {
  127. // DJGPP has similar problem with its regex support as libstdc++ 4.8
  128. #ifndef __DJGPP__
  129. REQUIRE_THAT( testStringForMatching(),
  130. Matches( "this string contains 'abc' as a substring" ) );
  131. REQUIRE_THAT( testStringForMatching(),
  132. Matches( "this string CONTAINS 'abc' as a substring",
  133. Catch::CaseSensitive::No ) );
  134. REQUIRE_THAT( testStringForMatching(),
  135. Matches( "^this string contains 'abc' as a substring$" ) );
  136. REQUIRE_THAT( testStringForMatching(), Matches( "^.* 'abc' .*$" ) );
  137. REQUIRE_THAT( testStringForMatching(),
  138. Matches( "^.* 'ABC' .*$", Catch::CaseSensitive::No ) );
  139. #endif
  140. REQUIRE_THAT( testStringForMatching2(),
  141. !Matches( "this string contains 'abc' as a substring" ) );
  142. }
  143. TEST_CASE( "Regex string matcher", "[matchers][.failing]" ) {
  144. CHECK_THAT( testStringForMatching(),
  145. Matches( "this STRING contains 'abc' as a substring" ) );
  146. CHECK_THAT( testStringForMatching(),
  147. Matches( "contains 'abc' as a substring" ) );
  148. CHECK_THAT( testStringForMatching(),
  149. Matches( "this string contains 'abc' as a" ) );
  150. }
  151. TEST_CASE( "Matchers can be (AllOf) composed with the && operator",
  152. "[matchers][operators][operator&&]" ) {
  153. CHECK_THAT( testStringForMatching(),
  154. ContainsSubstring( "string" ) && ContainsSubstring( "abc" ) &&
  155. ContainsSubstring( "substring" ) && ContainsSubstring( "contains" ) );
  156. }
  157. TEST_CASE( "Matchers can be (AnyOf) composed with the || operator",
  158. "[matchers][operators][operator||]" ) {
  159. CHECK_THAT( testStringForMatching(),
  160. ContainsSubstring( "string" ) || ContainsSubstring( "different" ) ||
  161. ContainsSubstring( "random" ) );
  162. CHECK_THAT( testStringForMatching2(),
  163. ContainsSubstring( "string" ) || ContainsSubstring( "different" ) ||
  164. ContainsSubstring( "random" ) );
  165. }
  166. TEST_CASE( "Matchers can be composed with both && and ||",
  167. "[matchers][operators][operator||][operator&&]" ) {
  168. CHECK_THAT( testStringForMatching(),
  169. ( ContainsSubstring( "string" ) || ContainsSubstring( "different" ) ) &&
  170. ContainsSubstring( "substring" ) );
  171. }
  172. TEST_CASE( "Matchers can be composed with both && and || - failing",
  173. "[matchers][operators][operator||][operator&&][.failing]" ) {
  174. CHECK_THAT( testStringForMatching(),
  175. ( ContainsSubstring( "string" ) || ContainsSubstring( "different" ) ) &&
  176. ContainsSubstring( "random" ) );
  177. }
  178. TEST_CASE( "Matchers can be negated (Not) with the ! operator",
  179. "[matchers][operators][not]" ) {
  180. CHECK_THAT( testStringForMatching(), !ContainsSubstring( "different" ) );
  181. }
  182. TEST_CASE( "Matchers can be negated (Not) with the ! operator - failing",
  183. "[matchers][operators][not][.failing]" ) {
  184. CHECK_THAT( testStringForMatching(), !ContainsSubstring( "substring" ) );
  185. }
  186. template <typename T> struct CustomAllocator : private std::allocator<T> {
  187. using size_type = size_t;
  188. using difference_type = ptrdiff_t;
  189. using pointer = T*;
  190. using const_pointer = const T*;
  191. using reference = T&;
  192. using const_reference = const T&;
  193. using value_type = T;
  194. template <typename U> struct rebind { using other = CustomAllocator<U>; };
  195. using propagate_on_container_move_assignment = std::true_type;
  196. using is_always_equal = std::true_type;
  197. CustomAllocator() = default;
  198. CustomAllocator( const CustomAllocator& other ):
  199. std::allocator<T>( other ) {}
  200. template <typename U> CustomAllocator( const CustomAllocator<U>& ) {}
  201. ~CustomAllocator() = default;
  202. using std::allocator<T>::allocate;
  203. using std::allocator<T>::deallocate;
  204. };
  205. TEST_CASE( "Vector matchers", "[matchers][vector]" ) {
  206. std::vector<int> v;
  207. v.push_back( 1 );
  208. v.push_back( 2 );
  209. v.push_back( 3 );
  210. std::vector<int> v2;
  211. v2.push_back( 1 );
  212. v2.push_back( 2 );
  213. std::vector<double> v3;
  214. v3.push_back( 1 );
  215. v3.push_back( 2 );
  216. v3.push_back( 3 );
  217. std::vector<double> v4;
  218. v4.push_back( 1 + 1e-8 );
  219. v4.push_back( 2 + 1e-8 );
  220. v4.push_back( 3 + 1e-8 );
  221. std::vector<int, CustomAllocator<int>> v5;
  222. v5.push_back( 1 );
  223. v5.push_back( 2 );
  224. v5.push_back( 3 );
  225. std::vector<int, CustomAllocator<int>> v6;
  226. v6.push_back( 1 );
  227. v6.push_back( 2 );
  228. std::vector<int> empty;
  229. SECTION( "Contains (element)" ) {
  230. CHECK_THAT( v, VectorContains( 1 ) );
  231. CHECK_THAT( v, VectorContains( 2 ) );
  232. CHECK_THAT( v5, ( VectorContains<int, CustomAllocator<int>>( 2 ) ) );
  233. }
  234. SECTION( "Contains (vector)" ) {
  235. CHECK_THAT( v, Contains( v2 ) );
  236. CHECK_THAT( v, Contains<int>( { 1, 2 } ) );
  237. CHECK_THAT( v5,
  238. ( Contains<int, std::allocator<int>, CustomAllocator<int>>(
  239. v2 ) ) );
  240. v2.push_back( 3 ); // now exactly matches
  241. CHECK_THAT( v, Contains( v2 ) );
  242. CHECK_THAT( v, Contains( empty ) );
  243. CHECK_THAT( empty, Contains( empty ) );
  244. CHECK_THAT( v5,
  245. ( Contains<int, std::allocator<int>, CustomAllocator<int>>(
  246. v2 ) ) );
  247. CHECK_THAT( v5, Contains( v6 ) );
  248. }
  249. SECTION( "Contains (element), composed" ) {
  250. CHECK_THAT( v, VectorContains( 1 ) && VectorContains( 2 ) );
  251. }
  252. SECTION( "Equals" ) {
  253. // Same vector
  254. CHECK_THAT( v, Equals( v ) );
  255. CHECK_THAT( empty, Equals( empty ) );
  256. // Different vector with same elements
  257. CHECK_THAT( v, Equals<int>( { 1, 2, 3 } ) );
  258. v2.push_back( 3 );
  259. CHECK_THAT( v, Equals( v2 ) );
  260. CHECK_THAT(
  261. v5,
  262. ( Equals<int, std::allocator<int>, CustomAllocator<int>>( v2 ) ) );
  263. v6.push_back( 3 );
  264. CHECK_THAT( v5, Equals( v6 ) );
  265. }
  266. SECTION( "UnorderedEquals" ) {
  267. CHECK_THAT( v, UnorderedEquals( v ) );
  268. CHECK_THAT( v, UnorderedEquals<int>( { 3, 2, 1 } ) );
  269. CHECK_THAT( empty, UnorderedEquals( empty ) );
  270. auto permuted = v;
  271. std::next_permutation( begin( permuted ), end( permuted ) );
  272. REQUIRE_THAT( permuted, UnorderedEquals( v ) );
  273. std::reverse( begin( permuted ), end( permuted ) );
  274. REQUIRE_THAT( permuted, UnorderedEquals( v ) );
  275. CHECK_THAT(
  276. v5,
  277. ( UnorderedEquals<int, std::allocator<int>, CustomAllocator<int>>(
  278. permuted ) ) );
  279. auto v5_permuted = v5;
  280. std::next_permutation( begin( v5_permuted ), end( v5_permuted ) );
  281. CHECK_THAT( v5_permuted, UnorderedEquals( v5 ) );
  282. }
  283. }
  284. TEST_CASE( "Vector matchers that fail", "[matchers][vector][.][failing]" ) {
  285. std::vector<int> v;
  286. v.push_back( 1 );
  287. v.push_back( 2 );
  288. v.push_back( 3 );
  289. std::vector<int> v2;
  290. v2.push_back( 1 );
  291. v2.push_back( 2 );
  292. std::vector<double> v3;
  293. v3.push_back( 1 );
  294. v3.push_back( 2 );
  295. v3.push_back( 3 );
  296. std::vector<double> v4;
  297. v4.push_back( 1.1 );
  298. v4.push_back( 2.1 );
  299. v4.push_back( 3.1 );
  300. std::vector<int> empty;
  301. SECTION( "Contains (element)" ) {
  302. CHECK_THAT( v, VectorContains( -1 ) );
  303. CHECK_THAT( empty, VectorContains( 1 ) );
  304. }
  305. SECTION( "Contains (vector)" ) {
  306. CHECK_THAT( empty, Contains( v ) );
  307. v2.push_back( 4 );
  308. CHECK_THAT( v, Contains( v2 ) );
  309. }
  310. SECTION( "Equals" ) {
  311. CHECK_THAT( v, Equals( v2 ) );
  312. CHECK_THAT( v2, Equals( v ) );
  313. CHECK_THAT( empty, Equals( v ) );
  314. CHECK_THAT( v, Equals( empty ) );
  315. }
  316. SECTION( "UnorderedEquals" ) {
  317. CHECK_THAT( v, UnorderedEquals( empty ) );
  318. CHECK_THAT( empty, UnorderedEquals( v ) );
  319. auto permuted = v;
  320. std::next_permutation( begin( permuted ), end( permuted ) );
  321. permuted.pop_back();
  322. CHECK_THAT( permuted, UnorderedEquals( v ) );
  323. std::reverse( begin( permuted ), end( permuted ) );
  324. CHECK_THAT( permuted, UnorderedEquals( v ) );
  325. }
  326. }
  327. TEST_CASE( "Exception matchers that succeed",
  328. "[matchers][exceptions][!throws]" ) {
  329. CHECK_THROWS_MATCHES(
  330. throwsSpecialException( 1 ), SpecialException, ExceptionMatcher{ 1 } );
  331. REQUIRE_THROWS_MATCHES(
  332. throwsSpecialException( 2 ), SpecialException, ExceptionMatcher{ 2 } );
  333. }
  334. TEST_CASE( "Exception matchers that fail",
  335. "[matchers][exceptions][!throws][.failing]" ) {
  336. SECTION( "No exception" ) {
  337. CHECK_THROWS_MATCHES(
  338. doesNotThrow(), SpecialException, ExceptionMatcher{ 1 } );
  339. REQUIRE_THROWS_MATCHES(
  340. doesNotThrow(), SpecialException, ExceptionMatcher{ 1 } );
  341. }
  342. SECTION( "Type mismatch" ) {
  343. CHECK_THROWS_MATCHES(
  344. throwsAsInt( 1 ), SpecialException, ExceptionMatcher{ 1 } );
  345. REQUIRE_THROWS_MATCHES(
  346. throwsAsInt( 1 ), SpecialException, ExceptionMatcher{ 1 } );
  347. }
  348. SECTION( "Contents are wrong" ) {
  349. CHECK_THROWS_MATCHES( throwsSpecialException( 3 ),
  350. SpecialException,
  351. ExceptionMatcher{ 1 } );
  352. REQUIRE_THROWS_MATCHES( throwsSpecialException( 4 ),
  353. SpecialException,
  354. ExceptionMatcher{ 1 } );
  355. }
  356. }
  357. TEST_CASE( "Floating point matchers: float", "[matchers][floating-point]" ) {
  358. SECTION( "Relative" ) {
  359. REQUIRE_THAT( 10.f, WithinRel( 11.1f, 0.1f ) );
  360. REQUIRE_THAT( 10.f, !WithinRel( 11.2f, 0.1f ) );
  361. REQUIRE_THAT( 1.f, !WithinRel( 0.f, 0.99f ) );
  362. REQUIRE_THAT( -0.f, WithinRel( 0.f ) );
  363. SECTION( "Some subnormal values" ) {
  364. auto v1 = std::numeric_limits<float>::min();
  365. auto v2 = v1;
  366. for ( int i = 0; i < 5; ++i ) {
  367. v2 = std::nextafter( v1, 0.f );
  368. }
  369. REQUIRE_THAT( v1, WithinRel( v2 ) );
  370. }
  371. }
  372. SECTION( "Margin" ) {
  373. REQUIRE_THAT( 1.f, WithinAbs( 1.f, 0 ) );
  374. REQUIRE_THAT( 0.f, WithinAbs( 1.f, 1 ) );
  375. REQUIRE_THAT( 0.f, !WithinAbs( 1.f, 0.99f ) );
  376. REQUIRE_THAT( 0.f, !WithinAbs( 1.f, 0.99f ) );
  377. REQUIRE_THAT( 0.f, WithinAbs( -0.f, 0 ) );
  378. REQUIRE_THAT( 11.f, !WithinAbs( 10.f, 0.5f ) );
  379. REQUIRE_THAT( 10.f, !WithinAbs( 11.f, 0.5f ) );
  380. REQUIRE_THAT( -10.f, WithinAbs( -10.f, 0.5f ) );
  381. REQUIRE_THAT( -10.f, WithinAbs( -9.6f, 0.5f ) );
  382. }
  383. SECTION( "ULPs" ) {
  384. REQUIRE_THAT( 1.f, WithinULP( 1.f, 0 ) );
  385. REQUIRE_THAT(-1.f, WithinULP( -1.f, 0 ) );
  386. REQUIRE_THAT( nextafter( 1.f, 2.f ), WithinULP( 1.f, 1 ) );
  387. REQUIRE_THAT( 0.f, WithinULP( nextafter( 0.f, 1.f ), 1 ) );
  388. REQUIRE_THAT( 1.f, WithinULP( nextafter( 1.f, 0.f ), 1 ) );
  389. REQUIRE_THAT( 1.f, !WithinULP( nextafter( 1.f, 2.f ), 0 ) );
  390. REQUIRE_THAT( 1.f, WithinULP( 1.f, 0 ) );
  391. REQUIRE_THAT( -0.f, WithinULP( 0.f, 0 ) );
  392. }
  393. SECTION( "Composed" ) {
  394. REQUIRE_THAT( 1.f, WithinAbs( 1.f, 0.5 ) || WithinULP( 1.f, 1 ) );
  395. REQUIRE_THAT( 1.f, WithinAbs( 2.f, 0.5 ) || WithinULP( 1.f, 0 ) );
  396. REQUIRE_THAT( 0.0001f,
  397. WithinAbs( 0.f, 0.001f ) || WithinRel( 0.f, 0.1f ) );
  398. }
  399. SECTION( "Constructor validation" ) {
  400. REQUIRE_NOTHROW( WithinAbs( 1.f, 0.f ) );
  401. REQUIRE_THROWS_AS( WithinAbs( 1.f, -1.f ), std::domain_error );
  402. REQUIRE_NOTHROW( WithinULP( 1.f, 0 ) );
  403. REQUIRE_THROWS_AS( WithinULP( 1.f, static_cast<uint64_t>( -1 ) ),
  404. std::domain_error );
  405. REQUIRE_NOTHROW( WithinRel( 1.f, 0.f ) );
  406. REQUIRE_THROWS_AS( WithinRel( 1.f, -0.2f ), std::domain_error );
  407. REQUIRE_THROWS_AS( WithinRel( 1.f, 1.f ), std::domain_error );
  408. }
  409. }
  410. TEST_CASE( "Floating point matchers: double", "[matchers][floating-point]" ) {
  411. SECTION( "Relative" ) {
  412. REQUIRE_THAT( 10., WithinRel( 11.1, 0.1 ) );
  413. REQUIRE_THAT( 10., !WithinRel( 11.2, 0.1 ) );
  414. REQUIRE_THAT( 1., !WithinRel( 0., 0.99 ) );
  415. REQUIRE_THAT( -0., WithinRel( 0. ) );
  416. SECTION( "Some subnormal values" ) {
  417. auto v1 = std::numeric_limits<double>::min();
  418. auto v2 = v1;
  419. for ( int i = 0; i < 5; ++i ) {
  420. v2 = std::nextafter( v1, 0 );
  421. }
  422. REQUIRE_THAT( v1, WithinRel( v2 ) );
  423. }
  424. }
  425. SECTION( "Margin" ) {
  426. REQUIRE_THAT( 1., WithinAbs( 1., 0 ) );
  427. REQUIRE_THAT( 0., WithinAbs( 1., 1 ) );
  428. REQUIRE_THAT( 0., !WithinAbs( 1., 0.99 ) );
  429. REQUIRE_THAT( 0., !WithinAbs( 1., 0.99 ) );
  430. REQUIRE_THAT( 11., !WithinAbs( 10., 0.5 ) );
  431. REQUIRE_THAT( 10., !WithinAbs( 11., 0.5 ) );
  432. REQUIRE_THAT( -10., WithinAbs( -10., 0.5 ) );
  433. REQUIRE_THAT( -10., WithinAbs( -9.6, 0.5 ) );
  434. }
  435. SECTION( "ULPs" ) {
  436. REQUIRE_THAT( 1., WithinULP( 1., 0 ) );
  437. REQUIRE_THAT( nextafter( 1., 2. ), WithinULP( 1., 1 ) );
  438. REQUIRE_THAT( 0., WithinULP( nextafter( 0., 1. ), 1 ) );
  439. REQUIRE_THAT( 1., WithinULP( nextafter( 1., 0. ), 1 ) );
  440. REQUIRE_THAT( 1., !WithinULP( nextafter( 1., 2. ), 0 ) );
  441. REQUIRE_THAT( 1., WithinULP( 1., 0 ) );
  442. REQUIRE_THAT( -0., WithinULP( 0., 0 ) );
  443. }
  444. SECTION( "Composed" ) {
  445. REQUIRE_THAT( 1., WithinAbs( 1., 0.5 ) || WithinULP( 2., 1 ) );
  446. REQUIRE_THAT( 1., WithinAbs( 2., 0.5 ) || WithinULP( 1., 0 ) );
  447. REQUIRE_THAT( 0.0001, WithinAbs( 0., 0.001 ) || WithinRel( 0., 0.1 ) );
  448. }
  449. SECTION( "Constructor validation" ) {
  450. REQUIRE_NOTHROW( WithinAbs( 1., 0. ) );
  451. REQUIRE_THROWS_AS( WithinAbs( 1., -1. ), std::domain_error );
  452. REQUIRE_NOTHROW( WithinULP( 1., 0 ) );
  453. REQUIRE_NOTHROW( WithinRel( 1., 0. ) );
  454. REQUIRE_THROWS_AS( WithinRel( 1., -0.2 ), std::domain_error );
  455. REQUIRE_THROWS_AS( WithinRel( 1., 1. ), std::domain_error );
  456. }
  457. }
  458. TEST_CASE( "Floating point matchers that are problematic in approvals",
  459. "[approvals][matchers][floating-point]" ) {
  460. REQUIRE_THAT( NAN, !WithinAbs( NAN, 0 ) );
  461. REQUIRE_THAT( NAN, !( WithinAbs( NAN, 100 ) || WithinULP( NAN, 123 ) ) );
  462. REQUIRE_THAT( NAN, !WithinULP( NAN, 123 ) );
  463. REQUIRE_THAT( INFINITY, WithinRel( INFINITY ) );
  464. REQUIRE_THAT( -INFINITY, !WithinRel( INFINITY ) );
  465. REQUIRE_THAT( 1., !WithinRel( INFINITY ) );
  466. REQUIRE_THAT( INFINITY, !WithinRel( 1. ) );
  467. REQUIRE_THAT( NAN, !WithinRel( NAN ) );
  468. REQUIRE_THAT( 1., !WithinRel( NAN ) );
  469. REQUIRE_THAT( NAN, !WithinRel( 1. ) );
  470. }
  471. TEST_CASE( "Arbitrary predicate matcher", "[matchers][generic]" ) {
  472. SECTION( "Function pointer" ) {
  473. REQUIRE_THAT( 1, Predicate<int>( alwaysTrue, "always true" ) );
  474. REQUIRE_THAT( 1, !Predicate<int>( alwaysFalse, "always false" ) );
  475. }
  476. SECTION( "Lambdas + different type" ) {
  477. REQUIRE_THAT( "Hello olleH",
  478. Predicate<std::string>(
  479. []( std::string const& str ) -> bool {
  480. return str.front() == str.back();
  481. },
  482. "First and last character should be equal" ) );
  483. REQUIRE_THAT(
  484. "This wouldn't pass",
  485. !Predicate<std::string>( []( std::string const& str ) -> bool {
  486. return str.front() == str.back();
  487. } ) );
  488. }
  489. }
  490. TEST_CASE( "Regression test #1", "[matchers][vector]" ) {
  491. // At some point, UnorderedEqualsMatcher skipped
  492. // mismatched prefixed before doing the comparison itself
  493. std::vector<char> actual = { 'a', 'b' };
  494. std::vector<char> expected = { 'c', 'b' };
  495. CHECK_THAT( actual, !UnorderedEquals( expected ) );
  496. }
  497. TEST_CASE( "Predicate matcher can accept const char*",
  498. "[matchers][compilation]" ) {
  499. REQUIRE_THAT( "foo", Predicate<const char*>( []( const char* const& ) {
  500. return true;
  501. } ) );
  502. }
  503. TEST_CASE( "Vector Approx matcher", "[matchers][approx][vector]" ) {
  504. using Catch::Matchers::Approx;
  505. SECTION( "Empty vector is roughly equal to an empty vector" ) {
  506. std::vector<double> empty;
  507. REQUIRE_THAT( empty, Approx( empty ) );
  508. }
  509. SECTION( "Vectors with elements" ) {
  510. std::vector<double> v1( { 1., 2., 3. } );
  511. SECTION( "A vector is approx equal to itself" ) {
  512. REQUIRE_THAT( v1, Approx( v1 ) );
  513. REQUIRE_THAT( v1, Approx<double>( { 1., 2., 3. } ) );
  514. }
  515. std::vector<double> v2( { 1.5, 2.5, 3.5 } );
  516. SECTION( "Different length" ) {
  517. auto temp( v1 );
  518. temp.push_back( 4 );
  519. REQUIRE_THAT( v1, !Approx( temp ) );
  520. }
  521. SECTION( "Same length, different elements" ) {
  522. REQUIRE_THAT( v1, !Approx( v2 ) );
  523. REQUIRE_THAT( v1, Approx( v2 ).margin( 0.5 ) );
  524. REQUIRE_THAT( v1, Approx( v2 ).epsilon( 0.5 ) );
  525. REQUIRE_THAT( v1, Approx( v2 ).epsilon( 0.1 ).scale( 500 ) );
  526. }
  527. }
  528. }
  529. TEST_CASE( "Vector Approx matcher -- failing",
  530. "[matchers][approx][vector][.failing]" ) {
  531. using Catch::Matchers::Approx;
  532. SECTION( "Empty and non empty vectors are not approx equal" ) {
  533. std::vector<double> empty, t1( { 1, 2 } );
  534. CHECK_THAT( empty, Approx( t1 ) );
  535. }
  536. SECTION( "Just different vectors" ) {
  537. std::vector<double> v1( { 2., 4., 6. } ), v2( { 1., 3., 5. } );
  538. CHECK_THAT( v1, Approx( v2 ) );
  539. }
  540. }
  541. TEST_CASE( "Exceptions matchers", "[matchers][exceptions][!throws]" ) {
  542. REQUIRE_THROWS_MATCHES( throwsDerivedException(),
  543. DerivedException,
  544. Message( "DerivedException::what" ) );
  545. REQUIRE_THROWS_MATCHES( throwsDerivedException(),
  546. DerivedException,
  547. !Message( "derivedexception::what" ) );
  548. REQUIRE_THROWS_MATCHES( throwsSpecialException( 2 ),
  549. SpecialException,
  550. !Message( "DerivedException::what" ) );
  551. REQUIRE_THROWS_MATCHES( throwsSpecialException( 2 ),
  552. SpecialException,
  553. Message( "SpecialException::what" ) );
  554. }
  555. struct CheckedTestingMatcher : Catch::Matchers::MatcherBase<int> {
  556. mutable bool matchCalled = false;
  557. bool matchSucceeds = false;
  558. bool match( int const& ) const override {
  559. matchCalled = true;
  560. return matchSucceeds;
  561. }
  562. std::string describe() const override {
  563. return "CheckedTestingMatcher set to " +
  564. ( matchSucceeds ? std::string( "succeed" )
  565. : std::string( "fail" ) );
  566. }
  567. };
  568. TEST_CASE( "Composed matchers shortcircuit", "[matchers][composed]" ) {
  569. // Check that if first returns false, second is not touched
  570. CheckedTestingMatcher first, second;
  571. SECTION( "MatchAllOf" ) {
  572. first.matchSucceeds = false;
  573. Detail::MatchAllOf<int> matcher =
  574. Detail::MatchAllOf<int>{} && first && second;
  575. CHECK_FALSE( matcher.match( 1 ) );
  576. // These two assertions are the important ones
  577. REQUIRE( first.matchCalled );
  578. REQUIRE( !second.matchCalled );
  579. }
  580. // Check that if first returns true, second is not touched
  581. SECTION( "MatchAnyOf" ) {
  582. first.matchSucceeds = true;
  583. Detail::MatchAnyOf<int> matcher =
  584. Detail::MatchAnyOf<int>{} || first || second;
  585. CHECK( matcher.match( 1 ) );
  586. // These two assertions are the important ones
  587. REQUIRE( first.matchCalled );
  588. REQUIRE( !second.matchCalled );
  589. }
  590. }
  591. struct CheckedTestingGenericMatcher : Catch::Matchers::MatcherGenericBase {
  592. mutable bool matchCalled = false;
  593. bool matchSucceeds = false;
  594. bool match( int const& ) const {
  595. matchCalled = true;
  596. return matchSucceeds;
  597. }
  598. std::string describe() const override {
  599. return "CheckedTestingGenericMatcher set to " +
  600. ( matchSucceeds ? std::string( "succeed" )
  601. : std::string( "fail" ) );
  602. }
  603. };
  604. TEST_CASE( "Composed generic matchers shortcircuit",
  605. "[matchers][composed][generic]" ) {
  606. // Check that if first returns false, second is not touched
  607. CheckedTestingGenericMatcher first, second;
  608. SECTION( "MatchAllOf" ) {
  609. first.matchSucceeds = false;
  610. Detail::MatchAllOfGeneric<CheckedTestingGenericMatcher,
  611. CheckedTestingGenericMatcher>
  612. matcher{ first, second };
  613. CHECK_FALSE( matcher.match( 1 ) );
  614. // These two assertions are the important ones
  615. REQUIRE( first.matchCalled );
  616. REQUIRE( !second.matchCalled );
  617. }
  618. // Check that if first returns true, second is not touched
  619. SECTION( "MatchAnyOf" ) {
  620. first.matchSucceeds = true;
  621. Detail::MatchAnyOfGeneric<CheckedTestingGenericMatcher,
  622. CheckedTestingGenericMatcher>
  623. matcher{ first, second };
  624. CHECK( matcher.match( 1 ) );
  625. // These two assertions are the important ones
  626. REQUIRE( first.matchCalled );
  627. REQUIRE( !second.matchCalled );
  628. }
  629. }
  630. template <typename Range>
  631. struct EqualsRangeMatcher : Catch::Matchers::MatcherGenericBase {
  632. EqualsRangeMatcher( Range const& range ): m_range{ range } {}
  633. template <typename OtherRange> bool match( OtherRange const& other ) const {
  634. using std::begin;
  635. using std::end;
  636. return std::equal(
  637. begin( m_range ), end( m_range ), begin( other ), end( other ) );
  638. }
  639. std::string describe() const override {
  640. return "Equals: " + Catch::rangeToString( m_range );
  641. }
  642. private:
  643. Range const& m_range;
  644. };
  645. template <typename Range>
  646. auto EqualsRange( const Range& range ) -> EqualsRangeMatcher<Range> {
  647. return EqualsRangeMatcher<Range>{ range };
  648. }
  649. TEST_CASE( "Combining templated matchers", "[matchers][templated]" ) {
  650. std::array<int, 3> container{ { 1, 2, 3 } };
  651. std::array<int, 3> a{ { 1, 2, 3 } };
  652. std::vector<int> b{ 0, 1, 2 };
  653. std::list<int> c{ 4, 5, 6 };
  654. REQUIRE_THAT( container,
  655. EqualsRange( a ) || EqualsRange( b ) || EqualsRange( c ) );
  656. }
  657. TEST_CASE( "Combining templated and concrete matchers",
  658. "[matchers][templated]" ) {
  659. std::vector<int> vec{ 1, 3, 5 };
  660. std::array<int, 3> a{ { 5, 3, 1 } };
  661. REQUIRE_THAT( vec,
  662. Predicate<std::vector<int>>(
  663. []( auto const& v ) {
  664. return std::all_of(
  665. v.begin(), v.end(), []( int elem ) {
  666. return elem % 2 == 1;
  667. } );
  668. },
  669. "All elements are odd" ) &&
  670. !EqualsRange( a ) );
  671. const std::string str = "foobar";
  672. const std::array<char, 6> arr{ { 'f', 'o', 'o', 'b', 'a', 'r' } };
  673. const std::array<char, 6> bad_arr{ { 'o', 'o', 'f', 'b', 'a', 'r' } };
  674. using Catch::Matchers::EndsWith;
  675. using Catch::Matchers::StartsWith;
  676. REQUIRE_THAT(
  677. str, StartsWith( "foo" ) && EqualsRange( arr ) && EndsWith( "bar" ) );
  678. REQUIRE_THAT( str,
  679. StartsWith( "foo" ) && !EqualsRange( bad_arr ) &&
  680. EndsWith( "bar" ) );
  681. REQUIRE_THAT(
  682. str, EqualsRange( arr ) && StartsWith( "foo" ) && EndsWith( "bar" ) );
  683. REQUIRE_THAT( str,
  684. !EqualsRange( bad_arr ) && StartsWith( "foo" ) &&
  685. EndsWith( "bar" ) );
  686. REQUIRE_THAT( str,
  687. EqualsRange( bad_arr ) ||
  688. ( StartsWith( "foo" ) && EndsWith( "bar" ) ) );
  689. REQUIRE_THAT( str,
  690. ( StartsWith( "foo" ) && EndsWith( "bar" ) ) ||
  691. EqualsRange( bad_arr ) );
  692. }
  693. TEST_CASE( "Combining concrete matchers does not use templated matchers",
  694. "[matchers][templated]" ) {
  695. using Catch::Matchers::EndsWith;
  696. using Catch::Matchers::StartsWith;
  697. STATIC_REQUIRE(
  698. std::is_same<decltype( StartsWith( "foo" ) ||
  699. ( StartsWith( "bar" ) && EndsWith( "bar" ) &&
  700. !EndsWith( "foo" ) ) ),
  701. Catch::Matchers::Detail::MatchAnyOf<std::string>>::value );
  702. }
  703. struct MatcherA : Catch::Matchers::MatcherGenericBase {
  704. std::string describe() const override {
  705. return "equals: (int) 1 or (string) \"1\"";
  706. }
  707. bool match( int i ) const { return i == 1; }
  708. bool match( std::string s ) const { return s == "1"; }
  709. };
  710. struct MatcherB : Catch::Matchers::MatcherGenericBase {
  711. std::string describe() const override { return "equals: (long long) 1"; }
  712. bool match( long long l ) const { return l == 1ll; }
  713. };
  714. struct MatcherC : Catch::Matchers::MatcherGenericBase {
  715. std::string describe() const override { return "equals: (T) 1"; }
  716. template <typename T> bool match( T t ) const { return t == T{ 1 }; }
  717. };
  718. struct MatcherD : Catch::Matchers::MatcherGenericBase {
  719. std::string describe() const override { return "equals: true"; }
  720. bool match( bool b ) const { return b == true; }
  721. };
  722. TEST_CASE( "Combining only templated matchers", "[matchers][templated]" ) {
  723. STATIC_REQUIRE(
  724. std::is_same<decltype( MatcherA() || MatcherB() ),
  725. Catch::Matchers::Detail::
  726. MatchAnyOfGeneric<MatcherA, MatcherB>>::value );
  727. REQUIRE_THAT( 1, MatcherA() || MatcherB() );
  728. STATIC_REQUIRE(
  729. std::is_same<decltype( MatcherA() && MatcherB() ),
  730. Catch::Matchers::Detail::
  731. MatchAllOfGeneric<MatcherA, MatcherB>>::value );
  732. REQUIRE_THAT( 1, MatcherA() && MatcherB() );
  733. STATIC_REQUIRE(
  734. std::is_same<
  735. decltype( MatcherA() || !MatcherB() ),
  736. Catch::Matchers::Detail::MatchAnyOfGeneric<
  737. MatcherA,
  738. Catch::Matchers::Detail::MatchNotOfGeneric<MatcherB>>>::value );
  739. REQUIRE_THAT( 1, MatcherA() || !MatcherB() );
  740. }
  741. TEST_CASE( "Combining MatchAnyOfGeneric does not nest",
  742. "[matchers][templated]" ) {
  743. // MatchAnyOfGeneric LHS + some matcher RHS
  744. STATIC_REQUIRE(
  745. std::is_same<
  746. decltype( ( MatcherA() || MatcherB() ) || MatcherC() ),
  747. Catch::Matchers::Detail::
  748. MatchAnyOfGeneric<MatcherA, MatcherB, MatcherC>>::value );
  749. REQUIRE_THAT( 1, ( MatcherA() || MatcherB() ) || MatcherC() );
  750. // some matcher LHS + MatchAnyOfGeneric RHS
  751. STATIC_REQUIRE(
  752. std::is_same<
  753. decltype( MatcherA() || ( MatcherB() || MatcherC() ) ),
  754. Catch::Matchers::Detail::
  755. MatchAnyOfGeneric<MatcherA, MatcherB, MatcherC>>::value );
  756. REQUIRE_THAT( 1, MatcherA() || ( MatcherB() || MatcherC() ) );
  757. // MatchAnyOfGeneric LHS + MatchAnyOfGeneric RHS
  758. STATIC_REQUIRE(
  759. std::is_same<
  760. decltype( ( MatcherA() || MatcherB() ) ||
  761. ( MatcherC() || MatcherD() ) ),
  762. Catch::Matchers::Detail::
  763. MatchAnyOfGeneric<MatcherA, MatcherB, MatcherC, MatcherD>>::
  764. value );
  765. REQUIRE_THAT(
  766. 1, ( MatcherA() || MatcherB() ) || ( MatcherC() || MatcherD() ) );
  767. }
  768. TEST_CASE( "Combining MatchAllOfGeneric does not nest",
  769. "[matchers][templated]" ) {
  770. // MatchAllOfGeneric lhs + some matcher RHS
  771. STATIC_REQUIRE(
  772. std::is_same<
  773. decltype( ( MatcherA() && MatcherB() ) && MatcherC() ),
  774. Catch::Matchers::Detail::
  775. MatchAllOfGeneric<MatcherA, MatcherB, MatcherC>>::value );
  776. REQUIRE_THAT( 1, ( MatcherA() && MatcherB() ) && MatcherC() );
  777. // some matcher LHS + MatchAllOfGeneric RSH
  778. STATIC_REQUIRE(
  779. std::is_same<
  780. decltype( MatcherA() && ( MatcherB() && MatcherC() ) ),
  781. Catch::Matchers::Detail::
  782. MatchAllOfGeneric<MatcherA, MatcherB, MatcherC>>::value );
  783. REQUIRE_THAT( 1, MatcherA() && ( MatcherB() && MatcherC() ) );
  784. // MatchAllOfGeneric LHS + MatchAllOfGeneric RHS
  785. STATIC_REQUIRE(
  786. std::is_same<
  787. decltype( ( MatcherA() && MatcherB() ) &&
  788. ( MatcherC() && MatcherD() ) ),
  789. Catch::Matchers::Detail::
  790. MatchAllOfGeneric<MatcherA, MatcherB, MatcherC, MatcherD>>::
  791. value );
  792. REQUIRE_THAT(
  793. 1, ( MatcherA() && MatcherB() ) && ( MatcherC() && MatcherD() ) );
  794. }
  795. TEST_CASE( "Combining MatchNotOfGeneric does not nest",
  796. "[matchers][templated]" ) {
  797. STATIC_REQUIRE(
  798. std::is_same<
  799. decltype( !MatcherA() ),
  800. Catch::Matchers::Detail::MatchNotOfGeneric<MatcherA>>::value );
  801. REQUIRE_THAT( 0, !MatcherA() );
  802. STATIC_REQUIRE(
  803. std::is_same<decltype( !!MatcherA() ), MatcherA const&>::value );
  804. REQUIRE_THAT( 1, !!MatcherA() );
  805. STATIC_REQUIRE(
  806. std::is_same<
  807. decltype( !!!MatcherA() ),
  808. Catch::Matchers::Detail::MatchNotOfGeneric<MatcherA>>::value );
  809. REQUIRE_THAT( 0, !!!MatcherA() );
  810. STATIC_REQUIRE(
  811. std::is_same<decltype( !!!!MatcherA() ), MatcherA const&>::value );
  812. REQUIRE_THAT( 1, !!!!MatcherA() );
  813. }
  814. struct EvilAddressOfOperatorUsed : std::exception {
  815. EvilAddressOfOperatorUsed() {}
  816. const char* what() const noexcept override {
  817. return "overloaded address-of operator of matcher was used instead of "
  818. "std::addressof";
  819. }
  820. };
  821. struct EvilCommaOperatorUsed : std::exception {
  822. EvilCommaOperatorUsed() {}
  823. const char* what() const noexcept override {
  824. return "overloaded comma operator of matcher was used";
  825. }
  826. };
  827. struct EvilMatcher : Catch::Matchers::MatcherGenericBase {
  828. std::string describe() const override { return "equals: 45"; }
  829. bool match( int i ) const { return i == 45; }
  830. EvilMatcher const* operator&() const { throw EvilAddressOfOperatorUsed(); }
  831. int operator,( EvilMatcher const& ) const { throw EvilCommaOperatorUsed(); }
  832. };
  833. TEST_CASE( "Overloaded comma or address-of operators are not used",
  834. "[matchers][templated]" ) {
  835. REQUIRE_THROWS_AS( ( EvilMatcher(), EvilMatcher() ),
  836. EvilCommaOperatorUsed );
  837. REQUIRE_THROWS_AS( &EvilMatcher(), EvilAddressOfOperatorUsed );
  838. REQUIRE_NOTHROW( EvilMatcher() || ( EvilMatcher() && !EvilMatcher() ) );
  839. REQUIRE_NOTHROW( ( EvilMatcher() && EvilMatcher() ) || !EvilMatcher() );
  840. }
  841. struct ImmovableMatcher : Catch::Matchers::MatcherGenericBase {
  842. ImmovableMatcher() = default;
  843. ImmovableMatcher( ImmovableMatcher const& ) = delete;
  844. ImmovableMatcher( ImmovableMatcher&& ) = delete;
  845. ImmovableMatcher& operator=( ImmovableMatcher const& ) = delete;
  846. ImmovableMatcher& operator=( ImmovableMatcher&& ) = delete;
  847. std::string describe() const override { return "always false"; }
  848. template <typename T> bool match( T&& ) const { return false; }
  849. };
  850. struct MatcherWasMovedOrCopied : std::exception {
  851. MatcherWasMovedOrCopied() {}
  852. const char* what() const noexcept override {
  853. return "attempted to copy or move a matcher";
  854. }
  855. };
  856. struct ThrowOnCopyOrMoveMatcher : Catch::Matchers::MatcherGenericBase {
  857. ThrowOnCopyOrMoveMatcher() = default;
  858. [[noreturn]] ThrowOnCopyOrMoveMatcher( ThrowOnCopyOrMoveMatcher const& ):
  859. Catch::Matchers::MatcherGenericBase() {
  860. throw MatcherWasMovedOrCopied();
  861. }
  862. [[noreturn]] ThrowOnCopyOrMoveMatcher( ThrowOnCopyOrMoveMatcher&& ):
  863. Catch::Matchers::MatcherGenericBase() {
  864. throw MatcherWasMovedOrCopied();
  865. }
  866. ThrowOnCopyOrMoveMatcher& operator=( ThrowOnCopyOrMoveMatcher const& ) {
  867. throw MatcherWasMovedOrCopied();
  868. }
  869. ThrowOnCopyOrMoveMatcher& operator=( ThrowOnCopyOrMoveMatcher&& ) {
  870. throw MatcherWasMovedOrCopied();
  871. }
  872. std::string describe() const override { return "always false"; }
  873. template <typename T> bool match( T&& ) const { return false; }
  874. };
  875. TEST_CASE( "Matchers are not moved or copied",
  876. "[matchers][templated][approvals]" ) {
  877. REQUIRE_NOTHROW(
  878. ( ThrowOnCopyOrMoveMatcher() && ThrowOnCopyOrMoveMatcher() ) ||
  879. !ThrowOnCopyOrMoveMatcher() );
  880. }
  881. TEST_CASE( "Immovable matchers can be used",
  882. "[matchers][templated][approvals]" ) {
  883. REQUIRE_THAT( 123,
  884. ( ImmovableMatcher() && ImmovableMatcher() ) ||
  885. !ImmovableMatcher() );
  886. }
  887. struct ReferencingMatcher : Catch::Matchers::MatcherGenericBase {
  888. std::string describe() const override { return "takes reference"; }
  889. bool match( int& i ) const { return i == 22; }
  890. };
  891. TEST_CASE( "Matchers can take references",
  892. "[matchers][templated][approvals]" ) {
  893. REQUIRE_THAT( 22, ReferencingMatcher{} );
  894. }
  895. #ifdef __clang__
  896. # pragma clang diagnostic pop
  897. #endif
  898. TEMPLATE_TEST_CASE(
  899. "#2152 - ULP checks between differently signed values were wrong",
  900. "[matchers][floating-point][ulp]",
  901. float,
  902. double ) {
  903. using Catch::Matchers::WithinULP;
  904. static constexpr TestType smallest_non_zero =
  905. std::numeric_limits<TestType>::denorm_min();
  906. CHECK_THAT( smallest_non_zero, WithinULP( -smallest_non_zero, 2 ) );
  907. CHECK_THAT( smallest_non_zero, !WithinULP( -smallest_non_zero, 1 ) );
  908. }