aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/testkit/test_comparators.h
blob: d6115c11b7be80ffb54f8cf34ae33a44997db111 (plain) (blame)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/vespalib/util/approx.h>
#include <ostream>
#include <chrono>

namespace std::chrono {
//TODO Move to a more suitable place
template <typename rep, typename period>
ostream & operator << (ostream & os, duration<rep, period> ts) {
    return os << ts.count();
}

ostream & operator << (ostream & os, system_clock::time_point ts);
ostream & operator << (ostream & os, steady_clock::time_point ts);

}

namespace vespalib {

/**
 * Collection of generic test comparators.
 **/
class TestComparators
{
private:
    TestComparators() {}
public:
    /** @brief Functor implementing approximately equals (with an epsilon) */
    struct approx {
        double _eps;
        approx(double eps) : _eps(eps) {}
        template <class A, class B>
        bool operator()(const A &a, const B &b) const {
            if (a < b) {
                return (b - a) <= _eps;
            } else {
                return (a - b) <= _eps;
            }
        }
    };
    /** @brief Functor implementing not approximately equals (with an epsilon) */
    struct not_approx {
        approx _approx;
        not_approx(double eps) : _approx(eps) {}
        template <class A, class B>
        bool operator()(const A &a, const B &b) const {
            return !_approx(a, b);
        }
    };
    /** @brief Functor implementing equals */
    struct equal {
        template <class A, class B>
        bool operator()(const A &a, const B &b) const { return (a == b); }
        bool operator()(double a, double b) const { return approx_equal(a, b); }
    };
    /** @brief Functor implementing not-equals */
    struct not_equal {
        template <class A, class B>
        bool operator()(const A &a, const B &b) const { return !equal()(a, b); }
    };
    /** @brief Functor implementing is-less-than */
    struct less {
        template <class A, class B>
        bool operator()(const A &a, const B &b) const { return (a < b); }
    };
    /** @brief Functor implementing is-less-than-or-equal */
    struct less_equal {
        template <class A, class B>
        bool operator()(const A &a, const B &b) const { return less()(a, b) || equal()(a, b); }
    };
    /** @brief Functor implementing is-greater-than */
    struct greater {
        template <class A, class B>
        bool operator()(const A &a, const B &b) const { return less()(b, a); }
    };
    /** @brief Functor implementing is-greater-than-or-equal */
    struct greater_equal {
        template <class A, class B>
        bool operator()(const A &a, const B &b) const { return greater()(a, b) || equal()(a, b); }
    };
};

} // namespace vespalib