mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-17 11:12:25 +01:00
Refactor toString vector test
Exectute allocator tests even for c++98.
This commit is contained in:
parent
fbceb5e4a2
commit
726b66022d
@ -1,77 +1,61 @@
|
||||
#include "catch.hpp"
|
||||
#include <vector>
|
||||
#include <deque>
|
||||
#include <list>
|
||||
|
||||
/// \file Test Catch::to_string for standard containors.
|
||||
|
||||
// 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 }" );
|
||||
/// \brief Test for sequence containors
|
||||
/// \tparm Sequence The containor to test.
|
||||
/// \tparm Allocator The containor element allocator to use.
|
||||
template <template <class T, class Allocator = std::allocator<T> > class Sequence,
|
||||
template <class T> class Allocator = std::allocator>
|
||||
struct SequenceTest {
|
||||
static void integers() {
|
||||
Sequence<int, Allocator<int> > integers;
|
||||
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 {
|
||||
/* Minimal Allocator */
|
||||
/** \brief Custom allocator, should not impact toString. */
|
||||
template<typename T>
|
||||
struct minimal_allocator {
|
||||
typedef T value_type;
|
||||
typedef std::size_t size_type;
|
||||
struct MinimalAllocator : std::allocator<T> {
|
||||
typedef typename std::allocator<T>::size_type 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 }" );
|
||||
// vector
|
||||
TEST_CASE( "vector<int,allocator> -> toString", "[toString][containers][vector][allocator]" ) {
|
||||
SequenceTest<std::vector, MinimalAllocator>::integers();
|
||||
}
|
||||
|
||||
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\" } }" );
|
||||
TEST_CASE( "vector<string,allocator> -> toString", "[toString][containers][vector][allocator]" ) {
|
||||
SequenceTest<std::vector, MinimalAllocator>::strings();
|
||||
}
|
||||
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
#endif // CATCH_CPP11_OR_GREATER
|
||||
|
Loading…
Reference in New Issue
Block a user