lite.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Data Differential YATL (i.e. libtest) library
  4. *
  5. * Copyright (C) 2012-2013 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #pragma once
  37. #ifdef __cplusplus
  38. # include <cstdarg>
  39. # include <cstddef>
  40. # include <cstdio>
  41. # include <cstdlib>
  42. # include <cstring>
  43. #else
  44. # include <stdarg.h>
  45. # include <stdbool.h>
  46. # include <stddef.h>
  47. # include <stdio.h>
  48. # include <stdlib.h>
  49. # include <string.h>
  50. #endif
  51. #if defined(WIN32)
  52. # include <malloc.h>
  53. #else
  54. # include <alloca.h>
  55. #endif
  56. #ifndef __PRETTY_FUNCTION__
  57. # define __PRETTY_FUNCTION__ __func__
  58. #endif
  59. #ifndef EXIT_SKIP
  60. # define EXIT_SKIP 77
  61. #endif
  62. #ifndef YATL_FULL
  63. # define YATL_FULL 0
  64. #endif
  65. #ifndef FAIL
  66. # define FAIL(__message_format, ...)
  67. #endif
  68. #ifndef SKIP
  69. # define SKIP(__message_format, ...)
  70. #endif
  71. #include <libtest/valgrind.h>
  72. #ifndef DUMPCORE
  73. # if defined(YATL_FULL) && YATL_FULL
  74. # include <libtest/core.h>
  75. # define DUMPCORE libtest_create_core()
  76. # else
  77. # define DUMPCORE
  78. # endif
  79. #endif
  80. static inline size_t yatl_strlen(const char *s)
  81. {
  82. if (s)
  83. {
  84. return strlen(s);
  85. }
  86. return (size_t)(0);
  87. }
  88. static inline int yatl_strcmp(const char *s1, const char *s2, size_t *s1_length, size_t *s2_length)
  89. {
  90. *s1_length= yatl_strlen(s1);
  91. *s2_length= yatl_strlen(s2);
  92. if (*s1_length == 0 && *s1_length == *s2_length)
  93. {
  94. return 0;
  95. }
  96. if (*s1_length == 0 && *s2_length)
  97. {
  98. return 1;
  99. }
  100. if (*s1_length && *s2_length == 0)
  101. {
  102. return 1;
  103. }
  104. return strcmp(s1, s2);
  105. }
  106. #define SKIP_IF(__expression) \
  107. do \
  108. { \
  109. if ((__expression)) { \
  110. if (YATL_FULL) { \
  111. SKIP(#__expression); \
  112. } \
  113. fprintf(stdout, "\n%s:%d: %s SKIP '!(%s)'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression); \
  114. exit(EXIT_SKIP); \
  115. } \
  116. } while (0)
  117. #define SKIP_IF_(__expression, ...) \
  118. do \
  119. { \
  120. if ((__expression)) { \
  121. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  122. ask++; \
  123. char *buffer= (char*)alloca(sizeof(char) * ask); \
  124. snprintf(buffer, ask, __VA_ARGS__); \
  125. if (YATL_FULL) { \
  126. SKIP(#__expression, buffer); \
  127. } \
  128. fprintf(stdout, "\n%s:%d: %s SKIP '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
  129. exit(EXIT_SKIP); \
  130. } \
  131. } while (0)
  132. #define SKIP_UNLESS(__expression) \
  133. do \
  134. { \
  135. if (! (__expression)) { \
  136. if (YATL_FULL) { \
  137. SKIP(#__expression); \
  138. } \
  139. fprintf(stdout, "\n%s:%d: %s SKIP '(%s)'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression); \
  140. exit(EXIT_SKIP); \
  141. } \
  142. } while (0)
  143. #define SKIP_UNLESS_(__expression, ...) \
  144. do \
  145. { \
  146. if (! (__expression)) { \
  147. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  148. ask++; \
  149. char *buffer= (char*)alloca(sizeof(char) * ask); \
  150. snprintf(buffer, ask, __VA_ARGS__); \
  151. if (YATL_FULL) { \
  152. SKIP(#__expression, buffer); \
  153. } \
  154. fprintf(stdout, "\n%s:%d: %s SKIP '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
  155. exit(EXIT_SKIP); \
  156. } \
  157. } while (0)
  158. #define ASSERT_TRUE(__expression) \
  159. do \
  160. { \
  161. if (! (__expression)) { \
  162. DUMPCORE; \
  163. if (YATL_FULL) { \
  164. FAIL("Assertion '%s'", #__expression); \
  165. } \
  166. fprintf(stderr, "\n%s:%d: %s Assertion '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
  167. exit(EXIT_FAILURE); \
  168. } \
  169. } while (0)
  170. #define ASSERT_FALSE(__expression) \
  171. do \
  172. { \
  173. if ((__expression)) { \
  174. if (YATL_FULL) { \
  175. FAIL("Assertion '!%s'", #__expression); \
  176. } \
  177. fprintf(stderr, "\n%s:%d: %s Assertion '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
  178. exit(EXIT_FAILURE); \
  179. } \
  180. } while (0)
  181. #define ASSERT_NULL(__expression) \
  182. do \
  183. { \
  184. if ((__expression) != NULL) { \
  185. if (YATL_FULL) { \
  186. FAIL("Assertion '%s' != NULL", #__expression);\
  187. } \
  188. fprintf(stderr, "\n%s:%d: %s Assertion '%s' != NULL\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
  189. exit(EXIT_FAILURE); \
  190. } \
  191. } while (0)
  192. #define ASSERT_NULL_(__expression, ...) \
  193. do \
  194. { \
  195. if ((__expression) != NULL) { \
  196. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  197. ask++; \
  198. char *buffer= (char*)alloca(sizeof(char) * ask); \
  199. snprintf(buffer, ask, __VA_ARGS__); \
  200. if (YATL_FULL) { \
  201. FAIL("Assertion '%s' != NULL [ %s ]", #__expression, buffer);\
  202. } \
  203. fprintf(stderr, "\n%s:%d: %s Assertion '%s' != NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
  204. exit(EXIT_FAILURE); \
  205. } \
  206. } while (0)
  207. #define ASSERT_NOT_NULL(__expression) \
  208. do \
  209. { \
  210. if ((__expression) == NULL) { \
  211. if (YATL_FULL) { \
  212. FAIL("Assertion '%s' == NULL", #__expression);\
  213. } \
  214. fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
  215. exit(EXIT_FAILURE); \
  216. } \
  217. } while (0)
  218. #define ASSERT_NOT_NULL_(__expression, ...) \
  219. do \
  220. { \
  221. if ((__expression) == NULL) { \
  222. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  223. ask++; \
  224. char *buffer= (char*)alloca(sizeof(char) * ask); \
  225. snprintf(buffer, ask, __VA_ARGS__); \
  226. if (YATL_FULL) { \
  227. FAIL("Assertion '%s' == NULL [ %s ]", #__expression, buffer);\
  228. } \
  229. fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
  230. exit(EXIT_FAILURE); \
  231. } \
  232. } while (0)
  233. #define ASSERT_TRUE_(__expression, ...) \
  234. do \
  235. { \
  236. if (! (__expression)) { \
  237. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  238. ask++; \
  239. char *buffer= (char*)alloca(sizeof(char) * ask); \
  240. snprintf(buffer, ask, __VA_ARGS__); \
  241. if (YATL_FULL) { \
  242. FAIL("Assertion '%s' [ %s ]", #__expression, buffer); \
  243. } \
  244. fprintf(stderr, "\n%s:%d: %s Assertion '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
  245. exit(EXIT_FAILURE); \
  246. } \
  247. } while (0)
  248. #define ASSERT_EQ(__expected, __actual) \
  249. do \
  250. { \
  251. if ((__expected) != (__actual)) { \
  252. if (YATL_FULL) { \
  253. FAIL("Assertion '%s' != '%s'", #__expected, #__actual); \
  254. } \
  255. fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
  256. exit(EXIT_FAILURE); \
  257. } \
  258. } while (0)
  259. #define ASSERT_EQ_(__expected, __actual, ...) \
  260. do \
  261. { \
  262. if ((__expected) != (__actual)) { \
  263. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  264. ask++; \
  265. char *buffer= (char*)alloca(sizeof(char) * ask); \
  266. snprintf(buffer, ask, __VA_ARGS__); \
  267. if (YATL_FULL) { \
  268. FAIL("Assertion '%s' != '%s' [ %s ]", #__expected, #__actual, buffer); \
  269. } \
  270. fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
  271. exit(EXIT_FAILURE); \
  272. } \
  273. } while (0)
  274. #define ASSERT_STREQ(__expected_str, __actual_str) \
  275. do \
  276. { \
  277. size_t __expected_length; \
  278. size_t __actual_length; \
  279. int __ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
  280. if (__ret) { \
  281. if (YATL_FULL) { \
  282. FAIL("Assertion '%.*s' != '%.*s'\n", \
  283. (int)(__expected_length), (__expected_str), \
  284. (int)__actual_length, (__actual_str)) ; \
  285. } \
  286. fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
  287. (int)(__expected_length), (__expected_str), \
  288. (int)__actual_length, (__actual_str)) ; \
  289. exit(EXIT_FAILURE); \
  290. } \
  291. } while (0)
  292. #define ASSERT_STREQ_(__expected_str, __actual_str, ...) \
  293. do \
  294. { \
  295. size_t __expected_length; \
  296. size_t __actual_length; \
  297. int __ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
  298. if (__ret) { \
  299. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  300. ask++; \
  301. char *buffer= (char*)alloca(sizeof(char) * ask); \
  302. ask= snprintf(buffer, ask, __VA_ARGS__); \
  303. if (YATL_FULL) { \
  304. FAIL("Assertion '%.*s' != '%.*s' [ %.*s ]", \
  305. (int)(__expected_length), (__expected_str), \
  306. (int)(__actual_length), (__actual_str), \
  307. (int)(ask), buffer); \
  308. } \
  309. fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
  310. (int)(__expected_length), (__expected_str), \
  311. (int)(__actual_length), (__actual_str), \
  312. (int)(ask), buffer); \
  313. exit(EXIT_FAILURE); \
  314. } \
  315. } while (0)
  316. #define ASSERT_STRNE(__expected_str, __actual_str) \
  317. do \
  318. { \
  319. size_t __expected_length; \
  320. size_t __actual_length; \
  321. int __ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
  322. if (__ret == 0) { \
  323. if (YATL_FULL) { \
  324. FAIL("Assertion '%.*s' == '%.*s'", \
  325. (int)(__expected_length), (__expected_str), \
  326. (int)__actual_length, (__actual_str)) ; \
  327. } \
  328. fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
  329. (int)(__expected_length), (__expected_str), \
  330. (int)__actual_length, (__actual_str)) ; \
  331. exit(EXIT_FAILURE); \
  332. } \
  333. } while (0)
  334. #define ASSERT_STRNE_(__expected_str, __actual_str, ...) \
  335. do \
  336. { \
  337. size_t __expected_length; \
  338. size_t __actual_length; \
  339. int __ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
  340. if (__ret == 0) { \
  341. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  342. ask++; \
  343. char *buffer= (char*)alloca(sizeof(char) * ask); \
  344. ask= snprintf(buffer, ask, __VA_ARGS__); \
  345. if (YATL_FULL) { \
  346. FAIL("Assertion '%.*s' == '%.*s' [ %.*s ]", \
  347. (int)(__expected_length), (__expected_str), \
  348. (int)(__actual_length), (__actual_str), \
  349. (int)(ask), buffer); \
  350. } \
  351. fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
  352. (int)(__expected_length), (__expected_str), \
  353. (int)(__actual_length), (__actual_str), \
  354. (int)(ask), buffer); \
  355. exit(EXIT_FAILURE); \
  356. } \
  357. } while (0)
  358. #define ASSERT_NEQ(__expected, __actual) \
  359. do \
  360. { \
  361. if ((__expected) == (__actual)) { \
  362. if (YATL_FULL) { \
  363. FAIL("Assertion '%s' == '%s'", #__expected, #__actual); \
  364. } \
  365. fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
  366. exit(EXIT_FAILURE); \
  367. } \
  368. } while (0)
  369. #define ASSERT_NEQ_(__expected, __actual, ...) \
  370. do \
  371. { \
  372. if ((__expected) == (__actual)) { \
  373. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  374. ask++; \
  375. char *buffer= (char*)alloca(sizeof(char) * ask); \
  376. snprintf(buffer, ask, __VA_ARGS__); \
  377. if (YATL_FULL) { \
  378. FAIL("Assertion '%s' == '%s' [ %s ]", #__expected, #__actual, buffer); \
  379. } \
  380. fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
  381. exit(EXIT_FAILURE); \
  382. } \
  383. } while (0)
  384. #define ASSERT_FALSE_(__expression, ...) \
  385. do \
  386. { \
  387. if ((__expression)) { \
  388. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  389. ask++; \
  390. char *buffer= (char*)alloca(sizeof(char) * ask); \
  391. snprintf(buffer, ask, __VA_ARGS__); \
  392. if (YATL_FULL) { \
  393. FAIL("Assertion '!%s' [ %s ]", #__expression, buffer); \
  394. } \
  395. fprintf(stderr, "\n%s:%d: %s Assertion '!%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
  396. exit(EXIT_FAILURE); \
  397. } \
  398. } while (0)