Compilation.tests.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Created by Martin on 17/02/2017.
  3. *
  4. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <type_traits>
  8. // Setup for #1403 -- look for global overloads of operator << for classes
  9. // in a different namespace.
  10. #include <ostream>
  11. namespace foo {
  12. struct helper_1403 {
  13. bool operator==(helper_1403) const { return true; }
  14. };
  15. }
  16. namespace bar {
  17. template <typename... Ts>
  18. struct TypeList {};
  19. }
  20. #ifdef __GNUC__
  21. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  22. #endif
  23. std::ostream& operator<<(std::ostream& out, foo::helper_1403 const&) {
  24. return out << "[1403 helper]";
  25. }
  26. ///////////////////////////////
  27. #include "catch.hpp"
  28. #include <cstring>
  29. namespace { namespace CompilationTests {
  30. #ifndef COMPILATION_TEST_HELPERS_INCLUDED // Don't compile this more than once per TU
  31. #define COMPILATION_TEST_HELPERS_INCLUDED
  32. // Comparison operators can return non-booleans.
  33. // This is unusual, but should be supported.
  34. struct logic_t {
  35. logic_t operator< (logic_t) const { return {}; }
  36. logic_t operator<=(logic_t) const { return {}; }
  37. logic_t operator> (logic_t) const { return {}; }
  38. logic_t operator>=(logic_t) const { return {}; }
  39. logic_t operator==(logic_t) const { return {}; }
  40. logic_t operator!=(logic_t) const { return {}; }
  41. explicit operator bool() const { return true; }
  42. };
  43. // This is a minimal example for an issue we have found in 1.7.0
  44. struct foo {
  45. int i;
  46. };
  47. template<typename T>
  48. bool operator==(const T &val, foo f) {
  49. return val == f.i;
  50. }
  51. struct Y {
  52. uint32_t v : 1;
  53. };
  54. void throws_int(bool b) {
  55. if (b) {
  56. throw 1;
  57. }
  58. }
  59. template<typename T>
  60. bool templated_tests(T t) {
  61. int a = 3;
  62. REQUIRE(a == t);
  63. CHECK(a == t);
  64. REQUIRE_THROWS(throws_int(true));
  65. CHECK_THROWS_AS(throws_int(true), int);
  66. REQUIRE_NOTHROW(throws_int(false));
  67. #ifndef CATCH_CONFIG_DISABLE_MATCHERS
  68. REQUIRE_THAT("aaa", Catch::EndsWith("aaa"));
  69. #endif
  70. return true;
  71. }
  72. struct A {
  73. };
  74. std::ostream &operator<<(std::ostream &o, const A &) { return o << 0; }
  75. struct B : private A {
  76. bool operator==(int) const { return true; }
  77. };
  78. #ifdef __clang__
  79. #pragma clang diagnostic push
  80. #pragma clang diagnostic ignored "-Wunused-function"
  81. #endif
  82. #ifdef __GNUC__
  83. // Note that because -~GCC~-, this warning cannot be silenced temporarily, by pushing diagnostic stack...
  84. // Luckily it is firing in test files and thus can be silenced for the whole file, without losing much.
  85. #pragma GCC diagnostic ignored "-Wunused-function"
  86. #endif
  87. B f();
  88. std::ostream g();
  89. #ifdef __clang__
  90. #pragma clang diagnostic pop
  91. #endif
  92. template <typename, typename>
  93. struct Fixture_1245 {};
  94. #endif
  95. TEST_CASE("#809") {
  96. foo f;
  97. f.i = 42;
  98. REQUIRE(42 == f);
  99. }
  100. // ------------------------------------------------------------------
  101. // Changes to REQUIRE_THROWS_AS made it stop working in a template in
  102. // an unfixable way (as long as C++03 compatibility is being kept).
  103. // To prevent these from happening in the future, this needs to compile
  104. TEST_CASE("#833") {
  105. REQUIRE(templated_tests<int>(3));
  106. }
  107. // Test containing example where original stream insertable check breaks compilation
  108. TEST_CASE("#872") {
  109. A dummy;
  110. CAPTURE(dummy);
  111. B x;
  112. REQUIRE (x == 4);
  113. }
  114. TEST_CASE("#1027") {
  115. Y y{0};
  116. REQUIRE(y.v == 0);
  117. REQUIRE(0 == y.v);
  118. }
  119. // Comparison operators can return non-booleans.
  120. // This is unusual, but should be supported.
  121. TEST_CASE("#1147") {
  122. logic_t t1, t2;
  123. REQUIRE(t1 == t2);
  124. REQUIRE(t1 != t2);
  125. REQUIRE(t1 < t2);
  126. REQUIRE(t1 > t2);
  127. REQUIRE(t1 <= t2);
  128. REQUIRE(t1 >= t2);
  129. }
  130. // unsigned array
  131. TEST_CASE("#1238") {
  132. unsigned char uarr[] = "123";
  133. CAPTURE(uarr);
  134. signed char sarr[] = "456";
  135. CAPTURE(sarr);
  136. REQUIRE(std::memcmp(uarr, "123", sizeof(uarr)) == 0);
  137. REQUIRE(std::memcmp(sarr, "456", sizeof(sarr)) == 0);
  138. }
  139. TEST_CASE_METHOD((Fixture_1245<int, int>), "#1245", "[compilation]") {
  140. SUCCEED();
  141. }
  142. TEST_CASE("#1403", "[compilation]") {
  143. ::foo::helper_1403 h1, h2;
  144. REQUIRE(h1 == h2);
  145. }
  146. TEST_CASE("Optionally static assertions", "[compilation]") {
  147. STATIC_REQUIRE( std::is_void<void>::value );
  148. STATIC_REQUIRE_FALSE( std::is_void<int>::value );
  149. }
  150. TEST_CASE("#1548", "[compilation]") {
  151. using namespace bar;
  152. REQUIRE(std::is_same<TypeList<int>, TypeList<int>>::value);
  153. }
  154. // #925
  155. using signal_t = void (*) (void*);
  156. struct TestClass {
  157. signal_t testMethod_uponComplete_arg = nullptr;
  158. };
  159. namespace utility {
  160. inline static void synchronizing_callback( void * ) { }
  161. }
  162. #if defined (_MSC_VER)
  163. #pragma warning(push)
  164. // The function pointer comparison below triggers warning because of
  165. // calling conventions
  166. #pragma warning(disable:4244)
  167. #endif
  168. TEST_CASE("#925: comparing function pointer to function address failed to compile", "[!nonportable]" ) {
  169. TestClass test;
  170. REQUIRE(utility::synchronizing_callback != test.testMethod_uponComplete_arg);
  171. }
  172. #if defined (_MSC_VER)
  173. #pragma warning(pop)
  174. #endif
  175. TEST_CASE( "#1027: Bitfields can be captured" ) {
  176. struct Y {
  177. uint32_t v : 1;
  178. };
  179. Y y{ 0 };
  180. REQUIRE( y.v == 0 );
  181. REQUIRE( 0 == y.v );
  182. }
  183. TEST_CASE("Lambdas in assertions") {
  184. REQUIRE([]() { return true; }());
  185. }
  186. }} // namespace CompilationTests
  187. namespace {
  188. struct HasBitOperators {
  189. int value;
  190. friend HasBitOperators operator| (HasBitOperators lhs, HasBitOperators rhs) {
  191. return { lhs.value | rhs.value };
  192. }
  193. friend HasBitOperators operator& (HasBitOperators lhs, HasBitOperators rhs) {
  194. return { lhs.value & rhs.value };
  195. }
  196. friend HasBitOperators operator^ (HasBitOperators lhs, HasBitOperators rhs) {
  197. return { lhs.value ^ rhs.value };
  198. }
  199. explicit operator bool() const {
  200. return !!value;
  201. }
  202. friend std::ostream& operator<<(std::ostream& out, HasBitOperators val) {
  203. out << "Val: " << val.value;
  204. return out;
  205. }
  206. };
  207. }
  208. TEST_CASE("Assertion macros support bit operators and bool conversions", "[compilation][bitops]") {
  209. HasBitOperators lhs{ 1 }, rhs{ 2 };
  210. REQUIRE(lhs | rhs);
  211. REQUIRE_FALSE(lhs & rhs);
  212. REQUIRE(HasBitOperators{ 1 } & HasBitOperators{ 1 });
  213. REQUIRE(lhs ^ rhs);
  214. REQUIRE_FALSE(lhs ^ lhs);
  215. }