Unitary_tests_utils.h
1/* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2 * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3 * Author(s): Vincent Rouvreau
4 *
5 * Copyright (C) 2017 Inria
6 *
7 * Modification(s):
8 * - YYYY/MM Author: Description of the modification
9 */
10#ifndef UNITARY_TESTS_UTILS_H_
11#define UNITARY_TESTS_UTILS_H_
12
13#include <boost/test/unit_test.hpp>
14
15#include <iostream>
16#include <limits> // for std::numeric_limits<>
17#include <cmath> // for std::fabs
18
19template<typename FloatingType >
20void GUDHI_TEST_FLOAT_EQUALITY_CHECK(FloatingType a, FloatingType b,
21 FloatingType epsilon = std::numeric_limits<FloatingType>::epsilon()) {
22#ifdef DEBUG_TRACES
23 std::clog << "GUDHI_TEST_FLOAT_EQUALITY_CHECK - " << a << " versus " << b
24 << " | diff = " << std::fabs(a - b) << " - epsilon = " << epsilon << std::endl;
25#endif
26 BOOST_CHECK(std::fabs(a - b) <= epsilon);
27}
28
29// That's the usual x86 issue where a+b==a+b can return false (without any NaN) because one of them was stored in
30// memory (and thus rounded to 64 bits) while the other is still in a register (80 bits).
31template<typename FloatingType >
32FloatingType GUDHI_PROTECT_FLOAT(FloatingType value) {
33 volatile FloatingType protected_value = value;
34#ifdef DEBUG_TRACES
35 std::clog << "GUDHI_PROTECT_FLOAT - " << protected_value << std::endl;
36#endif
37 return protected_value;
38}
39
40#endif // UNITARY_TESTS_UTILS_H_