libcruft-util/tap.ipp

144 lines
4.6 KiB
Plaintext
Raw Normal View History

2015-04-13 16:43:49 +10:00
/*
2015-04-13 18:05:28 +10:00
* 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
2015-04-13 16:43:49 +10:00
*
2015-04-13 18:05:28 +10:00
* http://www.apache.org/licenses/LICENSE-2.0
2015-04-13 16:43:49 +10:00
*
2015-04-13 18:05:28 +10:00
* 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.
2015-04-13 16:43:49 +10:00
*
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifdef __UTIL_TAP_IPP
#error
#endif
#define __UTIL_TAP_IPP
#include "maths.hpp"
#include <cstdlib>
#include <algorithm>
2015-07-21 02:53:46 +10:00
#include "format.hpp"
2015-04-13 21:45:49 +10:00
2015-04-13 16:43:49 +10:00
//-----------------------------------------------------------------------------
2015-04-13 21:45:49 +10:00
template <typename... Args>
2015-04-13 16:43:49 +10:00
void
2015-04-13 21:45:49 +10:00
util::TAP::logger::expect (std::function<bool(Args...)> test, Args&&... args, const std::string &msg)
2015-04-13 16:43:49 +10:00
{
2015-04-13 21:45:49 +10:00
expect (test (std::forward<Args> (args)...), msg);
2015-04-13 16:43:49 +10:00
}
2015-04-13 21:45:49 +10:00
2015-04-13 16:43:49 +10:00
//-----------------------------------------------------------------------------
2015-07-21 02:53:46 +10:00
template <typename T, typename U, typename ...Args>
2015-04-13 16:43:49 +10:00
void
2015-07-21 02:53:46 +10:00
util::TAP::logger::expect_eq (const T&a, const U &b, const std::string &fmt, Args&... args)
2015-04-13 16:43:49 +10:00
{
static const std::function<bool(const T&,const U&)> TEST = [] (const T &t, const U &u) -> bool {
2015-04-13 21:45:49 +10:00
return almost_equal (t, u);
};
2015-07-21 02:53:46 +10:00
expect<const T&, const U&> (TEST, a, b, util::format::render (fmt, std::forward<Args> (args)...));
2015-04-13 16:43:49 +10:00
}
//-----------------------------------------------------------------------------
2015-07-21 02:53:46 +10:00
template <typename T, typename U, typename ...Args>
2015-04-13 16:43:49 +10:00
void
2015-07-21 02:53:46 +10:00
util::TAP::logger::expect_neq (const T&a, const U &b, const std::string &fmt, Args&... args)
2015-04-13 16:43:49 +10:00
{
static const std::function<bool(const T&,const U&)> TEST = [] (const T &t, const U &u) -> bool {
2015-04-13 16:43:49 +10:00
return !almost_equal (t, u);
};
2015-07-21 02:53:46 +10:00
expect<const T&, const U&> (TEST, a, b, util::format::render (fmt, std::forward<Args> (args)...));
2015-04-13 16:43:49 +10:00
}
//-----------------------------------------------------------------------------
#define TAP_TEST(SUFFIX,OP) \
2015-07-21 02:53:46 +10:00
template <typename T, typename U, typename ...Args> \
2015-04-13 16:43:49 +10:00
void \
util::TAP::logger::expect_ ## SUFFIX (const T &a, \
const U &b, \
2015-07-21 02:53:46 +10:00
const std::string &fmt, \
Args&... args) \
2015-04-13 16:43:49 +10:00
{ \
2015-07-02 17:03:36 +10:00
static const std::function< \
bool(const T&,const U&) \
> TEST = [] (const T&t, const U&u) { return t OP u; }; \
2015-07-21 02:53:46 +10:00
\
expect<const T&, const U&> ( \
TEST, \
a, b, \
util::format::render ( \
fmt, \
std::forward<Args> (args)... \
) \
); \
2015-04-13 16:43:49 +10:00
}
TAP_TEST(gt, > )
TAP_TEST(ge, >=)
TAP_TEST(lt, < )
TAP_TEST(le, <=)
#undef TAP_TEST
2015-04-13 21:46:56 +10:00
//-----------------------------------------------------------------------------
2015-07-21 02:53:46 +10:00
template <typename T, typename ...Args>
2015-04-13 21:46:56 +10:00
void
2015-07-21 02:53:46 +10:00
util::TAP::logger::expect_nan (const T &t, const std::string &fmt, Args&... args)
2015-04-13 21:46:56 +10:00
{
bool(*func)(T) = std::isnan;
2015-07-21 02:53:46 +10:00
expect<const T&> (
std::function<bool(const T&)> (func),
t,
util::format::render (fmt, std::forward<Args> (args)...)
);
2015-04-13 21:46:56 +10:00
}
2015-07-02 17:03:36 +10:00
//-----------------------------------------------------------------------------
2015-07-21 02:53:46 +10:00
template <typename T, typename ...Args>
2015-07-02 17:03:36 +10:00
void
2015-07-21 02:53:46 +10:00
util::TAP::logger::expect_nothrow (T &&t, const std::string &fmt, Args&... args)
2015-07-02 17:03:36 +10:00
{
bool success = true;
try {
t ();
} catch (...) {
success = false;
}
2015-07-21 02:53:46 +10:00
expect (success, util::format::render (fmt, std::forward<Args> (args)...));
2015-07-02 17:03:36 +10:00
}
//-----------------------------------------------------------------------------
2015-07-21 02:53:46 +10:00
template <typename E, typename T, typename ...Args>
2015-07-02 17:03:36 +10:00
void
2015-07-21 02:53:46 +10:00
util::TAP::logger::expect_throw (T &&t, const std::string &fmt, Args&... args)
2015-07-02 17:03:36 +10:00
{
bool success = false;
try {
t ();
} catch (const E&) {
success = true;
2015-07-21 02:53:46 +10:00
} catch (...) {
success = false;
2015-07-02 17:03:36 +10:00
}
2015-07-21 02:53:46 +10:00
expect (success, util::format::render (fmt, std::forward<Args> (args)...));
2015-07-02 17:03:36 +10:00
}