1/*
2 * Copyright © 2019 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#pragma once
25
26#include <check.h>
27
28#undef ck_assert_double_eq
29#undef ck_assert_double_ne
30#undef ck_assert_double_lt
31#undef ck_assert_double_le
32#undef ck_assert_double_gt
33#undef ck_assert_double_ge
34#undef ck_assert_double_eq_tol
35#undef ck_assert_double_ne_tol
36
37#define CK_DOUBLE_EQ_EPSILON 1E-3
38#define _ck_assert_double_eq(X,Y, epsilon)  \
39	do { \
40		double _ck_x = X; \
41		double _ck_y = Y; \
42		ck_assert_msg(fabs(_ck_x - _ck_y) < epsilon, \
43			      "Assertion '" #X " == " #Y \
44			      "' failed: "#X"==%f, "#Y"==%f", \
45			      _ck_x, \
46			      _ck_y); \
47	} while (0)
48
49#define _ck_assert_double_ne(X,Y, epsilon)  \
50	do { \
51		double _ck_x = X; \
52		double _ck_y = Y; \
53		ck_assert_msg(fabs(_ck_x - _ck_y) > epsilon, \
54			      "Assertion '" #X " != " #Y \
55			      "' failed: "#X"==%f, "#Y"==%f", \
56			      _ck_x, \
57			      _ck_y); \
58	} while (0)
59
60#define ck_assert_double_eq(X, Y) _ck_assert_double_eq(X, Y, CK_DOUBLE_EQ_EPSILON)
61#define ck_assert_double_eq_tol(X, Y, tol) _ck_assert_double_eq(X, Y, tol)
62#define ck_assert_double_ne(X, Y) _ck_assert_double_ne(X, Y, CK_DOUBLE_EQ_EPSILON)
63#define ck_assert_double_ne_tol(X, Y, tol) _ck_assert_double_ne(X, Y, tol)
64
65#define _ck_assert_double_eq_op(X, OP, Y)  \
66	do { \
67		double _ck_x = X; \
68		double _ck_y = Y; \
69		ck_assert_msg(_ck_x OP _ck_y || \
70			      fabs(_ck_x - _ck_y) < CK_DOUBLE_EQ_EPSILON, \
71			      "Assertion '" #X#OP#Y \
72			      "' failed: "#X"==%f, "#Y"==%f", \
73			      _ck_x, \
74			      _ck_y); \
75	} while (0)
76
77#define _ck_assert_double_ne_op(X, OP,Y) \
78	do { \
79		double _ck_x = X; \
80		double _ck_y = Y; \
81		ck_assert_msg(_ck_x OP _ck_y && \
82			      fabs(_ck_x - _ck_y) > CK_DOUBLE_EQ_EPSILON, \
83			      "Assertion '" #X#OP#Y \
84			      "' failed: "#X"==%f, "#Y"==%f", \
85			      _ck_x, \
86			      _ck_y); \
87	} while (0)
88
89#define ck_assert_double_lt(X, Y) _ck_assert_double_ne_op(X, <, Y)
90#define ck_assert_double_le(X, Y) _ck_assert_double_eq_op(X, <=, Y)
91#define ck_assert_double_gt(X, Y) _ck_assert_double_ne_op(X, >, Y)
92#define ck_assert_double_ge(X, Y) _ck_assert_double_eq_op(X, >=, Y)
93
94