From 804de7ef3cbde4ff9bb447ccaeef9d7aa4c0d27f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 1 Jun 2022 11:01:55 +0200 Subject: [PATCH] TestSuite: make a non-templated base for Compare::Container. Same as was done for the generic Comparator, but here it's quite a lot heavier so the impact should be bigger. --- src/Corrade/TestSuite/CMakeLists.txt | 1 + src/Corrade/TestSuite/Comparator.cpp | 2 +- src/Corrade/TestSuite/Comparator.h | 12 ++--- src/Corrade/TestSuite/Compare/Container.h | 57 ++++++++++++----------- 4 files changed, 37 insertions(+), 35 deletions(-) diff --git a/src/Corrade/TestSuite/CMakeLists.txt b/src/Corrade/TestSuite/CMakeLists.txt index 1a3f3be8d..55ec26064 100644 --- a/src/Corrade/TestSuite/CMakeLists.txt +++ b/src/Corrade/TestSuite/CMakeLists.txt @@ -32,6 +32,7 @@ set(CorradeTestSuite_SRCS Comparator.cpp Tester.cpp + Compare/Container.cpp Compare/File.cpp Compare/FileToString.cpp Compare/FloatingPoint.cpp diff --git a/src/Corrade/TestSuite/Comparator.cpp b/src/Corrade/TestSuite/Comparator.cpp index 7475c7b9b..d55870d1c 100644 --- a/src/Corrade/TestSuite/Comparator.cpp +++ b/src/Corrade/TestSuite/Comparator.cpp @@ -60,7 +60,7 @@ Utility::Debug& operator<<(Utility::Debug& debug, const ComparisonStatusFlags va namespace Implementation { -void ComparatorBase::printMessage(ComparisonStatusFlags, Utility::Debug& out, const char* const actual, const char* const expected, void(*printer)(Utility::Debug&, const void*)) { +void ComparatorBase::printMessage(ComparisonStatusFlags, Utility::Debug& out, const char* const actual, const char* const expected, void(*printer)(Utility::Debug&, const void*)) const { CORRADE_INTERNAL_ASSERT(actualValue && expectedValue); out << "Values" << actual << "and" << expected << "are not the same, actual is\n "; printer(out, actualValue); diff --git a/src/Corrade/TestSuite/Comparator.h b/src/Corrade/TestSuite/Comparator.h index cda63f217..8a8b7b691 100644 --- a/src/Corrade/TestSuite/Comparator.h +++ b/src/Corrade/TestSuite/Comparator.h @@ -114,15 +114,13 @@ namespace Implementation { class CORRADE_TESTSUITE_EXPORT ComparatorBase { public: - explicit ComparatorBase(): actualValue{}, expectedValue{} {} - - void saveDiagnostic(ComparisonStatusFlags status, Utility::Debug& out, Containers::StringView path); + void saveDiagnostic(ComparisonStatusFlags status, Utility::Debug& out, Containers::StringView path); // TODO why even this?? protected: - void printMessage(ComparisonStatusFlags status, Utility::Debug& out, const char* actual, const char* expected, void(*printer)(Utility::Debug&, const void*)); + void printMessage(ComparisonStatusFlags status, Utility::Debug& out, const char* actual, const char* expected, void(*printer)(Utility::Debug&, const void*)) const; - const void* actualValue; - const void* expectedValue; + const void* actualValue{}; + const void* expectedValue{}; }; } @@ -276,7 +274,7 @@ template ComparisonStatusFlags Comparator::operator()(const T& actua return ComparisonStatusFlag::Failed; } -template void Comparator::printMessage(ComparisonStatusFlags status, Utility::Debug& out, const char* actual, const char* expected) { +template void Comparator::printMessage(const ComparisonStatusFlags status, Utility::Debug& out, const char* const actual, const char* const expected) { Implementation::ComparatorBase::printMessage(status, out, actual, expected, [](Utility::Debug& out, const void* value) { out << *static_cast(value); diff --git a/src/Corrade/TestSuite/Compare/Container.h b/src/Corrade/TestSuite/Compare/Container.h index 09c63fc87..f5993fff8 100644 --- a/src/Corrade/TestSuite/Compare/Container.h +++ b/src/Corrade/TestSuite/Compare/Container.h @@ -59,36 +59,48 @@ template class Container {}; } +namespace Implementation { + +class CORRADE_TESTSUITE_EXPORT ContainerComparatorBase { + protected: + void printMessage(ComparisonStatusFlags status, Utility::Debug& out, const char* actual, const char* expected, void(*printer)(Utility::Debug&, const void*), void(*itemPrinter)(Utility::Debug&, const void*, std::size_t)) const; + + const void* _actualContents{}; + const void* _expectedContents{}; + std::size_t _actualContentsSize{}; + std::size_t _expectedContentsSize{}; + std::size_t _firstDifferent{}; +}; + +} + #ifndef DOXYGEN_GENERATING_OUTPUT -template class Comparator> { +template class Comparator>: public Implementation::ContainerComparatorBase { public: ComparisonStatusFlags operator()(const T& actual, const T& expected); void printMessage(ComparisonStatusFlags, Utility::Debug& out, const char* actual, const char* expected) const; - - private: - const T* _actualContents; - const T* _expectedContents; - std::size_t _firstDifferent; }; template ComparisonStatusFlags Comparator>::operator()(const T& actual, const T& expected) { _actualContents = &actual; _expectedContents = &expected; + _actualContentsSize = actual.size(); + _expectedContentsSize = expected.size(); ComparisonStatusFlags status; - if(_actualContents->size() != _expectedContents->size()) + if(_actualContentsSize != _expectedContentsSize) status = ComparisonStatusFlag::Failed; /* Recursively use the comparator on the values, find the first different item in the common prefix. If there's none, then the first different item is right after the common prefix, and if both have the same size then it means the containers are the same. */ - Comparator::type> comparator; - const std::size_t commonPrefixSize = Utility::min(_actualContents->size(), _expectedContents->size()); + Comparator::type> comparator; + const std::size_t commonPrefixSize = Utility::min(_actualContentsSize, _expectedContentsSize); _firstDifferent = commonPrefixSize; for(std::size_t i = 0; i != commonPrefixSize; ++i) { - if(comparator((*_actualContents)[i], (*_expectedContents)[i]) & ComparisonStatusFlag::Failed) { + if(comparator(actual[i], expected[i]) & ComparisonStatusFlag::Failed) { _firstDifferent = i; status = ComparisonStatusFlag::Failed; break; @@ -98,23 +110,14 @@ template ComparisonStatusFlags Comparator>::opera return status; } -template void Comparator>::printMessage(ComparisonStatusFlags, Utility::Debug& out, const char* actual, const char* expected) const { - out << "Containers" << actual << "and" << expected << "have different"; - if(_actualContents->size() != _expectedContents->size()) - out << "size, actual" << _actualContents->size() << "but" << _expectedContents->size() << "expected. Actual contents:\n "; - else - out << "contents, actual:\n "; - - out << *_actualContents << Utility::Debug::newline << " but expected\n " << *_expectedContents << Utility::Debug::newline << " "; - - if(_actualContents->size() <= _firstDifferent) - out << "Expected has" << (*_expectedContents)[_firstDifferent]; - else if(_expectedContents->size() <= _firstDifferent) - out << "Actual has" << (*_actualContents)[_firstDifferent]; - else - out << "Actual" << (*_actualContents)[_firstDifferent] << "but" << (*_expectedContents)[_firstDifferent] << "expected"; - - out << "on position" << _firstDifferent << Utility::Debug::nospace << "."; +template void Comparator>::printMessage(const ComparisonStatusFlags status, Utility::Debug& out, const char* actual, const char* expected) const { + Implementation::ContainerComparatorBase::printMessage(status, out, actual, expected, + [](Utility::Debug& out, const void* contents) { + out << *static_cast(contents); + }, + [](Utility::Debug& out, const void* contents, std::size_t i) { + out << (*static_cast(contents))[i]; + }); } #endif