2017-02-21 17:05:04 +01:00
|
|
|
/*
|
|
|
|
* Created by Phil Nash on 21/02/2017.
|
|
|
|
* Copyright (c) 2017 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
#ifndef TWOBLUECUBES_CATCH_MATCHERS_VECTOR_H_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_MATCHERS_VECTOR_H_INCLUDED
|
|
|
|
|
2017-09-07 12:24:33 +02:00
|
|
|
#include "catch_matchers.h"
|
2017-02-21 17:05:04 +01:00
|
|
|
|
2017-12-07 17:07:25 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
2017-02-21 17:05:04 +01:00
|
|
|
namespace Catch {
|
|
|
|
namespace Matchers {
|
|
|
|
|
|
|
|
namespace Vector {
|
|
|
|
template<typename T>
|
2017-11-13 11:06:26 +01:00
|
|
|
struct ContainsElementMatcher : MatcherBase<std::vector<T>> {
|
2017-02-21 17:05:04 +01:00
|
|
|
|
|
|
|
ContainsElementMatcher(T const &comparator) : m_comparator( comparator) {}
|
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
bool match(std::vector<T> const &v) const override {
|
2017-07-28 15:11:05 +02:00
|
|
|
for (auto const& el : v) {
|
|
|
|
if (el == m_comparator) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2017-02-21 17:05:04 +01:00
|
|
|
}
|
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
std::string describe() const override {
|
2017-05-02 23:51:03 +02:00
|
|
|
return "Contains: " + ::Catch::Detail::stringify( m_comparator );
|
2017-02-21 17:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
T const& m_comparator;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2017-11-13 11:06:26 +01:00
|
|
|
struct ContainsMatcher : MatcherBase<std::vector<T>> {
|
2017-02-21 17:05:04 +01:00
|
|
|
|
|
|
|
ContainsMatcher(std::vector<T> const &comparator) : m_comparator( comparator ) {}
|
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
bool match(std::vector<T> const &v) const override {
|
2017-02-21 17:05:04 +01:00
|
|
|
// !TBD: see note in EqualsMatcher
|
|
|
|
if (m_comparator.size() > v.size())
|
|
|
|
return false;
|
2017-07-28 15:11:05 +02:00
|
|
|
for (auto const& comparator : m_comparator) {
|
|
|
|
auto present = false;
|
|
|
|
for (const auto& el : v) {
|
|
|
|
if (el == comparator) {
|
|
|
|
present = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!present) {
|
2017-02-21 17:05:04 +01:00
|
|
|
return false;
|
2017-07-28 15:11:05 +02:00
|
|
|
}
|
|
|
|
}
|
2017-02-21 17:05:04 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-07-20 16:57:17 +02:00
|
|
|
std::string describe() const override {
|
2017-05-02 23:51:03 +02:00
|
|
|
return "Contains: " + ::Catch::Detail::stringify( m_comparator );
|
2017-02-21 17:05:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<T> const& m_comparator;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2017-11-13 11:06:26 +01:00
|
|
|
struct EqualsMatcher : MatcherBase<std::vector<T>> {
|
2017-02-21 17:05:04 +01:00
|
|
|
|
|
|
|
EqualsMatcher(std::vector<T> const &comparator) : m_comparator( comparator ) {}
|
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
bool match(std::vector<T> const &v) const override {
|
2017-02-21 17:05:04 +01:00
|
|
|
// !TBD: This currently works if all elements can be compared using !=
|
|
|
|
// - a more general approach would be via a compare template that defaults
|
|
|
|
// to using !=. but could be specialised for, e.g. std::vector<T> etc
|
|
|
|
// - then just call that directly
|
|
|
|
if (m_comparator.size() != v.size())
|
|
|
|
return false;
|
2017-09-18 18:13:17 +02:00
|
|
|
for (std::size_t i = 0; i < v.size(); ++i)
|
2017-02-21 17:05:04 +01:00
|
|
|
if (m_comparator[i] != v[i])
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2017-07-20 16:57:17 +02:00
|
|
|
std::string describe() const override {
|
2017-05-02 23:51:03 +02:00
|
|
|
return "Equals: " + ::Catch::Detail::stringify( m_comparator );
|
2017-02-21 17:05:04 +01:00
|
|
|
}
|
|
|
|
std::vector<T> const& m_comparator;
|
|
|
|
};
|
|
|
|
|
2017-12-07 17:07:25 +01:00
|
|
|
template<typename T>
|
|
|
|
struct UnorderedEqualsMatcher : MatcherBase<std::vector<T>> {
|
|
|
|
UnorderedEqualsMatcher(std::vector<T> const& target) : m_target(target) {}
|
|
|
|
bool match(std::vector<T> const& vec) const override {
|
|
|
|
// Note: This is a reimplementation of std::is_permutation,
|
|
|
|
// because I don't want to include <algorithm> inside the common path
|
|
|
|
if (m_target.size() != vec.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-19 17:54:21 +02:00
|
|
|
return std::is_permutation(m_target.begin(), m_target.end(), vec.begin());
|
2017-12-07 17:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string describe() const override {
|
|
|
|
return "UnorderedEquals: " + ::Catch::Detail::stringify(m_target);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
std::vector<T> const& m_target;
|
|
|
|
};
|
|
|
|
|
2017-02-21 17:05:04 +01:00
|
|
|
} // namespace Vector
|
|
|
|
|
|
|
|
// The following functions create the actual matcher objects.
|
|
|
|
// This allows the types to be inferred
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
Vector::ContainsMatcher<T> Contains( std::vector<T> const& comparator ) {
|
|
|
|
return Vector::ContainsMatcher<T>( comparator );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
Vector::ContainsElementMatcher<T> VectorContains( T const& comparator ) {
|
|
|
|
return Vector::ContainsElementMatcher<T>( comparator );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
Vector::EqualsMatcher<T> Equals( std::vector<T> const& comparator ) {
|
|
|
|
return Vector::EqualsMatcher<T>( comparator );
|
|
|
|
}
|
|
|
|
|
2017-12-07 17:07:25 +01:00
|
|
|
template<typename T>
|
|
|
|
Vector::UnorderedEqualsMatcher<T> UnorderedEquals(std::vector<T> const& target) {
|
|
|
|
return Vector::UnorderedEqualsMatcher<T>(target);
|
|
|
|
}
|
|
|
|
|
2017-02-21 17:05:04 +01:00
|
|
|
} // namespace Matchers
|
|
|
|
} // namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_MATCHERS_VECTOR_H_INCLUDED
|