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

summaryrefslogtreecommitdiffhomepage
path: root/src/Progmasoft/Catch3/ToString.cpp
diff options
context:
space:
mode:
authorLeitwolf112026-10-25 02:37:35 +0300
committerLeitwolf112026-10-25 02:37:35 +0300
commit126dc333564cb72b5115d9a9242d66694694b6d0 (patch)
tree0bce4462b2e190c39b3e1c5f697e3e4abd1a5e2a /src/Progmasoft/Catch3/ToString.cpp
parent13f81a480a93807c14dddb5abdb97355ced4d4c5 (diff)
downloadcatch3-126dc333564cb72b5115d9a9242d66694694b6d0.tar.gz
Give Catch3 its own build names and headers
Catch2 compatibility means the API tests are written in: the macros, such as TEST_CASE and REQUIRE, and the Catch:: namespace. It does not mean Catch2's build names or header paths. find_package(Catch2) found Catch3, and a project using both frameworks met two packages, two sets of targets, two Catch.cmake modules and two catch2/ header trees with the same names. Build names: - CMake: project and package Catch3, targets Catch3::Catch3 and Catch3::Catch3WithMain, libraries Catch3 and Catch3Main, options CATCH3_*, lib/cmake/Catch3 with Catch3Config, and pkg-config catch3 and catch3-with-main. The version is Catch3's, 0.3.0, compatible within a minor version. - The CMake scripts are Catch3.cmake (catch3_discover_tests), Catch3AddTests.cmake and Catch3ShardTests.cmake (catch3_add_sharded_tests); the helpers they define at ctest time have a catch3_ prefix. ParseAndAddCatchTests.cmake, deprecated upstream, is gone. - Bazel //:catch3 and //:catch3_main, Meson catch3 and catch3-with-main (with override_dependency), Xmake catch3 and catch3_main, Premake Catch3 and Catch3WithMain. Visual Studio projects get GUIDs of their own. - The Progmasoft::Catch3 target and package are gone: property testing and snapshots are part of Catch3::Catch3. Their sources now build with the library's warning flags, so property failures are built field by field (no missing initializers) and the default property name is a thread-local pointer to the name its scope owns (no global destructor). Headers: the engine moved from src/catch2 to src/Progmasoft/Catch3 with Progmasoft names, so <catch2/catch_test_macros.hpp> is <Progmasoft/Catch3/TestMacros.hpp>, <catch2/matchers/catch_matchers_string.hpp> is <Progmasoft/Catch3/Matchers/String.hpp>, and interface headers are named after their interface, such as Interfaces/IConfig.hpp. Include guards became #pragma once. The installed headers go to include/Progmasoft. src/Progmasoft/Context.cpp is StrategyContext.cpp, so no two sources share a file name. tools/scripts/upstreamFiles.txt lists the files that come from Catch2. The license check expects BSL-1.0 on them and Progmasoft's header on the rest of src/Progmasoft; the convenience-header check, the amalgamation script and the surrogate tests take the engine's headers from it. The amalgamated files are regenerated. The header-guard workflow checks for #pragma once instead of checkguard. Documents, the README and the release notes describe the new names.
Diffstat (limited to 'src/Progmasoft/Catch3/ToString.cpp')
-rw-r--r--src/Progmasoft/Catch3/ToString.cpp308
1 files changed, 308 insertions, 0 deletions
diff --git a/src/Progmasoft/Catch3/ToString.cpp b/src/Progmasoft/Catch3/ToString.cpp
new file mode 100644
index 00000000..c415f44e
--- /dev/null
+++ b/src/Progmasoft/Catch3/ToString.cpp
@@ -0,0 +1,308 @@
+
+// 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 <Progmasoft/Catch3/ToString.hpp>
+#include <Progmasoft/Catch3/Interfaces/IConfig.hpp>
+#include <Progmasoft/Catch3/Interfaces/IRegistryHub.hpp>
+#include <Progmasoft/Catch3/Internal/Context.hpp>
+#include <Progmasoft/Catch3/Internal/Polyfills.hpp>
+
+#include <iomanip>
+
+namespace Catch {
+
+namespace Detail {
+
+ namespace {
+ const int hexThreshold = 255;
+
+ struct Endianness {
+ enum Arch : uint8_t {
+ Big,
+ Little
+ };
+
+ static Arch which() {
+ int one = 1;
+ // If the lowest byte we read is non-zero, we can assume
+ // that little endian format is used.
+ auto value = *reinterpret_cast<char*>(&one);
+ return value ? Little : Big;
+ }
+ };
+
+ template<typename T>
+ std::string fpToString(T value, int precision) {
+ if (Catch::isnan(value)) {
+ return "nan";
+ }
+
+ ReusableStringStream rss;
+ rss << std::setprecision(precision)
+ << std::fixed
+ << value;
+ std::string d = rss.str();
+ std::size_t i = d.find_last_not_of('0');
+ if (i != std::string::npos && i != d.size() - 1) {
+ if (d[i] == '.')
+ i++;
+ d = d.substr(0, i + 1);
+ }
+ return d;
+ }
+ } // end unnamed namespace
+
+ std::size_t catch_strnlen( const char* str, std::size_t n ) {
+ auto ret = std::char_traits<char>::find( str, n, '\0' );
+ if ( ret != nullptr ) { return static_cast<std::size_t>( ret - str ); }
+ return n;
+ }
+
+ std::string formatTimeT(std::time_t time) {
+ std::tm timeInfo = {};
+#ifdef _MSC_VER
+ const auto err = gmtime_s( &timeInfo, &time );
+ if ( err ) {
+ return "gmtime from provided timepoint has failed. This "
+ "happens e.g. with pre-1970 dates using Microsoft libc";
+ }
+#elif defined( __MINGW32__ )
+ if ( gmtime_s( &timeInfo, &time ) != 0 ) {
+ return "gmtime from provided timepoint has failed";
+ }
+#elif defined( CATCH_PLATFORM_PLAYSTATION )
+ gmtime_s( &time, &timeInfo );
+#elif defined( __IAR_SYSTEMS_ICC__ )
+ // This toolchain does not provide gmtime_r. Copy the result immediately
+ // so the formatter does not retain the C library's static buffer.
+ const std::tm* const converted = std::gmtime( &time );
+ if ( converted == nullptr ) {
+ return "gmtime from provided timepoint has failed";
+ }
+ timeInfo = *converted;
+#else
+ if ( gmtime_r( &time, &timeInfo ) == nullptr ) {
+ return "gmtime from provided timepoint has failed";
+ }
+#endif
+
+ auto const timeStampSize = sizeof( "2017-01-16T17:06:45Z" );
+ char timeStamp[timeStampSize] = {};
+ const char* const fmt = "%Y-%m-%dT%H:%M:%SZ";
+
+ if ( std::strftime( timeStamp, timeStampSize, fmt, &timeInfo ) == 0 ) {
+ return "gmtime from provided timepoint has failed";
+ }
+ return std::string( timeStamp, timeStampSize - 1 );
+ }
+
+ std::string convertIntoString(StringRef string, bool escapeInvisibles) {
+ std::string ret;
+ // This is enough for the "don't escape invisibles" case, and a good
+ // lower bound on the "escape invisibles" case.
+ ret.reserve( string.size() + 2 );
+
+ if ( !escapeInvisibles ) {
+ ret += '"';
+ ret += string;
+ ret += '"';
+ return ret;
+ }
+
+ size_t last_start = 0;
+ auto write_to = [&]( size_t idx ) {
+ if ( last_start < idx ) {
+ ret += string.substr( last_start, idx - last_start );
+ }
+ last_start = idx + 1;
+ };
+
+ ret += '"';
+ for ( size_t i = 0; i < string.size(); ++i ) {
+ const char c = string[i];
+ if ( c == '\r' || c == '\n' || c == '\t' || c == '\f' ) {
+ write_to( i );
+ if ( c == '\r' ) { ret.append( "\\r" ); }
+ if ( c == '\n' ) { ret.append( "\\n" ); }
+ if ( c == '\t' ) { ret.append( "\\t" ); }
+ if ( c == '\f' ) { ret.append( "\\f" ); }
+ }
+ }
+ write_to( string.size() );
+ ret += '"';
+
+ return ret;
+ }
+
+ std::string convertIntoString(StringRef string) {
+ return convertIntoString(string, getCurrentContext().getConfig()->showInvisibles());
+ }
+
+ std::string rawMemoryToString( const void *object, std::size_t size ) {
+ // Reverse order for little endian architectures
+ int i = 0, end = static_cast<int>( size ), inc = 1;
+ if( Endianness::which() == Endianness::Little ) {
+ i = end-1;
+ end = inc = -1;
+ }
+
+ unsigned char const *bytes = static_cast<unsigned char const *>(object);
+ ReusableStringStream rss;
+ rss << "0x" << std::setfill('0') << std::hex;
+ for( ; i != end; i += inc )
+ rss << std::setw(2) << static_cast<unsigned>(bytes[i]);
+ return rss.str();
+ }
+
+ std::string makeExceptionHappenedString() {
+ return "{ stringification failed with an exception: \"" +
+ translateActiveException() + "\" }";
+
+ }
+
+} // end Detail namespace
+
+
+
+//// ======================================================= ////
+//
+// Out-of-line defs for full specialization of StringMaker
+//
+//// ======================================================= ////
+
+std::string StringMaker<std::string>::convert(const std::string& str) {
+ return Detail::convertIntoString( str );
+}
+
+#ifdef CATCH_CONFIG_CPP17_STRING_VIEW
+std::string StringMaker<std::string_view>::convert(std::string_view str) {
+ return Detail::convertIntoString( StringRef( str.data(), str.size() ) );
+}
+#endif
+
+std::string StringMaker<char const*>::convert(char const* str) {
+ if (str) {
+ return Detail::convertIntoString( str );
+ } else {
+ return{ "{null string}" };
+ }
+}
+std::string StringMaker<char*>::convert(char* str) { // NOLINT(readability-non-const-parameter)
+ if (str) {
+ return Detail::convertIntoString( str );
+ } else {
+ return{ "{null string}" };
+ }
+}
+
+#ifdef CATCH_CONFIG_WCHAR
+std::string StringMaker<std::wstring>::convert(const std::wstring& wstr) {
+ std::string s;
+ s.reserve(wstr.size());
+ for (auto c : wstr) {
+ s += (c <= 0xff) ? static_cast<char>(c) : '?';
+ }
+ return ::Catch::Detail::stringify(s);
+}
+
+# ifdef CATCH_CONFIG_CPP17_STRING_VIEW
+std::string StringMaker<std::wstring_view>::convert(std::wstring_view str) {
+ return StringMaker<std::wstring>::convert(std::wstring(str));
+}
+# endif
+
+std::string StringMaker<wchar_t const*>::convert(wchar_t const * str) {
+ if (str) {
+ return ::Catch::Detail::stringify(std::wstring{ str });
+ } else {
+ return{ "{null string}" };
+ }
+}
+std::string StringMaker<wchar_t *>::convert(wchar_t * str) {
+ if (str) {
+ return ::Catch::Detail::stringify(std::wstring{ str });
+ } else {
+ return{ "{null string}" };
+ }
+}
+#endif
+
+#if defined(CATCH_CONFIG_CPP17_BYTE)
+#include <cstddef>
+std::string StringMaker<std::byte>::convert(std::byte value) {
+ return ::Catch::Detail::stringify(std::to_integer<unsigned long long>(value));
+}
+#endif // defined(CATCH_CONFIG_CPP17_BYTE)
+
+std::string StringMaker<int>::convert(int value) {
+ return ::Catch::Detail::stringify(static_cast<long long>(value));
+}
+std::string StringMaker<long>::convert(long value) {
+ return ::Catch::Detail::stringify(static_cast<long long>(value));
+}
+std::string StringMaker<long long>::convert(long long value) {
+ ReusableStringStream rss;
+ rss << value;
+ if (value > Detail::hexThreshold) {
+ rss << " (0x" << std::hex << value << ')';
+ }
+ return rss.str();
+}
+
+std::string StringMaker<unsigned int>::convert(unsigned int value) {
+ return ::Catch::Detail::stringify(static_cast<unsigned long long>(value));
+}
+std::string StringMaker<unsigned long>::convert(unsigned long value) {
+ return ::Catch::Detail::stringify(static_cast<unsigned long long>(value));
+}
+std::string StringMaker<unsigned long long>::convert(unsigned long long value) {
+ ReusableStringStream rss;
+ rss << value;
+ if (value > Detail::hexThreshold) {
+ rss << " (0x" << std::hex << value << ')';
+ }
+ return rss.str();
+}
+
+std::string StringMaker<signed char>::convert(signed char value) {
+ if (value == '\r') {
+ return "'\\r'";
+ } else if (value == '\f') {
+ return "'\\f'";
+ } else if (value == '\n') {
+ return "'\\n'";
+ } else if (value == '\t') {
+ return "'\\t'";
+ } else if ('\0' <= value && value < ' ') {
+ return ::Catch::Detail::stringify(static_cast<unsigned int>(value));
+ } else {
+ char chstr[] = "' '";
+ chstr[1] = value;
+ return chstr;
+ }
+}
+std::string StringMaker<char>::convert(char c) {
+ return ::Catch::Detail::stringify(static_cast<signed char>(c));
+}
+std::string StringMaker<unsigned char>::convert(unsigned char value) {
+ return ::Catch::Detail::stringify(static_cast<char>(value));
+}
+
+int StringMaker<float>::precision = std::numeric_limits<float>::max_digits10;
+
+std::string StringMaker<float>::convert(float value) {
+ return Detail::fpToString(value, precision) + 'f';
+}
+
+int StringMaker<double>::precision = std::numeric_limits<double>::max_digits10;
+
+std::string StringMaker<double>::convert(double value) {
+ return Detail::fpToString(value, precision);
+}
+
+} // end namespace Catch