Condition.tests.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. #ifdef __clang__
  7. # pragma clang diagnostic push
  8. # pragma clang diagnostic ignored "-Wpadded"
  9. // Wdouble-promotion is not supported until 3.8
  10. # if (__clang_major__ > 3) || (__clang_major__ == 3 && __clang_minor__ > 7)
  11. # pragma clang diagnostic ignored "-Wdouble-promotion"
  12. # endif
  13. #endif
  14. #include <catch2/catch_approx.hpp>
  15. #include <catch2/catch_test_macros.hpp>
  16. using Catch::Approx;
  17. #include <string>
  18. #include <limits>
  19. #include <cstdint>
  20. namespace {
  21. struct TestData {
  22. int int_seven = 7;
  23. std::string str_hello = "hello";
  24. float float_nine_point_one = 9.1f;
  25. double double_pi = 3.1415926535;
  26. };
  27. static const char* returnsConstNull() { return nullptr; }
  28. static char* returnsNull() { return nullptr; }
  29. } // end unnamed namespace
  30. // The "failing" tests all use the CHECK macro, which continues if the specific test fails.
  31. // This allows us to see all results, even if an earlier check fails
  32. // Equality tests
  33. TEST_CASE( "Equality checks that should succeed" )
  34. {
  35. TestData data;
  36. REQUIRE( data.int_seven == 7 );
  37. REQUIRE( data.float_nine_point_one == Approx( 9.1f ) );
  38. REQUIRE( data.double_pi == Approx( 3.1415926535 ) );
  39. REQUIRE( data.str_hello == "hello" );
  40. REQUIRE( "hello" == data.str_hello );
  41. REQUIRE( data.str_hello.size() == 5 );
  42. double x = 1.1 + 0.1 + 0.1;
  43. REQUIRE( x == Approx( 1.3 ) );
  44. }
  45. TEST_CASE( "Equality checks that should fail", "[.][failing][!mayfail]" )
  46. {
  47. TestData data;
  48. CHECK( data.int_seven == 6 );
  49. CHECK( data.int_seven == 8 );
  50. CHECK( data.int_seven == 0 );
  51. CHECK( data.float_nine_point_one == Approx( 9.11f ) );
  52. CHECK( data.float_nine_point_one == Approx( 9.0f ) );
  53. CHECK( data.float_nine_point_one == Approx( 1 ) );
  54. CHECK( data.float_nine_point_one == Approx( 0 ) );
  55. CHECK( data.double_pi == Approx( 3.1415 ) );
  56. CHECK( data.str_hello == "goodbye" );
  57. CHECK( data.str_hello == "hell" );
  58. CHECK( data.str_hello == "hello1" );
  59. CHECK( data.str_hello.size() == 6 );
  60. double x = 1.1 + 0.1 + 0.1;
  61. CHECK( x == Approx( 1.301 ) );
  62. }
  63. // Needed to test junit reporter's handling of mayfail test cases and sections
  64. TEST_CASE("Mayfail test case with nested sections", "[!mayfail]") {
  65. SECTION("A") {
  66. SECTION("1") { FAIL(); }
  67. SECTION("2") { FAIL(); }
  68. }
  69. SECTION("B") {
  70. SECTION("1") { FAIL(); }
  71. SECTION("2") { FAIL(); }
  72. }
  73. }
  74. TEST_CASE( "Inequality checks that should succeed" )
  75. {
  76. TestData data;
  77. REQUIRE( data.int_seven != 6 );
  78. REQUIRE( data.int_seven != 8 );
  79. REQUIRE( data.float_nine_point_one != Approx( 9.11f ) );
  80. REQUIRE( data.float_nine_point_one != Approx( 9.0f ) );
  81. REQUIRE( data.float_nine_point_one != Approx( 1 ) );
  82. REQUIRE( data.float_nine_point_one != Approx( 0 ) );
  83. REQUIRE( data.double_pi != Approx( 3.1415 ) );
  84. REQUIRE( data.str_hello != "goodbye" );
  85. REQUIRE( data.str_hello != "hell" );
  86. REQUIRE( data.str_hello != "hello1" );
  87. REQUIRE( data.str_hello.size() != 6 );
  88. }
  89. TEST_CASE( "Inequality checks that should fail", "[.][failing][!shouldfail]" )
  90. {
  91. TestData data;
  92. CHECK( data.int_seven != 7 );
  93. CHECK( data.float_nine_point_one != Approx( 9.1f ) );
  94. CHECK( data.double_pi != Approx( 3.1415926535 ) );
  95. CHECK( data.str_hello != "hello" );
  96. CHECK( data.str_hello.size() != 5 );
  97. }
  98. // Ordering comparison tests
  99. TEST_CASE( "Ordering comparison checks that should succeed" )
  100. {
  101. TestData data;
  102. REQUIRE( data.int_seven < 8 );
  103. REQUIRE( data.int_seven > 6 );
  104. REQUIRE( data.int_seven > 0 );
  105. REQUIRE( data.int_seven > -1 );
  106. REQUIRE( data.int_seven >= 7 );
  107. REQUIRE( data.int_seven >= 6 );
  108. REQUIRE( data.int_seven <= 7 );
  109. REQUIRE( data.int_seven <= 8 );
  110. REQUIRE( data.float_nine_point_one > 9 );
  111. REQUIRE( data.float_nine_point_one < 10 );
  112. REQUIRE( data.float_nine_point_one < 9.2 );
  113. REQUIRE( data.str_hello <= "hello" );
  114. REQUIRE( data.str_hello >= "hello" );
  115. REQUIRE( data.str_hello < "hellp" );
  116. REQUIRE( data.str_hello < "zebra" );
  117. REQUIRE( data.str_hello > "hellm" );
  118. REQUIRE( data.str_hello > "a" );
  119. }
  120. TEST_CASE( "Ordering comparison checks that should fail", "[.][failing]" )
  121. {
  122. TestData data;
  123. CHECK( data.int_seven > 7 );
  124. CHECK( data.int_seven < 7 );
  125. CHECK( data.int_seven > 8 );
  126. CHECK( data.int_seven < 6 );
  127. CHECK( data.int_seven < 0 );
  128. CHECK( data.int_seven < -1 );
  129. CHECK( data.int_seven >= 8 );
  130. CHECK( data.int_seven <= 6 );
  131. CHECK( data.float_nine_point_one < 9 );
  132. CHECK( data.float_nine_point_one > 10 );
  133. CHECK( data.float_nine_point_one > 9.2 );
  134. CHECK( data.str_hello > "hello" );
  135. CHECK( data.str_hello < "hello" );
  136. CHECK( data.str_hello > "hellp" );
  137. CHECK( data.str_hello > "z" );
  138. CHECK( data.str_hello < "hellm" );
  139. CHECK( data.str_hello < "a" );
  140. CHECK( data.str_hello >= "z" );
  141. CHECK( data.str_hello <= "a" );
  142. }
  143. #ifdef __clang__
  144. # pragma clang diagnostic pop
  145. #endif
  146. // Comparisons with int literals
  147. TEST_CASE( "Comparisons with int literals don't warn when mixing signed/ unsigned" )
  148. {
  149. int i = 1;
  150. unsigned int ui = 2;
  151. long l = 3;
  152. unsigned long ul = 4;
  153. char c = 5;
  154. unsigned char uc = 6;
  155. REQUIRE( i == 1 );
  156. REQUIRE( ui == 2 );
  157. REQUIRE( l == 3 );
  158. REQUIRE( ul == 4 );
  159. REQUIRE( c == 5 );
  160. REQUIRE( uc == 6 );
  161. REQUIRE( 1 == i );
  162. REQUIRE( 2 == ui );
  163. REQUIRE( 3 == l );
  164. REQUIRE( 4 == ul );
  165. REQUIRE( 5 == c );
  166. REQUIRE( 6 == uc );
  167. REQUIRE( (std::numeric_limits<uint32_t>::max)() > ul );
  168. }
  169. // Disable warnings about sign conversions for the next two tests
  170. // (as we are deliberately invoking them)
  171. // - Currently only disabled for GCC/ LLVM. Should add VC++ too
  172. #ifdef __GNUC__
  173. #pragma GCC diagnostic push
  174. #pragma GCC diagnostic ignored "-Wsign-compare"
  175. #pragma GCC diagnostic ignored "-Wsign-conversion"
  176. #endif
  177. #ifdef _MSC_VER
  178. #pragma warning(disable:4389) // '==' : signed/unsigned mismatch
  179. #endif
  180. TEST_CASE( "comparisons between int variables" )
  181. {
  182. long long_var = 1L;
  183. unsigned char unsigned_char_var = 1;
  184. unsigned short unsigned_short_var = 1;
  185. unsigned int unsigned_int_var = 1;
  186. unsigned long unsigned_long_var = 1L;
  187. REQUIRE( long_var == unsigned_char_var );
  188. REQUIRE( long_var == unsigned_short_var );
  189. REQUIRE( long_var == unsigned_int_var );
  190. REQUIRE( long_var == unsigned_long_var );
  191. }
  192. TEST_CASE( "comparisons between const int variables" )
  193. {
  194. const unsigned char unsigned_char_var = 1;
  195. const unsigned short unsigned_short_var = 1;
  196. const unsigned int unsigned_int_var = 1;
  197. const unsigned long unsigned_long_var = 1L;
  198. REQUIRE( unsigned_char_var == 1 );
  199. REQUIRE( unsigned_short_var == 1 );
  200. REQUIRE( unsigned_int_var == 1 );
  201. REQUIRE( unsigned_long_var == 1 );
  202. }
  203. TEST_CASE( "Comparisons between unsigned ints and negative signed ints match c++ standard behaviour" )
  204. {
  205. CHECK( ( -1 > 2u ) );
  206. CHECK( -1 > 2u );
  207. CHECK( ( 2u < -1 ) );
  208. CHECK( 2u < -1 );
  209. const int minInt = (std::numeric_limits<int>::min)();
  210. CHECK( ( minInt > 2u ) );
  211. CHECK( minInt > 2u );
  212. }
  213. TEST_CASE( "Comparisons between ints where one side is computed" )
  214. {
  215. CHECK( 54 == 6*9 );
  216. }
  217. #ifdef __GNUC__
  218. #pragma GCC diagnostic pop
  219. #endif
  220. TEST_CASE( "Pointers can be compared to null" )
  221. {
  222. TestData* p = nullptr;
  223. TestData* pNULL = nullptr;
  224. REQUIRE( p == nullptr );
  225. REQUIRE( p == pNULL );
  226. TestData data;
  227. p = &data;
  228. REQUIRE( p != nullptr );
  229. const TestData* cp = p;
  230. REQUIRE( cp != nullptr );
  231. const TestData* const cpc = p;
  232. REQUIRE( cpc != nullptr );
  233. REQUIRE( returnsNull() == nullptr );
  234. REQUIRE( returnsConstNull() == nullptr );
  235. REQUIRE( nullptr != p );
  236. }
  237. // Not (!) tests
  238. // The problem with the ! operator is that it has right-to-left associativity.
  239. // This means we can't isolate it when we decompose. The simple REQUIRE( !false ) form, therefore,
  240. // cannot have the operand value extracted. The test will work correctly, and the situation
  241. // is detected and a warning issued.
  242. // An alternative form of the macros (CHECK_FALSE and REQUIRE_FALSE) can be used instead to capture
  243. // the operand value.
  244. TEST_CASE( "'Not' checks that should succeed" )
  245. {
  246. bool falseValue = false;
  247. REQUIRE( false == false );
  248. REQUIRE( true == true );
  249. REQUIRE( !false );
  250. REQUIRE_FALSE( false );
  251. REQUIRE( !falseValue );
  252. REQUIRE_FALSE( falseValue );
  253. REQUIRE( !(1 == 2) );
  254. REQUIRE_FALSE( 1 == 2 );
  255. }
  256. TEST_CASE( "'Not' checks that should fail", "[.][failing]" )
  257. {
  258. bool trueValue = true;
  259. CHECK( false != false );
  260. CHECK( true != true );
  261. CHECK( !true );
  262. CHECK_FALSE( true );
  263. CHECK( !trueValue );
  264. CHECK_FALSE( trueValue );
  265. CHECK( !(1 == 1) );
  266. CHECK_FALSE( 1 == 1 );
  267. }