Debian Codegraph @

cpp/src/test_bits.hpp

raw
1 #ifndef TEST_BITS_HPP
2 #define TEST_BITS_HPP
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 #include "msg.hpp"
23 #include "proton/types.hpp"
24
25 #include <stdexcept>
26 #include <iostream>
27 #include <iterator>
28 #include <sstream>
29 #include <cstring>
30 #include <math.h>
31
32 namespace test {
33
34 struct fail : public std::logic_error {
35 explicit fail(const std::string& what) : logic_error(what) {}
36 };
37
38 struct error : public std::logic_error {
39 explicit error(const std::string& what) : logic_error(what) {}
40 };
41
42 template <class T, class U>
43 void assert_equal(const T& want, const U& got, const std::string& what) {
44 if (!(want == got))
45 throw fail(MSG(what << " " << want << " != " << got));
46 }
47
48 template <class T>
49 inline void assert_equalish(T want, T got, T delta, const std::string& what)
50 {
51 if (!(fabs(want-got) <= delta))
52 throw fail(MSG(what << " " << want << " !=~ " << got));
53 }
54
55 void assert_substring(const std::string& want, const std::string& got, const std::string& what) {
56 if (got.find(want) == std::string::npos)
57 throw fail(MSG(what << " '" << want << "' not found in '" << got << "'"));
58 }
59
60 #define FAIL_MSG(WHAT) (MSG(__FILE__ << ":" << __LINE__ << ": " << WHAT).str())
61 #define FAIL(WHAT) throw test::fail(FAIL_MSG(WHAT))
62 #define ASSERT(TEST) do { if (!(TEST)) FAIL("failed ASSERT(" #TEST ")"); } while(false)
63 #define ASSERT_EQUAL(WANT, GOT) \
64 test::assert_equal((WANT), (GOT), FAIL_MSG("failed ASSERT_EQUAL(" #WANT ", " #GOT ")"))
65 #define ASSERT_EQUALISH(WANT, GOT, DELTA) \
66 test::assert_equalish((WANT), (GOT), (DELTA), FAIL_MSG("failed ASSERT_EQUALISH(" #WANT ", " #GOT ")"))
67 #define ASSERT_SUBSTRING(WANT, GOT) \
68 test::assert_substring((WANT), (GOT), FAIL_MSG("failed ASSERT_SUBSTRING(" #WANT ", " #GOT ")"))
69 #define ASSERT_THROWS(WANT, EXPR) do { try { EXPR; FAIL("Expected " #WANT); } catch(const WANT&) {} } while(0)
70 #define ASSERT_THROWS_MSG(CLASS, MSG, EXPR) do { try { EXPR; FAIL("Expected " #CLASS); } catch(const CLASS& e) { ASSERT_SUBSTRING((MSG), e.what()); } } while(0)
71
72 #define RUN_TEST(BAD_COUNT, TEST) \
73 do { \
74 try { \
75 std::cout << "TEST: " << #TEST << std::endl; \
76 TEST; \
77 break; \
78 } catch(const test::fail& e) { \
79 std::cout << "FAIL " << #TEST << std::endl << e.what() << std::endl; \
80 } catch(const std::exception& e) { \
81 std::cout << "ERROR " << #TEST << std::endl << __FILE__ << ":" << __LINE__ << ": " << e.what() << std::endl; \
82 } catch(...) { \
83 std::cout << "ERROR " << #TEST << std::endl << __FILE__ << ":" << __LINE__ << ": (unknown)" << std::endl; \
84 } \
85 ++BAD_COUNT; \
86 } while(0)
87
88 /* Like RUN_TEST but only if one of the argv strings is found in the test EXPR */
89 #define RUN_ARGV_TEST(BAD_COUNT, EXPR) do { \
90 if (argc == 1) { \
91 RUN_TEST(BAD_COUNT, EXPR); \
92 } else { \
93 for (int i = 1; i < argc; ++i) { \
94 if (strstr(#EXPR, argv[i])) { \
95 RUN_TEST(BAD_COUNT, EXPR); \
96 break; \
97 } \
98 } \
99 } \
100 } while(0)
101
102
103 template<class T> std::string str(const T& x) {
104 std::ostringstream s; s << std::boolalpha << x; return s.str();
105 }
106
107 // A way to easily create literal collections that can be compared to std:: collections
108 // and to print std collections
109 // e.g.
110 // std::vector<string> v = ...;
111 // ASSERT_EQUAL(many<string>() + "a" + "b" + "c", v);
112 template <class T> struct many : public std::vector<T> {
113 many() {}
114 template<class S> explicit many(const S& s) : std::vector<T>(s.begin(), s.end()) {}
115 many& operator+=(const T& t) { this->push_back(t); return *this; }
116 many& operator<<(const T& t) { return *this += t; }
117 many operator+(const T& t) { many<T> l(*this); return l += t; }
118 };
119
120 template <class T, class S> bool operator==(const many<T>& m, const S& s) {
121 return m.size() == s.size() && S(m.begin(), m.end()) == s;
122 }
123
124 template <class T, class S> bool operator==(const S& s, const many<T>& m) {
125 return m.size() == s.size() && S(m.begin(), m.end()) == s;
126 }
127
128 template <class T> std::ostream& operator<<(std::ostream& o, const many<T>& m) {
129 std::ostream_iterator<T> oi(o, " ");
130 std::copy(m.begin(), m.end(), oi);
131 return o;
132 }
133
134 }
135
136 namespace std {
137 template <class T> std::ostream& operator<<(std::ostream& o, const std::vector<T>& s) {
138 return o << test::many<T>(s);
139 }
140
141 template <class T> std::ostream& operator<<(std::ostream& o, const std::deque<T>& s) {
142 return o << test::many<T>(s);
143 }
144
145 template <class T> std::ostream& operator<<(std::ostream& o, const std::list<T>& s) {
146 return o << test::many<T>(s);
147 }
148
149 template <class K, class T> std::ostream& operator<<(std::ostream& o, const std::map<K, T>& x) {
150 return o << test::many<std::pair<K, T> >(x);
151 }
152
153 template <class U, class V> std::ostream& operator<<(std::ostream& o, const std::pair<U, V>& p) {
154 return o << "( " << p.first << " , " << p.second << " )";
155 }
156
157 template <class K, class T> std::ostream& operator<<(std::ostream& o, const std::unordered_map<K, T>& x) {
158 return o << test::many<std::pair<const K, T> >(x);
159 }
160
161 template <class T> std::ostream& operator<<(std::ostream& o, const std::forward_list<T>& s) {
162 return o << test::many<T>(s);
163 }
164
165 }
166
167 #endif // TEST_BITS_HPP