lite.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Data Differential YATL (i.e. libtest) library
  4. *
  5. * Copyright (C) 2012 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. static inline size_t yatl_strlen(const char *s)
  73. {
  74. if (s)
  75. {
  76. return strlen(s);
  77. }
  78. return (size_t)(0);
  79. }
  80. static inline int yatl_strcmp(const char *s1, const char *s2, size_t *s1_length, size_t *s2_length)
  81. {
  82. *s1_length= yatl_strlen(s1);
  83. *s2_length= yatl_strlen(s2);
  84. if (*s1_length == 0 && *s1_length == *s2_length)
  85. {
  86. return 0;
  87. }
  88. if (*s1_length == 0 && *s2_length)
  89. {
  90. return 1;
  91. }
  92. if (*s1_length && *s2_length == 0)
  93. {
  94. return 1;
  95. }
  96. return strcmp(s1, s2);
  97. }
  98. #define SKIP_IF(__expression) \
  99. do \
  100. { \
  101. if ((__expression)) { \
  102. if (YATL_FULL) { \
  103. SKIP(#__expression); \
  104. } \
  105. fprintf(stdout, "\n%s:%d: %s SKIP '!(%s)'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression); \
  106. exit(EXIT_SKIP); \
  107. } \
  108. } while (0)
  109. #define SKIP_IF_(__expression, ...) \
  110. do \
  111. { \
  112. if ((__expression)) { \
  113. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  114. ask++; \
  115. char *buffer= (char*)alloca(sizeof(char) * ask); \
  116. snprintf(buffer, ask, __VA_ARGS__); \
  117. if (YATL_FULL) { \
  118. SKIP(#__expression, buffer); \
  119. } \
  120. fprintf(stdout, "\n%s:%d: %s SKIP '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
  121. exit(EXIT_SKIP); \
  122. } \
  123. } while (0)
  124. #define SKIP_UNLESS(__expression) \
  125. do \
  126. { \
  127. if (! (__expression)) { \
  128. if (YATL_FULL) { \
  129. SKIP(#__expression); \
  130. } \
  131. fprintf(stdout, "\n%s:%d: %s SKIP '(%s)'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression); \
  132. exit(EXIT_SKIP); \
  133. } \
  134. } while (0)
  135. #define SKIP_UNLESS_(__expression, ...) \
  136. do \
  137. { \
  138. if (! (__expression)) { \
  139. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  140. ask++; \
  141. char *buffer= (char*)alloca(sizeof(char) * ask); \
  142. snprintf(buffer, ask, __VA_ARGS__); \
  143. if (YATL_FULL) { \
  144. SKIP(#__expression, buffer); \
  145. } \
  146. fprintf(stdout, "\n%s:%d: %s SKIP '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
  147. exit(EXIT_SKIP); \
  148. } \
  149. } while (0)
  150. #define ASSERT_TRUE(__expression) \
  151. do \
  152. { \
  153. if (! (__expression)) { \
  154. if (YATL_FULL) { \
  155. FAIL("Assertion '%s'", #__expression); \
  156. } \
  157. fprintf(stderr, "\n%s:%d: %s Assertion '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
  158. exit(EXIT_FAILURE); \
  159. } \
  160. } while (0)
  161. #define ASSERT_FALSE(__expression) \
  162. do \
  163. { \
  164. if ((__expression)) { \
  165. if (YATL_FULL) { \
  166. FAIL("Assertion '!%s'", #__expression); \
  167. } \
  168. fprintf(stderr, "\n%s:%d: %s Assertion '!%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression);\
  169. exit(EXIT_FAILURE); \
  170. } \
  171. } while (0)
  172. #define ASSERT_NULL_(__expression, ...) \
  173. do \
  174. { \
  175. if ((__expression) != NULL) { \
  176. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  177. ask++; \
  178. char *buffer= (char*)alloca(sizeof(char) * ask); \
  179. snprintf(buffer, ask, __VA_ARGS__); \
  180. if (YATL_FULL) { \
  181. FAIL("Assertion '%s' != NULL [ %s ]", #__expression, buffer);\
  182. } \
  183. fprintf(stderr, "\n%s:%d: %s Assertion '%s' != NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
  184. exit(EXIT_FAILURE); \
  185. } \
  186. } while (0)
  187. #define ASSERT_NOT_NULL(__expression) \
  188. do \
  189. { \
  190. if ((__expression) == NULL) { \
  191. if (YATL_FULL) { \
  192. FAIL("Assertion '%s' == NULL", #__expression,);\
  193. } \
  194. fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression,);\
  195. exit(EXIT_FAILURE); \
  196. } \
  197. } while (0)
  198. #define ASSERT_NOT_NULL_(__expression, ...) \
  199. do \
  200. { \
  201. if ((__expression) == NULL) { \
  202. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  203. ask++; \
  204. char *buffer= (char*)alloca(sizeof(char) * ask); \
  205. snprintf(buffer, ask, __VA_ARGS__); \
  206. if (YATL_FULL) { \
  207. FAIL("Assertion '%s' == NULL [ %s ]", #__expression, buffer);\
  208. } \
  209. fprintf(stderr, "\n%s:%d: %s Assertion '%s' == NULL [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer);\
  210. exit(EXIT_FAILURE); \
  211. } \
  212. } while (0)
  213. #define ASSERT_TRUE_(__expression, ...) \
  214. do \
  215. { \
  216. if (! (__expression)) { \
  217. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  218. ask++; \
  219. char *buffer= (char*)alloca(sizeof(char) * ask); \
  220. snprintf(buffer, ask, __VA_ARGS__); \
  221. if (YATL_FULL) { \
  222. FAIL("Assertion '%s' [ %s ]", #__expression, buffer); \
  223. } \
  224. fprintf(stderr, "\n%s:%d: %s Assertion '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
  225. exit(EXIT_FAILURE); \
  226. } \
  227. } while (0)
  228. #define ASSERT_EQ(__expected, __actual) \
  229. do \
  230. { \
  231. if ((__expected) != (__actual)) { \
  232. if (YATL_FULL) { \
  233. FAIL("Assertion '%s' != '%s'", #__expected, #__actual); \
  234. } \
  235. fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
  236. exit(EXIT_FAILURE); \
  237. } \
  238. } while (0)
  239. #define ASSERT_EQ_(__expected, __actual, ...) \
  240. do \
  241. { \
  242. if ((__expected) != (__actual)) { \
  243. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  244. ask++; \
  245. char *buffer= (char*)alloca(sizeof(char) * ask); \
  246. snprintf(buffer, ask, __VA_ARGS__); \
  247. if (YATL_FULL) { \
  248. FAIL("Assertion '%s' != '%s' [ %s ]", #__expected, #__actual, buffer); \
  249. } \
  250. fprintf(stderr, "\n%s:%d: %s Assertion '%s' != '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
  251. exit(EXIT_FAILURE); \
  252. } \
  253. } while (0)
  254. #define ASSERT_STREQ(__expected_str, __actual_str) \
  255. do \
  256. { \
  257. size_t __expected_length; \
  258. size_t __actual_length; \
  259. int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
  260. if (ret) { \
  261. if (YATL_FULL) { \
  262. FAIL("Assertion '%.*s' != '%.*s'\n", \
  263. (int)(__expected_length), (__expected_str), \
  264. (int)__actual_length, (__actual_str)) ; \
  265. } \
  266. fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
  267. (int)(__expected_length), (__expected_str), \
  268. (int)__actual_length, (__actual_str)) ; \
  269. exit(EXIT_FAILURE); \
  270. } \
  271. } while (0)
  272. #define ASSERT_STREQ_(__expected_str, __actual_str, ...) \
  273. do \
  274. { \
  275. size_t __expected_length; \
  276. size_t __actual_length; \
  277. int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
  278. if (ret) { \
  279. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  280. ask++; \
  281. char *buffer= (char*)alloca(sizeof(char) * ask); \
  282. ask= snprintf(buffer, ask, __VA_ARGS__); \
  283. if (YATL_FULL) { \
  284. FAIL("Assertion '%.*s' != '%.*s' [ %.*s ]", \
  285. (int)(__expected_length), (__expected_str), \
  286. (int)(__actual_length), (__actual_str), \
  287. (int)(ask), buffer); \
  288. } \
  289. fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' != '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
  290. (int)(__expected_length), (__expected_str), \
  291. (int)(__actual_length), (__actual_str), \
  292. (int)(ask), buffer); \
  293. exit(EXIT_FAILURE); \
  294. } \
  295. } while (0)
  296. #define ASSERT_STRNE(__expected_str, __actual_str) \
  297. do \
  298. { \
  299. size_t __expected_length; \
  300. size_t __actual_length; \
  301. int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
  302. if (ret == 0) { \
  303. if (YATL_FULL) { \
  304. FAIL("Assertion '%.*s' == '%.*s'", \
  305. (int)(__expected_length), (__expected_str), \
  306. (int)__actual_length, (__actual_str)) ; \
  307. } \
  308. fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
  309. (int)(__expected_length), (__expected_str), \
  310. (int)__actual_length, (__actual_str)) ; \
  311. exit(EXIT_FAILURE); \
  312. } \
  313. } while (0)
  314. #define ASSERT_STRNE_(__expected_str, __actual_str, ...) \
  315. do \
  316. { \
  317. size_t __expected_length; \
  318. size_t __actual_length; \
  319. int ret= yatl_strcmp(__expected_str, __actual_str, &__expected_length, &__actual_length); \
  320. if (ret == 0) { \
  321. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  322. ask++; \
  323. char *buffer= (char*)alloca(sizeof(char) * ask); \
  324. ask= snprintf(buffer, ask, __VA_ARGS__); \
  325. if (YATL_FULL) { \
  326. FAIL("Assertion '%.*s' == '%.*s' [ %.*s ]", \
  327. (int)(__expected_length), (__expected_str), \
  328. (int)(__actual_length), (__actual_str), \
  329. (int)(ask), buffer); \
  330. } \
  331. fprintf(stderr, "\n%s:%d: %s Assertion '%.*s' == '%.*s' [ %.*s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, \
  332. (int)(__expected_length), (__expected_str), \
  333. (int)(__actual_length), (__actual_str), \
  334. (int)(ask), buffer); \
  335. exit(EXIT_FAILURE); \
  336. } \
  337. } while (0)
  338. #define ASSERT_NEQ(__expected, __actual) \
  339. do \
  340. { \
  341. if ((__expected) == (__actual)) { \
  342. if (YATL_FULL) { \
  343. FAIL("Assertion '%s' == '%s'", #__expected, #__actual); \
  344. } \
  345. fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s'\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual); \
  346. exit(EXIT_FAILURE); \
  347. } \
  348. } while (0)
  349. #define ASSERT_NEQ_(__expected, __actual, ...) \
  350. do \
  351. { \
  352. if ((__expected) == (__actual)) { \
  353. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  354. ask++; \
  355. char *buffer= (char*)alloca(sizeof(char) * ask); \
  356. snprintf(buffer, ask, __VA_ARGS__); \
  357. if (YATL_FULL) { \
  358. FAIL("Assertion '%s' == '%s' [ %s ]", #__expected, #__actual, buffer); \
  359. } \
  360. fprintf(stderr, "\n%s:%d: %s Assertion '%s' == '%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expected, #__actual, buffer); \
  361. exit(EXIT_FAILURE); \
  362. } \
  363. } while (0)
  364. #define ASSERT_FALSE_(__expression, ...) \
  365. do \
  366. { \
  367. if ((__expression)) { \
  368. size_t ask= snprintf(0, 0, __VA_ARGS__); \
  369. ask++; \
  370. char *buffer= (char*)alloca(sizeof(char) * ask); \
  371. snprintf(buffer, ask, __VA_ARGS__); \
  372. if (YATL_FULL) { \
  373. FAIL("Assertion '!%s' [ %s ]", #__expression, buffer); \
  374. } \
  375. fprintf(stderr, "\n%s:%d: %s Assertion '!%s' [ %s ]\n", __FILE__, __LINE__, __PRETTY_FUNCTION__, #__expression, buffer); \
  376. exit(EXIT_FAILURE); \
  377. } \
  378. } while (0)