blob: eafe886d7e82eafeec7e9e024e9703be0001de76 [file] [log] [blame]
Andy Sawyerabf9ffc2014-09-01 18:09:37 +01001#include "catch.hpp"
2#include <vector>
3
4
5// vedctor
6TEST_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 Nash6ed74b52015-05-20 18:28:22 +010012 vv.push_back( 250 );
13 REQUIRE( Catch::toString(vv) == "{ 42, 250 }" );
Andy Sawyerabf9ffc2014-09-01 18:09:37 +010014}
15
16TEST_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 Sawyerabf9ffc2014-09-01 18:09:37 +010026namespace {
27 /* Minimal Allocator */
28 template<typename T>
29 struct minimal_allocator {
Martin Hořeňovský53864de2017-04-25 19:54:22 +020030 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 Nash886ef162014-09-04 07:27:09 +010038 T *allocate( size_type n ) {
Andy Sawyerabf9ffc2014-09-01 18:09:37 +010039 return static_cast<T *>( ::operator new( n * sizeof(T) ) );
40 }
Phil Nash886ef162014-09-04 07:27:09 +010041 void deallocate( T *p, size_type /*n*/ ) {
Andy Sawyerabf9ffc2014-09-01 18:09:37 +010042 ::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 Nash0f0dcd32017-01-09 14:35:03 +000051TEST_CASE( "vector<int,allocator> -> toString", "[toString][vector,allocator][c++11][.]" ) {
Andy Sawyerabf9ffc2014-09-01 18:09:37 +010052 std::vector<int,minimal_allocator<int> > vv;
53 REQUIRE( Catch::toString(vv) == "{ }" );
54 vv.push_back( 42 );
55 REQUIRE( Catch::toString(vv) == "{ 42 }" );
Phil Nash6ed74b52015-05-20 18:28:22 +010056 vv.push_back( 250 );
57 REQUIRE( Catch::toString(vv) == "{ 42, 250 }" );
Andy Sawyerabf9ffc2014-09-01 18:09:37 +010058}
59
Phil Nash0f0dcd32017-01-09 14:35:03 +000060TEST_CASE( "vec<vec<string,alloc>> -> toString", "[toString][vector,allocator][c++11][.]" ) {
Martin Hořeňovský53864de2017-04-25 19:54:22 +020061 using inner = std::vector<std::string, minimal_allocator<std::string>>;
62 using vector = std::vector<inner>;
Andy Sawyerabf9ffc2014-09-01 18:09:37 +010063 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}