summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/tests/test_tools.h
blob: c5451643bea7e01fc6018c5447922a7e67148317 (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
#ifndef TEST_TOOLS_H
#define TEST_TOOLS_H

/*
 *
 * Copyright (c) 2006 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <limits.h>             // Include before boost/test headers.

#include <boost/test/test_tools.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/regex.hpp>
#include <boost/assign/list_of.hpp>
#include <vector>
#include <ostream>

// Print a sequence
template <class T> std::ostream& seqPrint(std::ostream& o, const T& seq) {
    std::copy(seq.begin(), seq.end(), std::ostream_iterator<typename T::value_type>(o, " "));
    return o;
}

// Compare sequences 
template <class T, class U>
bool seqEqual(const T& a, const U& b) {
    typename T::const_iterator i = a.begin();
    typename U::const_iterator j = b.begin();
    while (i != a.end() && j != b.end() && *i == *j) { ++i; ++j; }
    return (i == a.end()) && (j == b.end());
}

// ostream and == operators so we can compare vectors and boost::assign::list_of
// with BOOST_CHECK_EQUALS
namespace std {                 // In namespace std so boost can find them.

template <class T>
ostream& operator<<(ostream& o, const vector<T>& v) { return seqPrint(o, v); }

template <class T>
ostream& operator<<(ostream& o, const boost::assign_detail::generic_list<T>& l) { return seqPrint(o, l); }

template <class T>
bool operator == (const vector<T>& a, const boost::assign_detail::generic_list<T>& b) { return seqEqual(a, b); }

template <class T>
bool operator == (const boost::assign_detail::generic_list<T>& b, const vector<T>& a) { return seqEqual(a, b); }
}

/** NB: order of parameters is regex first, in line with
 * CHECK(expected, actual) convention.
 */
inline bool regexPredicate(const std::string& re, const std::string& text) {
    return boost::regex_match(text, boost::regex(re));
}

/** Check for regular expression match. You must #include <boost/regex.hpp> */
#define BOOST_CHECK_REGEX(re, text) \
    BOOST_CHECK_PREDICATE(regexPredicate, (re)(text))

/** Check if types of two objects (as given by typeinfo::name()) match. */
#define BOOST_CHECK_TYPEID_EQUAL(a,b) BOOST_CHECK_EQUAL(typeid(a).name(),typeid(b).name())

#endif  /*!TEST_TOOLS_H*/