1 #include "catch.hpp"
2 #include <vector>
3 #include <array>
4 
5 // vector
6 TEST_CASE( "vector<int> -> toString", "[toString][vector]" )
7 {
8     std::vector<int> vv;
9     REQUIRE( ::Catch::Detail::stringify(vv) == "{  }" );
10     vv.push_back( 42 );
11     REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42 }" );
12     vv.push_back( 250 );
13     REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42, 250 }" );
14 }
15 
16 TEST_CASE( "vector<string> -> toString", "[toString][vector]" )
17 {
18     std::vector<std::string> vv;
19     REQUIRE( ::Catch::Detail::stringify(vv) == "{  }" );
20     vv.push_back( "hello" );
21     REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\" }" );
22     vv.push_back( "world" );
23     REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\", \"world\" }" );
24 }
25 
26 namespace {
27     /* Minimal Allocator */
28     template<typename T>
29     struct minimal_allocator {
30         using value_type = T;
31         using size_type = std::size_t;
32 
33         minimal_allocator() = default;
34         template <typename U>
minimal_allocator__anona2ec493d0111::minimal_allocator35         minimal_allocator(const minimal_allocator<U>&) {}
36 
37 
allocate__anona2ec493d0111::minimal_allocator38         T *allocate( size_type n ) {
39             return static_cast<T *>( ::operator new( n * sizeof(T) ) );
40         }
deallocate__anona2ec493d0111::minimal_allocator41         void deallocate( T *p, size_type /*n*/ ) {
42             ::operator delete( static_cast<void *>(p) );
43         }
44         template<typename U>
operator ==__anona2ec493d0111::minimal_allocator45         bool operator==( const minimal_allocator<U>& ) const { return true; }
46         template<typename U>
operator !=__anona2ec493d0111::minimal_allocator47         bool operator!=( const minimal_allocator<U>& ) const { return false; }
48     };
49 }
50 
51 TEST_CASE( "vector<int,allocator> -> toString", "[toString][vector,allocator]" ) {
52     std::vector<int,minimal_allocator<int> > vv;
53     REQUIRE( ::Catch::Detail::stringify(vv) == "{  }" );
54     vv.push_back( 42 );
55     REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42 }" );
56     vv.push_back( 250 );
57     REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42, 250 }" );
58 }
59 
60 TEST_CASE( "vec<vec<string,alloc>> -> toString", "[toString][vector,allocator]" ) {
61     using inner = std::vector<std::string, minimal_allocator<std::string>>;
62     using vector = std::vector<inner>;
63     vector v;
64     REQUIRE( ::Catch::Detail::stringify(v) == "{  }" );
65     v.push_back( inner { "hello" } );
66     v.push_back( inner { "world" } );
67     REQUIRE( ::Catch::Detail::stringify(v) == "{ { \"hello\" }, { \"world\" } }" );
68 }
69 
70 // Based on PR by mat-so: https://github.com/catchorg/Catch2/pull/606/files#diff-43562f40f8c6dcfe2c54557316e0f852
71 TEST_CASE( "vector<bool> -> toString", "[toString][containers][vector]" ) {
72     std::vector<bool> bools;
73     REQUIRE( ::Catch::Detail::stringify(bools) == "{  }");
74     bools.push_back(true);
75     REQUIRE( ::Catch::Detail::stringify(bools) == "{ true }");
76     bools.push_back(false);
77     REQUIRE( ::Catch::Detail::stringify(bools) == "{ true, false }");
78 }
79 TEST_CASE( "array<int, N> -> toString", "[toString][containers][array]" ) {
80     std::array<int, 0> empty;
81     REQUIRE( Catch::Detail::stringify( empty ) == "{  }" );
82     std::array<int, 1> oneValue = {{ 42 }};
83     REQUIRE( Catch::Detail::stringify( oneValue ) == "{ 42 }" );
84     std::array<int, 2> twoValues = {{ 42, 250 }};
85     REQUIRE( Catch::Detail::stringify( twoValues ) == "{ 42, 250 }" );
86 }