mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-22 13:26:10 +01:00
Performance improvement in AssertionInfo.
By using char const * instead of std::string we avoid significant copying per assertion. In a simple loop with 10000000 CHECKS on my system, this reduces the run time from 9.8s to 6s.
This commit is contained in:
parent
017a63da62
commit
dcab8a5971
@ -40,15 +40,17 @@ namespace Catch {
|
|||||||
struct AssertionInfo
|
struct AssertionInfo
|
||||||
{
|
{
|
||||||
AssertionInfo() {}
|
AssertionInfo() {}
|
||||||
AssertionInfo( std::string const& _macroName,
|
AssertionInfo( char const * _macroName,
|
||||||
SourceLineInfo const& _lineInfo,
|
SourceLineInfo const& _lineInfo,
|
||||||
std::string const& _capturedExpression,
|
char const * _capturedExpression,
|
||||||
ResultDisposition::Flags _resultDisposition );
|
ResultDisposition::Flags _resultDisposition,
|
||||||
|
char const * _secondArg = "");
|
||||||
|
|
||||||
std::string macroName;
|
char const * macroName;
|
||||||
SourceLineInfo lineInfo;
|
SourceLineInfo lineInfo;
|
||||||
std::string capturedExpression;
|
char const * capturedExpression;
|
||||||
ResultDisposition::Flags resultDisposition;
|
ResultDisposition::Flags resultDisposition;
|
||||||
|
char const * secondArg;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct AssertionResultData
|
struct AssertionResultData
|
||||||
|
@ -13,14 +13,16 @@
|
|||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
|
|
||||||
AssertionInfo::AssertionInfo( std::string const& _macroName,
|
AssertionInfo::AssertionInfo( char const * _macroName,
|
||||||
SourceLineInfo const& _lineInfo,
|
SourceLineInfo const& _lineInfo,
|
||||||
std::string const& _capturedExpression,
|
char const * _capturedExpression,
|
||||||
ResultDisposition::Flags _resultDisposition )
|
ResultDisposition::Flags _resultDisposition,
|
||||||
|
char const * _secondArg)
|
||||||
: macroName( _macroName ),
|
: macroName( _macroName ),
|
||||||
lineInfo( _lineInfo ),
|
lineInfo( _lineInfo ),
|
||||||
capturedExpression( _capturedExpression ),
|
capturedExpression( _capturedExpression ),
|
||||||
resultDisposition( _resultDisposition )
|
resultDisposition( _resultDisposition ),
|
||||||
|
secondArg( _secondArg )
|
||||||
{}
|
{}
|
||||||
|
|
||||||
AssertionResult::AssertionResult() {}
|
AssertionResult::AssertionResult() {}
|
||||||
@ -47,24 +49,30 @@ namespace Catch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool AssertionResult::hasExpression() const {
|
bool AssertionResult::hasExpression() const {
|
||||||
return !m_info.capturedExpression.empty();
|
return m_info.capturedExpression[0] != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AssertionResult::hasMessage() const {
|
bool AssertionResult::hasMessage() const {
|
||||||
return !m_resultData.message.empty();
|
return !m_resultData.message.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string capturedExpressionWithSecondArgument( char const * capturedExpression, char const * secondArg ) {
|
||||||
|
return (secondArg[0] == 0 || secondArg[0] == '"' && secondArg[1] == '"')
|
||||||
|
? capturedExpression
|
||||||
|
: std::string(capturedExpression) + ", " + secondArg;
|
||||||
|
}
|
||||||
|
|
||||||
std::string AssertionResult::getExpression() const {
|
std::string AssertionResult::getExpression() const {
|
||||||
if( isFalseTest( m_info.resultDisposition ) )
|
if( isFalseTest( m_info.resultDisposition ) )
|
||||||
return '!' + m_info.capturedExpression;
|
return '!' + capturedExpressionWithSecondArgument(m_info.capturedExpression, m_info.secondArg);
|
||||||
else
|
else
|
||||||
return m_info.capturedExpression;
|
return capturedExpressionWithSecondArgument(m_info.capturedExpression, m_info.secondArg);
|
||||||
}
|
}
|
||||||
std::string AssertionResult::getExpressionInMacro() const {
|
std::string AssertionResult::getExpressionInMacro() const {
|
||||||
if( m_info.macroName.empty() )
|
if( m_info.macroName[0] == 0 )
|
||||||
return m_info.capturedExpression;
|
return capturedExpressionWithSecondArgument(m_info.capturedExpression, m_info.secondArg);
|
||||||
else
|
else
|
||||||
return m_info.macroName + "( " + m_info.capturedExpression + " )";
|
return std::string(m_info.macroName) + "( " + capturedExpressionWithSecondArgument(m_info.capturedExpression, m_info.secondArg) + " )";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AssertionResult::hasExpandedExpression() const {
|
bool AssertionResult::hasExpandedExpression() const {
|
||||||
|
@ -18,17 +18,12 @@
|
|||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
std::string capturedExpressionWithSecondArgument( std::string const& capturedExpression, std::string const& secondArg ) {
|
|
||||||
return secondArg.empty() || secondArg == "\"\""
|
|
||||||
? capturedExpression
|
|
||||||
: capturedExpression + ", " + secondArg;
|
|
||||||
}
|
|
||||||
ResultBuilder::ResultBuilder( char const* macroName,
|
ResultBuilder::ResultBuilder( char const* macroName,
|
||||||
SourceLineInfo const& lineInfo,
|
SourceLineInfo const& lineInfo,
|
||||||
char const* capturedExpression,
|
char const* capturedExpression,
|
||||||
ResultDisposition::Flags resultDisposition,
|
ResultDisposition::Flags resultDisposition,
|
||||||
char const* secondArg )
|
char const* secondArg )
|
||||||
: m_assertionInfo( macroName, lineInfo, capturedExpressionWithSecondArgument( capturedExpression, secondArg ), resultDisposition ),
|
: m_assertionInfo( macroName, lineInfo, capturedExpression, resultDisposition, secondArg ),
|
||||||
m_shouldDebugBreak( false ),
|
m_shouldDebugBreak( false ),
|
||||||
m_shouldThrow( false ),
|
m_shouldThrow( false ),
|
||||||
m_guardException( false )
|
m_guardException( false )
|
||||||
@ -82,7 +77,7 @@ namespace Catch {
|
|||||||
assert( !isFalseTest( m_assertionInfo.resultDisposition ) );
|
assert( !isFalseTest( m_assertionInfo.resultDisposition ) );
|
||||||
AssertionResultData data = m_data;
|
AssertionResultData data = m_data;
|
||||||
data.resultType = ResultWas::Ok;
|
data.resultType = ResultWas::Ok;
|
||||||
data.reconstructedExpression = m_assertionInfo.capturedExpression;
|
data.reconstructedExpression = capturedExpressionWithSecondArgument(m_assertionInfo.capturedExpression, m_assertionInfo.secondArg);
|
||||||
|
|
||||||
std::string actualMessage = Catch::translateActiveException();
|
std::string actualMessage = Catch::translateActiveException();
|
||||||
if( !matcher.match( actualMessage ) ) {
|
if( !matcher.match( actualMessage ) ) {
|
||||||
@ -154,7 +149,7 @@ namespace Catch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ResultBuilder::reconstructExpression( std::string& dest ) const {
|
void ResultBuilder::reconstructExpression( std::string& dest ) const {
|
||||||
dest = m_assertionInfo.capturedExpression;
|
dest = capturedExpressionWithSecondArgument(m_assertionInfo.capturedExpression, m_assertionInfo.secondArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ResultBuilder::setExceptionGuard() {
|
void ResultBuilder::setExceptionGuard() {
|
||||||
|
@ -150,7 +150,7 @@ namespace Catch {
|
|||||||
static_cast<void>(m_reporter->assertionEnded(AssertionStats(result, m_messages, m_totals)));
|
static_cast<void>(m_reporter->assertionEnded(AssertionStats(result, m_messages, m_totals)));
|
||||||
|
|
||||||
// Reset working state
|
// Reset working state
|
||||||
m_lastAssertionInfo = AssertionInfo( std::string(), m_lastAssertionInfo.lineInfo, "{Unknown expression after the reported line}" , m_lastAssertionInfo.resultDisposition );
|
m_lastAssertionInfo = AssertionInfo( "", m_lastAssertionInfo.lineInfo, "{Unknown expression after the reported line}" , m_lastAssertionInfo.resultDisposition );
|
||||||
m_lastResult = result;
|
m_lastResult = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,7 +280,7 @@ namespace Catch {
|
|||||||
double duration = 0;
|
double duration = 0;
|
||||||
m_shouldReportUnexpected = true;
|
m_shouldReportUnexpected = true;
|
||||||
try {
|
try {
|
||||||
m_lastAssertionInfo = AssertionInfo( "TEST_CASE", testCaseInfo.lineInfo, std::string(), ResultDisposition::Normal );
|
m_lastAssertionInfo = AssertionInfo( "TEST_CASE", testCaseInfo.lineInfo, "", ResultDisposition::Normal );
|
||||||
|
|
||||||
seedRng( *m_config );
|
seedRng( *m_config );
|
||||||
|
|
||||||
@ -332,9 +332,9 @@ namespace Catch {
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
ResultBuilder makeUnexpectedResultBuilder() const {
|
ResultBuilder makeUnexpectedResultBuilder() const {
|
||||||
return ResultBuilder( m_lastAssertionInfo.macroName.c_str(),
|
return ResultBuilder( m_lastAssertionInfo.macroName,
|
||||||
m_lastAssertionInfo.lineInfo,
|
m_lastAssertionInfo.lineInfo,
|
||||||
m_lastAssertionInfo.capturedExpression.c_str(),
|
m_lastAssertionInfo.capturedExpression,
|
||||||
m_lastAssertionInfo.resultDisposition );
|
m_lastAssertionInfo.resultDisposition );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user