| 1 | |
| 2 | // Copyright Catch2 Authors |
| 3 | // Distributed under the Boost Software License, Version 1.0. |
| 4 | // (See accompanying file LICENSE.txt or copy at |
| 5 | // https://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | |
| 9 | #include <catch2/catch_test_macros.hpp> |
| 10 | #include <vector> |
| 11 | #include <array> |
| 12 | |
| 13 | // vector |
| 14 | TEST_CASE( "vector<int> -> toString", "[toString][vector]" ) |
| 15 | { |
| 16 | std::vector<int> vv; |
| 17 | REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" ); |
| 18 | vv.push_back( 42 ); |
| 19 | REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42 }" ); |
| 20 | vv.push_back( 250 ); |
| 21 | REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42, 250 }" ); |
| 22 | } |
| 23 | |
| 24 | TEST_CASE( "vector<string> -> toString", "[toString][vector]" ) |
| 25 | { |
| 26 | std::vector<std::string> vv; |
| 27 | REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" ); |
| 28 | vv.emplace_back( "hello" ); |
| 29 | REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\" }" ); |
| 30 | vv.emplace_back( "world" ); |
| 31 | REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\", \"world\" }" ); |
| 32 | } |
| 33 | |
| 34 | namespace { |
| 35 | /* Minimal Allocator */ |
| 36 | template<typename T> |
| 37 | struct minimal_allocator { |
| 38 | using value_type = T; |
| 39 | using size_type = std::size_t; |
| 40 | |
| 41 | minimal_allocator() = default; |
| 42 | template <typename U> |
| 43 | minimal_allocator(const minimal_allocator<U>&) {} |
| 44 | |
| 45 | |
| 46 | T *allocate( size_type n ) { |
| 47 | return static_cast<T *>( ::operator new( n * sizeof(T) ) ); |
| 48 | } |
| 49 | void deallocate( T *p, size_type /*n*/ ) { |
| 50 | ::operator delete( static_cast<void *>(p) ); |
| 51 | } |
| 52 | template<typename U> |
| 53 | bool operator==( const minimal_allocator<U>& ) const { return true; } |
| 54 | template<typename U> |
| 55 | bool operator!=( const minimal_allocator<U>& ) const { return false; } |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | TEST_CASE( "vector<int,allocator> -> toString", "[toString][vector,allocator]" ) { |
| 60 | std::vector<int,minimal_allocator<int> > vv; |
| 61 | REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" ); |
| 62 | vv.push_back( 42 ); |
| 63 | REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42 }" ); |
| 64 | vv.push_back( 250 ); |
| 65 | REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42, 250 }" ); |
| 66 | } |
| 67 | |
| 68 | TEST_CASE( "vec<vec<string,alloc>> -> toString", "[toString][vector,allocator]" ) { |
| 69 | using inner = std::vector<std::string, minimal_allocator<std::string>>; |
| 70 | using vector = std::vector<inner>; |
| 71 | vector v; |
| 72 | REQUIRE( ::Catch::Detail::stringify(v) == "{ }" ); |
| 73 | v.push_back( inner { "hello" } ); |
| 74 | v.push_back( inner { "world" } ); |
| 75 | REQUIRE( ::Catch::Detail::stringify(v) == "{ { \"hello\" }, { \"world\" } }" ); |
| 76 | } |
| 77 | |
| 78 | // Based on PR by mat-so: https://github.com/catchorg/Catch2/pull/606/files#diff-43562f40f8c6dcfe2c54557316e0f852 |
| 79 | TEST_CASE( "vector<bool> -> toString", "[toString][containers][vector]" ) { |
| 80 | std::vector<bool> bools; |
| 81 | REQUIRE( ::Catch::Detail::stringify(bools) == "{ }"); |
| 82 | bools.push_back(true); |
| 83 | REQUIRE( ::Catch::Detail::stringify(bools) == "{ true }"); |
| 84 | bools.push_back(false); |
| 85 | REQUIRE( ::Catch::Detail::stringify(bools) == "{ true, false }"); |
| 86 | } |
| 87 | TEST_CASE( "array<int, N> -> toString", "[toString][containers][array]" ) { |
| 88 | std::array<int, 0> empty; |
| 89 | REQUIRE( Catch::Detail::stringify( empty ) == "{ }" ); |
| 90 | std::array<int, 1> oneValue = {{ 42 }}; |
| 91 | REQUIRE( Catch::Detail::stringify( oneValue ) == "{ 42 }" ); |
| 92 | std::array<int, 2> twoValues = {{ 42, 250 }}; |
| 93 | REQUIRE( Catch::Detail::stringify( twoValues ) == "{ 42, 250 }" ); |
| 94 | } |