2014-09-01 19:09:37 +02:00
|
|
|
#include "catch.hpp"
|
|
|
|
#include <vector>
|
2016-03-08 02:04:00 +01:00
|
|
|
#include <deque>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
/// \file Test Catch::to_string for standard containors.
|
|
|
|
|
|
|
|
/// \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 }" );
|
|
|
|
};
|
2014-09-01 19:09:37 +02:00
|
|
|
|
2016-03-08 02:04:00 +01:00
|
|
|
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();
|
2014-09-01 19:09:37 +02:00
|
|
|
}
|
2016-03-08 02:04:00 +01:00
|
|
|
TEST_CASE( "vector<string> -> toString", "[toString][containers][vector]" ) {
|
|
|
|
SequenceTest<std::vector>::strings();
|
2014-09-01 19:09:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2016-03-08 02:04:00 +01:00
|
|
|
/** \brief Custom allocator, should not impact toString. */
|
2014-09-01 19:09:37 +02:00
|
|
|
template<typename T>
|
2016-03-08 02:04:00 +01:00
|
|
|
struct MinimalAllocator : std::allocator<T> {
|
|
|
|
typedef typename std::allocator<T>::size_type size_type;
|
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) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-03-08 02:04:00 +01:00
|
|
|
// vector
|
|
|
|
TEST_CASE( "vector<int,allocator> -> toString", "[toString][containers][vector][allocator]" ) {
|
|
|
|
SequenceTest<std::vector, MinimalAllocator>::integers();
|
2014-09-01 19:09:37 +02:00
|
|
|
}
|
2016-03-08 02:04:00 +01:00
|
|
|
TEST_CASE( "vector<string,allocator> -> toString", "[toString][containers][vector][allocator]" ) {
|
|
|
|
SequenceTest<std::vector, MinimalAllocator>::strings();
|
2014-09-01 19:09:37 +02:00
|
|
|
}
|