summaryrefslogtreecommitdiff
path: root/vendor/bandit/bandit/assertion_frameworks/snowhouse/snowhouse/constraints/equalscontainerconstraint.h
blob: f865095234336adfc8ef9db14e6f288fc060feef (plain)
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
//          Copyright Joakim Karlsson & Kim Gräsman 2010-2012.
// 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 IGLOO_EQUALSCONTAINERCONSTRAINT_H
#define IGLOO_EQUALSCONTAINERCONSTRAINT_H

namespace snowhouse {

  namespace constraint_internal {
    template<typename T>
    inline bool default_comparer(const T& lhs, const T& rhs)
    {
      return lhs == rhs;
    }
  }

  template< typename ExpectedType, typename BinaryPredicate>
    struct EqualsContainerConstraint : Expression< EqualsContainerConstraint<ExpectedType, BinaryPredicate> >
  {
    EqualsContainerConstraint(const ExpectedType& expected, const BinaryPredicate predicate)
      : expected_(expected), predicate_(predicate)
    {}

    template<typename ActualType>
      bool operator()(const ActualType& actual) const
      {
        typename ActualType::const_iterator actual_it;
        typename ExpectedType::const_iterator expected_it;

        for(actual_it = actual.begin(), expected_it = expected_.begin(); actual_it != actual.end() && expected_it != expected_.end(); actual_it++, expected_it++)
        {
          if(!predicate_(*actual_it, *expected_it))
          {
            return false;
          }
        }

        return actual.size() == expected_.size();
      }

    const ExpectedType expected_;
    const BinaryPredicate predicate_;

  private:

#if __cplusplus > 199711L
#else
    EqualsContainerConstraint& operator=(const EqualsContainerConstraint&) { return *this; }
#endif

  };

  template< typename ExpectedType>
    inline EqualsContainerConstraint<ExpectedType, bool (*)(const typename ExpectedType::value_type&, const typename ExpectedType::value_type&)> EqualsContainer(const ExpectedType& expected)
    {
      return EqualsContainerConstraint<ExpectedType, bool (*)(const typename ExpectedType::value_type&, const typename ExpectedType::value_type&)>(expected, constraint_internal::default_comparer);
    }

  template< typename ExpectedType, typename BinaryPredicate >
    inline EqualsContainerConstraint<ExpectedType, BinaryPredicate> EqualsContainer(const ExpectedType& expected, const BinaryPredicate predicate)
    {
      return EqualsContainerConstraint<ExpectedType, BinaryPredicate>(expected, predicate);
    }

  template< typename ExpectedType, typename BinaryPredicate >
    struct Stringizer< EqualsContainerConstraint<ExpectedType, BinaryPredicate> >
    {
      static std::string ToString(const EqualsContainerConstraint<ExpectedType, BinaryPredicate>& constraint)
      {
        std::ostringstream builder;
        builder << snowhouse::Stringize(constraint.expected_);
        return builder.str();
      }
    };
}

#endif