luasandbox  1.4.0
Generic Lua sandbox for dynamic data analysis
mu_test.h
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /** @brief Unit test macros and global data @file */
8 
9 #ifndef luasandbox_test_mu_test_h_
10 #define luasandbox_test_mu_test_h_
11 
12 #include <stdio.h>
13 
14 #ifdef _WIN32
15 #if _MSC_VER < 1900
16 #define snprintf _snprintf
17 #endif
18 #endif
19 
20 #if defined(_MSC_VER)
21 #define PRIuSIZE "Iu"
22 #else
23 #define PRIuSIZE "zu"
24 #endif
25 
26 #define MU_ERR_LEN 1024
27 
28 #define mu_assert(cond, ...) \
29 do { \
30  if (!(cond)) { \
31  int cnt = snprintf(mu_err, MU_ERR_LEN, "line: %d (%s) ", __LINE__, #cond); \
32  if (cnt > 0 && cnt < MU_ERR_LEN) { \
33  cnt = snprintf(mu_err+cnt, MU_ERR_LEN-cnt, __VA_ARGS__); \
34  if (cnt > 0 && cnt < MU_ERR_LEN) { \
35  return mu_err; \
36  } \
37  } \
38  mu_err[MU_ERR_LEN - 1] = 0; \
39  return mu_err; \
40  } \
41 } while (0)
42 
43 #define mu_assert_rv(rv, fn) \
44 do { \
45  int result = fn; \
46  if (rv != result) { \
47  int cnt = snprintf(mu_err, MU_ERR_LEN, "line: %d %s expected: %d " \
48  " received: %d", __LINE__, #fn, rv, result); \
49  if (cnt > 0 && cnt < MU_ERR_LEN) { \
50  return mu_err; \
51  } \
52  mu_err[MU_ERR_LEN - 1] = 0; \
53  return mu_err; \
54  } \
55 } while (0)
56 
57 #define mu_run_test(test) \
58 do { \
59  char *message = test(); \
60  mu_tests_run++; \
61  if (message) \
62  return message; \
63 } while (0)
64 
65 int mu_tests_run = 0;
66 char mu_err[MU_ERR_LEN] = { 0 };
67 
68 #endif
char mu_err[MU_ERR_LEN]
Definition: mu_test.h:66
int mu_tests_run
Definition: mu_test.h:65
#define MU_ERR_LEN
Definition: mu_test.h:26