aboutsummaryrefslogtreecommitdiff
path: root/unittest/Catch/projects/SelfTest/ToStringVector.cpp
blob: c3a8d4ed647ce94d45a7f8694403542b69e7884b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "catch.hpp"
#include <vector>


// vedctor
TEST_CASE( "vector<int> -> toString", "[toString][vector]" )
{
    std::vector<int> vv;
    REQUIRE( Catch::toString(vv) == "{  }" );
    vv.push_back( 42 );
    REQUIRE( Catch::toString(vv) == "{ 42 }" );
    vv.push_back( 250 );
    REQUIRE( Catch::toString(vv) == "{ 42, 250 }" );
}

TEST_CASE( "vector<string> -> toString", "[toString][vector]" )
{
    std::vector<std::string> vv;
    REQUIRE( Catch::toString(vv) == "{  }" );
    vv.push_back( "hello" );
    REQUIRE( Catch::toString(vv) == "{ \"hello\" }" );
    vv.push_back( "world" );
    REQUIRE( Catch::toString(vv) == "{ \"hello\", \"world\" }" );
}

#if defined(CATCH_CPP11_OR_GREATER)
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc++98-compat"
#endif

/*
  Note: These tests *can* be made to work with C++ < 11, but the
  allocator is a lot more work...
*/
namespace {
    /* Minimal Allocator */
    template<typename T>
    struct minimal_allocator {
        typedef T value_type;
        typedef std::size_t size_type;
        T *allocate( size_type n ) {
            return static_cast<T *>( ::operator new( n * sizeof(T) ) );
        }
        void deallocate( T *p, size_type /*n*/ ) {
            ::operator delete( static_cast<void *>(p) );
        }
        template<typename U>
        bool operator==( const minimal_allocator<U>& ) const { return true; }
        template<typename U>
        bool operator!=( const minimal_allocator<U>& ) const { return false; }
    };
}

TEST_CASE( "vector<int,allocator> -> toString", "[toString][vector,allocator]" ) {
    std::vector<int,minimal_allocator<int> > vv;
    REQUIRE( Catch::toString(vv) == "{  }" );
    vv.push_back( 42 );
    REQUIRE( Catch::toString(vv) == "{ 42 }" );
    vv.push_back( 250 );
    REQUIRE( Catch::toString(vv) == "{ 42, 250 }" );
}

TEST_CASE( "vec<vec<string,alloc>> -> toString", "[toString][vector,allocator]" ) {
    typedef std::vector<std::string,minimal_allocator<std::string> > inner;
    typedef std::vector<inner> vector;
    vector v;
    REQUIRE( Catch::toString(v) == "{  }" );
    v.push_back( inner { "hello" } );
    v.push_back( inner { "world" } );
    REQUIRE( Catch::toString(v) == "{ { \"hello\" }, { \"world\" } }" );
}

#ifdef __clang__
#pragma clang diagnostic pop
#endif
#endif // CATCH_CPP11_OR_GREATER