test.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* uTest
  2. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  3. * Copyright (C) 2006-2009 Brian Aker
  4. * All rights reserved.
  5. *
  6. * Use and distribution licensed under the BSD license. See
  7. * the COPYING file in the parent directory for full text.
  8. */
  9. /*
  10. Structures for generic tests.
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <libtest/visibility.h>
  17. #pragma once
  18. enum test_return_t {
  19. TEST_SUCCESS= 0, /* Backwards compatibility */
  20. TEST_FAILURE,
  21. TEST_MEMORY_ALLOCATION_FAILURE,
  22. TEST_SKIPPED,
  23. TEST_MAXIMUM_RETURN /* Always add new error code before */
  24. };
  25. typedef void *(*test_callback_create_fn)(test_return_t *error);
  26. typedef test_return_t (*test_callback_fn)(void *);
  27. typedef test_return_t (*test_callback_runner_fn)(test_callback_fn, void *);
  28. typedef test_return_t (*test_callback_error_fn)(test_return_t, void *);
  29. /**
  30. A structure describing the test case.
  31. */
  32. struct test_st {
  33. const char *name;
  34. bool requires_flush;
  35. test_callback_fn test_fn;
  36. };
  37. /**
  38. A structure which describes a collection of test cases.
  39. */
  40. struct collection_st {
  41. const char *name;
  42. test_callback_fn pre;
  43. test_callback_fn post;
  44. test_st *tests;
  45. };
  46. /**
  47. Structure which houses the actual callers for the test cases contained in
  48. the collections.
  49. */
  50. struct world_runner_st {
  51. test_callback_runner_fn pre;
  52. test_callback_runner_fn run;
  53. test_callback_runner_fn post;
  54. };
  55. /**
  56. world_st is the structure which is passed to the test implementation to be filled.
  57. This must be implemented in order for the test framework to load the tests. We call
  58. get_world() in order to fill this structure.
  59. */
  60. struct world_st {
  61. collection_st *collections;
  62. /* These methods are called outside of any collection call. */
  63. test_callback_create_fn create;
  64. test_callback_fn destroy;
  65. /* This is called a the beginning of any collection run. */
  66. test_callback_fn collection_startup;
  67. /* This called on a test if the test requires a flush call (the bool is from test_st) */
  68. test_callback_fn flush;
  69. /**
  70. These are run before/after the test. If implemented. Their execution is not controlled
  71. by the test.
  72. */
  73. test_callback_fn pre_run;
  74. test_callback_fn post_run;
  75. /**
  76. If an error occurs during the test, this is called.
  77. */
  78. test_callback_error_fn on_error;
  79. /**
  80. Runner represents the callers for the tests. If not implemented we will use
  81. a set of default implementations.
  82. */
  83. world_runner_st *runner;
  84. world_st() :
  85. collections(NULL),
  86. create(NULL),
  87. destroy(NULL),
  88. collection_startup(NULL),
  89. flush(NULL),
  90. pre_run(NULL),
  91. post_run(NULL),
  92. on_error(NULL),
  93. runner(NULL)
  94. { }
  95. virtual ~world_st()
  96. { }
  97. };
  98. /**
  99. @note world_stats_st is a simple structure for tracking test successes.
  100. */
  101. struct world_stats_st {
  102. uint32_t success;
  103. uint32_t skipped;
  104. uint32_t failed;
  105. uint32_t total;
  106. world_stats_st() :
  107. success(0),
  108. skipped(0),
  109. failed(0),
  110. total(0)
  111. { }
  112. };
  113. #ifdef __cplusplus
  114. extern "C" {
  115. #endif
  116. /* How we make all of this work :) */
  117. LIBTEST_API
  118. void get_world(world_st *world);
  119. LIBTEST_INTERNAL_API
  120. void create_core(void);
  121. /**
  122. @note Friendly print function for errors.
  123. */
  124. LIBTEST_INTERNAL_API
  125. const char *test_strerror(test_return_t code);
  126. #define test_truth(A) \
  127. do \
  128. { \
  129. if (! (A)) { \
  130. fprintf(stderr, "\nAssertion failed in %s:%d: %s\n", __FILE__, __LINE__, #A);\
  131. create_core(); \
  132. return TEST_FAILURE; \
  133. } \
  134. } while (0)
  135. #define test_true_got(A,B) \
  136. do \
  137. { \
  138. if (! (A)) { \
  139. fprintf(stderr, "\nAssertion failed at %s:%d: \"%s\" received \"%s\"\n", __FILE__, __LINE__, #A, (B));\
  140. create_core(); \
  141. return TEST_FAILURE; \
  142. } \
  143. } while (0)
  144. #define test_false(A) \
  145. do \
  146. { \
  147. if ((A)) { \
  148. fprintf(stderr, "\nAssertion failed in %s:%d: %s\n", __FILE__, __LINE__, #A);\
  149. create_core(); \
  150. return TEST_FAILURE; \
  151. } \
  152. } while (0)
  153. #define test_strcmp(A,B) \
  154. do \
  155. { \
  156. if (strcmp((A), (B))) \
  157. { \
  158. fprintf(stderr, "\n%s:%d: %s -> %s\n", __FILE__, __LINE__, (A), (B)); \
  159. create_core(); \
  160. return TEST_FAILURE; \
  161. } \
  162. } while (0)
  163. #ifdef __cplusplus
  164. }
  165. #endif