1 #include "catch.hpp" 2 #include <vector> 3 4 5 // vedctor 6 TEST_CASE( "vector<int> -> toString", "[toString][vector]" ) 7 { 8 std::vector<int> vv; 9 REQUIRE( Catch::toString(vv) == "{ }" ); 10 vv.push_back( 42 ); 11 REQUIRE( Catch::toString(vv) == "{ 42 }" ); 12 vv.push_back( 250 ); 13 REQUIRE( Catch::toString(vv) == "{ 42, 250 }" ); 14 } 15 16 TEST_CASE( "vector<string> -> toString", "[toString][vector]" ) 17 { 18 std::vector<std::string> vv; 19 REQUIRE( Catch::toString(vv) == "{ }" ); 20 vv.push_back( "hello" ); 21 REQUIRE( Catch::toString(vv) == "{ \"hello\" }" ); 22 vv.push_back( "world" ); 23 REQUIRE( Catch::toString(vv) == "{ \"hello\", \"world\" }" ); 24 } 25 26 #if defined(CATCH_CPP11_OR_GREATER) 27 #ifdef __clang__ 28 #pragma clang diagnostic push 29 #pragma clang diagnostic ignored "-Wc++98-compat" 30 #endif 31 32 /* 33 Note: These tests *can* be made to work with C++ < 11, but the 34 allocator is a lot more work... 35 */ 36 namespace { 37 /* Minimal Allocator */ 38 template<typename T> 39 struct minimal_allocator { 40 typedef T value_type; 41 typedef std::size_t size_type; allocate__anon8c36f8940111::minimal_allocator42 T *allocate( size_type n ) { 43 return static_cast<T *>( ::operator new( n * sizeof(T) ) ); 44 } deallocate__anon8c36f8940111::minimal_allocator45 void deallocate( T *p, size_type /*n*/ ) { 46 ::operator delete( static_cast<void *>(p) ); 47 } 48 template<typename U> operator ==__anon8c36f8940111::minimal_allocator49 bool operator==( const minimal_allocator<U>& ) const { return true; } 50 template<typename U> operator !=__anon8c36f8940111::minimal_allocator51 bool operator!=( const minimal_allocator<U>& ) const { return false; } 52 }; 53 } 54 55 TEST_CASE( "vector<int,allocator> -> toString", "[toString][vector,allocator][c++11][.]" ) { 56 std::vector<int,minimal_allocator<int> > vv; 57 REQUIRE( Catch::toString(vv) == "{ }" ); 58 vv.push_back( 42 ); 59 REQUIRE( Catch::toString(vv) == "{ 42 }" ); 60 vv.push_back( 250 ); 61 REQUIRE( Catch::toString(vv) == "{ 42, 250 }" ); 62 } 63 64 TEST_CASE( "vec<vec<string,alloc>> -> toString", "[toString][vector,allocator][c++11][.]" ) { 65 typedef std::vector<std::string,minimal_allocator<std::string> > inner; 66 typedef std::vector<inner> vector; 67 vector v; 68 REQUIRE( Catch::toString(v) == "{ }" ); 69 v.push_back( inner { "hello" } ); 70 v.push_back( inner { "world" } ); 71 REQUIRE( Catch::toString(v) == "{ { \"hello\" }, { \"world\" } }" ); 72 } 73 74 #ifdef __clang__ 75 #pragma clang diagnostic pop 76 #endif 77 #endif // CATCH_CPP11_OR_GREATER 78