| 1 | |
| 2 | // Copyright Catch2 Authors |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE.txt or copy at |
| 5 | // https://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | |
| 9 | #include <helpers/type_with_lit_0_comparisons.hpp> |
| 10 | |
| 11 | #include <type_traits> |
| 12 | |
| 13 | // Setup for #1403 -- look for global overloads of operator << for classes |
| 14 | // in a different namespace. |
| 15 | #include <ostream> |
| 16 | |
| 17 | namespace foo { |
| 18 | struct helper_1403 { |
| 19 | bool operator==(helper_1403) const { return true; } |
| 20 | }; |
| 21 | } |
| 22 | |
| 23 | namespace bar { |
| 24 | template <typename... Ts> |
| 25 | struct TypeList {}; |
| 26 | } |
| 27 | |
| 28 | #ifdef __GNUC__ |
| 29 | #pragma GCC diagnostic ignored "-Wmissing-declarations" |
| 30 | #endif |
| 31 | static std::ostream& operator<<(std::ostream& out, foo::helper_1403 const&) { |
| 32 | return out << "[1403 helper]"; |
| 33 | } |
| 34 | /////////////////////////////// |
| 35 | |
| 36 | #include <catch2/catch_test_macros.hpp> |
| 37 | #include <catch2/matchers/catch_matchers_string.hpp> |
| 38 | |
| 39 | #include <cstring> |
| 40 | |
| 41 | // Comparison operators can return non-booleans. |
| 42 | // This is unusual, but should be supported. |
| 43 | struct logic_t { |
| 44 | logic_t operator< (logic_t) const { return {}; } |
| 45 | logic_t operator<=(logic_t) const { return {}; } |
| 46 | logic_t operator> (logic_t) const { return {}; } |
| 47 | logic_t operator>=(logic_t) const { return {}; } |
| 48 | logic_t operator==(logic_t) const { return {}; } |
| 49 | logic_t operator!=(logic_t) const { return {}; } |
| 50 | explicit operator bool() const { return true; } |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | static void throws_int(bool b) { |
| 55 | if (b) { |
| 56 | throw 1; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | template<typename T> |
| 61 | bool templated_tests(T t) { |
| 62 | int a = 3; |
| 63 | REQUIRE(a == t); |
| 64 | CHECK(a == t); |
| 65 | REQUIRE_THROWS(throws_int(true)); |
| 66 | CHECK_THROWS_AS(throws_int(true), int); |
| 67 | REQUIRE_NOTHROW(throws_int(false)); |
| 68 | REQUIRE_THAT("aaa", Catch::Matchers::EndsWith("aaa")); |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | struct A {}; |
| 73 | |
| 74 | static std::ostream &operator<<(std::ostream &o, const A &) { return o << 0; } |
| 75 | |
| 76 | struct B : private A { |
| 77 | bool operator==(int) const { return true; } |
| 78 | }; |
| 79 | |
| 80 | #ifdef __clang__ |
| 81 | #pragma clang diagnostic push |
| 82 | #pragma clang diagnostic ignored "-Wunused-function" |
| 83 | #endif |
| 84 | #ifdef __GNUC__ |
| 85 | // Note that because -~GCC~-, this warning cannot be silenced temporarily, by pushing diagnostic stack... |
| 86 | // Luckily it is firing in test files and thus can be silenced for the whole file, without losing much. |
| 87 | #pragma GCC diagnostic ignored "-Wunused-function" |
| 88 | #endif |
| 89 | |
| 90 | B f(); |
| 91 | |
| 92 | std::ostream g(); |
| 93 | |
| 94 | #ifdef __clang__ |
| 95 | #pragma clang diagnostic pop |
| 96 | #endif |
| 97 | |
| 98 | template <typename, typename> |
| 99 | struct Fixture_1245 {}; |
| 100 | |
| 101 | // This is a minimal example for an issue we have found in 1.7.0 |
| 102 | struct dummy_809 { |
| 103 | int i; |
| 104 | }; |
| 105 | |
| 106 | template<typename T> |
| 107 | bool operator==(const T& val, dummy_809 f) { |
| 108 | return val == f.i; |
| 109 | } |
| 110 | |
| 111 | TEST_CASE("#809") { |
| 112 | dummy_809 f; |
| 113 | f.i = 42; |
| 114 | REQUIRE(42 == f); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | // ------------------------------------------------------------------ |
| 119 | // Changes to REQUIRE_THROWS_AS made it stop working in a template in |
| 120 | // an unfixable way (as long as C++03 compatibility is being kept). |
| 121 | // To prevent these from happening in the future, this needs to compile |
| 122 | |
| 123 | TEST_CASE("#833") { |
| 124 | REQUIRE(templated_tests<int>(3)); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | // Test containing example where original stream insertable check breaks compilation |
| 129 | TEST_CASE("#872") { |
| 130 | A dummy; |
| 131 | CAPTURE(dummy); |
| 132 | B x; |
| 133 | REQUIRE (x == 4); |
| 134 | } |
| 135 | |
| 136 | TEST_CASE("#1027: Bitfields can be captured") { |
| 137 | struct Y { |
| 138 | uint32_t v : 1; |
| 139 | }; |
| 140 | Y y{ 0 }; |
| 141 | REQUIRE(y.v == 0); |
| 142 | REQUIRE(0 == y.v); |
| 143 | } |
| 144 | |
| 145 | // Comparison operators can return non-booleans. |
| 146 | // This is unusual, but should be supported. |
| 147 | TEST_CASE("#1147") { |
| 148 | logic_t t1, t2; |
| 149 | REQUIRE(t1 == t2); |
| 150 | REQUIRE(t1 != t2); |
| 151 | REQUIRE(t1 < t2); |
| 152 | REQUIRE(t1 > t2); |
| 153 | REQUIRE(t1 <= t2); |
| 154 | REQUIRE(t1 >= t2); |
| 155 | } |
| 156 | |
| 157 | // unsigned array |
| 158 | TEST_CASE("#1238") { |
| 159 | unsigned char uarr[] = "123"; |
| 160 | CAPTURE(uarr); |
| 161 | signed char sarr[] = "456"; |
| 162 | CAPTURE(sarr); |
| 163 | |
| 164 | REQUIRE(std::memcmp(uarr, "123", sizeof(uarr)) == 0); |
| 165 | REQUIRE(std::memcmp(sarr, "456", sizeof(sarr)) == 0); |
| 166 | } |
| 167 | |
| 168 | TEST_CASE_METHOD((Fixture_1245<int, int>), "#1245", "[compilation]") { |
| 169 | SUCCEED(); |
| 170 | } |
| 171 | |
| 172 | TEST_CASE("#1403", "[compilation]") { |
| 173 | ::foo::helper_1403 h1, h2; |
| 174 | REQUIRE(h1 == h2); |
| 175 | } |
| 176 | |
| 177 | TEST_CASE("Optionally static assertions", "[compilation]") { |
| 178 | STATIC_REQUIRE( std::is_void<void>::value ); |
| 179 | STATIC_REQUIRE_FALSE( std::is_void<int>::value ); |
| 180 | STATIC_CHECK( std::is_void<void>::value ); |
| 181 | STATIC_CHECK_FALSE( std::is_void<int>::value ); |
| 182 | } |
| 183 | |
| 184 | TEST_CASE("#1548", "[compilation]") { |
| 185 | using namespace bar; |
| 186 | REQUIRE(std::is_same<TypeList<int>, TypeList<int>>::value); |
| 187 | } |
| 188 | |
| 189 | // #925 |
| 190 | using signal_t = void (*) (void*); |
| 191 | |
| 192 | struct TestClass { |
| 193 | signal_t testMethod_uponComplete_arg = nullptr; |
| 194 | }; |
| 195 | |
| 196 | namespace utility { |
| 197 | inline static void synchronizing_callback( void * ) { } |
| 198 | } |
| 199 | |
| 200 | #if defined (_MSC_VER) |
| 201 | #pragma warning(push) |
| 202 | // The function pointer comparison below triggers warning because of |
| 203 | // calling conventions |
| 204 | #pragma warning(disable:4244) |
| 205 | #endif |
| 206 | TEST_CASE("#925: comparing function pointer to function address failed to compile", "[!nonportable]" ) { |
| 207 | TestClass test; |
| 208 | REQUIRE(utility::synchronizing_callback != test.testMethod_uponComplete_arg); |
| 209 | } |
| 210 | #if defined (_MSC_VER) |
| 211 | #pragma warning(pop) |
| 212 | #endif |
| 213 | |
| 214 | TEST_CASE( "#1319: Sections can have description (even if it is not saved", |
| 215 | "[compilation]" ) { |
| 216 | SECTION( "SectionName", "This is a long form section description" ) { |
| 217 | SUCCEED(); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | TEST_CASE("Lambdas in assertions") { |
| 222 | REQUIRE([]() { return true; }()); |
| 223 | } |
| 224 | |
| 225 | namespace { |
| 226 | struct HasBitOperators { |
| 227 | int value; |
| 228 | |
| 229 | friend HasBitOperators operator| (HasBitOperators lhs, HasBitOperators rhs) { |
| 230 | return { lhs.value | rhs.value }; |
| 231 | } |
| 232 | friend HasBitOperators operator& (HasBitOperators lhs, HasBitOperators rhs) { |
| 233 | return { lhs.value & rhs.value }; |
| 234 | } |
| 235 | friend HasBitOperators operator^ (HasBitOperators lhs, HasBitOperators rhs) { |
| 236 | return { lhs.value ^ rhs.value }; |
| 237 | } |
| 238 | explicit operator bool() const { |
| 239 | return !!value; |
| 240 | } |
| 241 | |
| 242 | friend std::ostream& operator<<(std::ostream& out, HasBitOperators val) { |
| 243 | out << "Val: " << val.value; |
| 244 | return out; |
| 245 | } |
| 246 | }; |
| 247 | } |
| 248 | |
| 249 | TEST_CASE("Assertion macros support bit operators and bool conversions", "[compilation][bitops]") { |
| 250 | HasBitOperators lhs{ 1 }, rhs{ 2 }; |
| 251 | REQUIRE(lhs | rhs); |
| 252 | REQUIRE_FALSE(lhs & rhs); |
| 253 | REQUIRE(HasBitOperators{ 1 } & HasBitOperators{ 1 }); |
| 254 | REQUIRE(lhs ^ rhs); |
| 255 | REQUIRE_FALSE(lhs ^ lhs); |
| 256 | } |
| 257 | |
| 258 | namespace { |
| 259 | struct ImmovableType { |
| 260 | ImmovableType() = default; |
| 261 | |
| 262 | ImmovableType(ImmovableType const&) = delete; |
| 263 | ImmovableType& operator=(ImmovableType const&) = delete; |
| 264 | ImmovableType(ImmovableType&&) = delete; |
| 265 | ImmovableType& operator=(ImmovableType&&) = delete; |
| 266 | |
| 267 | friend bool operator==(ImmovableType const&, ImmovableType const&) { |
| 268 | return true; |
| 269 | } |
| 270 | }; |
| 271 | } |
| 272 | |
| 273 | TEST_CASE("Immovable types are supported in basic assertions", "[compilation][.approvals]") { |
| 274 | REQUIRE(ImmovableType{} == ImmovableType{}); |
| 275 | } |
| 276 | |
| 277 | namespace adl { |
| 278 | |
| 279 | struct always_true { |
| 280 | explicit operator bool() const { return true; } |
| 281 | }; |
| 282 | |
| 283 | #define COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(op) \ |
| 284 | template <class T, class U> \ |
| 285 | auto operator op (T&&, U&&) { \ |
| 286 | return always_true{}; \ |
| 287 | } |
| 288 | |
| 289 | COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(==) |
| 290 | COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(!=) |
| 291 | COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(<) |
| 292 | COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(>) |
| 293 | COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(<=) |
| 294 | COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(>=) |
| 295 | COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(|) |
| 296 | COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(&) |
| 297 | COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR(^) |
| 298 | |
| 299 | #undef COMPILATION_TEST_DEFINE_UNIVERSAL_OPERATOR |
| 300 | |
| 301 | } |
| 302 | |
| 303 | TEST_CASE("ADL universal operators don't hijack expression deconstruction", "[compilation][.approvals]") { |
| 304 | REQUIRE(adl::always_true{}); |
| 305 | REQUIRE(0 == adl::always_true{}); |
| 306 | REQUIRE(0 != adl::always_true{}); |
| 307 | REQUIRE(0 < adl::always_true{}); |
| 308 | REQUIRE(0 > adl::always_true{}); |
| 309 | REQUIRE(0 <= adl::always_true{}); |
| 310 | REQUIRE(0 >= adl::always_true{}); |
| 311 | REQUIRE(0 | adl::always_true{}); |
| 312 | REQUIRE(0 & adl::always_true{}); |
| 313 | REQUIRE(0 ^ adl::always_true{}); |
| 314 | } |
| 315 | |
| 316 | TEST_CASE( "#2555 - types that can only be compared with 0 literal (not int/long) are supported", "[compilation][approvals]" ) { |
| 317 | REQUIRE( TypeWithLit0Comparisons{} < 0 ); |
| 318 | REQUIRE_FALSE( 0 < TypeWithLit0Comparisons{} ); |
| 319 | REQUIRE( TypeWithLit0Comparisons{} <= 0 ); |
| 320 | REQUIRE_FALSE( 0 > TypeWithLit0Comparisons{} ); |
| 321 | |
| 322 | REQUIRE( TypeWithLit0Comparisons{} > 0 ); |
| 323 | REQUIRE_FALSE( 0 > TypeWithLit0Comparisons{} ); |
| 324 | REQUIRE( TypeWithLit0Comparisons{} >= 0 ); |
| 325 | REQUIRE_FALSE( 0 >= TypeWithLit0Comparisons{} ); |
| 326 | |
| 327 | REQUIRE( TypeWithLit0Comparisons{} == 0 ); |
| 328 | REQUIRE_FALSE( 0 == TypeWithLit0Comparisons{} ); |
| 329 | REQUIRE( TypeWithLit0Comparisons{} != 0 ); |
| 330 | REQUIRE_FALSE( 0 != TypeWithLit0Comparisons{} ); |
| 331 | } |
| 332 | |
| 333 | namespace { |
| 334 | struct MultipleImplicitConstructors { |
| 335 | MultipleImplicitConstructors( double ) {} |
| 336 | MultipleImplicitConstructors( int64_t ) {} |
| 337 | bool operator==( MultipleImplicitConstructors ) const { return true; } |
| 338 | bool operator!=( MultipleImplicitConstructors ) const { return true; } |
| 339 | bool operator<( MultipleImplicitConstructors ) const { return true; } |
| 340 | bool operator<=( MultipleImplicitConstructors ) const { return true; } |
| 341 | bool operator>( MultipleImplicitConstructors ) const { return true; } |
| 342 | bool operator>=( MultipleImplicitConstructors ) const { return true; } |
| 343 | }; |
| 344 | } |
| 345 | TEST_CASE("#2571 - tests compile types that have multiple implicit constructors from lit 0", |
| 346 | "[compilation][approvals]") { |
| 347 | MultipleImplicitConstructors mic1( 0.0 ); |
| 348 | MultipleImplicitConstructors mic2( 0.0 ); |
| 349 | REQUIRE( mic1 == mic2 ); |
| 350 | REQUIRE( mic1 != mic2 ); |
| 351 | REQUIRE( mic1 < mic2 ); |
| 352 | REQUIRE( mic1 <= mic2 ); |
| 353 | REQUIRE( mic1 > mic2 ); |
| 354 | REQUIRE( mic1 >= mic2 ); |
| 355 | } |