summaryrefslogtreecommitdiff
path: root/3rdparty/plibsys/tests/ptestmacros.h
blob: 206a8a67774b1fe1b41400be338350d2d74c1cc6 (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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * The MIT License
 *
 * Copyright (C) 2017 Alexander Saprykin <saprykin.spb@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * 'Software'), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * @file ptestmacros.h
 * @brief Macros for unit-tests
 * @author Alexander Saprykin
 */

#ifndef PLIBSYS_HEADER_PTESTMACROS_H
#define PLIBSYS_HEADER_PTESTMACROS_H

#include <stdlib.h>

#include <plibsys.h>

#if defined (P_CC_MSVC)
#  pragma warning (push)
#  pragma warning (disable : 4723)
#elif defined (P_CC_BORLAND)
#  pragma option -w-8008
#  pragma option -w-8066
#elif defined (P_CC_WATCOM)
#  pragma disable_message (13)
#  pragma disable_message (367)
#  pragma disable_message (368)
#endif

inline double p_test_safe_division (double f1, double f2)
{
	return (f2 < 1.0 && f1 > f2 * P_MAXDOUBLE) ? P_MAXDOUBLE :
	        (((f2 > 1.0 && f1 < f2 * P_MINDOUBLE) || f1 == 0) ? 0 : f1 / f2);
}

#ifdef P_CC_MSVC
#  pragma warning (pop)
#endif

#define P_TEST_MODULE_FAIL_COUNTER p_test_module_fail_counter
#define P_TEST_SUITE_FAIL_COUNTER p_test_suite_fail_counter

#define P_TEST_MODULE_INIT() static pint P_TEST_MODULE_FAIL_COUNTER = 0

#define P_TEST_CASE_BEGIN(test_case_name)						\
	pint p_test_case_##test_case_name (void)					\
	{										\
		P_TEST_MODULE_FAIL_COUNTER = 0;

#define P_TEST_CASE_END()								\
		return (P_TEST_MODULE_FAIL_COUNTER == 0) ? 0 : -1;			\
	}

#define P_TEST_CASE_RETURN() return 0

#define P_TEST_SUITE_BEGIN()								\
	int main (void)									\
	{										\
		pint P_TEST_SUITE_FAIL_COUNTER = 0;

#define P_TEST_SUITE_ARGS_BEGIN()							\
	int main (int argc, char *argv[])						\
	{										\
		pint P_TEST_SUITE_FAIL_COUNTER = 0;

#define P_TEST_SUITE_END()								\
		if (P_TEST_SUITE_FAIL_COUNTER == 0)					\
			printf ("Test passed\n");					\
		else									\
			printf ("Test failed\n");					\
											\
		return P_TEST_SUITE_FAIL_COUNTER == 0 ? 0 : -1;				\
	}

#define P_TEST_SUITE_RUN_CASE(a) 							\
	printf ("Running test case: %s\n", #a);						\
	P_TEST_SUITE_FAIL_COUNTER += (p_test_case_##a)()

#define P_TEST_CHECK(a)									\
	do {										\
		if (!(a)) {								\
			printf ("%s:%d: check failed\n", __FILE__, __LINE__);		\
			p_atomic_int_inc (&P_TEST_MODULE_FAIL_COUNTER);			\
		}									\
	} while (0)

#define P_TEST_CHECK_CLOSE(a, b, eps)							\
	do {										\
		double p_test_eps_diff = (a) > (b) ? (a) - (b) : (b) - (a);		\
		double p_test_d1 = p_test_safe_division (p_test_eps_diff,		\
							 ((a) < 0.0 ? (-(a)) : (a)));	\
		double p_test_d2 = p_test_safe_division (p_test_eps_diff,		\
							 ((b) < 0.0 ? (-(b)) : (b)));	\
		double p_test_tol = (eps) * 0.01;					\
											\
		if (!(p_test_d1 <= p_test_tol && p_test_d2 <= p_test_tol)) {		\
			printf ("%s:%d: check failed\n", __FILE__, __LINE__);		\
			p_atomic_int_inc (&P_TEST_MODULE_FAIL_COUNTER);			\
		}									\
	} while (0)

#define P_TEST_REQUIRE(a)								\
	do {										\
		if (!(a)) {								\
			printf ("%s:%d: required check failed\n", __FILE__, __LINE__);	\
			exit (-1);							\
		}									\
	} while (0)

#endif /* PLIBSYS_HEADER_PTESTMACROS_H */