Refactor toString vector test

Exectute allocator tests even for c++98.
This commit is contained in:
mat tso 2016-03-08 02:04:00 +01:00
parent fbceb5e4a2
commit 726b66022d

View File

@ -1,77 +1,61 @@
#include "catch.hpp" #include "catch.hpp"
#include <vector> #include <vector>
#include <deque>
#include <list>
/// \file Test Catch::to_string for standard containors.
// vedctor /// \brief Test for sequence containors
TEST_CASE( "vector<int> -> toString", "[toString][vector]" ) /// \tparm Sequence The containor to test.
{ /// \tparm Allocator The containor element allocator to use.
std::vector<int> vv; template <template <class T, class Allocator = std::allocator<T> > class Sequence,
REQUIRE( Catch::toString(vv) == "{ }" ); template <class T> class Allocator = std::allocator>
vv.push_back( 42 ); struct SequenceTest {
REQUIRE( Catch::toString(vv) == "{ 42 }" ); static void integers() {
vv.push_back( 250 ); Sequence<int, Allocator<int> > integers;
REQUIRE( Catch::toString(vv) == "{ 42, 250 }" ); REQUIRE( Catch::toString(integers) == "{ }" );
integers.push_back( 42 );
REQUIRE( Catch::toString(integers) == "{ 42 }" );
integers.push_back( 250 );
REQUIRE( Catch::toString(integers) == "{ 42, 250 }" );
};
static void strings() {
Sequence<std::string, Allocator<std::string> > strings;
REQUIRE( Catch::toString(strings) == "{ }" );
strings.push_back( "hello" );
REQUIRE( Catch::toString(strings) == "{ \"hello\" }" );
strings.push_back( "world" );
REQUIRE( Catch::toString(strings) == "{ \"hello\", \"world\" }" );
}
};
// vector
TEST_CASE( "vector<int> -> toString", "[toString][containers][vector]" ) {
SequenceTest<std::vector>::integers();
}
TEST_CASE( "vector<string> -> toString", "[toString][containers][vector]" ) {
SequenceTest<std::vector>::strings();
} }
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 { namespace {
/* Minimal Allocator */ /** \brief Custom allocator, should not impact toString. */
template<typename T> template<typename T>
struct minimal_allocator { struct MinimalAllocator : std::allocator<T> {
typedef T value_type; typedef typename std::allocator<T>::size_type size_type;
typedef std::size_t size_type;
T *allocate( size_type n ) { T *allocate( size_type n ) {
return static_cast<T *>( ::operator new( n * sizeof(T) ) ); return static_cast<T *>( ::operator new( n * sizeof(T) ) );
} }
void deallocate( T *p, size_type /*n*/ ) { void deallocate( T *p, size_type /*n*/ ) {
::operator delete( static_cast<void *>(p) ); ::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]" ) { // vector
std::vector<int,minimal_allocator<int> > vv; TEST_CASE( "vector<int,allocator> -> toString", "[toString][containers][vector][allocator]" ) {
REQUIRE( Catch::toString(vv) == "{ }" ); SequenceTest<std::vector, MinimalAllocator>::integers();
vv.push_back( 42 );
REQUIRE( Catch::toString(vv) == "{ 42 }" );
vv.push_back( 250 );
REQUIRE( Catch::toString(vv) == "{ 42, 250 }" );
} }
TEST_CASE( "vector<string,allocator> -> toString", "[toString][containers][vector][allocator]" ) {
TEST_CASE( "vec<vec<string,alloc>> -> toString", "[toString][vector,allocator]" ) { SequenceTest<std::vector, MinimalAllocator>::strings();
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