ToStringVector.tests.cpp 3.4 KB

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