Hash :
945de4a0
Author :
Thomas de Grivel
Date :
2024-07-24T18:31:19
CI :
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#ifndef TEST_H
#define TEST_H
#include <float.h>
#include <stdio.h>
#define TEST_COLOR_KO "\033[0;91m"
#define TEST_COLOR_OK "\033[0;92m"
#define TEST_COLOR_RESET "\033[0m"
#define TEST_ASSERT(test) \
do { \
if (test) { \
g_test_assert_count++; \
g_test_assert_ok++; \
} \
else { \
test_ko(); \
fprintf(stderr, "\nAssertion failed in %s:%d %s\n%s\n", \
__FILE__, __LINE__, __func__, # test); \
return 1; \
} \
} while(0)
#define TEST_CASE(name) \
char test_case_ ## name (void) \
{ \
g_test_case_name = # name; \
#define TEST_CASE_END(name) \
test_ok(); \
return 0; \
}
#define TEST_CASE_PROTOTYPE(name) \
char test_case_ ## name (void)
#define TEST_CASE_RUN(name) \
test_case_ ## name()
#define TEST_EQ(test, expected) \
do { \
long long signed TEST_EQ_tmp = (long long signed) (test); \
if (TEST_EQ_tmp == (long long signed) (expected)) { \
g_test_assert_count++; \
g_test_assert_ok++; \
} \
else { \
test_ko(); \
fprintf(stderr, "\n%sAssertion failed in %s:%d %s\n" \
"%s == %s\n" \
"Expected %s got %lld.%s\n", \
TEST_COLOR_KO, \
__FILE__, __LINE__, __func__, \
# test, # expected, # expected, TEST_EQ_tmp, \
TEST_COLOR_RESET); \
return 1; \
} \
} while (0)
#define TEST_FLOAT_EQ(test, expected) \
do { \
float TEST_FLOAT_EQ_tmp = (float) (test); \
if (fabsf(TEST_FLOAT_EQ_tmp - (float) (expected)) <= \
FLT_EPSILON) { \
g_test_assert_count++; \
g_test_assert_ok++; \
} \
else { \
test_ko(); \
fprintf(stderr, "\n%sAssertion failed in %s:%d %s\n" \
"%s == %s\n" \
"Expected %s got %.8g.%s\n", \
TEST_COLOR_KO, \
__FILE__, __LINE__, __func__, \
# test, # expected, # expected, TEST_FLOAT_EQ_tmp, \
TEST_COLOR_RESET); \
return 1; \
} \
} while (0)
#define TEST_FLOAT_EQ2(test, expected1, expected2) \
do { \
float TEST_FLOAT_EQ2_tmp = (float) (test); \
if (fabsf(TEST_FLOAT_EQ2_tmp - \
(float) (expected1)) <= FLT_EPSILON || \
fabsf(TEST_FLOAT_EQ2_tmp - \
(float) (expected2)) <= FLT_EPSILON) { \
g_test_assert_count++; \
g_test_assert_ok++; \
} \
else { \
test_ko(); \
fprintf(stderr, "\n%sAssertion failed in %s:%d %s\n" \
"%s == %s\n" \
"Expected %s got %.8g.%s\n", \
TEST_COLOR_KO, \
__FILE__, __LINE__, __func__, \
# test, # expected1, # expected1, TEST_FLOAT_EQ2_tmp, \
TEST_COLOR_RESET); \
return 1; \
} \
} while (0)
#define TEST_DOUBLE_EQ(test, expected) \
do { \
double TEST_DOUBLE_EQ_tmp = (double) (test); \
if (fabs(TEST_DOUBLE_EQ_tmp - (expected)) <= DBL_EPSILON) { \
g_test_assert_count++; \
g_test_assert_ok++; \
} \
else { \
test_ko(); \
fprintf(stderr, "\n%sAssertion failed in %s:%d %s\n" \
"%s == %s\n" \
"Expected %s got %.16lg.%s\n", \
TEST_COLOR_KO, \
__FILE__, __LINE__, __func__, \
# test, # expected, # expected, TEST_DOUBLE_EQ_tmp, \
TEST_COLOR_RESET); \
return 1; \
} \
} while (0)
#define TEST_STR_COMPARE(a, b, expected) \
do { \
sw TEST_STR_COMPARE_tmp = str_compare(a, b); \
if (TEST_STR_COMPARE_tmp == expected) { \
g_test_assert_count++; \
g_test_assert_ok++; \
} \
else { \
test_ko(); \
fprintf(stderr, "\n%sAssertion failed in %s:%d %s\n" \
"str_compare(%s, %s) == %s\n" \
"Expected %s got %ld.%s\n", \
TEST_COLOR_KO, \
__FILE__, __LINE__, __func__, \
# a, # b, # expected, # expected, TEST_STR_COMPARE_tmp, \
TEST_COLOR_RESET); \
return 1; \
} \
} while (0)
#define TEST_STRNCMP(test, result, bytes) \
do { \
const char *TEST_STRNCMP_tmp = (test); \
if (strncmp(TEST_STRNCMP_tmp, (result), (bytes)) == 0) { \
g_test_assert_count++; \
g_test_assert_ok++; \
} \
else { \
test_ko(); \
fprintf(stderr, "\n%sAssertion failed in %s:%d %s\n" \
"strncmp(%s, %s, %ld) == 0\n", \
TEST_COLOR_KO, \
__FILE__, __LINE__, __func__, \
# test, # result, (long) (bytes)); \
fprintf(stderr, "Expected %s got \"", \
# result); \
fwrite(TEST_STRNCMP_tmp, (bytes), 1, stderr); \
fprintf(stderr, "\".%s\n", TEST_COLOR_RESET); \
return 1; \
} \
} while (0)
extern long g_test_assert_count;
extern long g_test_assert_ko;
extern long g_test_assert_ok;
extern const char *g_test_case_name;
extern long g_test_count;
extern long g_test_ko;
extern long g_test_last_ok;
extern long g_test_ok;
extern const char **g_test_targets;
/* Init and clean. */
void test_clean (void);
void test_init (char *argv0, int *argc, char ***argv);
/* Operators. */
void test_context (const char *context);
int test_file_compare (const char *path_a, const char *path_b);
void test_ko (void);
void test_ok (void);
void test_summary (void);
int test_target (const char *target);
#endif /* TEST_H */