From bc524d70253a4ab2fe40c3ca3e5666e267c0a4d1 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Sun, 29 Jun 2025 19:25:29 +0100 Subject: import catch2 3.8.1 --- tests/SelfTest/UsageTests/ToStringVector.tests.cpp | 94 ++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 tests/SelfTest/UsageTests/ToStringVector.tests.cpp (limited to 'tests/SelfTest/UsageTests/ToStringVector.tests.cpp') diff --git a/tests/SelfTest/UsageTests/ToStringVector.tests.cpp b/tests/SelfTest/UsageTests/ToStringVector.tests.cpp new file mode 100644 index 0000000..c042744 --- /dev/null +++ b/tests/SelfTest/UsageTests/ToStringVector.tests.cpp @@ -0,0 +1,94 @@ + +// Copyright Catch2 Authors +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// SPDX-License-Identifier: BSL-1.0 + +#include +#include +#include + +// vector +TEST_CASE( "vector -> toString", "[toString][vector]" ) +{ + std::vector vv; + REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" ); + vv.push_back( 42 ); + REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42 }" ); + vv.push_back( 250 ); + REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42, 250 }" ); +} + +TEST_CASE( "vector -> toString", "[toString][vector]" ) +{ + std::vector vv; + REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" ); + vv.emplace_back( "hello" ); + REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\" }" ); + vv.emplace_back( "world" ); + REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\", \"world\" }" ); +} + +namespace { + /* Minimal Allocator */ + template + struct minimal_allocator { + using value_type = T; + using size_type = std::size_t; + + minimal_allocator() = default; + template + minimal_allocator(const minimal_allocator&) {} + + + T *allocate( size_type n ) { + return static_cast( ::operator new( n * sizeof(T) ) ); + } + void deallocate( T *p, size_type /*n*/ ) { + ::operator delete( static_cast(p) ); + } + template + bool operator==( const minimal_allocator& ) const { return true; } + template + bool operator!=( const minimal_allocator& ) const { return false; } + }; +} + +TEST_CASE( "vector -> toString", "[toString][vector,allocator]" ) { + std::vector > vv; + REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" ); + vv.push_back( 42 ); + REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42 }" ); + vv.push_back( 250 ); + REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42, 250 }" ); +} + +TEST_CASE( "vec> -> toString", "[toString][vector,allocator]" ) { + using inner = std::vector>; + using vector = std::vector; + vector v; + REQUIRE( ::Catch::Detail::stringify(v) == "{ }" ); + v.push_back( inner { "hello" } ); + v.push_back( inner { "world" } ); + REQUIRE( ::Catch::Detail::stringify(v) == "{ { \"hello\" }, { \"world\" } }" ); +} + +// Based on PR by mat-so: https://github.com/catchorg/Catch2/pull/606/files#diff-43562f40f8c6dcfe2c54557316e0f852 +TEST_CASE( "vector -> toString", "[toString][containers][vector]" ) { + std::vector bools; + REQUIRE( ::Catch::Detail::stringify(bools) == "{ }"); + bools.push_back(true); + REQUIRE( ::Catch::Detail::stringify(bools) == "{ true }"); + bools.push_back(false); + REQUIRE( ::Catch::Detail::stringify(bools) == "{ true, false }"); +} +TEST_CASE( "array -> toString", "[toString][containers][array]" ) { + std::array empty; + REQUIRE( Catch::Detail::stringify( empty ) == "{ }" ); + std::array oneValue = {{ 42 }}; + REQUIRE( Catch::Detail::stringify( oneValue ) == "{ 42 }" ); + std::array twoValues = {{ 42, 250 }}; + REQUIRE( Catch::Detail::stringify( twoValues ) == "{ 42, 250 }" ); +} -- cgit v1.2.3