Atto  1.4.0
The microscopic C test framework
atto.h
Go to the documentation of this file.
1 
33 #ifndef ATTO_H
34 #define ATTO_H
35 
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #endif
40 
44 #define ATTO_VERSION "1.4.0"
45 
46 #include <stdio.h> /* For printf() */
47 #include <math.h> /* For fabs(), fabsf(), isnan(), isinf(), isfinite() */
48 #include <string.h> /* For strncmp(), memcmp() */
49 #include <stddef.h> /* For size_t */
50 
58 extern char atto_at_least_one_fail;
59 
68 extern size_t atto_counter_assert_failures;
69 
77 extern size_t atto_counter_assert_passes;
78 
86 #define ATTO_FLOAT_EQ_ABSTOL (1e-5f)
87 
95 #define ATTO_DOUBLE_EQ_ABSTOL (1e-8)
96 
97 
108 #define atto_report() \
109  printf("REPORT | File: %s:%d | Test case: %s" \
110  " | Passes: %5zu | Failures: %5zu\n", \
111  __FILE__, __LINE__, __func__, \
112  atto_counter_assert_passes, atto_counter_assert_failures)
113 
136 #define atto_assert(expression) do { \
137  if (!(expression)) { \
138  printf("FAIL | File: %s:%d | Test case: %s\n", \
139  __FILE__, __LINE__, __func__); \
140  atto_counter_assert_failures++; \
141  atto_at_least_one_fail = 1; \
142  return; \
143  } else { \
144  atto_counter_assert_passes++; \
145  } \
146 } while (0)
147 
153 #define atto_true(x) atto_assert(x)
154 
167 #define atto_false(x) atto_assert(!(x))
168 
186 #define atto_eq(a, b) atto_assert((a) == (b))
187 
201 #define atto_neq(a, b) atto_assert((a) != (b))
202 
216 #define atto_gt(a, b) atto_assert((a) > (b))
217 
231 #define atto_ge(a, b) atto_assert((a) >= (b))
232 
246 #define atto_lt(a, b) atto_assert((a) < (b))
247 
261 #define atto_le(a, b) atto_assert((a) <= (b))
262 
276 #define atto_fdelta(a, b, delta) atto_assert(fabsf((a) - (b)) <= fabsf(delta))
277 
292 #define atto_fapprox(a, b) atto_fdelta((a), (b), ATTO_FLOAT_EQ_ABSTOL)
293 
307 #define atto_ddelta(a, b, delta) \
308  atto_assert(fabs((a) - (b)) <= fabs(delta))
309 
324 #define atto_dapprox(a, b) atto_ddelta((a), (b), ATTO_DOUBLE_EQ_ABSTOL)
325 
340 #define atto_nan(value) atto_assert(isnan(value))
341 
357 #define atto_inf(value) atto_assert(isinf(value))
358 
373 #define atto_plusinf(value) atto_assert((isinf(value)) && ((value) > 0))
374 
389 #define atto_minusinf(value) atto_assert((isinf(value)) && ((value) < 0))
390 
406 #define atto_finite(value) atto_assert(isfinite(value))
407 
423 #define atto_notfinite(value) atto_assert(!isfinite(value))
424 
438 #define atto_flag(value, mask) atto_assert(((value) & (mask)))
439 
452 #define atto_noflag(value, mask) atto_assert(((value) & (mask)) == 0)
453 
468 #define atto_streq(a, b, maxlen) \
469  atto_assert(strncmp((a), (b), (maxlen)) == 0)
470 
484 #define atto_memeq(a, b, len) atto_assert(memcmp((a), (b), len) == 0)
485 
499 #define atto_memneq(a, b, len) atto_assert(memcmp((a), (b), len) != 0)
500 
517 #define atto_zeros(x, len) do { \
518  for (size_t __atto_idx = 0; __atto_idx < (size_t)(len); __atto_idx++) \
519  { atto_eq(((uint8_t*)(x))[__atto_idx], 0); } \
520  } while(0)
521 
543 #define atto_nzeros(x, len) do { \
544  uint8_t __atto_all_zero = 1; \
545  for (size_t __atto_idx = 0; __atto_idx < (size_t)(len); __atto_idx++) \
546  { if(((uint8_t*)(x))[__atto_idx] != 0) \
547  { __atto_all_zero = 0; break; } \
548  } \
549  atto_false(__atto_all_zero); \
550  } while(0)
551 
556 #define atto_fail() atto_assert(0)
557 
558 #ifdef __cplusplus
559 }
560 #endif
561 
562 #endif /* ATTO_H */
char atto_at_least_one_fail
Boolean indicating if all tests passed successfully (when 0) or not.
size_t atto_counter_assert_passes
Counter of all Atto assertion macro calls that passed the check.
size_t atto_counter_assert_failures
Counter of all Atto assertion macro calls that failed the check.