blob: 50e6a3943cbc0e3ecbf434713dcca770dc8094bb [file] [log] [blame]
Phil Nashd8026002010-11-09 23:24:00 +00001/*
Phil Nashd8026002010-11-09 23:24:00 +00002 * 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 Nashd8026002010-11-09 23:24:00 +00007 */
8
Phil Nashcda21492012-08-16 18:47:41 +01009#ifdef __clang__
Phil Nasha695eb92012-08-13 07:46:10 +010010#pragma clang diagnostic ignored "-Wpadded"
Phil Nashcda21492012-08-16 18:47:41 +010011#endif
Phil Nasha695eb92012-08-13 07:46:10 +010012
Phil Nash823ea3e2011-04-26 08:32:40 +010013#include "catch.hpp"
Phil Nashd8026002010-11-09 23:24:00 +000014
15namespace 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 Nashe0e74772011-03-10 14:09:32 +000027///////////////////////////////////////////////////////////////////////////////
28TEST_CASE
29(
30 "./succeeding/Tricky/std::pair",
31 "Parsing a std::pair"
32)
Phil Nashd8026002010-11-09 23:24:00 +000033{
34 std::pair<int, int> aNicePair( 1, 2 );
35
36 // !TBD: would be nice if this could compile without the extra parentheses
Phil Nasha2d20952010-12-14 09:00:09 +000037 REQUIRE( (std::pair<int, int>( 1, 2 )) == aNicePair );
Phil Nashd8026002010-11-09 23:24:00 +000038
Phil Nash3d0fed72010-11-10 19:18:46 +000039}
40
Phil Nashe0e74772011-03-10 14:09:32 +000041///////////////////////////////////////////////////////////////////////////////
42TEST_CASE
43(
Phil Nashb8262432011-03-11 19:46:18 +000044 "./inprogress/failing/Tricky/trailing expression",
45 "Where the is more to the expression after the RHS"
Phil Nashe0e74772011-03-10 14:09:32 +000046)
Phil Nash3d0fed72010-11-10 19:18:46 +000047{
Phil Nash2e444862011-03-10 19:18:14 +000048 /*
Phil Nash3d0fed72010-11-10 19:18:46 +000049 int a = 1;
50 int b = 2;
51
Phil Nash48a70222010-11-11 07:21:57 +000052 // This only captures part of the expression, but issues a warning about the rest
Phil Nasha2d20952010-12-14 09:00:09 +000053 REQUIRE( a == 2 || b == 2 );
Phil Nash2e444862011-03-10 19:18:14 +000054 */
Phil Nashb8262432011-03-11 19:46:18 +000055 WARN( "Uncomment the code in this test to check that it gives a sensible compiler error" );
Phil Nash2e444862011-03-10 19:18:14 +000056}
57///////////////////////////////////////////////////////////////////////////////
58TEST_CASE
59(
Phil Nashb8262432011-03-11 19:46:18 +000060 "./inprogress/failing/Tricky/compound lhs",
Phil Nash2e444862011-03-10 19:18:14 +000061 "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 Nashb8262432011-03-11 19:46:18 +000071 WARN( "Uncomment the code in this test to check that it gives a sensible compiler error" );
Phil Nash92f2d322010-11-11 20:37:46 +000072}
Phil Nashe6b2b0e2010-11-29 19:40:44 +000073
Phil Nash7d1ce5f2010-11-16 19:30:41 +000074struct Opaque
75{
76 int val;
Phil Nashe0e74772011-03-10 14:09:32 +000077 bool operator ==( const Opaque& o ) const
Phil Nash7d1ce5f2010-11-16 19:30:41 +000078 {
Phil Nash5f43a432010-11-16 19:48:05 +000079 return val == o.val;
Phil Nash7d1ce5f2010-11-16 19:30:41 +000080 }
81};
82
Phil Nashe0e74772011-03-10 14:09:32 +000083///////////////////////////////////////////////////////////////////////////////
84TEST_CASE
85(
86 "./failing/Tricky/non streamable type",
87 "A failing expression with a non streamable type is still captured"
88)
Phil Nash7d1ce5f2010-11-16 19:30:41 +000089{
90
91 Opaque o1, o2;
92 o1.val = 7;
93 o2.val = 8;
94
95 CHECK( &o1 == &o2 );
96 CHECK( o1 == o2 );
Phil Nashceab2a62010-11-29 19:48:37 +000097}
Phil Nashe0e74772011-03-10 14:09:32 +000098
99///////////////////////////////////////////////////////////////////////////////
100TEST_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///////////////////////////////////////////////////////////////////////////////
111TEST_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 Nash46b49092011-04-06 22:26:16 +0100123
124namespace 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
135namespace 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 Nash458e5ee2011-12-27 10:59:41 +0000145
146inline bool operator==(const A::X& lhs, const B::Y& rhs)
Phil Nash46b49092011-04-06 22:26:16 +0100147{
148 return (lhs.a == rhs.a);
149}
150
Phil Nash458e5ee2011-12-27 10:59:41 +0000151inline bool operator==(const B::Y& lhs, const A::X& rhs)
Phil Nash46b49092011-04-06 22:26:16 +0100152{
153 return (lhs.a == rhs.a);
154}
155
Phil Nash458e5ee2011-12-27 10:59:41 +0000156
Phil Nash46b49092011-04-06 22:26:16 +0100157///////////////////////////////////////////////////////////////////////////////
Phil Nash458e5ee2011-12-27 10:59:41 +0000158/* This, currently, does not compile with LLVM
Phil Nash46b49092011-04-06 22:26:16 +0100159TEST_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 Nash458e5ee2011-12-27 10:59:41 +0000169*/
Phil Nash46b49092011-04-06 22:26:16 +0100170
171namespace 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 Nash0f2e5d62011-04-11 08:32:55 +0100184 {
185 Object o;
186 REQUIRE(0xc0000000 == o );
Phil Nash46b49092011-04-06 22:26:16 +0100187 }
188}
189
190namespace 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 Nashd06dced2012-05-09 08:17:51 +0100212 REQUIRE( t == 1u );
Phil Nash46b49092011-04-06 22:26:16 +0100213 }
214}
215
216namespace 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 Nash0f2e5d62011-04-11 08:32:55 +0100227
228struct Obj
229{
230 Obj():prop(&p){}
231
232 int p;
233 int* prop;
234};
235
236TEST_CASE("./succeeding/boolean member", "")
237{
238 Obj obj;
Phil Nash823ea3e2011-04-26 08:32:40 +0100239 REQUIRE( obj.prop != NULL );
Phil Nash0f2e5d62011-04-11 08:32:55 +0100240}
Phil Nash4d0a8d92011-08-09 08:18:27 +0100241
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.
249template <bool B>
250struct is_true
251{
252 static const bool value = B;
253};
254
255TEST_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 Nash3fd7dc02012-02-09 08:34:01 +0000284
285// Uncomment these tests to produce an error at test registration time
286/*
287TEST_CASE( "./sameName", "Tests with the same name are not allowed" )
288{
289
290}
291TEST_CASE( "./sameName", "Tests with the same name are not allowed" )
292{
293
294}
Phil Nash73e1bc22012-05-24 08:23:55 +0100295*/