ToStringVector.tests.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "catch.hpp"
  2. #include <vector>
  3. #include <array>
  4. // vector
  5. TEST_CASE( "vector<int> -> toString", "[toString][vector]" )
  6. {
  7. std::vector<int> vv;
  8. REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" );
  9. vv.push_back( 42 );
  10. REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42 }" );
  11. vv.push_back( 250 );
  12. REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42, 250 }" );
  13. }
  14. TEST_CASE( "vector<string> -> toString", "[toString][vector]" )
  15. {
  16. std::vector<std::string> vv;
  17. REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" );
  18. vv.emplace_back( "hello" );
  19. REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\" }" );
  20. vv.emplace_back( "world" );
  21. REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\", \"world\" }" );
  22. }
  23. namespace {
  24. /* Minimal Allocator */
  25. template<typename T>
  26. struct minimal_allocator {
  27. using value_type = T;
  28. using size_type = std::size_t;
  29. minimal_allocator() = default;
  30. template <typename U>
  31. minimal_allocator(const minimal_allocator<U>&) {}
  32. T *allocate( size_type n ) {
  33. return static_cast<T *>( ::operator new( n * sizeof(T) ) );
  34. }
  35. void deallocate( T *p, size_type /*n*/ ) {
  36. ::operator delete( static_cast<void *>(p) );
  37. }
  38. template<typename U>
  39. bool operator==( const minimal_allocator<U>& ) const { return true; }
  40. template<typename U>
  41. bool operator!=( const minimal_allocator<U>& ) const { return false; }
  42. };
  43. }
  44. TEST_CASE( "vector<int,allocator> -> toString", "[toString][vector,allocator]" ) {
  45. std::vector<int,minimal_allocator<int> > vv;
  46. REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" );
  47. vv.push_back( 42 );
  48. REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42 }" );
  49. vv.push_back( 250 );
  50. REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42, 250 }" );
  51. }
  52. TEST_CASE( "vec<vec<string,alloc>> -> toString", "[toString][vector,allocator]" ) {
  53. using inner = std::vector<std::string, minimal_allocator<std::string>>;
  54. using vector = std::vector<inner>;
  55. vector v;
  56. REQUIRE( ::Catch::Detail::stringify(v) == "{ }" );
  57. v.push_back( inner { "hello" } );
  58. v.push_back( inner { "world" } );
  59. REQUIRE( ::Catch::Detail::stringify(v) == "{ { \"hello\" }, { \"world\" } }" );
  60. }
  61. // Based on PR by mat-so: https://github.com/catchorg/Catch2/pull/606/files#diff-43562f40f8c6dcfe2c54557316e0f852
  62. TEST_CASE( "vector<bool> -> toString", "[toString][containers][vector]" ) {
  63. std::vector<bool> bools;
  64. REQUIRE( ::Catch::Detail::stringify(bools) == "{ }");
  65. bools.push_back(true);
  66. REQUIRE( ::Catch::Detail::stringify(bools) == "{ true }");
  67. bools.push_back(false);
  68. REQUIRE( ::Catch::Detail::stringify(bools) == "{ true, false }");
  69. }
  70. TEST_CASE( "array<int, N> -> toString", "[toString][containers][array]" ) {
  71. std::array<int, 0> empty;
  72. REQUIRE( Catch::Detail::stringify( empty ) == "{ }" );
  73. std::array<int, 1> oneValue = {{ 42 }};
  74. REQUIRE( Catch::Detail::stringify( oneValue ) == "{ 42 }" );
  75. std::array<int, 2> twoValues = {{ 42, 250 }};
  76. REQUIRE( Catch::Detail::stringify( twoValues ) == "{ 42, 250 }" );
  77. }