Andy Sawyer | abf9ffc | 2014-09-01 18:09:37 +0100 | [diff] [blame] | 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 }" ); |
Phil Nash | 6ed74b5 | 2015-05-20 18:28:22 +0100 | [diff] [blame] | 12 | vv.push_back( 250 ); |
| 13 | REQUIRE( Catch::toString(vv) == "{ 42, 250 }" ); |
Andy Sawyer | abf9ffc | 2014-09-01 18:09:37 +0100 | [diff] [blame] | 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 | |
Andy Sawyer | abf9ffc | 2014-09-01 18:09:37 +0100 | [diff] [blame] | 26 | namespace { |
| 27 | /* Minimal Allocator */ |
| 28 | template<typename T> |
| 29 | struct minimal_allocator { |
Martin Hořeňovský | 53864de | 2017-04-25 19:54:22 +0200 | [diff] [blame^] | 30 | using value_type = T; |
| 31 | using size_type = std::size_t; |
| 32 | |
| 33 | minimal_allocator() = default; |
| 34 | template <typename U> |
| 35 | minimal_allocator(const minimal_allocator<U>&) {} |
| 36 | |
| 37 | |
Phil Nash | 886ef16 | 2014-09-04 07:27:09 +0100 | [diff] [blame] | 38 | T *allocate( size_type n ) { |
Andy Sawyer | abf9ffc | 2014-09-01 18:09:37 +0100 | [diff] [blame] | 39 | return static_cast<T *>( ::operator new( n * sizeof(T) ) ); |
| 40 | } |
Phil Nash | 886ef16 | 2014-09-04 07:27:09 +0100 | [diff] [blame] | 41 | void deallocate( T *p, size_type /*n*/ ) { |
Andy Sawyer | abf9ffc | 2014-09-01 18:09:37 +0100 | [diff] [blame] | 42 | ::operator delete( static_cast<void *>(p) ); |
| 43 | } |
| 44 | template<typename U> |
| 45 | bool operator==( const minimal_allocator<U>& ) const { return true; } |
| 46 | template<typename U> |
| 47 | bool operator!=( const minimal_allocator<U>& ) const { return false; } |
| 48 | }; |
| 49 | } |
| 50 | |
Phil Nash | 0f0dcd3 | 2017-01-09 14:35:03 +0000 | [diff] [blame] | 51 | TEST_CASE( "vector<int,allocator> -> toString", "[toString][vector,allocator][c++11][.]" ) { |
Andy Sawyer | abf9ffc | 2014-09-01 18:09:37 +0100 | [diff] [blame] | 52 | std::vector<int,minimal_allocator<int> > vv; |
| 53 | REQUIRE( Catch::toString(vv) == "{ }" ); |
| 54 | vv.push_back( 42 ); |
| 55 | REQUIRE( Catch::toString(vv) == "{ 42 }" ); |
Phil Nash | 6ed74b5 | 2015-05-20 18:28:22 +0100 | [diff] [blame] | 56 | vv.push_back( 250 ); |
| 57 | REQUIRE( Catch::toString(vv) == "{ 42, 250 }" ); |
Andy Sawyer | abf9ffc | 2014-09-01 18:09:37 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Phil Nash | 0f0dcd3 | 2017-01-09 14:35:03 +0000 | [diff] [blame] | 60 | TEST_CASE( "vec<vec<string,alloc>> -> toString", "[toString][vector,allocator][c++11][.]" ) { |
Martin Hořeňovský | 53864de | 2017-04-25 19:54:22 +0200 | [diff] [blame^] | 61 | using inner = std::vector<std::string, minimal_allocator<std::string>>; |
| 62 | using vector = std::vector<inner>; |
Andy Sawyer | abf9ffc | 2014-09-01 18:09:37 +0100 | [diff] [blame] | 63 | vector v; |
| 64 | REQUIRE( Catch::toString(v) == "{ }" ); |
| 65 | v.push_back( inner { "hello" } ); |
| 66 | v.push_back( inner { "world" } ); |
| 67 | REQUIRE( Catch::toString(v) == "{ { \"hello\" }, { \"world\" } }" ); |
| 68 | } |