Coverage Report

Created: 2021-04-05 01:08

/home/liu/buildslave/linux-x64-runtests/build/test/case.h
Line
Count
Source (jump to first uncovered line)
1
#ifndef _GUARD_case_h_
2
#define _GUARD_case_h_
3
4
#include <stdio.h>
5
#include <stdint.h>
6
#include <unistd.h>
7
8
#ifndef CASE_UNIQUE_NAME_PREFIX
9
#define CASE_UNIQUE_NAME_PREFIX
10
#endif
11
12
#ifndef CASE_TEST_DIR
13
#define CASE_TEST_DIR "."
14
#endif
15
16
#define CASE_TESTS
17
18
/* the following 9 lines to generate unique name was taken from Catch: https://github.com/philsquared/Catch
19
 * here is the licence:
20
 * Boost Software License - Version 1.0 - August 17th, 2003
21
 *
22
 * Permission is hereby granted, free of charge, to any person or organization
23
 * obtaining a copy of the software and accompanying documentation covered by
24
 * this license (the "Software") to use, reproduce, display, distribute,
25
 * execute, and transmit the Software, and to prepare derivative works of the
26
 * Software, and to permit third-parties to whom the Software is furnished to
27
 * do so, all subject to the following:
28
 *
29
 * The copyright notices in the Software and this entire statement, including
30
 * the above license grant, this restriction and the following disclaimer,
31
 * must be included in all copies of the Software, in whole or in part, and
32
 * all derivative works of the Software, unless such copies or derivative
33
 * works are solely in the form of machine-executable object code generated by
34
 * a source language processor.
35
 *
36
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
39
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
40
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
41
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
42
 * DEALINGS IN THE SOFTWARE. */
