Skip to content

Commit

Permalink
TestSuite: split out Compare::SortedContainer into a dedicated header.
Browse files Browse the repository at this point in the history
To avoid including the heavy <algorithm> header when not strictly
needed.
  • Loading branch information
mosra committed Apr 3, 2019
1 parent 2ceaa2d commit 1719c57
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 43 deletions.
5 changes: 5 additions & 0 deletions doc/corrade-changelog.dox
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ namespace Corrade {
header, @ref Corrade/Utility/FormatStl.h, to speed up compile times. Since
including it for backwards compatibility would practically revert the
optimization, you have to @cpp #include @ce it explicitly where needed.
- @ref TestSuite::Compare::SortedContainer was split from the
@ref Corrade/TestSuite/Compare/Container.h header into
@ref Corrade/TestSuite/Compare/SortedContainer.h to isolate the heavy
`<algorithm>` include. Backwards compatibility is not provided in order to
reduce compile times, please include the new header now.

@subsection corrade-changelog-latest-documentation Documentation

Expand Down
2 changes: 1 addition & 1 deletion doc/snippets/TestSuite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
#include <cmath>

#include "Corrade/Containers/Pointer.h"
#include "Corrade/TestSuite/Compare/Container.h"
#include "Corrade/TestSuite/Compare/File.h"
#include "Corrade/TestSuite/Compare/FileToString.h"
#include "Corrade/TestSuite/Compare/Numeric.h"
#include "Corrade/TestSuite/Compare/SortedContainer.h"
#include "Corrade/TestSuite/Compare/StringToFile.h"
#include "Corrade/TestSuite/Comparator.h"
#include "Corrade/TestSuite/Tester.h"
Expand Down
2 changes: 1 addition & 1 deletion src/Corrade/Interconnect/Test/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "Corrade/Interconnect/Emitter.h"
#include "Corrade/Interconnect/Receiver.h"
#include "Corrade/TestSuite/Tester.h"
#include "Corrade/TestSuite/Compare/Container.h"
#include "Corrade/TestSuite/Compare/SortedContainer.h"
#include "Corrade/Utility/DebugStl.h"

namespace Corrade { namespace Interconnect { namespace Test { namespace {
Expand Down
1 change: 1 addition & 0 deletions src/Corrade/TestSuite/Compare/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ set(CorradeTestSuiteCompare_HEADERS
FileToString.h
FloatingPoint.h
Numeric.h
SortedContainer.h
StringToFile.h)

# Force IDEs to display all header files in project view
Expand Down
34 changes: 6 additions & 28 deletions src/Corrade/TestSuite/Compare/Container.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
*/

/** @file
* @brief Class @ref Corrade::TestSuite::Compare::Container, @ref Corrade::TestSuite::Compare::SortedContainer
* @brief Class @ref Corrade::TestSuite::Compare::Container
*/

#include <algorithm>

#include "Corrade/TestSuite/Comparator.h"

namespace Corrade { namespace TestSuite {
Expand Down Expand Up @@ -58,13 +56,11 @@ See @ref TestSuite-Comparator-pseudo-types for more information.
*/
template<class> class Container {};

/**
@brief Pseudo-type for comparing sorted container contents
See @ref Container for more information.
*/
template<class T> class SortedContainer: public Container<T> {};
}

namespace Implementation {
/* Copied from Magnum/Math/Vector.h, to avoid #include <algorithm> */
inline std::size_t max(std::size_t a, std::size_t b) { return a < b ? b : a; }
}

#ifndef DOXYGEN_GENERATING_OUTPUT
Expand Down Expand Up @@ -103,7 +99,7 @@ template<class T> void Comparator<Compare::Container<T>>::printErrorMessage(Util
e << *_actualContents << Utility::Debug::newline << " but expected\n " << *_expectedContents << Utility::Debug::newline << " ";

Comparator<typename std::decay<decltype((*_actualContents)[0])>::type> comparator;
for(std::size_t i = 0, end = std::max(_actualContents->size(), _expectedContents->size()); i != end; ++i) {
for(std::size_t i = 0, end = Implementation::max(_actualContents->size(), _expectedContents->size()); i != end; ++i) {
if(_actualContents->size() > i && _expectedContents->size() > i &&
comparator((*_actualContents)[i], (*_expectedContents)[i])) continue;

Expand All @@ -118,24 +114,6 @@ template<class T> void Comparator<Compare::Container<T>>::printErrorMessage(Util
break;
}
}

template<class T> class Comparator<Compare::SortedContainer<T>>: public Comparator<Compare::Container<T>> {
public:
bool operator()(const T& actual, const T& expected);

private:
T _actualSorted, _expectedSorted;
};

template<class T> bool Comparator<Compare::SortedContainer<T>>::operator()(const T& actual, const T& expected) {
_actualSorted = actual;
_expectedSorted = expected;

std::sort(_actualSorted.begin(), _actualSorted.end());
std::sort(_expectedSorted.begin(), _expectedSorted.end());

return Comparator<Compare::Container<T>>::operator()(_actualSorted, _expectedSorted);
}
#endif

}}
Expand Down
72 changes: 72 additions & 0 deletions src/Corrade/TestSuite/Compare/SortedContainer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#ifndef Corrade_TestSuite_Compare_SortedContainer_h
#define Corrade_TestSuite_Compare_SortedContainer_h
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019 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.
*/

/** @file
* @brief Class @ref Corrade::TestSuite::Compare::SortedContainer
*/

#include <algorithm>

#include "Corrade/TestSuite/Compare/Container.h"

namespace Corrade { namespace TestSuite {

namespace Compare {

/**
@brief Pseudo-type for comparing sorted container contents
See @ref Container for more information.
*/
template<class T> class SortedContainer: public Container<T> {};

}

#ifndef DOXYGEN_GENERATING_OUTPUT
template<class T> class Comparator<Compare::SortedContainer<T>>: public Comparator<Compare::Container<T>> {
public:
bool operator()(const T& actual, const T& expected);

private:
T _actualSorted, _expectedSorted;
};

template<class T> bool Comparator<Compare::SortedContainer<T>>::operator()(const T& actual, const T& expected) {
_actualSorted = actual;
_expectedSorted = expected;

std::sort(_actualSorted.begin(), _actualSorted.end());
std::sort(_expectedSorted.begin(), _expectedSorted.end());

return Comparator<Compare::Container<T>>::operator()(_actualSorted, _expectedSorted);
}
#endif

}}

#endif

1 change: 1 addition & 0 deletions src/Corrade/TestSuite/Compare/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/configure.h.cmake
${CMAKE_CURRENT_BINARY_DIR}/configure.h)

corrade_add_test(TestSuiteCompareContainerTest ContainerTest.cpp)
corrade_add_test(TestSuiteCompareSortedContainerTest SortedContainerTest.cpp)
corrade_add_test(TestSuiteCompareFileTest FileTest.cpp
FILES
FileTestFiles/base.txt
Expand Down
12 changes: 0 additions & 12 deletions src/Corrade/TestSuite/Compare/Test/ContainerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ struct ContainerTest: Tester {
void outputActualSmaller();
void outputExpectedSmaller();
void output();
void sorted();
void floatingPoint();

void nonCopyableArray();
Expand All @@ -51,7 +50,6 @@ ContainerTest::ContainerTest() {
&ContainerTest::outputActualSmaller,
&ContainerTest::outputExpectedSmaller,
&ContainerTest::output,
&ContainerTest::sorted,
&ContainerTest::floatingPoint,

&ContainerTest::nonCopyableArray});
Expand Down Expand Up @@ -123,16 +121,6 @@ void ContainerTest::output() {
" Actual 9 but 2 expected on position 1.\n");
}

void ContainerTest::sorted() {
std::vector<int> a{1, 2, 4, 3};
std::vector<int> b{1, 4, 3, 2};
std::vector<int> c{1, 4, 3, 3};

CORRADE_VERIFY((Comparator<Compare::SortedContainer<std::vector<int>>>()(a, b)));
CORRADE_VERIFY((Comparator<Compare::SortedContainer<std::vector<int>>>()(b, a)));
CORRADE_VERIFY((!Comparator<Compare::SortedContainer<std::vector<int>>>()(a, c)));
}

void ContainerTest::floatingPoint() {
std::stringstream out;

Expand Down
58 changes: 58 additions & 0 deletions src/Corrade/TestSuite/Compare/Test/SortedContainerTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019 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 <vector>

#include "Corrade/Containers/Array.h"
#include "Corrade/TestSuite/Tester.h"
#include "Corrade/TestSuite/Compare/SortedContainer.h"

namespace Corrade { namespace TestSuite { namespace Compare { namespace Test { namespace {

struct SortedContainerTest: Tester {
explicit SortedContainerTest();

void test();
};

SortedContainerTest::SortedContainerTest() {
addTests({&SortedContainerTest::test});
}

/* The rest is tested in ContainerTest, this is just a derived class */

void SortedContainerTest::test() {
std::vector<int> a{1, 2, 4, 3};
std::vector<int> b{1, 4, 3, 2};
std::vector<int> c{1, 4, 3, 3};

CORRADE_VERIFY((Comparator<Compare::SortedContainer<std::vector<int>>>()(a, b)));
CORRADE_VERIFY((Comparator<Compare::SortedContainer<std::vector<int>>>()(b, a)));
CORRADE_VERIFY((!Comparator<Compare::SortedContainer<std::vector<int>>>()(a, c)));
}

}}}}}

CORRADE_TEST_MAIN(Corrade::TestSuite::Compare::Test::SortedContainerTest)
3 changes: 2 additions & 1 deletion src/Corrade/Utility/Test/DirectoryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@

#include "Corrade/Containers/Array.h"
#include "Corrade/TestSuite/Tester.h"
#include "Corrade/TestSuite/Compare/Container.h"
#include "Corrade/TestSuite/Compare/File.h"
#include "Corrade/TestSuite/Compare/FileToString.h"
#include "Corrade/TestSuite/Compare/Container.h"
#include "Corrade/TestSuite/Compare/SortedContainer.h"
#include "Corrade/Utility/DebugStl.h"
#include "Corrade/Utility/Directory.h"

Expand Down

0 comments on commit 1719c57

Please sign in to comment.