diff options
| author | Martin Hořeňovský | 2024-09-13 11:16:20 +0200 |
|---|---|---|
| committer | Martin Hořeňovský | 2024-09-13 16:40:11 +0200 |
| commit | 18df97df00848e91bfe1b63408bba19f8b8237ae (patch) | |
| tree | 91b5056d1a6b945500c397eea60437b4a3ad79f1 /src/catch2/internal | |
| parent | e97ebe62e700dad37fe460644e121ec462e75596 (diff) | |
| download | catch3-18df97df00848e91bfe1b63408bba19f8b8237ae.tar.gz | |
Sprinkle some constexpr around to make Jason happy
Most of these will not matter in practice due to C++14 imposing
significant limitations on what else we can make constexpr, and we cannot
have references outliving the constexpr context either way.
Diffstat (limited to 'src/catch2/internal')
| -rw-r--r-- | src/catch2/internal/catch_assertion_handler.hpp | 2 | ||||
| -rw-r--r-- | src/catch2/internal/catch_context.cpp | 6 | ||||
| -rw-r--r-- | src/catch2/internal/catch_context.hpp | 13 | ||||
| -rw-r--r-- | src/catch2/internal/catch_decomposer.hpp | 11 | ||||
| -rw-r--r-- | src/catch2/internal/catch_is_permutation.hpp | 27 | ||||
| -rw-r--r-- | src/catch2/internal/catch_lazy_expr.hpp | 6 | ||||
| -rw-r--r-- | src/catch2/internal/catch_random_integer_helpers.hpp | 4 | ||||
| -rw-r--r-- | src/catch2/internal/catch_result_type.cpp | 26 | ||||
| -rw-r--r-- | src/catch2/internal/catch_result_type.hpp | 23 | ||||
| -rw-r--r-- | src/catch2/internal/catch_test_registry.cpp | 2 | ||||
| -rw-r--r-- | src/catch2/internal/catch_test_registry.hpp | 6 | ||||
| -rw-r--r-- | src/catch2/internal/catch_uniform_integer_distribution.hpp | 16 | ||||
| -rw-r--r-- | src/catch2/internal/catch_xmlwriter.cpp | 24 | ||||
| -rw-r--r-- | src/catch2/internal/catch_xmlwriter.hpp | 18 |
14 files changed, 81 insertions, 103 deletions
diff --git a/src/catch2/internal/catch_assertion_handler.hpp b/src/catch2/internal/catch_assertion_handler.hpp index 098ed7a9..c71c6898 100644 --- a/src/catch2/internal/catch_assertion_handler.hpp +++ b/src/catch2/internal/catch_assertion_handler.hpp @@ -42,7 +42,7 @@ namespace Catch { template<typename T> - void handleExpr( ExprLhs<T> const& expr ) { + constexpr void handleExpr( ExprLhs<T> const& expr ) { handleExpr( expr.makeUnaryExpr() ); } void handleExpr( ITransientExpression const& expr ); diff --git a/src/catch2/internal/catch_context.cpp b/src/catch2/internal/catch_context.cpp index 3b1cc277..8acf1eda 100644 --- a/src/catch2/internal/catch_context.cpp +++ b/src/catch2/internal/catch_context.cpp @@ -27,12 +27,6 @@ namespace Catch { return *Context::currentContext; } - void Context::setResultCapture( IResultCapture* resultCapture ) { - m_resultCapture = resultCapture; - } - - void Context::setConfig( IConfig const* config ) { m_config = config; } - SimplePcg32& sharedRng() { static SimplePcg32 s_rng; return s_rng; diff --git a/src/catch2/internal/catch_context.hpp b/src/catch2/internal/catch_context.hpp index 6ccb3b31..4d8a5da1 100644 --- a/src/catch2/internal/catch_context.hpp +++ b/src/catch2/internal/catch_context.hpp @@ -26,10 +26,15 @@ namespace Catch { friend void cleanUpContext(); public: - IResultCapture* getResultCapture() const { return m_resultCapture; } - IConfig const* getConfig() const { return m_config; } - void setResultCapture( IResultCapture* resultCapture ); - void setConfig( IConfig const* config ); + constexpr IResultCapture* getResultCapture() const { + return m_resultCapture; + } + constexpr IConfig const* getConfig() const { return m_config; } + constexpr void setResultCapture( IResultCapture* resultCapture ) { + m_resultCapture = resultCapture; + } + constexpr void setConfig( IConfig const* config ) { m_config = config; } + }; Context& getCurrentMutableContext(); diff --git a/src/catch2/internal/catch_decomposer.hpp b/src/catch2/internal/catch_decomposer.hpp index 7962c9ef..adce89f2 100644 --- a/src/catch2/internal/catch_decomposer.hpp +++ b/src/catch2/internal/catch_decomposer.hpp @@ -157,6 +157,9 @@ namespace Catch { bool m_isBinaryExpression; bool m_result; + protected: + ~ITransientExpression() = default; + public: constexpr auto isBinaryExpression() const -> bool { return m_isBinaryExpression; } constexpr auto getResult() const -> bool { return m_result; } @@ -168,17 +171,13 @@ namespace Catch { m_result( result ) {} - ITransientExpression() = default; - ITransientExpression(ITransientExpression const&) = default; - ITransientExpression& operator=(ITransientExpression const&) = default; + constexpr ITransientExpression( ITransientExpression const& ) = default; + constexpr ITransientExpression& operator=( ITransientExpression const& ) = default; friend std::ostream& operator<<(std::ostream& out, ITransientExpression const& expr) { expr.streamReconstructedExpression(out); return out; } - - protected: - ~ITransientExpression() = default; }; void formatReconstructedExpression( std::ostream &os, std::string const& lhs, StringRef op, std::string const& rhs ); diff --git a/src/catch2/internal/catch_is_permutation.hpp b/src/catch2/internal/catch_is_permutation.hpp index 708053d3..c77a6d3b 100644 --- a/src/catch2/internal/catch_is_permutation.hpp +++ b/src/catch2/internal/catch_is_permutation.hpp @@ -18,6 +18,7 @@ namespace Catch { typename Sentinel, typename T, typename Comparator> + constexpr ForwardIter find_sentinel( ForwardIter start, Sentinel sentinel, T const& value, @@ -33,6 +34,7 @@ namespace Catch { typename Sentinel, typename T, typename Comparator> + constexpr std::ptrdiff_t count_sentinel( ForwardIter start, Sentinel sentinel, T const& value, @@ -46,6 +48,7 @@ namespace Catch { } template <typename ForwardIter, typename Sentinel> + constexpr std::enable_if_t<!std::is_same<ForwardIter, Sentinel>::value, std::ptrdiff_t> sentinel_distance( ForwardIter iter, const Sentinel sentinel ) { @@ -58,8 +61,8 @@ namespace Catch { } template <typename ForwardIter> - std::ptrdiff_t sentinel_distance( ForwardIter first, - ForwardIter last ) { + constexpr std::ptrdiff_t sentinel_distance( ForwardIter first, + ForwardIter last ) { return std::distance( first, last ); } @@ -68,11 +71,11 @@ namespace Catch { typename ForwardIter2, typename Sentinel2, typename Comparator> - bool check_element_counts( ForwardIter1 first_1, - const Sentinel1 end_1, - ForwardIter2 first_2, - const Sentinel2 end_2, - Comparator cmp ) { + constexpr bool check_element_counts( ForwardIter1 first_1, + const Sentinel1 end_1, + ForwardIter2 first_2, + const Sentinel2 end_2, + Comparator cmp ) { auto cursor = first_1; while ( cursor != end_1 ) { if ( find_sentinel( first_1, cursor, *cursor, cmp ) == @@ -102,11 +105,11 @@ namespace Catch { typename ForwardIter2, typename Sentinel2, typename Comparator> - bool is_permutation( ForwardIter1 first_1, - const Sentinel1 end_1, - ForwardIter2 first_2, - const Sentinel2 end_2, - Comparator cmp ) { + constexpr bool is_permutation( ForwardIter1 first_1, + const Sentinel1 end_1, + ForwardIter2 first_2, + const Sentinel2 end_2, + Comparator cmp ) { // TODO: no optimization for stronger iterators, because we would also have to constrain on sentinel vs not sentinel types // TODO: Comparator has to be "both sides", e.g. a == b => b == a // This skips shared prefix of the two ranges diff --git a/src/catch2/internal/catch_lazy_expr.hpp b/src/catch2/internal/catch_lazy_expr.hpp index 36e0ac50..c6ff2241 100644 --- a/src/catch2/internal/catch_lazy_expr.hpp +++ b/src/catch2/internal/catch_lazy_expr.hpp @@ -22,13 +22,13 @@ namespace Catch { ITransientExpression const* m_transientExpression = nullptr; bool m_isNegated; public: - LazyExpression( bool isNegated ): + constexpr LazyExpression( bool isNegated ): m_isNegated(isNegated) {} - LazyExpression(LazyExpression const& other) = default; + constexpr LazyExpression(LazyExpression const& other) = default; LazyExpression& operator = ( LazyExpression const& ) = delete; - explicit operator bool() const { + constexpr explicit operator bool() const { return m_transientExpression != nullptr; } diff --git a/src/catch2/internal/catch_random_integer_helpers.hpp b/src/catch2/internal/catch_random_integer_helpers.hpp index cb5e004f..be4bbe92 100644 --- a/src/catch2/internal/catch_random_integer_helpers.hpp +++ b/src/catch2/internal/catch_random_integer_helpers.hpp @@ -69,7 +69,7 @@ namespace Catch { struct ExtendedMultResult { T upper; T lower; - bool operator==( ExtendedMultResult const& rhs ) const { + constexpr bool operator==( ExtendedMultResult const& rhs ) const { return upper == rhs.upper && lower == rhs.lower; } }; @@ -187,6 +187,7 @@ namespace Catch { * get by simple casting ([0, ..., INT_MAX, INT_MIN, ..., -1]) */ template <typename OriginalType, typename UnsignedType> + constexpr std::enable_if_t<std::is_signed<OriginalType>::value, UnsignedType> transposeToNaturalOrder( UnsignedType in ) { static_assert( @@ -207,6 +208,7 @@ namespace Catch { template <typename OriginalType, typename UnsignedType> + constexpr std::enable_if_t<std::is_unsigned<OriginalType>::value, UnsignedType> transposeToNaturalOrder(UnsignedType in) { static_assert( diff --git a/src/catch2/internal/catch_result_type.cpp b/src/catch2/internal/catch_result_type.cpp deleted file mode 100644 index 6cedce71..00000000 --- a/src/catch2/internal/catch_result_type.cpp +++ /dev/null @@ -1,26 +0,0 @@ - -// 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 <catch2/internal/catch_result_type.hpp> - -namespace Catch { - - bool isOk( ResultWas::OfType resultType ) { - return ( resultType & ResultWas::FailureBit ) == 0; - } - bool isJustInfo( int flags ) { - return flags == ResultWas::Info; - } - - ResultDisposition::Flags operator | ( ResultDisposition::Flags lhs, ResultDisposition::Flags rhs ) { - return static_cast<ResultDisposition::Flags>( static_cast<int>( lhs ) | static_cast<int>( rhs ) ); - } - - bool shouldContinueOnFailure( int flags ) { return ( flags & ResultDisposition::ContinueOnFailure ) != 0; } - bool shouldSuppressFailure( int flags ) { return ( flags & ResultDisposition::SuppressFail ) != 0; } - -} // end namespace Catch diff --git a/src/catch2/internal/catch_result_type.hpp b/src/catch2/internal/catch_result_type.hpp index e66afaff..69a6ef14 100644 --- a/src/catch2/internal/catch_result_type.hpp +++ b/src/catch2/internal/catch_result_type.hpp @@ -33,8 +33,10 @@ namespace Catch { }; }; - bool isOk( ResultWas::OfType resultType ); - bool isJustInfo( int flags ); + constexpr bool isOk( ResultWas::OfType resultType ) { + return ( resultType & ResultWas::FailureBit ) == 0; + } + constexpr bool isJustInfo( int flags ) { return flags == ResultWas::Info; } // ResultDisposition::Flags enum @@ -46,11 +48,18 @@ namespace Catch { SuppressFail = 0x08 // Failures are reported but do not fail the test }; }; - ResultDisposition::Flags operator | ( ResultDisposition::Flags lhs, ResultDisposition::Flags rhs ); - - bool shouldContinueOnFailure( int flags ); - inline bool isFalseTest( int flags ) { return ( flags & ResultDisposition::FalseTest ) != 0; } - bool shouldSuppressFailure( int flags ); + constexpr ResultDisposition::Flags operator|( ResultDisposition::Flags lhs, + ResultDisposition::Flags rhs ) { + return static_cast<ResultDisposition::Flags>( static_cast<int>( lhs ) | + static_cast<int>( rhs ) ); + } + + constexpr bool isFalseTest( int flags ) { + return ( flags & ResultDisposition::FalseTest ) != 0; + } + constexpr bool shouldSuppressFailure( int flags ) { + return ( flags & ResultDisposition::SuppressFail ) != 0; + } } // end namespace Catch diff --git a/src/catch2/internal/catch_test_registry.cpp b/src/catch2/internal/catch_test_registry.cpp index 07e44617..d017c50e 100644 --- a/src/catch2/internal/catch_test_registry.cpp +++ b/src/catch2/internal/catch_test_registry.cpp @@ -54,7 +54,7 @@ namespace Catch { TestType m_testAsFunction; public: - TestInvokerAsFunction( TestType testAsFunction ) noexcept: + constexpr TestInvokerAsFunction( TestType testAsFunction ) noexcept: m_testAsFunction( testAsFunction ) {} void invoke() const override { m_testAsFunction(); } diff --git a/src/catch2/internal/catch_test_registry.hpp b/src/catch2/internal/catch_test_registry.hpp index e275f2b9..5c3a226d 100644 --- a/src/catch2/internal/catch_test_registry.hpp +++ b/src/catch2/internal/catch_test_registry.hpp @@ -32,7 +32,8 @@ template<typename C> class TestInvokerAsMethod : public ITestInvoker { void (C::*m_testAsMethod)(); public: - TestInvokerAsMethod( void (C::*testAsMethod)() ) noexcept : m_testAsMethod( testAsMethod ) {} + constexpr TestInvokerAsMethod( void ( C::*testAsMethod )() ) noexcept: + m_testAsMethod( testAsMethod ) {} void invoke() const override { C obj; @@ -53,7 +54,8 @@ class TestInvokerFixture : public ITestInvoker { Detail::unique_ptr<C> m_fixture = nullptr; public: - TestInvokerFixture( void ( C::*testAsMethod )() const) noexcept : m_testAsMethod( testAsMethod ) {} + constexpr TestInvokerFixture( void ( C::*testAsMethod )() const ) noexcept: + m_testAsMethod( testAsMethod ) {} void prepareTestCase() override { m_fixture = Detail::make_unique<C>(); diff --git a/src/catch2/internal/catch_uniform_integer_distribution.hpp b/src/catch2/internal/catch_uniform_integer_distribution.hpp index 3332489a..799a93e2 100644 --- a/src/catch2/internal/catch_uniform_integer_distribution.hpp +++ b/src/catch2/internal/catch_uniform_integer_distribution.hpp @@ -48,24 +48,24 @@ class uniform_integer_distribution { // distribution will be reused many times and this is an optimization. UnsignedIntegerType m_rejection_threshold = 0; - UnsignedIntegerType computeDistance(IntegerType a, IntegerType b) const { + static constexpr UnsignedIntegerType computeDistance(IntegerType a, IntegerType b) { // This overflows and returns 0 if a == 0 and b == TYPE_MAX. // We handle that later when generating the number. return transposeTo(b) - transposeTo(a) + 1; } - static UnsignedIntegerType computeRejectionThreshold(UnsignedIntegerType ab_distance) { + static constexpr UnsignedIntegerType computeRejectionThreshold(UnsignedIntegerType ab_distance) { // distance == 0 means that we will return all possible values from // the type's range, and that we shouldn't reject anything. if ( ab_distance == 0 ) { return 0; } return ( ~ab_distance + 1 ) % ab_distance; } - static UnsignedIntegerType transposeTo(IntegerType in) { + static constexpr UnsignedIntegerType transposeTo(IntegerType in) { return Detail::transposeToNaturalOrder<IntegerType>( static_cast<UnsignedIntegerType>( in ) ); } - static IntegerType transposeBack(UnsignedIntegerType in) { + static constexpr IntegerType transposeBack(UnsignedIntegerType in) { return static_cast<IntegerType>( Detail::transposeToNaturalOrder<IntegerType>(in) ); } @@ -73,7 +73,7 @@ class uniform_integer_distribution { public: using result_type = IntegerType; - uniform_integer_distribution( IntegerType a, IntegerType b ): + constexpr uniform_integer_distribution( IntegerType a, IntegerType b ): m_a( transposeTo(a) ), m_ab_distance( computeDistance(a, b) ), m_rejection_threshold( computeRejectionThreshold(m_ab_distance) ) { @@ -81,7 +81,7 @@ public: } template <typename Generator> - result_type operator()( Generator& g ) { + constexpr result_type operator()( Generator& g ) { // All possible values of result_type are valid. if ( m_ab_distance == 0 ) { return transposeBack( Detail::fillBitsFrom<UnsignedIntegerType>( g ) ); @@ -99,8 +99,8 @@ public: return transposeBack(m_a + emul.upper); } - result_type a() const { return transposeBack(m_a); } - result_type b() const { return transposeBack(m_ab_distance + m_a - 1); } + constexpr result_type a() const { return transposeBack(m_a); } + constexpr result_type b() const { return transposeBack(m_ab_distance + m_a - 1); } }; } // end namespace Catch diff --git a/src/catch2/internal/catch_xmlwriter.cpp b/src/catch2/internal/catch_xmlwriter.cpp index 6c1d45df..ccf63a56 100644 --- a/src/catch2/internal/catch_xmlwriter.cpp +++ b/src/catch2/internal/catch_xmlwriter.cpp @@ -53,36 +53,16 @@ namespace { os.flags(f); } - bool shouldNewline(XmlFormatting fmt) { + constexpr bool shouldNewline(XmlFormatting fmt) { return !!(static_cast<std::underlying_type_t<XmlFormatting>>(fmt & XmlFormatting::Newline)); } - bool shouldIndent(XmlFormatting fmt) { + constexpr bool shouldIndent(XmlFormatting fmt) { return !!(static_cast<std::underlying_type_t<XmlFormatting>>(fmt & XmlFormatting::Indent)); } } // anonymous namespace - XmlFormatting operator | (XmlFormatting lhs, XmlFormatting rhs) { - return static_cast<XmlFormatting>( - static_cast<std::underlying_type_t<XmlFormatting>>(lhs) | - static_cast<std::underlying_type_t<XmlFormatting>>(rhs) - ); - } - - XmlFormatting operator & (XmlFormatting lhs, XmlFormatting rhs) { - return static_cast<XmlFormatting>( - static_cast<std::underlying_type_t<XmlFormatting>>(lhs) & - static_cast<std::underlying_type_t<XmlFormatting>>(rhs) - ); - } - - - XmlEncode::XmlEncode( StringRef str, ForWhat forWhat ) - : m_str( str ), - m_forWhat( forWhat ) - {} - void XmlEncode::encodeTo( std::ostream& os ) const { // Apostrophe escaping not necessary if we always use " to write attributes // (see: http://www.w3.org/TR/xml/#syntax) diff --git a/src/catch2/internal/catch_xmlwriter.hpp b/src/catch2/internal/catch_xmlwriter.hpp index ec55f3c4..e0476b90 100644 --- a/src/catch2/internal/catch_xmlwriter.hpp +++ b/src/catch2/internal/catch_xmlwriter.hpp @@ -15,14 +15,22 @@ #include <vector> namespace Catch { - enum class XmlFormatting { + enum class XmlFormatting : uint8_t { None = 0x00, Indent = 0x01, Newline = 0x02, }; - XmlFormatting operator | (XmlFormatting lhs, XmlFormatting rhs); - XmlFormatting operator & (XmlFormatting lhs, XmlFormatting rhs); + constexpr XmlFormatting operator|( XmlFormatting lhs, XmlFormatting rhs ) { + return static_cast<XmlFormatting>( static_cast<uint8_t>( lhs ) | + static_cast<uint8_t>( rhs ) ); + } + + constexpr XmlFormatting operator&( XmlFormatting lhs, XmlFormatting rhs ) { + return static_cast<XmlFormatting>( static_cast<uint8_t>( lhs ) & + static_cast<uint8_t>( rhs ) ); + } + /** * Helper for XML-encoding text (escaping angle brackets, quotes, etc) @@ -34,7 +42,9 @@ namespace Catch { public: enum ForWhat { ForTextNodes, ForAttributes }; - XmlEncode( StringRef str, ForWhat forWhat = ForTextNodes ); + constexpr XmlEncode( StringRef str, ForWhat forWhat = ForTextNodes ): + m_str( str ), m_forWhat( forWhat ) {} + void encodeTo( std::ostream& os ) const; |