Skip to content

Commit

Permalink
TestSuite: make a non-templated base for Compare::Container.
Browse files Browse the repository at this point in the history
Same as was done for the generic Comparator, but here it's quite a lot
heavier so the impact should be bigger.
  • Loading branch information
mosra committed Jun 1, 2022
1 parent ac3ad3e commit 468afdb
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/Corrade/TestSuite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(CorradeTestSuite_SRCS
Comparator.cpp
Tester.cpp

Compare/Container.cpp
Compare/File.cpp
Compare/FileToString.cpp
Compare/FloatingPoint.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/Corrade/TestSuite/Comparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 5 additions & 7 deletions src/Corrade/TestSuite/Comparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
};

}
Expand Down Expand Up @@ -276,7 +274,7 @@ template<class T> ComparisonStatusFlags Comparator<T>::operator()(const T& actua
return ComparisonStatusFlag::Failed;
}

template<class T> void Comparator<T>::printMessage(ComparisonStatusFlags status, Utility::Debug& out, const char* actual, const char* expected) {
template<class T> void Comparator<T>::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<const T*>(value);
Expand Down
62 changes: 62 additions & 0 deletions src/Corrade/TestSuite/Compare/Container.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019, 2020, 2021, 2022
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#include "Container.h"

namespace Corrade { namespace TestSuite { namespace Implementation {

void ContainerComparatorBase::printMessage(ComparisonStatusFlags, Utility::Debug& out, const char* const actual, const char* const expected, void(*printer)(Utility::Debug&, const void*), void(*itemPrinter)(Utility::Debug&, const void*, std::size_t)) const {
CORRADE_INTERNAL_ASSERT(_actualContents && _expectedContents);

out << "Containers" << actual << "and" << expected << "have different";
if(_actualContentsSize != _expectedContentsSize)
out << "size, actual" << _actualContentsSize << "but" << _expectedContentsSize << "expected. Actual contents:\n ";
else
out << "contents, actual:\n ";

printer(out, _actualContents);
out << Utility::Debug::newline << " but expected\n ";
printer(out, _expectedContents);
out << Utility::Debug::newline << " ";

if(_actualContentsSize <= _firstDifferent) {
out << "Expected has";
itemPrinter(out, _expectedContents, _firstDifferent);
} else if(_expectedContentsSize <= _firstDifferent) {
out << "Actual has";
itemPrinter(out, _actualContents, _firstDifferent);
} else {
out << "Actual";
itemPrinter(out, _actualContents, _firstDifferent);
out << "but";
itemPrinter(out, _expectedContents, _firstDifferent);
out << "expected";
}

out << "on position" << _firstDifferent << Utility::Debug::nospace << ".";
}

}}}
57 changes: 30 additions & 27 deletions src/Corrade/TestSuite/Compare/Container.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,36 +59,48 @@ template<class> 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 T> class Comparator<Compare::Container<T>> {
template<class T> class Comparator<Compare::Container<T>>: 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<class T> ComparisonStatusFlags Comparator<Compare::Container<T>>::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<typename std::decay<decltype((*_actualContents)[0])>::type> comparator;
const std::size_t commonPrefixSize = Utility::min(_actualContents->size(), _expectedContents->size());
Comparator<typename std::decay<decltype(actual[0])>::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;
Expand All @@ -98,23 +110,14 @@ template<class T> ComparisonStatusFlags Comparator<Compare::Container<T>>::opera
return status;
}

template<class T> void Comparator<Compare::Container<T>>::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<class T> void Comparator<Compare::Container<T>>::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<const T*>(contents);
},
[](Utility::Debug& out, const void* contents, std::size_t i) {
out << (*static_cast<const T*>(contents))[i];
});
}
#endif

Expand Down

0 comments on commit 468afdb

Please sign in to comment.