#ifndef TEST_BITS_HPP #define TEST_BITS_HPP /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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 "msg.hpp" #include "proton/types.hpp" #include #include #include #include #include #include namespace test { struct fail : public std::logic_error { explicit fail(const std::string& what) : logic_error(what) {} }; struct error : public std::logic_error { explicit error(const std::string& what) : logic_error(what) {} }; template void assert_equal(const T& want, const U& got, const std::string& what) { if (!(want == got)) throw fail(MSG(what << " " << want << " != " << got)); } template inline void assert_equalish(T want, T got, T delta, const std::string& what) { if (!(fabs(want-got) <= delta)) throw fail(MSG(what << " " << want << " !=~ " << got)); } void assert_substring(const std::string& want, const std::string& got, const std::string& what) { if (got.find(want) == std::string::npos) throw fail(MSG(what << " '" << want << "' not found in '" << got << "'")); } #define FAIL_MSG(WHAT) (MSG(__FILE__ << ":" << __LINE__ << ": " << WHAT).str()) #define FAIL(WHAT) throw test::fail(FAIL_MSG(WHAT)) #define ASSERT(TEST) do { if (!(TEST)) FAIL("failed ASSERT(" #TEST ")"); } while(false) #define ASSERT_EQUAL(WANT, GOT) \ test::assert_equal((WANT), (GOT), FAIL_MSG("failed ASSERT_EQUAL(" #WANT ", " #GOT ")")) #define ASSERT_EQUALISH(WANT, GOT, DELTA) \ test::assert_equalish((WANT), (GOT), (DELTA), FAIL_MSG("failed ASSERT_EQUALISH(" #WANT ", " #GOT ")")) #define ASSERT_SUBSTRING(WANT, GOT) \ test::assert_substring((WANT), (GOT), FAIL_MSG("failed ASSERT_SUBSTRING(" #WANT ", " #GOT ")")) #define ASSERT_THROWS(WANT, EXPR) do { try { EXPR; FAIL("Expected " #WANT); } catch(const WANT&) {} } while(0) #define ASSERT_THROWS_MSG(CLASS, MSG, EXPR) do { try { EXPR; FAIL("Expected " #CLASS); } catch(const CLASS& e) { ASSERT_SUBSTRING((MSG), e.what()); } } while(0) #define RUN_TEST(BAD_COUNT, TEST) \ do { \ try { \ std::cout << "TEST: " << #TEST << std::endl; \ TEST; \ break; \ } catch(const test::fail& e) { \ std::cout << "FAIL " << #TEST << std::endl << e.what() << std::endl; \ } catch(const std::exception& e) { \ std::cout << "ERROR " << #TEST << std::endl << __FILE__ << ":" << __LINE__ << ": " << e.what() << std::endl; \ } catch(...) { \ std::cout << "ERROR " << #TEST << std::endl << __FILE__ << ":" << __LINE__ << ": (unknown)" << std::endl; \ } \ ++BAD_COUNT; \ } while(0) /* Like RUN_TEST but only if one of the argv strings is found in the test EXPR */ #define RUN_ARGV_TEST(BAD_COUNT, EXPR) do { \ if (argc == 1) { \ RUN_TEST(BAD_COUNT, EXPR); \ } else { \ for (int i = 1; i < argc; ++i) { \ if (strstr(#EXPR, argv[i])) { \ RUN_TEST(BAD_COUNT, EXPR); \ break; \ } \ } \ } \ } while(0) template std::string str(const T& x) { std::ostringstream s; s << std::boolalpha << x; return s.str(); } // A way to easily create literal collections that can be compared to std:: collections // and to print std collections // e.g. // std::vector v = ...; // ASSERT_EQUAL(many() + "a" + "b" + "c", v); template struct many : public std::vector { many() {} template explicit many(const S& s) : std::vector(s.begin(), s.end()) {} many& operator+=(const T& t) { this->push_back(t); return *this; } many& operator<<(const T& t) { return *this += t; } many operator+(const T& t) { many l(*this); return l += t; } }; template bool operator==(const many& m, const S& s) { return m.size() == s.size() && S(m.begin(), m.end()) == s; } template bool operator==(const S& s, const many& m) { return m.size() == s.size() && S(m.begin(), m.end()) == s; } template std::ostream& operator<<(std::ostream& o, const many& m) { std::ostream_iterator oi(o, " "); std::copy(m.begin(), m.end(), oi); return o; } } namespace std { template std::ostream& operator<<(std::ostream& o, const std::vector& s) { return o << test::many(s); } template std::ostream& operator<<(std::ostream& o, const std::deque& s) { return o << test::many(s); } template std::ostream& operator<<(std::ostream& o, const std::list& s) { return o << test::many(s); } template std::ostream& operator<<(std::ostream& o, const std::map& x) { return o << test::many >(x); } template std::ostream& operator<<(std::ostream& o, const std::pair& p) { return o << "( " << p.first << " , " << p.second << " )"; } template std::ostream& operator<<(std::ostream& o, const std::unordered_map& x) { return o << test::many >(x); } template std::ostream& operator<<(std::ostream& o, const std::forward_list& s) { return o << test::many(s); } } #endif // TEST_BITS_HPP