aboutsummaryrefslogtreecommitdiff
path: root/Client/ThirdParty/fpm/tests/common.hpp
blob: d4dfe8949cb8e53e15ebcf64738f7cc82e43e13a (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
#ifndef FPM_TESTS_COMMON_HPP
#define FPM_TESTS_COMMON_HPP

#include <fpm/fixed.hpp>
#include <gtest/gtest.h>
#include <iomanip>
#include <ostream>

namespace fpm
{
template <typename B, typename I, unsigned int F>
void PrintTo(const fpm::fixed<B, I, F>& val, ::std::ostream* os)
{
    auto f = os->flags();
    *os << static_cast<double>(val)
        << " (0x" << std::hex << std::setw(sizeof(B) * 2) << std::setfill('0') << val.raw_value() << ")";
    os->flags(f);
}
}

inline ::testing::AssertionResult HasMaximumError(double value, double reference, double max_error)
{
    auto diff = std::abs(value - reference);
    if (reference < 1e-10 && diff <= max_error)
        return ::testing::AssertionSuccess();
    if (std::abs(diff / reference) <= max_error)
        return ::testing::AssertionSuccess();
    return ::testing::AssertionFailure() << value << " is not within " << (max_error * 100) << "% of " << reference;
}

#endif