-
Notifications
You must be signed in to change notification settings - Fork 109
/
Container.h
119 lines (90 loc) · 4.86 KB
/
Container.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#ifndef Corrade_TestSuite_Compare_Container_h
#define Corrade_TestSuite_Compare_Container_h
/*
This file is part of Corrade.
Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024
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::Container
*/
#include "Corrade/TestSuite/Comparator.h"
#include "Corrade/Utility/Math.h"
namespace Corrade { namespace TestSuite {
namespace Compare {
/**
@brief Pseudo-type for comparing container contents
Prints the length of both containers (if they are different) and then prints
value of the first different item in both containers. Example usage:
@snippet TestSuite.cpp Compare-Container
Comparison of containers of floating-point types is by default done as a
fuzzy-compare, delegated to @ref Comparator<float> and @ref Comparator<double>.
This comparator can only compare containers that have random access (i.e.,
implementing @cpp operator[]() @ce). For comparing non-randomly-accessible
containers (such as @ref std::list or @ref std::map) and unordered containers
(such as @ref std::unordered_map) use @ref SortedContainer instead.
See @ref TestSuite-Comparator-pseudo-types for more information.
*/
template<class> class Container {};
}
#ifndef DOXYGEN_GENERATING_OUTPUT
template<class T> class Comparator<Compare::Container<T>> {
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;
};
template<class T> ComparisonStatusFlags Comparator<Compare::Container<T>>::operator()(const T& actual, const T& expected) {
_actualContents = &actual;
_expectedContents = &expected;
if(_actualContents->size() != _expectedContents->size())
return ComparisonStatusFlag::Failed;
/* Recursively use comparator on the values */
Comparator<typename std::decay<decltype((*_actualContents)[0])>::type> comparator;
for(std::size_t i = 0; i != _actualContents->size(); ++i)
if(comparator((*_actualContents)[i], (*_expectedContents)[i]) & ComparisonStatusFlag::Failed)
return ComparisonStatusFlag::Failed;
return {};
}
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 << " ";
Comparator<typename std::decay<decltype((*_actualContents)[0])>::type> comparator;
for(std::size_t i = 0, end = Utility::max(_actualContents->size(), _expectedContents->size()); i != end; ++i) {
if(_actualContents->size() > i && _expectedContents->size() > i &&
!(comparator((*_actualContents)[i], (*_expectedContents)[i]) & ComparisonStatusFlag::Failed)) continue;
if(_actualContents->size() <= i)
out << "Expected has" << (*_expectedContents)[i];
else if(_expectedContents->size() <= i)
out << "Actual has" << (*_actualContents)[i];
else
out << "Actual" << (*_actualContents)[i] << "but" << (*_expectedContents)[i] << "expected";
out << "on position" << i << Utility::Debug::nospace << ".";
break;
}
}
#endif
}}
#endif