43
#ifndef INTERNAL_CATCH_UNIQUE_NAME_LINE2
44
#define INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) CASE_UNIQUE_NAME_PREFIX##name##line
45
#endif
46
#ifndef INTERNAL_CATCH_UNIQUE_NAME_LINE
47
#define INTERNAL_CATCH_UNIQUE_NAME_LINE( name, line ) INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line )
48
#endif
49
#ifndef INTERNAL_CATCH_UNIQUE_NAME
50
#define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __LINE__ )
51
#endif
52
53
typedef void (*case_f)(char*, int*);
54
55
void __attribute__((weak)) __test_case_setup(void);
56
void __attribute__((weak)) __test_case_teardown(void);
57
58
#define TEST_SETUP() void __test_case_setup(void)
59
#define TEST_TEARDOWN() void __test_case_teardown(void)
60
61
#ifdef __ELF__
62
// in ELF object format, we can simply query custom section rather than scan through the whole binary memory
63
// to find function pointer. We do this whenever possible because in this way, we don't have access error
64
// when hooking up with memory checkers such as address sanitizer or valgrind
65
typedef struct {
66
  uint64_t sig_head;
67
  case_f func;
68
  char* name;
69
  char* dir;
70
  uint64_t sig_tail;
71
} case_t;
72
73
#define TEST_CASE(desc) \
74
static void __attribute__((used)) INTERNAL_CATCH_UNIQUE_NAME(__test_case_func__) (char* __case_name__, int* __case_result__); \
75
static case_t INTERNAL_CATCH_UNIQUE_NAME(__test_case_ctx__) __attribute__((used, section("case_data"), aligned(8))) = { .func = INTERNAL_CATCH_UNIQUE_NAME(__test_case_func__), .sig_head = 0x883253372849284B, .name = desc, .dir = CASE_TEST_DIR, .sig_tail = 0x883253372849284B + 2 }; \
76
static void INTERNAL_CATCH_UNIQUE_NAME(__test_case_func__) (char* __case_name__, int* __case_result__) 
77
#else
78
typedef struct {
79
  uint64_t sig_head;
80
  case_f func;
81
  char* name;
82
  char* dir;
83
  uint64_t sig_tail;
84
} case_t;
85
86
#define TEST_CASE(desc) \
87
static void __attribute__((used)) INTERNAL_CATCH_UNIQUE_NAME(__test_case_func__) (char* __case_name__, int* __case_result__); \
88
static case_t INTERNAL_CATCH_UNIQUE_NAME(__test_case_ctx__) __attribute__((used, aligned(8))) = { .func = INTERNAL_CATCH_UNIQUE_NAME(__test_case_func__), .sig_head = 0x883253372849284B, .name = desc, .dir = CASE_TEST_DIR, .sig_tail = 0x883253372849284B + 2 }; \
89
static void INTERNAL_CATCH_UNIQUE_NAME(__test_case_func__) (char* __case_name__, int* __case_result__) 
90
#endif
91
92
0
#define ABORT_CASE (*__case_result__) = -1; return;
93
94
510
#define GUARD_ELSE_RETURN(x) 
if (232
x)
{}232
else
{ (*__case_result__) = -2; return; }0
95
96
100M
#define REQUIRE(a, err, ...) { \
97
100M
if (!(
a106
)) \
98
0
{ \
99
0
  if (isatty(fileno(stdout))) \
100
0
    printf("\n\t\033[0;31mREQUIRE\033[0;0m: %s:%d: %s is not true, " err, __FILE__, __LINE__, #a, ##__VA_ARGS__); \
101
0
  else \
102
0
    printf("\n\tREQUIRE: %s:%d: %s is not true, " err, __FILE__, __LINE__, #a, ##__VA_ARGS__); \
103
0
  ABORT_CASE; \
104
0
} }
105
106
103M
#define REQUIRE_EQ(a, b, err, ...) { \
107
103M
if (
(a) != (b)103M
) \
108
0
{ \
109
0
  if (isatty(fileno(stdout))) \
110
0
    printf("\n\t\033[0;31mREQUIRE_EQ\033[0;0m: %s:%d: %s(%lg) != %s(%lg), " err, __FILE__, __LINE__, #a, (double)(a), #b, (double)(b), ##__VA_ARGS__); \
111
0
  else \
112
0
    printf("\n\tREQUIRE_EQ: %s:%d: %s(%lg) != %s(%lg), " err, __FILE__, __LINE__, #a, (double)(a), #b, (double)(b), ##__VA_ARGS__); \
113
0
  ABORT_CASE; \
114
0
} }
115
116
75
#define REQUIRE_ARRAY_EQ(type, a, b, len, err, ...) { \
117
75
int __case_i__; \
118
67.2k
for (__case_i__ = 0; __case_i__ < (len); 
__case_i__++67.1k
) \
119
67.1k
  if (((type*)(a))[__case_i__] != ((type*)(b))[__case_i__]) \
120
67.1k
  { \
121
0
    if (isatty(fileno(stdout))) \
122
0
      printf("\n\t\033[0;31mREQUIRE_ARRAY_EQ\033[0;0m: %s:%d: %s[%d](%lg) != %s[%d](%lg), " err, __FILE__, __LINE__, #a, __case_i__, (double)((type*)(a))[__case_i__], #b, __case_i__, (double)((type*)(b))[__case_i__], ##__VA_ARGS__); \
123
0
    else \
124
0
      printf("\n\tREQUIRE_ARRAY_EQ: %s:%d: %s[%d](%lg) != %s[%d](%lg), " err, __FILE__, __LINE__, #a, __case_i__, (double)((type*)(a))[__case_i__], #b, __case_i__, (double)((type*)(b))[__case_i__], ##__VA_ARGS__); \
125
0
    ABORT_CASE; \
126
0
  } }
127
128
3.27k
#define REQUIRE_EQ_WITH_TOLERANCE(a, b, t, err, ...) { \
129
3.27k
if ((double)((a) - (
b9
)) > (t) || (double)((a) - (
b9
)) < -(t)) \
130
0
{ \
131
0
  if (isatty(fileno(stdout))) \
132
0
    printf("\n\t\033[0;31mREQUIRE_EQ_WITH_TOLERANCE\033[0;0m: %s:%d: %s(%lg) != %s(%lg) | +-%lg, " err, __FILE__, __LINE__, #a, (double)(a), #b, (double)(b), (double)(t), ##__VA_ARGS__); \
133
0
  else \
134
0
    printf("\n\tREQUIRE_EQ_WITH_TOLERANCE: %s:%d: %s(%lg) != %s(%lg) | +-%lg, " err, __FILE__, __LINE__, #a, (double)(a), #b, (double)(b), (double)(t), ##__VA_ARGS__); \
135
0
  ABORT_CASE; \
136
0
} }
137
138
131
#define REQUIRE_ARRAY_EQ_WITH_TOLERANCE(type, a, b, len, t, err, ...) { \
139
131
int __case_i__; \
140
151M
for (__case_i__ = 0; __case_i__ < (len); 
__case_i__++151M
) \
141
151M
  if ((double)(((type*)(a))[__case_i__] - ((type*)(b))[__case_i__]) > (t) || (double)(((type*)(a))[__case_i__] - ((type*)(b))[__case_i__]) < -(t)) \
142
151M
  { \
143
0
    if (isatty(fileno(stdout))) \
144
0
      printf("\n\t\033[0;31mREQUIRE_ARRAY_EQ_WITH_TOLERANCE\033[0;0m: %s:%d: %s[%d](%lg) != %s[%d](%lg) | +-%lg, " err, __FILE__, __LINE__, #a, __case_i__, (double)((type*)(a))[__case_i__], #b, __case_i__, (double)((type*)(b))[__case_i__], (double)(t), ##__VA_ARGS__); \
145
0
    else \
146
0
      printf("\n\tREQUIRE_ARRAY_EQ_WITH_TOLERANCE: %s:%d: %s[%d](%lg) != %s[%d](%lg) | +-%lg, " err, __FILE__, __LINE__, #a, __case_i__, (double)((type*)(a))[__case_i__], #b, __case_i__, (double)((type*)(b))[__case_i__], (double)(t), ##__VA_ARGS__); \
147
0
    ABORT_CASE; \
148
0
  } }
149
150
13
#define REQUIRE_NOT_EQ(a, b, err, ...) { \
151
13
if ((a) == (b)) \
152
0
{ \
153
0
  if (isatty(fileno(stdout))) \
154
0
    printf("\n\t\033[0;31mREQUIRE_NOT_EQ\033[0;0m: %s:%d: %s(%lg) == %s(%lg), " err, __FILE__, __LINE__, #a, (double)(a), #b, (double)(b), ##__VA_ARGS__); \
155
0
  else \
156
0
    printf("\n\tREQUIRE_NOT_EQ: %s:%d: %s(%lg) == %s(%lg), " err, __FILE__, __LINE__, #a, (double)(a), #b, (double)(b), ##__VA_ARGS__); \
157
0
  ABORT_CASE; \
158
0
} }
159
160
1
#define REQUIRE_ARRAY_NOT_EQ(type, a, b, len, err, ...) { \
161
1
int __case_i__; \
162
1
int __flag__ = 0; \
163
3
for (__case_i__ = 0; !__flag__ && 
__case_i__ < (len)2
;
__case_i__++2
) \
164
2
  __flag__ = (((type*)(a))[__case_i__] != ((type*)(b))[__case_i__]); \
165
1
if (!__flag__) \
166
0
{ \
167
0
  if (isatty(fileno(stdout))) \
168
0
    printf("\n\t\033[0;31mREQUIRE_ARRAY_NOT_EQ\033[0;0m: %s:%d: %s[%d](%lg) == %s[%d](%lg), " err, __FILE__, __LINE__, #a, __case_i__, (double)((type*)(a))[__case_i__], #b, __case_i__, (double)((type*)(b))[__case_i__], ##__VA_ARGS__); \
169
0
  else \
170
0
    printf("\n\tREQUIRE_ARRAY_NOT_EQ: %s:%d: %s[%d](%lg) == %s[%d](%lg), " err, __FILE__, __LINE__, #a, __case_i__, (double)((type*)(a))[__case_i__], #b, __case_i__, (double)((type*)(b))[__case_i__], ##__VA_ARGS__); \
171
0
  ABORT_CASE; \
172
0
} }
173
174
4
#define REQUIRE_NOT_EQ_WITH_TOLERANCE(a, b, t, err, ...) { \
175
4
if ((double)((a) - (b)) <= (t) && (double)((a) - (b)) >= -(t)) \
176
0
{ \
177
0
  if (isatty(fileno(stdout))) \
178
0
    printf("\n\t\033[0;31mREQUIRE_NOT_EQ_WITH_TLERANCE\033[0;0m: %s:%d: %s(%lg) == %s(%lg) | +-%lg, " err, __FILE__, __LINE__, #a, (double)(a), #b, (double)(b), (double)(t), ##__VA_ARGS__); \
179
0
  else \
180
0
    printf("\n\tREQUIRE_NOT_EQ_WITH_TLERANCE: %s:%d: %s(%lg) == %s(%lg) | +-%lg, " err, __FILE__, __LINE__, #a, (double)(a), #b, (double)(b), (double)(t), ##__VA_ARGS__); \
181
0
  ABORT_CASE; \
182
0
} }
183
184
#define REQUIRE_ARRAY_NOT_EQ_WITH_TOLERANCE(type, a, b, len, t, err, ...) { \
185
int __case_i__; \
186
int __flag__ = 0; \
187
for (__case_i__ = 0; !__flag__ && __case_i__ < (len); __case_i__++) \
188
  __flag__ = ((double)(((type*)(a))[__case_i__] - ((type*)(b))[__case_i__]) > (t) || (double)(((type*)(a))[__case_i__] - ((type*)(b))[__case_i__]) < -(t)); \
189
if (!__flag__) \
190
{ \
191
  if (isatty(fileno(stdout))) \
192
    printf("\n\t\033[0;31mREQUIRE_ARRAY_NOT_EQ_WITH_TOLERANCE\033[0;0m: %s:%d: %s[%d](%lg) == %s[%d](%lg) | +-%lg, " err, __FILE__, __LINE__, #a, __case_i__, (double)((type*)(a))[__case_i__], #b, __case_i__, (double)((type*)(b))[__case_i__], (double)(t), ##__VA_ARGS__); \
193
  else \
194
    printf("\n\tREQUIRE_ARRAY_NOT_EQ_WITH_TOLERANCE: %s:%d: %s[%d](%lg) == %s[%d](%lg) | +-%lg, " err, __FILE__, __LINE__, #a, __case_i__, (double)((type*)(a))[__case_i__], #b, __case_i__, (double)((type*)(b))[__case_i__], (double)(t), ##__VA_ARGS__); \
195
  ABORT_CASE; \
196
} }
197
198
#endif