catch2/projects/SelfTest/TrickyTests.cpp

317 lines
6.8 KiB
C++
Raw Normal View History

2010-11-10 00:24:00 +01:00
/*
* Created by Phil on 09/11/2010.
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
2012-08-16 19:47:41 +02:00
#ifdef __clang__
#pragma clang diagnostic ignored "-Wpadded"
2012-08-16 19:47:41 +02:00
#endif
2011-04-26 09:32:40 +02:00
#include "catch.hpp"
2010-11-10 00:24:00 +01:00
namespace Catch
{
template<>
std::string toString<std::pair<int, int> >( const std::pair<int, int>& value )
{
std::ostringstream oss;
oss << "std::pair( " << value.first << ", " << value.second << " )";
return oss.str();
}
}
2011-03-10 15:09:32 +01:00
///////////////////////////////////////////////////////////////////////////////
TEST_CASE
(
"./succeeding/Tricky/std::pair",
"Parsing a std::pair"
)
2010-11-10 00:24:00 +01:00
{
std::pair<int, int> aNicePair( 1, 2 );
// !TBD: would be nice if this could compile without the extra parentheses
REQUIRE( (std::pair<int, int>( 1, 2 )) == aNicePair );
2010-11-10 00:24:00 +01:00
2010-11-10 20:18:46 +01:00
}
2011-03-10 15:09:32 +01:00
///////////////////////////////////////////////////////////////////////////////
TEST_CASE
(
"./inprogress/failing/Tricky/trailing expression",
"Where the is more to the expression after the RHS"
2011-03-10 15:09:32 +01:00
)
2010-11-10 20:18:46 +01:00
{
/*
2010-11-10 20:18:46 +01:00
int a = 1;
int b = 2;
// This only captures part of the expression, but issues a warning about the rest
REQUIRE( a == 2 || b == 2 );
*/
WARN( "Uncomment the code in this test to check that it gives a sensible compiler error" );
}
///////////////////////////////////////////////////////////////////////////////
TEST_CASE
(
"./inprogress/failing/Tricky/compound lhs",
"Where the LHS is not a simple value"
)
{
/*
int a = 1;
int b = 2;
// This only captures part of the expression, but issues a warning about the rest
REQUIRE( a+1 == b-1 );
*/
WARN( "Uncomment the code in this test to check that it gives a sensible compiler error" );
2010-11-11 21:37:46 +01:00
}
struct Opaque
{
int val;
2011-03-10 15:09:32 +01:00
bool operator ==( const Opaque& o ) const
{
2010-11-16 20:48:05 +01:00
return val == o.val;
}
};
2011-03-10 15:09:32 +01:00
///////////////////////////////////////////////////////////////////////////////
TEST_CASE
(
"./failing/Tricky/non streamable type",
"A failing expression with a non streamable type is still captured"
)
{
Opaque o1, o2;
o1.val = 7;
o2.val = 8;
CHECK( &o1 == &o2 );
CHECK( o1 == o2 );
2010-11-29 20:48:37 +01:00
}
2011-03-10 15:09:32 +01:00
///////////////////////////////////////////////////////////////////////////////
TEST_CASE
(
"./failing/string literals",
"string literals of different sizes can be compared"
)
{
REQUIRE( std::string( "first" ) == "second" );
}
///////////////////////////////////////////////////////////////////////////////
TEST_CASE
(
"./succeeding/side-effects",
"An expression with side-effects should only be evaluated once"
)
{
int i = 7;
REQUIRE( i++ == 7 );
REQUIRE( i++ == 8 );
}
2011-04-06 23:26:16 +02:00
namespace A {
struct X
{
X() : a(4), b(2), c(7) {}
X(int v) : a(v), b(2), c(7) {}
int a;
int b;
int c;
};
}
namespace B {
struct Y
{
Y() : a(4), b(2), c(7) {}
Y(int v) : a(v), b(2), c(7) {}
int a;
int b;
int c;
};
}
inline bool operator==(const A::X& lhs, const B::Y& rhs)
2011-04-06 23:26:16 +02:00
{
return (lhs.a == rhs.a);
}
inline bool operator==(const B::Y& lhs, const A::X& rhs)
2011-04-06 23:26:16 +02:00
{
return (lhs.a == rhs.a);
}
2011-04-06 23:26:16 +02:00
///////////////////////////////////////////////////////////////////////////////
/* This, currently, does not compile with LLVM
2011-04-06 23:26:16 +02:00
TEST_CASE
(
"./succeeding/koenig",
"Operators at different namespace levels not hijacked by Koenig lookup"
)
{
A::X x;
B::Y y;
REQUIRE( x == y );
}
*/
2011-04-06 23:26:16 +02:00
namespace ObjectWithConversions
{
struct Object
{
operator unsigned int() {return 0xc0000000;}
};
///////////////////////////////////////////////////////////////////////////////
TEST_CASE
(
"./succeeding/koenig",
"Operators at different namespace levels not hijacked by Koenig lookup"
)
{
Object o;
REQUIRE(0xc0000000 == o );
2011-04-06 23:26:16 +02:00
}
}
namespace ObjectWithNonConstEqualityOperator
{
struct Test
{
Test( unsigned int v )
: m_value(v)
{}
bool operator==( const Test&rhs )
{
return (m_value == rhs.m_value);
}
bool operator==( const Test&rhs ) const
{
return (m_value != rhs.m_value);
}
unsigned int m_value;
};
TEST_CASE("./succeeding/non-const==", "Demonstrate that a non-const == is not used")
{
Test t( 1 );
REQUIRE( t == 1u );
2011-04-06 23:26:16 +02:00
}
}
namespace EnumBitFieldTests
{
enum Bits {bit0 = 0x0001, bit1 = 0x0002, bit2 = 0x0004, bit3 = 0x0008, bit1and2 = 0x0006,
bit30 = 0x40000000, bit31 = 0x80000000,
bit30and31 = 0xc0000000};
TEST_CASE("./succeeding/enum/bits", "Test enum bit values")
{
REQUIRE( 0xc0000000 == bit30and31 );
}
}
struct Obj
{
Obj():prop(&p){}
int p;
int* prop;
};
TEST_CASE("./succeeding/boolean member", "")
{
Obj obj;
2011-04-26 09:32:40 +02:00
REQUIRE( obj.prop != NULL );
}
// Tests for a problem submitted by Ralph McArdell
//
// The static bool value should not need to be defined outside the
// struct it is declared in - but when evaluating it in a deduced
// context it appears to require the extra definition.
// The issue was fixed by adding bool overloads to bypass the
// templates that were deduce it.
template <bool B>
struct is_true
{
static const bool value = B;
};
TEST_CASE( "./succeeding/unimplemented static bool", "static bools can be evaluated" )
{
SECTION("compare to true","")
{
REQUIRE( is_true<true>::value == true );
REQUIRE( true == is_true<true>::value );
}
SECTION("compare to false","")
{
REQUIRE( is_true<false>::value == false );
REQUIRE( false == is_true<false>::value );
}
SECTION("negation", "")
{
REQUIRE( !is_true<false>::value );
}
SECTION("double negation","")
{
REQUIRE( !!is_true<true>::value );
}
SECTION("direct","")
{
REQUIRE( is_true<true>::value );
REQUIRE_FALSE( is_true<false>::value );
}
}
// Uncomment these tests to produce an error at test registration time
/*
TEST_CASE( "./sameName", "Tests with the same name are not allowed" )
{
}
TEST_CASE( "./sameName", "Tests with the same name are not allowed" )
{
}
2012-05-24 09:23:55 +02:00
*/
Squashed commit of the following: commit 2a1e8bfc6ed3bcc1e3746d717726a9e525de6369 Author: Phil Nash <github@philnash.me> Date: Thu Nov 1 08:16:15 2012 +0000 Updated colour comments commit f0f407fc3ebdcbdad50fc1f9a45f15ab3cc54c63 Author: Phil Nash <github@philnash.me> Date: Wed Oct 31 18:28:21 2012 +0000 Manually applied merge #133 from Master commit 355b95fda1de28ba830541768aad32c7b1790290 Author: Phil Nash <github@philnash.me> Date: Wed Oct 31 18:04:22 2012 +0000 Cleaned up ANSI colour code impl a bit commit 778f9c4fc7fa66566679d9f1d8a721079d8589bf Author: Phil Nash <github@philnash.me> Date: Tue Oct 30 09:09:30 2012 +0000 Removed "no-" from Wno-global-constructors when disabling commit 5efa4bcb8a620efa13e8eb5e68d0899681919ca1 Author: Phil Nash <github@philnash.me> Date: Mon Oct 29 20:49:22 2012 +0000 Regenerated single_include commit 108f1937d8cbe04038ab8d2aaf9f6e958fe8168c Author: Phil Nash <github@philnash.me> Date: Mon Oct 29 20:46:45 2012 +0000 Added terminal colour codes for POSIX With thanks to Adam Strzelecki commit 8f4cc541d55cf55b2a67b5d6b77b5c4ceb4d99ee Author: Phil Nash <github@philnash.me> Date: Mon Oct 29 19:55:34 2012 +0000 Added regression test baselines commit 2e203a183474672e386f4cf840847cf29ccf90b9 Author: Phil Nash <github@philnash.me> Date: Mon Oct 29 19:55:13 2012 +0000 Fixed remaining reporting regressions commit 134e45b3ade922e5cbdb2df6b81830fbd93fe448 Author: Phil Nash <github@philnash.me> Date: Sun Oct 28 20:57:21 2012 +0000 Fixed #132 commit 2f92db9898978ca1ee27c964d8a51fa507391935 Author: Phil Nash <github@philnash.me> Date: Sun Oct 28 12:15:34 2012 +0000 Updated the readme specifically for the Integration branch commit 82acc2ca05459ce353de71469f141ba93e9b63cf Author: Phil Nash <github@philnash.me> Date: Sun Oct 28 12:07:17 2012 +0000 Regenerated single include commit fe1d7c1d08f06c0a83b727e9b2e00cc49e74175d Author: Phil Nash <github@philnash.me> Date: Sun Oct 28 10:27:44 2012 +0000 Small fixes and tweaks commit 355b5e546d2f64dd0abbc0134dd3579ce6c28cea Author: Phil Nash <github@philnash.me> Date: Fri Oct 26 09:05:36 2012 +0100 Some tidy-up commit f847186ebbf0e7eeabd99925574c8988add600fb Author: Phil Nash <github@philnash.me> Date: Fri Oct 26 08:45:23 2012 +0100 AssertionResultBuilder -> ExpressionResultBuilder commit 8cca2f13699aa6aa5cd4a60e293f49a1bae54629 Author: Phil Nash <github@philnash.me> Date: Wed Oct 24 22:09:01 2012 +0100 ExpressionBuilder ->ExpressionDecomposer Expression -> ExpressionLhs commit e04e74f896c714de1f03a9cc006bcd071318ea11 Author: Phil Nash <github@philnash.me> Date: Wed Oct 24 21:59:47 2012 +0100 More AssertionResult refactoring commit 1dd56d4d2b29520cdd62f5f449a28e67f954f5db Author: Phil Nash <github@philnash.me> Date: Fri Oct 19 08:01:34 2012 +0100 AssertionResultBuilder can be constructed from result type commit f2d5f1b3e444055ff02ed0536d5d0610b08ecb9f Author: Phil Nash <github@philnash.me> Date: Fri Oct 19 08:01:05 2012 +0100 Expression has its own result builder - not passed in from expression builder commit e3b111a39a4edd3bfe4d88290c99c723b8b74417 Author: Phil Nash <github@philnash.me> Date: Thu Oct 18 22:59:16 2012 +0100 streamlined acceptResult commit 3ad13256e1e50a6ee4a2697e7b6d0f669ae82ab5 Author: Phil Nash <github@philnash.me> Date: Thu Oct 18 08:39:44 2012 +0100 Refactored assertion builder stuff out of expression builder commit c96f9330a095f527673de4d8db4f434d38a7e813 Author: Phil Nash <github@philnash.me> Date: Wed Oct 17 08:14:22 2012 +0100 Collect assertion info up front commit a5fa78284d6d95770cd647a2ec10df09d378ba46 Author: Phil Nash <github@philnash.me> Date: Tue Oct 16 08:33:13 2012 +0100 ResultData -> AssertionResultData commit c597a893fa95462b8d2d5029547d4054eda1839f Author: Phil Nash <github@philnash.me> Date: Tue Oct 16 08:31:05 2012 +0100 ResultInfo -> AssertionResult filenames and variables commit d16955f63a4a128221da41602215b5592df39eb9 Author: Phil Nash <github@philnash.me> Date: Tue Oct 16 08:27:21 2012 +0100 Renamed ResultInfo -> AssertionResult commit 175da3ef6454b6fa0465d22c96d21d58dd9d2f90 Author: Phil Nash <github@philnash.me> Date: Fri Oct 12 18:39:22 2012 +0100 regen test 3
2012-11-01 09:27:09 +01:00
struct Boolable
{
explicit Boolable( bool value ) : m_value( value ) {}
operator Catch::SafeBool::type() const {
return Catch::SafeBool::makeSafe( m_value );
}
bool m_value;
};
TEST_CASE( "./succeeding/SafeBool", "Objects that evaluated in boolean contexts can be checked")
{
Boolable True( true );
Boolable False( false );
CHECK( True );
CHECK( !False );
CHECK_FALSE( False );
}