Compilation.tests.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 <type_traits>
  7. // Setup for #1403 -- look for global overloads of operator << for classes
  8. // in a different namespace.
  9. #include <ostream>
  10. namespace foo {
  11. struct helper_1403 {
  12. bool operator==(helper_1403) const { return true; }
  13. };
  14. }
  15. namespace bar {
  16. template <typename... Ts>
  17. struct TypeList {};
  18. }
  19. #ifdef __GNUC__
  20. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  21. #endif
  22. static std::ostream& operator<<(std::ostream& out, foo::helper_1403 const&) {
  23. return out << "[1403 helper]";
  24. }
  25. ///////////////////////////////
  26. #include <catch2/catch_test_macros.hpp>
  27. #include <catch2/matchers/catch_matchers_string.hpp>
  28. #include <cstring>
  29. // Comparison operators can return non-booleans.
  30. // This is unusual, but should be supported.
  31. struct logic_t {
  32. logic_t operator< (logic_t) const { return {}; }
  33. logic_t operator<=(logic_t) const { return {}; }
  34. logic_t operator> (logic_t) const { return {}; }
  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. explicit operator bool() const { return true; }
  39. };
  40. static void throws_int(bool b) {
  41. if (b) {
  42. throw 1;
  43. }
  44. }
  45. template<typename T>
  46. bool templated_tests(T t) {
  47. int a = 3;
  48. REQUIRE(a == t);
  49. CHECK(a == t);
  50. REQUIRE_THROWS(throws_int(true));
  51. CHECK_THROWS_AS(throws_int(true), int);
  52. REQUIRE_NOTHROW(throws_int(false));
  53. REQUIRE_THAT("aaa", Catch::Matchers::EndsWith("aaa"));
  54. return true;
  55. }
  56. struct A {};
  57. static std::ostream &operator<<(std::ostream &o, const A &) { return o << 0; }
  58. struct B : private A {
  59. bool operator==(int) const { return true; }
  60. };
  61. #ifdef __clang__
  62. #pragma clang diagnostic push
  63. #pragma clang diagnostic ignored "-Wunused-function"
  64. #endif
  65. #ifdef __GNUC__
  66. // Note that because -~GCC~-, this warning cannot be silenced temporarily, by pushing diagnostic stack...
  67. // Luckily it is firing in test files and thus can be silenced for the whole file, without losing much.
  68. #pragma GCC diagnostic ignored "-Wunused-function"
  69. #endif
  70. B f();
  71. std::ostream g();
  72. #ifdef __clang__
  73. #pragma clang diagnostic pop
  74. #endif
  75. template <typename, typename>
  76. struct Fixture_1245 {};
  77. // This is a minimal example for an issue we have found in 1.7.0
  78. struct dummy_809 {
  79. int i;
  80. };
  81. template<typename T>
  82. bool operator==(const T& val, dummy_809 f) {
  83. return val == f.i;
  84. }
  85. TEST_CASE("#809") {
  86. dummy_809 f;
  87. f.i = 42;
  88. REQUIRE(42 == f);
  89. }
  90. // ------------------------------------------------------------------
  91. // Changes to REQUIRE_THROWS_AS made it stop working in a template in
  92. // an unfixable way (as long as C++03 compatibility is being kept).
  93. // To prevent these from happening in the future, this needs to compile
  94. TEST_CASE("#833") {
  95. REQUIRE(templated_tests<int>(3));
  96. }
  97. // Test containing example where original stream insertable check breaks compilation
  98. TEST_CASE("#872") {
  99. A dummy;
  100. CAPTURE(dummy);
  101. B x;
  102. REQUIRE (x == 4);
  103. }
  104. TEST_CASE("#1027: Bitfields can be captured") {
  105. struct Y {
  106. uint32_t v : 1;
  107. };
  108. Y y{ 0 };
  109. REQUIRE(y.v == 0);
  110. REQUIRE(0 == y.v);
  111. }
  112. // Comparison operators can return non-booleans.
  113. // This is unusual, but should be supported.
  114. TEST_CASE("#1147") {
  115. logic_t t1, t2;
  116. REQUIRE(t1 == t2);
  117. REQUIRE(t1 != t2);
  118. REQUIRE(t1 < t2);
  119. REQUIRE(t1 > t2);
  120. REQUIRE(t1 <= t2);
  121. REQUIRE(t1 >= t2);
  122. }
  123. // unsigned array
  124. TEST_CASE("#1238") {
  125. unsigned char uarr[] = "123";
  126. CAPTURE(uarr);
  127. signed char sarr[] = "456";
  128. CAPTURE(sarr);
  129. REQUIRE(std::memcmp(uarr, "123", sizeof(uarr)) == 0);
  130. REQUIRE(std::memcmp(sarr, "456", sizeof(sarr)) == 0);
  131. }
  132. TEST_CASE_METHOD((Fixture_1245<int, int>), "#1245", "[compilation]") {
  133. SUCCEED();
  134. }
  135. TEST_CASE("#1403", "[compilation]") {
  136. ::foo::helper_1403 h1, h2;
  137. REQUIRE(h1 == h2);
  138. }
  139. TEST_CASE("Optionally static assertions", "[compilation]") {
  140. STATIC_REQUIRE( std::is_void<void>::value );
  141. STATIC_REQUIRE_FALSE( std::is_void<int>::value );
  142. STATIC_CHECK( std::is_void<void>::value );
  143. STATIC_CHECK_FALSE( std::is_void<int>::value );
  144. }
  145. TEST_CASE("#1548", "[compilation]") {
  146. using namespace bar;
  147. REQUIRE(std::is_same<TypeList<int>, TypeList<int>>::value);
  148. }
  149. // #925
  150. using signal_t = void (*) (void*);
  151. struct TestClass {
  152. signal_t testMethod_uponComplete_arg = nullptr;
  153. };
  154. namespace utility {
  155. inline static void synchronizing_callback( void * ) { }
  156. }
  157. #if defined (_MSC_VER)
  158. #pragma warning(push)
  159. // The function pointer comparison below triggers warning because of
  160. // calling conventions
  161. #pragma warning(disable:4244)
  162. #endif
  163. TEST_CASE("#925: comparing function pointer to function address failed to compile", "[!nonportable]" ) {
  164. TestClass test;
  165. REQUIRE(utility::synchronizing_callback != test.testMethod_uponComplete_arg);
  166. }
  167. #if defined (_MSC_VER)
  168. #pragma warning(pop)
  169. #endif
  170. TEST_CASE( "#1319: Sections can have description (even if it is not saved",
  171. "[compilation]" ) {
  172. SECTION( "SectionName", "This is a long form section description" ) {
  173. SUCCEED();
  174. }
  175. }
  176. TEST_CASE("Lambdas in assertions") {
  177. REQUIRE([]() { return true; }());
  178. }
  179. namespace {
  180. struct HasBitOperators {
  181. int value;
  182. friend HasBitOperators operator| (HasBitOperators lhs, HasBitOperators rhs) {
  183. return { lhs.value | rhs.value };
  184. }
  185. friend HasBitOperators operator& (HasBitOperators lhs, HasBitOperators rhs) {
  186. return { lhs.value & rhs.value };
  187. }
  188. friend HasBitOperators operator^ (HasBitOperators lhs, HasBitOperators rhs) {
  189. return { lhs.value ^ rhs.value };
  190. }
  191. explicit operator bool() const {
  192. return !!value;
  193. }
  194. friend std::ostream& operator<<(std::ostream& out, HasBitOperators val) {
  195. out << "Val: " << val.value;
  196. return out;
  197. }
  198. };
  199. }
  200. TEST_CASE("Assertion macros support bit operators and bool conversions", "[compilation][bitops]") {
  201. HasBitOperators lhs{ 1 }, rhs{ 2 };
  202. REQUIRE(lhs | rhs);
  203. REQUIRE_FALSE(lhs & rhs);
  204. REQUIRE(HasBitOperators{ 1 } & HasBitOperators{ 1 });
  205. REQUIRE(lhs ^ rhs);
  206. REQUIRE_FALSE(lhs ^ lhs);
  207. }
  208. namespace {
  209. struct ImmovableType {
  210. ImmovableType() = default;
  211. ImmovableType(ImmovableType const&) = delete;
  212. ImmovableType& operator=(ImmovableType const&) = delete;
  213. ImmovableType(ImmovableType&&) = delete;
  214. ImmovableType& operator=(ImmovableType&&) = delete;
  215. friend bool operator==(ImmovableType const&, ImmovableType const&) {
  216. return true;
  217. }
  218. };
  219. }
  220. TEST_CASE("Immovable types are supported in basic assertions", "[compilation][.approvals]") {
  221. REQUIRE(ImmovableType{} == ImmovableType{});
  222. }
  223. namespace adl {
  224. struct always_true {
  225. explicit operator bool() const { return true; }
  226. };
  227. #define COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(op) \
  228. template <class T, class U> \
  229. auto operator op (T&&, U&&) { \
  230. return always_true{}; \
  231. }
  232. COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(==)
  233. COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(!=)
  234. COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(<)
  235. COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(>)
  236. COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(<=)
  237. COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(>=)
  238. COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(|)
  239. COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(&)
  240. COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(^)
  241. #undef COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR
  242. }
  243. TEST_CASE("ADL universal operators don't hijack expression deconstruction", "[compilation][.approvals]") {
  244. REQUIRE(adl::always_true{});
  245. REQUIRE(0 == adl::always_true{});
  246. REQUIRE(0 != adl::always_true{});
  247. REQUIRE(0 < adl::always_true{});
  248. REQUIRE(0 > adl::always_true{});
  249. REQUIRE(0 <= adl::always_true{});
  250. REQUIRE(0 >= adl::always_true{});
  251. REQUIRE(0 | adl::always_true{});
  252. REQUIRE(0 & adl::always_true{});
  253. REQUIRE(0 ^ adl::always_true{});
  254. }