2019-12-02 12:23:10 +01:00
|
|
|
#include <catch2/catch.hpp>
|
2014-09-01 19:09:37 +02:00
|
|
|
#include <vector>
|
2018-01-04 11:52:55 +01:00
|
|
|
#include <array>
|
2014-09-01 19:09:37 +02:00
|
|
|
|
2018-03-07 10:08:35 +01:00
|
|
|
// vector
|
2014-09-01 19:09:37 +02:00
|
|
|
TEST_CASE( "vector<int> -> toString", "[toString][vector]" )
|
|
|
|
{
|
|
|
|
std::vector<int> vv;
|
2017-05-02 23:51:03 +02:00
|
|
|
REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" );
|
2014-09-01 19:09:37 +02:00
|
|
|
vv.push_back( 42 );
|
2017-05-02 23:51:03 +02:00
|
|
|
REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42 }" );
|
2015-05-20 19:28:22 +02:00
|
|
|
vv.push_back( 250 );
|
2017-05-02 23:51:03 +02:00
|
|
|
REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42, 250 }" );
|
2014-09-01 19:09:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "vector<string> -> toString", "[toString][vector]" )
|
|
|
|
{
|
|
|
|
std::vector<std::string> vv;
|
2017-05-02 23:51:03 +02:00
|
|
|
REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" );
|
2014-09-01 19:09:37 +02:00
|
|
|
vv.push_back( "hello" );
|
2017-05-02 23:51:03 +02:00
|
|
|
REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\" }" );
|
2014-09-01 19:09:37 +02:00
|
|
|
vv.push_back( "world" );
|
2017-05-02 23:51:03 +02:00
|
|
|
REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\", \"world\" }" );
|
2014-09-01 19:09:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/* Minimal Allocator */
|
|
|
|
template<typename T>
|
|
|
|
struct minimal_allocator {
|
2017-04-25 19:54:22 +02:00
|
|
|
using value_type = T;
|
|
|
|
using size_type = std::size_t;
|
|
|
|
|
|
|
|
minimal_allocator() = default;
|
|
|
|
template <typename U>
|
|
|
|
minimal_allocator(const minimal_allocator<U>&) {}
|
|
|
|
|
|
|
|
|
2014-09-04 08:27:09 +02:00
|
|
|
T *allocate( size_type n ) {
|
2014-09-01 19:09:37 +02:00
|
|
|
return static_cast<T *>( ::operator new( n * sizeof(T) ) );
|
|
|
|
}
|
2014-09-04 08:27:09 +02:00
|
|
|
void deallocate( T *p, size_type /*n*/ ) {
|
2014-09-01 19:09:37 +02:00
|
|
|
::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; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-13 09:52:51 +02:00
|
|
|
TEST_CASE( "vector<int,allocator> -> toString", "[toString][vector,allocator]" ) {
|
2014-09-01 19:09:37 +02:00
|
|
|
std::vector<int,minimal_allocator<int> > vv;
|
2017-05-02 23:51:03 +02:00
|
|
|
REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" );
|
2014-09-01 19:09:37 +02:00
|
|
|
vv.push_back( 42 );
|
2017-05-02 23:51:03 +02:00
|
|
|
REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42 }" );
|
2015-05-20 19:28:22 +02:00
|
|
|
vv.push_back( 250 );
|
2017-05-02 23:51:03 +02:00
|
|
|
REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42, 250 }" );
|
2014-09-01 19:09:37 +02:00
|
|
|
}
|
|
|
|
|
2017-07-13 09:52:51 +02:00
|
|
|
TEST_CASE( "vec<vec<string,alloc>> -> toString", "[toString][vector,allocator]" ) {
|
2017-04-25 19:54:22 +02:00
|
|
|
using inner = std::vector<std::string, minimal_allocator<std::string>>;
|
|
|
|
using vector = std::vector<inner>;
|
2014-09-01 19:09:37 +02:00
|
|
|
vector v;
|
2017-05-02 23:51:03 +02:00
|
|
|
REQUIRE( ::Catch::Detail::stringify(v) == "{ }" );
|
2014-09-01 19:09:37 +02:00
|
|
|
v.push_back( inner { "hello" } );
|
|
|
|
v.push_back( inner { "world" } );
|
2017-05-02 23:51:03 +02:00
|
|
|
REQUIRE( ::Catch::Detail::stringify(v) == "{ { \"hello\" }, { \"world\" } }" );
|
2014-09-01 19:09:37 +02:00
|
|
|
}
|
2018-01-04 11:05:02 +01:00
|
|
|
|
|
|
|
// Based on PR by mat-so: https://github.com/catchorg/Catch2/pull/606/files#diff-43562f40f8c6dcfe2c54557316e0f852
|
|
|
|
TEST_CASE( "vector<bool> -> toString", "[toString][containers][vector]" ) {
|
|
|
|
std::vector<bool> bools;
|
|
|
|
REQUIRE( ::Catch::Detail::stringify(bools) == "{ }");
|
|
|
|
bools.push_back(true);
|
|
|
|
REQUIRE( ::Catch::Detail::stringify(bools) == "{ true }");
|
|
|
|
bools.push_back(false);
|
|
|
|
REQUIRE( ::Catch::Detail::stringify(bools) == "{ true, false }");
|
|
|
|
}
|
2018-01-04 11:52:55 +01:00
|
|
|
TEST_CASE( "array<int, N> -> toString", "[toString][containers][array]" ) {
|
|
|
|
std::array<int, 0> empty;
|
|
|
|
REQUIRE( Catch::Detail::stringify( empty ) == "{ }" );
|
|
|
|
std::array<int, 1> oneValue = {{ 42 }};
|
|
|
|
REQUIRE( Catch::Detail::stringify( oneValue ) == "{ 42 }" );
|
|
|
|
std::array<int, 2> twoValues = {{ 42, 250 }};
|
|
|
|
REQUIRE( Catch::Detail::stringify( twoValues ) == "{ 42, 250 }" );
|
|
|
|
}
|