Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/projects/SelfTest/ToStringVector.cpp
diff options
context:
space:
mode:
authorPhil Nash2014-09-03 19:19:05 +0100
committerPhil Nash2014-09-03 19:19:05 +0100
commitee5e7ed2c99d0f8a49efe4d491ccb4b7941431cf (patch)
tree49b3e884cf4a579f01eb01c787020d9fde8559da /projects/SelfTest/ToStringVector.cpp
parentdd22431fea6d92a9f518eb0227c1cf450fb1b22b (diff)
parenta9d14c8f7cca4b67576c5b4fa6f5179f3fe5a266 (diff)
downloadcatch3-ee5e7ed2c99d0f8a49efe4d491ccb4b7941431cf.tar.gz
Merge branch 'develop' of github.com:philsquared/Catch into develop
# By Andy Sawyer # Via Andy Sawyer (1) and Phil Nash (1) * 'develop' of github.com:philsquared/Catch: Updated CMakeLists.txt for toString work A bunch of Catch::toString tests
Diffstat (limited to 'projects/SelfTest/ToStringVector.cpp')
-rw-r--r--projects/SelfTest/ToStringVector.cpp72
1 files changed, 72 insertions, 0 deletions
diff --git a/projects/SelfTest/ToStringVector.cpp b/projects/SelfTest/ToStringVector.cpp
new file mode 100644
index 00000000..f3b036e5
--- /dev/null
+++ b/projects/SelfTest/ToStringVector.cpp
@@ -0,0 +1,72 @@
+#include "catch.hpp"
+#include <vector>
+
+
+// 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( 512 );
+ REQUIRE( Catch::toString(vv) == "{ 42, 512 }" );
+}
+
+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)
+/*
+ Note: These tests *can* be made to work with C++ < 11, but the
+ allocator is a lot more work...
+*/
+namespace {
+ /* Minimal Allocator */
+ template<typename T>
+ struct minimal_allocator {
+ typedef T value_type;
+ typedef std::size_t 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( 512 );
+ REQUIRE( Catch::toString(vv) == "{ 42, 512 }" );
+}
+
+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\" } }" );
+}
+#endif