1
0

litest-runner.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright © 2024 Red Hat, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "config.h"
  25. #include "util-mem.h"
  26. #include "util-range.h"
  27. #include "litest.h"
  28. #define LITEST_RUNNER_DEFAULT_TIMEOUT 30
  29. /**
  30. * Result returned from tests or suites.
  31. */
  32. enum litest_runner_result {
  33. LITEST_PASS = 75, /**< test successful */
  34. LITEST_FAIL = 76, /**< test failed. Should not be returned directly,
  35. Use the litest_ macros instead */
  36. LITEST_SKIP = 77, /**< test was skipped */
  37. LITEST_NOT_APPLICABLE = 78, /**< test does not apply */
  38. LITEST_TIMEOUT = 79, /**< test aborted after timeout */
  39. LITEST_SYSTEM_ERROR = 80, /**< unrelated error occurred */
  40. };
  41. /* For parametrized tests (litest_add_parametrized and friends)
  42. * a list of these is passed to every test. This struct isn't used
  43. * directly, use litest_test_param_fetch() instead.
  44. */
  45. struct litest_test_param {
  46. struct list link;
  47. char name[128];
  48. struct multivalue value;
  49. };
  50. struct litest_test_parameters {
  51. int refcnt;
  52. struct list test_params;
  53. };
  54. struct litest_test_parameters *
  55. litest_test_parameters_new(void);
  56. struct litest_test_parameters *
  57. litest_test_parameters_unref(struct litest_test_parameters *params);
  58. #define litest_test_param_fetch(...) \
  59. _litest_test_param_fetch(__VA_ARGS__, NULL)
  60. void
  61. _litest_test_param_fetch(const struct litest_test_parameters *params, ...);
  62. static inline const char *
  63. litest_test_param_get_string(const struct litest_test_parameters *params,
  64. const char *name)
  65. {
  66. const char *p;
  67. litest_test_param_fetch(params, name, 's', &p);
  68. return p;
  69. }
  70. static inline bool
  71. litest_test_param_get_bool(const struct litest_test_parameters *params,
  72. const char *name)
  73. {
  74. bool p;
  75. litest_test_param_fetch(params, name, 'b', &p);
  76. return p;
  77. }
  78. static inline int32_t
  79. litest_test_param_get_i32(const struct litest_test_parameters *params, const char *name)
  80. {
  81. int32_t p;
  82. litest_test_param_fetch(params, name, 'i', &p);
  83. return p;
  84. }
  85. static inline uint32_t
  86. litest_test_param_get_u32(const struct litest_test_parameters *params, const char *name)
  87. {
  88. uint32_t p;
  89. litest_test_param_fetch(params, name, 'u', &p);
  90. return p;
  91. }
  92. static inline char
  93. litest_test_param_get_char(const struct litest_test_parameters *params,
  94. const char *name)
  95. {
  96. char p;
  97. litest_test_param_fetch(params, name, 'c', &p);
  98. return p;
  99. }
  100. static inline double
  101. litest_test_param_get_double(const struct litest_test_parameters *params,
  102. const char *name)
  103. {
  104. double p;
  105. litest_test_param_fetch(params, name, 'd', &p);
  106. return p;
  107. }
  108. /**
  109. * This struct is passed into every test.
  110. */
  111. struct litest_runner_test_env {
  112. int rangeval; /* The current value within the args.range (or 0) */
  113. const struct litest_test_parameters *params;
  114. };
  115. struct litest_runner_test_description {
  116. char name[512]; /* The name of the test */
  117. int rangeval; /* The current value within the args.range (or 0) */
  118. struct litest_test_parameters *params;
  119. /* test function and corresponding setup/teardown, if any */
  120. enum litest_runner_result (*func)(const struct litest_runner_test_env *);
  121. void (*setup)(const struct litest_runner_test_description *);
  122. void (*teardown)(const struct litest_runner_test_description *);
  123. struct {
  124. struct range range; /* The range this test applies to */
  125. int signal; /* expected signal for fail tests */
  126. } args;
  127. };
  128. struct litest_runner;
  129. struct litest_runner *
  130. litest_runner_new(void);
  131. /**
  132. * Default is nprocs * 2.
  133. * Setting this to 0 means *no* forking. Setting this to 1 means only one test
  134. * is run at a time but in a child process.
  135. */
  136. void
  137. litest_runner_set_num_parallel(struct litest_runner *runner, size_t num_jobs);
  138. void
  139. litest_runner_set_timeout(struct litest_runner *runner, unsigned int timeout);
  140. void
  141. litest_runner_set_verbose(struct litest_runner *runner, bool verbose);
  142. void
  143. litest_runner_set_use_colors(struct litest_runner *runner, bool use_colors);
  144. void
  145. litest_runner_set_exit_on_fail(struct litest_runner *runner, bool do_exit);
  146. void
  147. litest_runner_set_output_file(struct litest_runner *runner, FILE *fp);
  148. void
  149. litest_runner_add_test(struct litest_runner *runner,
  150. const struct litest_runner_test_description *t);
  151. enum litest_runner_result
  152. litest_runner_run_tests(struct litest_runner *runner);
  153. typedef enum litest_runner_result (*litest_runner_global_setup_func_t)(void *userdata);
  154. typedef void (*litest_runner_global_teardown_func_t)(void *userdata);
  155. void
  156. litest_runner_set_setup_funcs(struct litest_runner *runner,
  157. litest_runner_global_setup_func_t setup,
  158. litest_runner_global_teardown_func_t teardown,
  159. void *userdata);
  160. void
  161. litest_runner_destroy(struct litest_runner *runner);
  162. DEFINE_DESTROY_CLEANUP_FUNC(litest_runner);
  163. /*
  164. * Function to call abort(). Depending on the number of forks permitted,
  165. * this function may simply abort() or it may longjmp back out to collect
  166. * errors from non-forking tests.
  167. */
  168. __attribute__((noreturn)) void
  169. litest_runner_abort(void);