Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 1 | /* |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 2 | * Created by Phil on 09/11/2010. |
| 3 | * Copyright 2010 Two Blue Cubes Ltd. All rights reserved. |
| 4 | * |
| 5 | * Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Phil Nash | cda2149 | 2012-08-16 18:47:41 +0100 | [diff] [blame] | 9 | #ifdef __clang__ |
Phil Nash | a695eb9 | 2012-08-13 07:46:10 +0100 | [diff] [blame] | 10 | #pragma clang diagnostic ignored "-Wpadded" |
Phil Nash | cda2149 | 2012-08-16 18:47:41 +0100 | [diff] [blame] | 11 | #endif |
Phil Nash | a695eb9 | 2012-08-13 07:46:10 +0100 | [diff] [blame] | 12 | |
Phil Nash | 823ea3e | 2011-04-26 08:32:40 +0100 | [diff] [blame] | 13 | #include "catch.hpp" |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 14 | |
| 15 | namespace Catch |
| 16 | { |
| 17 | template<> |
| 18 | std::string toString<std::pair<int, int> >( const std::pair<int, int>& value ) |
| 19 | { |
| 20 | std::ostringstream oss; |
| 21 | oss << "std::pair( " << value.first << ", " << value.second << " )"; |
| 22 | return oss.str(); |
| 23 | |
| 24 | } |
| 25 | } |
| 26 | |
Phil Nash | e0e7477 | 2011-03-10 14:09:32 +0000 | [diff] [blame] | 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | TEST_CASE |
| 29 | ( |
| 30 | "./succeeding/Tricky/std::pair", |
| 31 | "Parsing a std::pair" |
| 32 | ) |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 33 | { |
| 34 | std::pair<int, int> aNicePair( 1, 2 ); |
| 35 | |
| 36 | // !TBD: would be nice if this could compile without the extra parentheses |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 37 | REQUIRE( (std::pair<int, int>( 1, 2 )) == aNicePair ); |
Phil Nash | d802600 | 2010-11-09 23:24:00 +0000 | [diff] [blame] | 38 | |
Phil Nash | 3d0fed7 | 2010-11-10 19:18:46 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Phil Nash | e0e7477 | 2011-03-10 14:09:32 +0000 | [diff] [blame] | 41 | /////////////////////////////////////////////////////////////////////////////// |
| 42 | TEST_CASE |
| 43 | ( |
Phil Nash | b826243 | 2011-03-11 19:46:18 +0000 | [diff] [blame] | 44 | "./inprogress/failing/Tricky/trailing expression", |
| 45 | "Where the is more to the expression after the RHS" |
Phil Nash | e0e7477 | 2011-03-10 14:09:32 +0000 | [diff] [blame] | 46 | ) |
Phil Nash | 3d0fed7 | 2010-11-10 19:18:46 +0000 | [diff] [blame] | 47 | { |
Phil Nash | 2e44486 | 2011-03-10 19:18:14 +0000 | [diff] [blame] | 48 | /* |
Phil Nash | 3d0fed7 | 2010-11-10 19:18:46 +0000 | [diff] [blame] | 49 | int a = 1; |
| 50 | int b = 2; |
| 51 | |
Phil Nash | 48a7022 | 2010-11-11 07:21:57 +0000 | [diff] [blame] | 52 | // This only captures part of the expression, but issues a warning about the rest |
Phil Nash | a2d2095 | 2010-12-14 09:00:09 +0000 | [diff] [blame] | 53 | REQUIRE( a == 2 || b == 2 ); |
Phil Nash | 2e44486 | 2011-03-10 19:18:14 +0000 | [diff] [blame] | 54 | */ |
Phil Nash | b826243 | 2011-03-11 19:46:18 +0000 | [diff] [blame] | 55 | WARN( "Uncomment the code in this test to check that it gives a sensible compiler error" ); |
Phil Nash | 2e44486 | 2011-03-10 19:18:14 +0000 | [diff] [blame] | 56 | } |
| 57 | /////////////////////////////////////////////////////////////////////////////// |
| 58 | TEST_CASE |
| 59 | ( |
Phil Nash | b826243 | 2011-03-11 19:46:18 +0000 | [diff] [blame] | 60 | "./inprogress/failing/Tricky/compound lhs", |
Phil Nash | 2e44486 | 2011-03-10 19:18:14 +0000 | [diff] [blame] | 61 | "Where the LHS is not a simple value" |
| 62 | ) |
| 63 | { |
| 64 | /* |
| 65 | int a = 1; |
| 66 | int b = 2; |
| 67 | |
| 68 | // This only captures part of the expression, but issues a warning about the rest |
| 69 | REQUIRE( a+1 == b-1 ); |
| 70 | */ |
Phil Nash | b826243 | 2011-03-11 19:46:18 +0000 | [diff] [blame] | 71 | WARN( "Uncomment the code in this test to check that it gives a sensible compiler error" ); |
Phil Nash | 92f2d32 | 2010-11-11 20:37:46 +0000 | [diff] [blame] | 72 | } |
Phil Nash | e6b2b0e | 2010-11-29 19:40:44 +0000 | [diff] [blame] | 73 | |
Phil Nash | 7d1ce5f | 2010-11-16 19:30:41 +0000 | [diff] [blame] | 74 | struct Opaque |
| 75 | { |
| 76 | int val; |
Phil Nash | e0e7477 | 2011-03-10 14:09:32 +0000 | [diff] [blame] | 77 | bool operator ==( const Opaque& o ) const |
Phil Nash | 7d1ce5f | 2010-11-16 19:30:41 +0000 | [diff] [blame] | 78 | { |
Phil Nash | 5f43a43 | 2010-11-16 19:48:05 +0000 | [diff] [blame] | 79 | return val == o.val; |
Phil Nash | 7d1ce5f | 2010-11-16 19:30:41 +0000 | [diff] [blame] | 80 | } |
| 81 | }; |
| 82 | |
Phil Nash | e0e7477 | 2011-03-10 14:09:32 +0000 | [diff] [blame] | 83 | /////////////////////////////////////////////////////////////////////////////// |
| 84 | TEST_CASE |
| 85 | ( |
| 86 | "./failing/Tricky/non streamable type", |
| 87 | "A failing expression with a non streamable type is still captured" |
| 88 | ) |
Phil Nash | 7d1ce5f | 2010-11-16 19:30:41 +0000 | [diff] [blame] | 89 | { |
| 90 | |
| 91 | Opaque o1, o2; |
| 92 | o1.val = 7; |
| 93 | o2.val = 8; |
| 94 | |
| 95 | CHECK( &o1 == &o2 ); |
| 96 | CHECK( o1 == o2 ); |
Phil Nash | ceab2a6 | 2010-11-29 19:48:37 +0000 | [diff] [blame] | 97 | } |
Phil Nash | e0e7477 | 2011-03-10 14:09:32 +0000 | [diff] [blame] | 98 | |
| 99 | /////////////////////////////////////////////////////////////////////////////// |
| 100 | TEST_CASE |
| 101 | ( |
| 102 | "./failing/string literals", |
| 103 | "string literals of different sizes can be compared" |
| 104 | ) |
| 105 | { |
| 106 | REQUIRE( std::string( "first" ) == "second" ); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | /////////////////////////////////////////////////////////////////////////////// |
| 111 | TEST_CASE |
| 112 | ( |
| 113 | "./succeeding/side-effects", |
| 114 | "An expression with side-effects should only be evaluated once" |
| 115 | ) |
| 116 | { |
| 117 | int i = 7; |
| 118 | |
| 119 | REQUIRE( i++ == 7 ); |
| 120 | REQUIRE( i++ == 8 ); |
| 121 | |
| 122 | } |
Phil Nash | 46b4909 | 2011-04-06 22:26:16 +0100 | [diff] [blame] | 123 | |
| 124 | namespace A { |
| 125 | struct X |
| 126 | { |
| 127 | X() : a(4), b(2), c(7) {} |
| 128 | X(int v) : a(v), b(2), c(7) {} |
| 129 | int a; |
| 130 | int b; |
| 131 | int c; |
| 132 | }; |
| 133 | } |
| 134 | |
| 135 | namespace B { |
| 136 | struct Y |
| 137 | { |
| 138 | Y() : a(4), b(2), c(7) {} |
| 139 | Y(int v) : a(v), b(2), c(7) {} |
| 140 | int a; |
| 141 | int b; |
| 142 | int c; |
| 143 | }; |
| 144 | } |
Phil Nash | 458e5ee | 2011-12-27 10:59:41 +0000 | [diff] [blame] | 145 | |
| 146 | inline bool operator==(const A::X& lhs, const B::Y& rhs) |
Phil Nash | 46b4909 | 2011-04-06 22:26:16 +0100 | [diff] [blame] | 147 | { |
| 148 | return (lhs.a == rhs.a); |
| 149 | } |
| 150 | |
Phil Nash | 458e5ee | 2011-12-27 10:59:41 +0000 | [diff] [blame] | 151 | inline bool operator==(const B::Y& lhs, const A::X& rhs) |
Phil Nash | 46b4909 | 2011-04-06 22:26:16 +0100 | [diff] [blame] | 152 | { |
| 153 | return (lhs.a == rhs.a); |
| 154 | } |
| 155 | |
Phil Nash | 458e5ee | 2011-12-27 10:59:41 +0000 | [diff] [blame] | 156 | |
Phil Nash | 46b4909 | 2011-04-06 22:26:16 +0100 | [diff] [blame] | 157 | /////////////////////////////////////////////////////////////////////////////// |
Phil Nash | 458e5ee | 2011-12-27 10:59:41 +0000 | [diff] [blame] | 158 | /* This, currently, does not compile with LLVM |
Phil Nash | 46b4909 | 2011-04-06 22:26:16 +0100 | [diff] [blame] | 159 | TEST_CASE |
| 160 | ( |
| 161 | "./succeeding/koenig", |
| 162 | "Operators at different namespace levels not hijacked by Koenig lookup" |
| 163 | ) |
| 164 | { |
| 165 | A::X x; |
| 166 | B::Y y; |
| 167 | REQUIRE( x == y ); |
| 168 | } |
Phil Nash | 458e5ee | 2011-12-27 10:59:41 +0000 | [diff] [blame] | 169 | */ |
Phil Nash | 46b4909 | 2011-04-06 22:26:16 +0100 | [diff] [blame] | 170 | |
| 171 | namespace ObjectWithConversions |
| 172 | { |
| 173 | struct Object |
| 174 | { |
| 175 | operator unsigned int() {return 0xc0000000;} |
| 176 | }; |
| 177 | |
| 178 | /////////////////////////////////////////////////////////////////////////////// |
| 179 | TEST_CASE |
| 180 | ( |
| 181 | "./succeeding/koenig", |
| 182 | "Operators at different namespace levels not hijacked by Koenig lookup" |
| 183 | ) |
Phil Nash | 0f2e5d6 | 2011-04-11 08:32:55 +0100 | [diff] [blame] | 184 | { |
| 185 | Object o; |
| 186 | REQUIRE(0xc0000000 == o ); |
Phil Nash | 46b4909 | 2011-04-06 22:26:16 +0100 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | namespace ObjectWithNonConstEqualityOperator |
| 191 | { |
| 192 | struct Test |
| 193 | { |
| 194 | Test( unsigned int v ) |
| 195 | : m_value(v) |
| 196 | {} |
| 197 | |
| 198 | bool operator==( const Test&rhs ) |
| 199 | { |
| 200 | return (m_value == rhs.m_value); |
| 201 | } |
| 202 | bool operator==( const Test&rhs ) const |
| 203 | { |
| 204 | return (m_value != rhs.m_value); |
| 205 | } |
| 206 | unsigned int m_value; |
| 207 | }; |
| 208 | |
| 209 | TEST_CASE("./succeeding/non-const==", "Demonstrate that a non-const == is not used") |
| 210 | { |
| 211 | Test t( 1 ); |
Phil Nash | d06dced | 2012-05-09 08:17:51 +0100 | [diff] [blame] | 212 | REQUIRE( t == 1u ); |
Phil Nash | 46b4909 | 2011-04-06 22:26:16 +0100 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | namespace EnumBitFieldTests |
| 217 | { |
| 218 | enum Bits {bit0 = 0x0001, bit1 = 0x0002, bit2 = 0x0004, bit3 = 0x0008, bit1and2 = 0x0006, |
| 219 | bit30 = 0x40000000, bit31 = 0x80000000, |
| 220 | bit30and31 = 0xc0000000}; |
| 221 | |
| 222 | TEST_CASE("./succeeding/enum/bits", "Test enum bit values") |
| 223 | { |
| 224 | REQUIRE( 0xc0000000 == bit30and31 ); |
| 225 | } |
| 226 | } |
Phil Nash | 0f2e5d6 | 2011-04-11 08:32:55 +0100 | [diff] [blame] | 227 | |
| 228 | struct Obj |
| 229 | { |
| 230 | Obj():prop(&p){} |
| 231 | |
| 232 | int p; |
| 233 | int* prop; |
| 234 | }; |
| 235 | |
| 236 | TEST_CASE("./succeeding/boolean member", "") |
| 237 | { |
| 238 | Obj obj; |
Phil Nash | 823ea3e | 2011-04-26 08:32:40 +0100 | [diff] [blame] | 239 | REQUIRE( obj.prop != NULL ); |
Phil Nash | 0f2e5d6 | 2011-04-11 08:32:55 +0100 | [diff] [blame] | 240 | } |
Phil Nash | 4d0a8d9 | 2011-08-09 08:18:27 +0100 | [diff] [blame] | 241 | |
| 242 | // Tests for a problem submitted by Ralph McArdell |
| 243 | // |
| 244 | // The static bool value should not need to be defined outside the |
| 245 | // struct it is declared in - but when evaluating it in a deduced |
| 246 | // context it appears to require the extra definition. |
| 247 | // The issue was fixed by adding bool overloads to bypass the |
| 248 | // templates that were deduce it. |
| 249 | template <bool B> |
| 250 | struct is_true |
| 251 | { |
| 252 | static const bool value = B; |
| 253 | }; |
| 254 | |
| 255 | TEST_CASE( "./succeeding/unimplemented static bool", "static bools can be evaluated" ) |
| 256 | { |
| 257 | SECTION("compare to true","") |
| 258 | { |
| 259 | REQUIRE( is_true<true>::value == true ); |
| 260 | REQUIRE( true == is_true<true>::value ); |
| 261 | } |
| 262 | SECTION("compare to false","") |
| 263 | { |
| 264 | REQUIRE( is_true<false>::value == false ); |
| 265 | REQUIRE( false == is_true<false>::value ); |
| 266 | } |
| 267 | |
| 268 | SECTION("negation", "") |
| 269 | { |
| 270 | REQUIRE( !is_true<false>::value ); |
| 271 | } |
| 272 | |
| 273 | SECTION("double negation","") |
| 274 | { |
| 275 | REQUIRE( !!is_true<true>::value ); |
| 276 | } |
| 277 | |
| 278 | SECTION("direct","") |
| 279 | { |
| 280 | REQUIRE( is_true<true>::value ); |
| 281 | REQUIRE_FALSE( is_true<false>::value ); |
| 282 | } |
| 283 | } |
Phil Nash | 3fd7dc0 | 2012-02-09 08:34:01 +0000 | [diff] [blame] | 284 | |
| 285 | // Uncomment these tests to produce an error at test registration time |
| 286 | /* |
| 287 | TEST_CASE( "./sameName", "Tests with the same name are not allowed" ) |
| 288 | { |
| 289 | |
| 290 | } |
| 291 | TEST_CASE( "./sameName", "Tests with the same name are not allowed" ) |
| 292 | { |
| 293 | |
| 294 | } |
Phil Nash | 73e1bc2 | 2012-05-24 08:23:55 +0100 | [diff] [blame] | 295 | */ |
Phil Nash | 134e45b | 2012-10-28 20:57:21 +0000 | [diff] [blame] | 296 | |
| 297 | struct Boolable |
| 298 | { |
| 299 | explicit Boolable( bool value ) : m_value( value ) {} |
| 300 | |
| 301 | operator Catch::SafeBool::type() const { |
| 302 | return Catch::SafeBool::makeSafe( m_value ); |
| 303 | } |
| 304 | |
| 305 | bool m_value; |
| 306 | }; |
| 307 | |
| 308 | TEST_CASE( "./succeeding/SafeBool", "Objects that evaluated in boolean contexts can be checked") |
| 309 | { |
| 310 | Boolable True( true ); |
| 311 | Boolable False( false ); |
| 312 | |
| 313 | CHECK( True ); |
| 314 | CHECK( !False ); |
| 315 | CHECK_FALSE( False ); |
| 316 | } |