test.h 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Base unit test (KUnit) API.
  4. *
  5. * Copyright (C) 2019, Google LLC.
  6. * Author: Brendan Higgins <brendanhiggins@google.com>
  7. */
  8. #ifndef _KUNIT_TEST_H
  9. #define _KUNIT_TEST_H
  10. #include <kunit/assert.h>
  11. #include <kunit/try-catch.h>
  12. #include <linux/args.h>
  13. #include <linux/compiler.h>
  14. #include <linux/container_of.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/jump_label.h>
  18. #include <linux/kconfig.h>
  19. #include <linux/kref.h>
  20. #include <linux/list.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/string.h>
  25. #include <linux/types.h>
  26. #include <asm/rwonce.h>
  27. #include <asm/sections.h>
  28. /* Static key: true if any KUnit tests are currently running */
  29. DECLARE_STATIC_KEY_FALSE(kunit_running);
  30. struct kunit;
  31. struct string_stream;
  32. /* Maximum size of parameter description string. */
  33. #define KUNIT_PARAM_DESC_SIZE 128
  34. /* Maximum size of a status comment. */
  35. #define KUNIT_STATUS_COMMENT_SIZE 256
  36. /*
  37. * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
  38. * sub-subtest. See the "Subtests" section in
  39. * https://node-tap.org/tap-protocol/
  40. */
  41. #define KUNIT_INDENT_LEN 4
  42. #define KUNIT_SUBTEST_INDENT " "
  43. #define KUNIT_SUBSUBTEST_INDENT " "
  44. /**
  45. * enum kunit_status - Type of result for a test or test suite
  46. * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
  47. * @KUNIT_FAILURE: Denotes the test has failed.
  48. * @KUNIT_SKIPPED: Denotes the test has been skipped.
  49. */
  50. enum kunit_status {
  51. KUNIT_SUCCESS,
  52. KUNIT_FAILURE,
  53. KUNIT_SKIPPED,
  54. };
  55. /* Attribute struct/enum definitions */
  56. /*
  57. * Speed Attribute is stored as an enum and separated into categories of
  58. * speed: very_slowm, slow, and normal. These speeds are relative to
  59. * other KUnit tests.
  60. *
  61. * Note: unset speed attribute acts as default of KUNIT_SPEED_NORMAL.
  62. */
  63. enum kunit_speed {
  64. KUNIT_SPEED_UNSET,
  65. KUNIT_SPEED_VERY_SLOW,
  66. KUNIT_SPEED_SLOW,
  67. KUNIT_SPEED_NORMAL,
  68. KUNIT_SPEED_MAX = KUNIT_SPEED_NORMAL,
  69. };
  70. /* Holds attributes for each test case and suite */
  71. struct kunit_attributes {
  72. enum kunit_speed speed;
  73. };
  74. /**
  75. * struct kunit_case - represents an individual test case.
  76. *
  77. * @run_case: the function representing the actual test case.
  78. * @name: the name of the test case.
  79. * @generate_params: the generator function for parameterized tests.
  80. * @attr: the attributes associated with the test
  81. *
  82. * A test case is a function with the signature,
  83. * ``void (*)(struct kunit *)``
  84. * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
  85. * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
  86. * with a &struct kunit_suite and will be run after the suite's init
  87. * function and followed by the suite's exit function.
  88. *
  89. * A test case should be static and should only be created with the
  90. * KUNIT_CASE() macro; additionally, every array of test cases should be
  91. * terminated with an empty test case.
  92. *
  93. * Example:
  94. *
  95. * .. code-block:: c
  96. *
  97. * void add_test_basic(struct kunit *test)
  98. * {
  99. * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
  100. * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
  101. * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
  102. * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
  103. * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
  104. * }
  105. *
  106. * static struct kunit_case example_test_cases[] = {
  107. * KUNIT_CASE(add_test_basic),
  108. * {}
  109. * };
  110. *
  111. */
  112. struct kunit_case {
  113. void (*run_case)(struct kunit *test);
  114. const char *name;
  115. const void* (*generate_params)(const void *prev, char *desc);
  116. struct kunit_attributes attr;
  117. /* private: internal use only. */
  118. enum kunit_status status;
  119. char *module_name;
  120. struct string_stream *log;
  121. };
  122. static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
  123. {
  124. switch (status) {
  125. case KUNIT_SKIPPED:
  126. case KUNIT_SUCCESS:
  127. return "ok";
  128. case KUNIT_FAILURE:
  129. return "not ok";
  130. }
  131. return "invalid";
  132. }
  133. /**
  134. * KUNIT_CASE - A helper for creating a &struct kunit_case
  135. *
  136. * @test_name: a reference to a test case function.
  137. *
  138. * Takes a symbol for a function representing a test case and creates a
  139. * &struct kunit_case object from it. See the documentation for
  140. * &struct kunit_case for an example on how to use it.
  141. */
  142. #define KUNIT_CASE(test_name) \
  143. { .run_case = test_name, .name = #test_name, \
  144. .module_name = KBUILD_MODNAME}
  145. /**
  146. * KUNIT_CASE_ATTR - A helper for creating a &struct kunit_case
  147. * with attributes
  148. *
  149. * @test_name: a reference to a test case function.
  150. * @attributes: a reference to a struct kunit_attributes object containing
  151. * test attributes
  152. */
  153. #define KUNIT_CASE_ATTR(test_name, attributes) \
  154. { .run_case = test_name, .name = #test_name, \
  155. .attr = attributes, .module_name = KBUILD_MODNAME}
  156. /**
  157. * KUNIT_CASE_SLOW - A helper for creating a &struct kunit_case
  158. * with the slow attribute
  159. *
  160. * @test_name: a reference to a test case function.
  161. */
  162. #define KUNIT_CASE_SLOW(test_name) \
  163. { .run_case = test_name, .name = #test_name, \
  164. .attr.speed = KUNIT_SPEED_SLOW, .module_name = KBUILD_MODNAME}
  165. /**
  166. * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
  167. *
  168. * @test_name: a reference to a test case function.
  169. * @gen_params: a reference to a parameter generator function.
  170. *
  171. * The generator function::
  172. *
  173. * const void* gen_params(const void *prev, char *desc)
  174. *
  175. * is used to lazily generate a series of arbitrarily typed values that fit into
  176. * a void*. The argument @prev is the previously returned value, which should be
  177. * used to derive the next value; @prev is set to NULL on the initial generator
  178. * call. When no more values are available, the generator must return NULL.
  179. * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
  180. * describing the parameter.
  181. */
  182. #define KUNIT_CASE_PARAM(test_name, gen_params) \
  183. { .run_case = test_name, .name = #test_name, \
  184. .generate_params = gen_params, .module_name = KBUILD_MODNAME}
  185. /**
  186. * KUNIT_CASE_PARAM_ATTR - A helper for creating a parameterized &struct
  187. * kunit_case with attributes
  188. *
  189. * @test_name: a reference to a test case function.
  190. * @gen_params: a reference to a parameter generator function.
  191. * @attributes: a reference to a struct kunit_attributes object containing
  192. * test attributes
  193. */
  194. #define KUNIT_CASE_PARAM_ATTR(test_name, gen_params, attributes) \
  195. { .run_case = test_name, .name = #test_name, \
  196. .generate_params = gen_params, \
  197. .attr = attributes, .module_name = KBUILD_MODNAME}
  198. /**
  199. * struct kunit_suite - describes a related collection of &struct kunit_case
  200. *
  201. * @name: the name of the test. Purely informational.
  202. * @suite_init: called once per test suite before the test cases.
  203. * @suite_exit: called once per test suite after all test cases.
  204. * @init: called before every test case.
  205. * @exit: called after every test case.
  206. * @test_cases: a null terminated array of test cases.
  207. * @attr: the attributes associated with the test suite
  208. *
  209. * A kunit_suite is a collection of related &struct kunit_case s, such that
  210. * @init is called before every test case and @exit is called after every
  211. * test case, similar to the notion of a *test fixture* or a *test class*
  212. * in other unit testing frameworks like JUnit or Googletest.
  213. *
  214. * Note that @exit and @suite_exit will run even if @init or @suite_init
  215. * fail: make sure they can handle any inconsistent state which may result.
  216. *
  217. * Every &struct kunit_case must be associated with a kunit_suite for KUnit
  218. * to run it.
  219. */
  220. struct kunit_suite {
  221. const char name[256];
  222. int (*suite_init)(struct kunit_suite *suite);
  223. void (*suite_exit)(struct kunit_suite *suite);
  224. int (*init)(struct kunit *test);
  225. void (*exit)(struct kunit *test);
  226. struct kunit_case *test_cases;
  227. struct kunit_attributes attr;
  228. /* private: internal use only */
  229. char status_comment[KUNIT_STATUS_COMMENT_SIZE];
  230. struct dentry *debugfs;
  231. struct string_stream *log;
  232. int suite_init_err;
  233. bool is_init;
  234. };
  235. /* Stores an array of suites, end points one past the end */
  236. struct kunit_suite_set {
  237. struct kunit_suite * const *start;
  238. struct kunit_suite * const *end;
  239. };
  240. /**
  241. * struct kunit - represents a running instance of a test.
  242. *
  243. * @priv: for user to store arbitrary data. Commonly used to pass data
  244. * created in the init function (see &struct kunit_suite).
  245. *
  246. * Used to store information about the current context under which the test
  247. * is running. Most of this data is private and should only be accessed
  248. * indirectly via public functions; the one exception is @priv which can be
  249. * used by the test writer to store arbitrary data.
  250. */
  251. struct kunit {
  252. void *priv;
  253. /* private: internal use only. */
  254. const char *name; /* Read only after initialization! */
  255. struct string_stream *log; /* Points at case log after initialization */
  256. struct kunit_try_catch try_catch;
  257. /* param_value is the current parameter value for a test case. */
  258. const void *param_value;
  259. /* param_index stores the index of the parameter in parameterized tests. */
  260. int param_index;
  261. /*
  262. * success starts as true, and may only be set to false during a
  263. * test case; thus, it is safe to update this across multiple
  264. * threads using WRITE_ONCE; however, as a consequence, it may only
  265. * be read after the test case finishes once all threads associated
  266. * with the test case have terminated.
  267. */
  268. spinlock_t lock; /* Guards all mutable test state. */
  269. enum kunit_status status; /* Read only after test_case finishes! */
  270. /*
  271. * Because resources is a list that may be updated multiple times (with
  272. * new resources) from any thread associated with a test case, we must
  273. * protect it with some type of lock.
  274. */
  275. struct list_head resources; /* Protected by lock. */
  276. char status_comment[KUNIT_STATUS_COMMENT_SIZE];
  277. /* Saves the last seen test. Useful to help with faults. */
  278. struct kunit_loc last_seen;
  279. };
  280. static inline void kunit_set_failure(struct kunit *test)
  281. {
  282. WRITE_ONCE(test->status, KUNIT_FAILURE);
  283. }
  284. bool kunit_enabled(void);
  285. const char *kunit_action(void);
  286. const char *kunit_filter_glob(void);
  287. char *kunit_filter(void);
  288. char *kunit_filter_action(void);
  289. void kunit_init_test(struct kunit *test, const char *name, struct string_stream *log);
  290. int kunit_run_tests(struct kunit_suite *suite);
  291. size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
  292. unsigned int kunit_test_case_num(struct kunit_suite *suite,
  293. struct kunit_case *test_case);
  294. struct kunit_suite_set
  295. kunit_filter_suites(const struct kunit_suite_set *suite_set,
  296. const char *filter_glob,
  297. char *filters,
  298. char *filter_action,
  299. int *err);
  300. void kunit_free_suite_set(struct kunit_suite_set suite_set);
  301. int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
  302. void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
  303. void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin);
  304. void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr);
  305. struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_set,
  306. struct kunit_suite_set suite_set);
  307. #if IS_BUILTIN(CONFIG_KUNIT)
  308. int kunit_run_all_tests(void);
  309. #else
  310. static inline int kunit_run_all_tests(void)
  311. {
  312. return 0;
  313. }
  314. #endif /* IS_BUILTIN(CONFIG_KUNIT) */
  315. #define __kunit_test_suites(unique_array, ...) \
  316. static struct kunit_suite *unique_array[] \
  317. __aligned(sizeof(struct kunit_suite *)) \
  318. __used __section(".kunit_test_suites") = { __VA_ARGS__ }
  319. /**
  320. * kunit_test_suites() - used to register one or more &struct kunit_suite
  321. * with KUnit.
  322. *
  323. * @__suites: a statically allocated list of &struct kunit_suite.
  324. *
  325. * Registers @suites with the test framework.
  326. * This is done by placing the array of struct kunit_suite * in the
  327. * .kunit_test_suites ELF section.
  328. *
  329. * When builtin, KUnit tests are all run via the executor at boot, and when
  330. * built as a module, they run on module load.
  331. *
  332. */
  333. #define kunit_test_suites(__suites...) \
  334. __kunit_test_suites(__UNIQUE_ID(array), \
  335. ##__suites)
  336. #define kunit_test_suite(suite) kunit_test_suites(&suite)
  337. #define __kunit_init_test_suites(unique_array, ...) \
  338. static struct kunit_suite *unique_array[] \
  339. __aligned(sizeof(struct kunit_suite *)) \
  340. __used __section(".kunit_init_test_suites") = { __VA_ARGS__ }
  341. /**
  342. * kunit_test_init_section_suites() - used to register one or more &struct
  343. * kunit_suite containing init functions or
  344. * init data.
  345. *
  346. * @__suites: a statically allocated list of &struct kunit_suite.
  347. *
  348. * This functions similar to kunit_test_suites() except that it compiles the
  349. * list of suites during init phase.
  350. *
  351. * This macro also suffixes the array and suite declarations it makes with
  352. * _probe; so that modpost suppresses warnings about referencing init data
  353. * for symbols named in this manner.
  354. *
  355. * Note: these init tests are not able to be run after boot so there is no
  356. * "run" debugfs file generated for these tests.
  357. *
  358. * Also, do not mark the suite or test case structs with __initdata because
  359. * they will be used after the init phase with debugfs.
  360. */
  361. #define kunit_test_init_section_suites(__suites...) \
  362. __kunit_init_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \
  363. ##__suites)
  364. #define kunit_test_init_section_suite(suite) \
  365. kunit_test_init_section_suites(&suite)
  366. #define kunit_suite_for_each_test_case(suite, test_case) \
  367. for (test_case = suite->test_cases; test_case->run_case; test_case++)
  368. enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
  369. /**
  370. * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
  371. * @test: The test context object.
  372. * @n: number of elements.
  373. * @size: The size in bytes of the desired memory.
  374. * @gfp: flags passed to underlying kmalloc().
  375. *
  376. * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
  377. * and is automatically cleaned up after the test case concludes. See kunit_add_action()
  378. * for more information.
  379. *
  380. * Note that some internal context data is also allocated with GFP_KERNEL,
  381. * regardless of the gfp passed in.
  382. */
  383. void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
  384. /**
  385. * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
  386. * @test: The test context object.
  387. * @size: The size in bytes of the desired memory.
  388. * @gfp: flags passed to underlying kmalloc().
  389. *
  390. * See kmalloc() and kunit_kmalloc_array() for more information.
  391. *
  392. * Note that some internal context data is also allocated with GFP_KERNEL,
  393. * regardless of the gfp passed in.
  394. */
  395. static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
  396. {
  397. return kunit_kmalloc_array(test, 1, size, gfp);
  398. }
  399. /**
  400. * kunit_kfree() - Like kfree except for allocations managed by KUnit.
  401. * @test: The test case to which the resource belongs.
  402. * @ptr: The memory allocation to free.
  403. */
  404. void kunit_kfree(struct kunit *test, const void *ptr);
  405. /**
  406. * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
  407. * @test: The test context object.
  408. * @size: The size in bytes of the desired memory.
  409. * @gfp: flags passed to underlying kmalloc().
  410. *
  411. * See kzalloc() and kunit_kmalloc_array() for more information.
  412. */
  413. static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
  414. {
  415. return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
  416. }
  417. /**
  418. * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
  419. * @test: The test context object.
  420. * @n: number of elements.
  421. * @size: The size in bytes of the desired memory.
  422. * @gfp: flags passed to underlying kmalloc().
  423. *
  424. * See kcalloc() and kunit_kmalloc_array() for more information.
  425. */
  426. static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
  427. {
  428. return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
  429. }
  430. /**
  431. * kunit_kfree_const() - conditionally free test managed memory
  432. * @test: The test context object.
  433. * @x: pointer to the memory
  434. *
  435. * Calls kunit_kfree() only if @x is not in .rodata section.
  436. * See kunit_kstrdup_const() for more information.
  437. */
  438. void kunit_kfree_const(struct kunit *test, const void *x);
  439. /**
  440. * kunit_kstrdup() - Duplicates a string into a test managed allocation.
  441. *
  442. * @test: The test context object.
  443. * @str: The NULL-terminated string to duplicate.
  444. * @gfp: flags passed to underlying kmalloc().
  445. *
  446. * See kstrdup() and kunit_kmalloc_array() for more information.
  447. */
  448. static inline char *kunit_kstrdup(struct kunit *test, const char *str, gfp_t gfp)
  449. {
  450. size_t len;
  451. char *buf;
  452. if (!str)
  453. return NULL;
  454. len = strlen(str) + 1;
  455. buf = kunit_kmalloc(test, len, gfp);
  456. if (buf)
  457. memcpy(buf, str, len);
  458. return buf;
  459. }
  460. /**
  461. * kunit_kstrdup_const() - Conditionally duplicates a string into a test managed allocation.
  462. *
  463. * @test: The test context object.
  464. * @str: The NULL-terminated string to duplicate.
  465. * @gfp: flags passed to underlying kmalloc().
  466. *
  467. * Calls kunit_kstrdup() only if @str is not in the rodata section. Must be freed with
  468. * kunit_kfree_const() -- not kunit_kfree().
  469. * See kstrdup_const() and kunit_kmalloc_array() for more information.
  470. */
  471. const char *kunit_kstrdup_const(struct kunit *test, const char *str, gfp_t gfp);
  472. /**
  473. * kunit_vm_mmap() - Allocate KUnit-tracked vm_mmap() area
  474. * @test: The test context object.
  475. * @file: struct file pointer to map from, if any
  476. * @addr: desired address, if any
  477. * @len: how many bytes to allocate
  478. * @prot: mmap PROT_* bits
  479. * @flag: mmap flags
  480. * @offset: offset into @file to start mapping from.
  481. *
  482. * See vm_mmap() for more information.
  483. */
  484. unsigned long kunit_vm_mmap(struct kunit *test, struct file *file,
  485. unsigned long addr, unsigned long len,
  486. unsigned long prot, unsigned long flag,
  487. unsigned long offset);
  488. void kunit_cleanup(struct kunit *test);
  489. void __printf(2, 3) kunit_log_append(struct string_stream *log, const char *fmt, ...);
  490. /**
  491. * kunit_mark_skipped() - Marks @test_or_suite as skipped
  492. *
  493. * @test_or_suite: The test context object.
  494. * @fmt: A printk() style format string.
  495. *
  496. * Marks the test as skipped. @fmt is given output as the test status
  497. * comment, typically the reason the test was skipped.
  498. *
  499. * Test execution continues after kunit_mark_skipped() is called.
  500. */
  501. #define kunit_mark_skipped(test_or_suite, fmt, ...) \
  502. do { \
  503. WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
  504. scnprintf((test_or_suite)->status_comment, \
  505. KUNIT_STATUS_COMMENT_SIZE, \
  506. fmt, ##__VA_ARGS__); \
  507. } while (0)
  508. /**
  509. * kunit_skip() - Marks @test_or_suite as skipped
  510. *
  511. * @test_or_suite: The test context object.
  512. * @fmt: A printk() style format string.
  513. *
  514. * Skips the test. @fmt is given output as the test status
  515. * comment, typically the reason the test was skipped.
  516. *
  517. * Test execution is halted after kunit_skip() is called.
  518. */
  519. #define kunit_skip(test_or_suite, fmt, ...) \
  520. do { \
  521. kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
  522. kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
  523. } while (0)
  524. /*
  525. * printk and log to per-test or per-suite log buffer. Logging only done
  526. * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
  527. */
  528. #define kunit_log(lvl, test_or_suite, fmt, ...) \
  529. do { \
  530. printk(lvl fmt, ##__VA_ARGS__); \
  531. kunit_log_append((test_or_suite)->log, fmt, \
  532. ##__VA_ARGS__); \
  533. } while (0)
  534. #define kunit_printk(lvl, test, fmt, ...) \
  535. kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
  536. (test)->name, ##__VA_ARGS__)
  537. /**
  538. * kunit_info() - Prints an INFO level message associated with @test.
  539. *
  540. * @test: The test context object.
  541. * @fmt: A printk() style format string.
  542. *
  543. * Prints an info level message associated with the test suite being run.
  544. * Takes a variable number of format parameters just like printk().
  545. */
  546. #define kunit_info(test, fmt, ...) \
  547. kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
  548. /**
  549. * kunit_warn() - Prints a WARN level message associated with @test.
  550. *
  551. * @test: The test context object.
  552. * @fmt: A printk() style format string.
  553. *
  554. * Prints a warning level message.
  555. */
  556. #define kunit_warn(test, fmt, ...) \
  557. kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
  558. /**
  559. * kunit_err() - Prints an ERROR level message associated with @test.
  560. *
  561. * @test: The test context object.
  562. * @fmt: A printk() style format string.
  563. *
  564. * Prints an error level message.
  565. */
  566. #define kunit_err(test, fmt, ...) \
  567. kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
  568. /*
  569. * Must be called at the beginning of each KUNIT_*_ASSERTION().
  570. * Cf. KUNIT_CURRENT_LOC.
  571. */
  572. #define _KUNIT_SAVE_LOC(test) do { \
  573. WRITE_ONCE(test->last_seen.file, __FILE__); \
  574. WRITE_ONCE(test->last_seen.line, __LINE__); \
  575. } while (0)
  576. /**
  577. * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
  578. * @test: The test context object.
  579. *
  580. * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
  581. * words, it does nothing and only exists for code clarity. See
  582. * KUNIT_EXPECT_TRUE() for more information.
  583. */
  584. #define KUNIT_SUCCEED(test) _KUNIT_SAVE_LOC(test)
  585. void __noreturn __kunit_abort(struct kunit *test);
  586. void __printf(6, 7) __kunit_do_failed_assertion(struct kunit *test,
  587. const struct kunit_loc *loc,
  588. enum kunit_assert_type type,
  589. const struct kunit_assert *assert,
  590. assert_format_t assert_format,
  591. const char *fmt, ...);
  592. #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INITIALIZER, fmt, ...) do { \
  593. static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
  594. const struct assert_class __assertion = INITIALIZER; \
  595. __kunit_do_failed_assertion(test, \
  596. &__loc, \
  597. assert_type, \
  598. &__assertion.assert, \
  599. assert_format, \
  600. fmt, \
  601. ##__VA_ARGS__); \
  602. if (assert_type == KUNIT_ASSERTION) \
  603. __kunit_abort(test); \
  604. } while (0)
  605. #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) do { \
  606. _KUNIT_SAVE_LOC(test); \
  607. _KUNIT_FAILED(test, \
  608. assert_type, \
  609. kunit_fail_assert, \
  610. kunit_fail_assert_format, \
  611. {}, \
  612. fmt, \
  613. ##__VA_ARGS__); \
  614. } while (0)
  615. /**
  616. * KUNIT_FAIL() - Always causes a test to fail when evaluated.
  617. * @test: The test context object.
  618. * @fmt: an informational message to be printed when the assertion is made.
  619. * @...: string format arguments.
  620. *
  621. * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
  622. * other words, it always results in a failed expectation, and consequently
  623. * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
  624. * for more information.
  625. */
  626. #define KUNIT_FAIL(test, fmt, ...) \
  627. KUNIT_FAIL_ASSERTION(test, \
  628. KUNIT_EXPECTATION, \
  629. fmt, \
  630. ##__VA_ARGS__)
  631. /* Helper to safely pass around an initializer list to other macros. */
  632. #define KUNIT_INIT_ASSERT(initializers...) { initializers }
  633. #define KUNIT_UNARY_ASSERTION(test, \
  634. assert_type, \
  635. condition_, \
  636. expected_true_, \
  637. fmt, \
  638. ...) \
  639. do { \
  640. _KUNIT_SAVE_LOC(test); \
  641. if (likely(!!(condition_) == !!expected_true_)) \
  642. break; \
  643. \
  644. _KUNIT_FAILED(test, \
  645. assert_type, \
  646. kunit_unary_assert, \
  647. kunit_unary_assert_format, \
  648. KUNIT_INIT_ASSERT(.condition = #condition_, \
  649. .expected_true = expected_true_), \
  650. fmt, \
  651. ##__VA_ARGS__); \
  652. } while (0)
  653. #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
  654. KUNIT_UNARY_ASSERTION(test, \
  655. assert_type, \
  656. condition, \
  657. true, \
  658. fmt, \
  659. ##__VA_ARGS__)
  660. #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
  661. KUNIT_UNARY_ASSERTION(test, \
  662. assert_type, \
  663. condition, \
  664. false, \
  665. fmt, \
  666. ##__VA_ARGS__)
  667. /*
  668. * A factory macro for defining the assertions and expectations for the basic
  669. * comparisons defined for the built in types.
  670. *
  671. * Unfortunately, there is no common type that all types can be promoted to for
  672. * which all the binary operators behave the same way as for the actual types
  673. * (for example, there is no type that long long and unsigned long long can
  674. * both be cast to where the comparison result is preserved for all values). So
  675. * the best we can do is do the comparison in the original types and then coerce
  676. * everything to long long for printing; this way, the comparison behaves
  677. * correctly and the printed out value usually makes sense without
  678. * interpretation, but can always be interpreted to figure out the actual
  679. * value.
  680. */
  681. #define KUNIT_BASE_BINARY_ASSERTION(test, \
  682. assert_class, \
  683. format_func, \
  684. assert_type, \
  685. left, \
  686. op, \
  687. right, \
  688. fmt, \
  689. ...) \
  690. do { \
  691. const typeof(left) __left = (left); \
  692. const typeof(right) __right = (right); \
  693. static const struct kunit_binary_assert_text __text = { \
  694. .operation = #op, \
  695. .left_text = #left, \
  696. .right_text = #right, \
  697. }; \
  698. \
  699. _KUNIT_SAVE_LOC(test); \
  700. if (likely(__left op __right)) \
  701. break; \
  702. \
  703. _KUNIT_FAILED(test, \
  704. assert_type, \
  705. assert_class, \
  706. format_func, \
  707. KUNIT_INIT_ASSERT(.text = &__text, \
  708. .left_value = __left, \
  709. .right_value = __right), \
  710. fmt, \
  711. ##__VA_ARGS__); \
  712. } while (0)
  713. #define KUNIT_BINARY_INT_ASSERTION(test, \
  714. assert_type, \
  715. left, \
  716. op, \
  717. right, \
  718. fmt, \
  719. ...) \
  720. KUNIT_BASE_BINARY_ASSERTION(test, \
  721. kunit_binary_assert, \
  722. kunit_binary_assert_format, \
  723. assert_type, \
  724. left, op, right, \
  725. fmt, \
  726. ##__VA_ARGS__)
  727. #define KUNIT_BINARY_PTR_ASSERTION(test, \
  728. assert_type, \
  729. left, \
  730. op, \
  731. right, \
  732. fmt, \
  733. ...) \
  734. KUNIT_BASE_BINARY_ASSERTION(test, \
  735. kunit_binary_ptr_assert, \
  736. kunit_binary_ptr_assert_format, \
  737. assert_type, \
  738. left, op, right, \
  739. fmt, \
  740. ##__VA_ARGS__)
  741. #define KUNIT_BINARY_STR_ASSERTION(test, \
  742. assert_type, \
  743. left, \
  744. op, \
  745. right, \
  746. fmt, \
  747. ...) \
  748. do { \
  749. const char *__left = (left); \
  750. const char *__right = (right); \
  751. static const struct kunit_binary_assert_text __text = { \
  752. .operation = #op, \
  753. .left_text = #left, \
  754. .right_text = #right, \
  755. }; \
  756. \
  757. _KUNIT_SAVE_LOC(test); \
  758. if (likely((__left) && (__right) && (strcmp(__left, __right) op 0))) \
  759. break; \
  760. \
  761. \
  762. _KUNIT_FAILED(test, \
  763. assert_type, \
  764. kunit_binary_str_assert, \
  765. kunit_binary_str_assert_format, \
  766. KUNIT_INIT_ASSERT(.text = &__text, \
  767. .left_value = __left, \
  768. .right_value = __right), \
  769. fmt, \
  770. ##__VA_ARGS__); \
  771. } while (0)
  772. #define KUNIT_MEM_ASSERTION(test, \
  773. assert_type, \
  774. left, \
  775. op, \
  776. right, \
  777. size_, \
  778. fmt, \
  779. ...) \
  780. do { \
  781. const void *__left = (left); \
  782. const void *__right = (right); \
  783. const size_t __size = (size_); \
  784. static const struct kunit_binary_assert_text __text = { \
  785. .operation = #op, \
  786. .left_text = #left, \
  787. .right_text = #right, \
  788. }; \
  789. \
  790. _KUNIT_SAVE_LOC(test); \
  791. if (likely(__left && __right)) \
  792. if (likely(memcmp(__left, __right, __size) op 0)) \
  793. break; \
  794. \
  795. _KUNIT_FAILED(test, \
  796. assert_type, \
  797. kunit_mem_assert, \
  798. kunit_mem_assert_format, \
  799. KUNIT_INIT_ASSERT(.text = &__text, \
  800. .left_value = __left, \
  801. .right_value = __right, \
  802. .size = __size), \
  803. fmt, \
  804. ##__VA_ARGS__); \
  805. } while (0)
  806. #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
  807. assert_type, \
  808. ptr, \
  809. fmt, \
  810. ...) \
  811. do { \
  812. const typeof(ptr) __ptr = (ptr); \
  813. \
  814. _KUNIT_SAVE_LOC(test); \
  815. if (!IS_ERR_OR_NULL(__ptr)) \
  816. break; \
  817. \
  818. _KUNIT_FAILED(test, \
  819. assert_type, \
  820. kunit_ptr_not_err_assert, \
  821. kunit_ptr_not_err_assert_format, \
  822. KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr), \
  823. fmt, \
  824. ##__VA_ARGS__); \
  825. } while (0)
  826. /**
  827. * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
  828. * @test: The test context object.
  829. * @condition: an arbitrary boolean expression. The test fails when this does
  830. * not evaluate to true.
  831. *
  832. * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
  833. * to fail when the specified condition is not met; however, it will not prevent
  834. * the test case from continuing to run; this is otherwise known as an
  835. * *expectation failure*.
  836. */
  837. #define KUNIT_EXPECT_TRUE(test, condition) \
  838. KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
  839. #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
  840. KUNIT_TRUE_MSG_ASSERTION(test, \
  841. KUNIT_EXPECTATION, \
  842. condition, \
  843. fmt, \
  844. ##__VA_ARGS__)
  845. /**
  846. * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
  847. * @test: The test context object.
  848. * @condition: an arbitrary boolean expression. The test fails when this does
  849. * not evaluate to false.
  850. *
  851. * Sets an expectation that @condition evaluates to false. See
  852. * KUNIT_EXPECT_TRUE() for more information.
  853. */
  854. #define KUNIT_EXPECT_FALSE(test, condition) \
  855. KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
  856. #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
  857. KUNIT_FALSE_MSG_ASSERTION(test, \
  858. KUNIT_EXPECTATION, \
  859. condition, \
  860. fmt, \
  861. ##__VA_ARGS__)
  862. /**
  863. * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
  864. * @test: The test context object.
  865. * @left: an arbitrary expression that evaluates to a primitive C type.
  866. * @right: an arbitrary expression that evaluates to a primitive C type.
  867. *
  868. * Sets an expectation that the values that @left and @right evaluate to are
  869. * equal. This is semantically equivalent to
  870. * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
  871. * more information.
  872. */
  873. #define KUNIT_EXPECT_EQ(test, left, right) \
  874. KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
  875. #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
  876. KUNIT_BINARY_INT_ASSERTION(test, \
  877. KUNIT_EXPECTATION, \
  878. left, ==, right, \
  879. fmt, \
  880. ##__VA_ARGS__)
  881. /**
  882. * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
  883. * @test: The test context object.
  884. * @left: an arbitrary expression that evaluates to a pointer.
  885. * @right: an arbitrary expression that evaluates to a pointer.
  886. *
  887. * Sets an expectation that the values that @left and @right evaluate to are
  888. * equal. This is semantically equivalent to
  889. * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
  890. * more information.
  891. */
  892. #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
  893. KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
  894. #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
  895. KUNIT_BINARY_PTR_ASSERTION(test, \
  896. KUNIT_EXPECTATION, \
  897. left, ==, right, \
  898. fmt, \
  899. ##__VA_ARGS__)
  900. /**
  901. * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
  902. * @test: The test context object.
  903. * @left: an arbitrary expression that evaluates to a primitive C type.
  904. * @right: an arbitrary expression that evaluates to a primitive C type.
  905. *
  906. * Sets an expectation that the values that @left and @right evaluate to are not
  907. * equal. This is semantically equivalent to
  908. * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
  909. * more information.
  910. */
  911. #define KUNIT_EXPECT_NE(test, left, right) \
  912. KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
  913. #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
  914. KUNIT_BINARY_INT_ASSERTION(test, \
  915. KUNIT_EXPECTATION, \
  916. left, !=, right, \
  917. fmt, \
  918. ##__VA_ARGS__)
  919. /**
  920. * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
  921. * @test: The test context object.
  922. * @left: an arbitrary expression that evaluates to a pointer.
  923. * @right: an arbitrary expression that evaluates to a pointer.
  924. *
  925. * Sets an expectation that the values that @left and @right evaluate to are not
  926. * equal. This is semantically equivalent to
  927. * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
  928. * more information.
  929. */
  930. #define KUNIT_EXPECT_PTR_NE(test, left, right) \
  931. KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
  932. #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
  933. KUNIT_BINARY_PTR_ASSERTION(test, \
  934. KUNIT_EXPECTATION, \
  935. left, !=, right, \
  936. fmt, \
  937. ##__VA_ARGS__)
  938. /**
  939. * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
  940. * @test: The test context object.
  941. * @left: an arbitrary expression that evaluates to a primitive C type.
  942. * @right: an arbitrary expression that evaluates to a primitive C type.
  943. *
  944. * Sets an expectation that the value that @left evaluates to is less than the
  945. * value that @right evaluates to. This is semantically equivalent to
  946. * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
  947. * more information.
  948. */
  949. #define KUNIT_EXPECT_LT(test, left, right) \
  950. KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
  951. #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
  952. KUNIT_BINARY_INT_ASSERTION(test, \
  953. KUNIT_EXPECTATION, \
  954. left, <, right, \
  955. fmt, \
  956. ##__VA_ARGS__)
  957. /**
  958. * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
  959. * @test: The test context object.
  960. * @left: an arbitrary expression that evaluates to a primitive C type.
  961. * @right: an arbitrary expression that evaluates to a primitive C type.
  962. *
  963. * Sets an expectation that the value that @left evaluates to is less than or
  964. * equal to the value that @right evaluates to. Semantically this is equivalent
  965. * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
  966. * more information.
  967. */
  968. #define KUNIT_EXPECT_LE(test, left, right) \
  969. KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
  970. #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
  971. KUNIT_BINARY_INT_ASSERTION(test, \
  972. KUNIT_EXPECTATION, \
  973. left, <=, right, \
  974. fmt, \
  975. ##__VA_ARGS__)
  976. /**
  977. * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
  978. * @test: The test context object.
  979. * @left: an arbitrary expression that evaluates to a primitive C type.
  980. * @right: an arbitrary expression that evaluates to a primitive C type.
  981. *
  982. * Sets an expectation that the value that @left evaluates to is greater than
  983. * the value that @right evaluates to. This is semantically equivalent to
  984. * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
  985. * more information.
  986. */
  987. #define KUNIT_EXPECT_GT(test, left, right) \
  988. KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
  989. #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
  990. KUNIT_BINARY_INT_ASSERTION(test, \
  991. KUNIT_EXPECTATION, \
  992. left, >, right, \
  993. fmt, \
  994. ##__VA_ARGS__)
  995. /**
  996. * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
  997. * @test: The test context object.
  998. * @left: an arbitrary expression that evaluates to a primitive C type.
  999. * @right: an arbitrary expression that evaluates to a primitive C type.
  1000. *
  1001. * Sets an expectation that the value that @left evaluates to is greater than
  1002. * the value that @right evaluates to. This is semantically equivalent to
  1003. * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
  1004. * more information.
  1005. */
  1006. #define KUNIT_EXPECT_GE(test, left, right) \
  1007. KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
  1008. #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
  1009. KUNIT_BINARY_INT_ASSERTION(test, \
  1010. KUNIT_EXPECTATION, \
  1011. left, >=, right, \
  1012. fmt, \
  1013. ##__VA_ARGS__)
  1014. /**
  1015. * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
  1016. * @test: The test context object.
  1017. * @left: an arbitrary expression that evaluates to a null terminated string.
  1018. * @right: an arbitrary expression that evaluates to a null terminated string.
  1019. *
  1020. * Sets an expectation that the values that @left and @right evaluate to are
  1021. * equal. This is semantically equivalent to
  1022. * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
  1023. * for more information.
  1024. */
  1025. #define KUNIT_EXPECT_STREQ(test, left, right) \
  1026. KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
  1027. #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
  1028. KUNIT_BINARY_STR_ASSERTION(test, \
  1029. KUNIT_EXPECTATION, \
  1030. left, ==, right, \
  1031. fmt, \
  1032. ##__VA_ARGS__)
  1033. /**
  1034. * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
  1035. * @test: The test context object.
  1036. * @left: an arbitrary expression that evaluates to a null terminated string.
  1037. * @right: an arbitrary expression that evaluates to a null terminated string.
  1038. *
  1039. * Sets an expectation that the values that @left and @right evaluate to are
  1040. * not equal. This is semantically equivalent to
  1041. * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
  1042. * for more information.
  1043. */
  1044. #define KUNIT_EXPECT_STRNEQ(test, left, right) \
  1045. KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
  1046. #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
  1047. KUNIT_BINARY_STR_ASSERTION(test, \
  1048. KUNIT_EXPECTATION, \
  1049. left, !=, right, \
  1050. fmt, \
  1051. ##__VA_ARGS__)
  1052. /**
  1053. * KUNIT_EXPECT_MEMEQ() - Expects that the first @size bytes of @left and @right are equal.
  1054. * @test: The test context object.
  1055. * @left: An arbitrary expression that evaluates to the specified size.
  1056. * @right: An arbitrary expression that evaluates to the specified size.
  1057. * @size: Number of bytes compared.
  1058. *
  1059. * Sets an expectation that the values that @left and @right evaluate to are
  1060. * equal. This is semantically equivalent to
  1061. * KUNIT_EXPECT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
  1062. * KUNIT_EXPECT_TRUE() for more information.
  1063. *
  1064. * Although this expectation works for any memory block, it is not recommended
  1065. * for comparing more structured data, such as structs. This expectation is
  1066. * recommended for comparing, for example, data arrays.
  1067. */
  1068. #define KUNIT_EXPECT_MEMEQ(test, left, right, size) \
  1069. KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, NULL)
  1070. #define KUNIT_EXPECT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
  1071. KUNIT_MEM_ASSERTION(test, \
  1072. KUNIT_EXPECTATION, \
  1073. left, ==, right, \
  1074. size, \
  1075. fmt, \
  1076. ##__VA_ARGS__)
  1077. /**
  1078. * KUNIT_EXPECT_MEMNEQ() - Expects that the first @size bytes of @left and @right are not equal.
  1079. * @test: The test context object.
  1080. * @left: An arbitrary expression that evaluates to the specified size.
  1081. * @right: An arbitrary expression that evaluates to the specified size.
  1082. * @size: Number of bytes compared.
  1083. *
  1084. * Sets an expectation that the values that @left and @right evaluate to are
  1085. * not equal. This is semantically equivalent to
  1086. * KUNIT_EXPECT_TRUE(@test, memcmp((@left), (@right), (@size))). See
  1087. * KUNIT_EXPECT_TRUE() for more information.
  1088. *
  1089. * Although this expectation works for any memory block, it is not recommended
  1090. * for comparing more structured data, such as structs. This expectation is
  1091. * recommended for comparing, for example, data arrays.
  1092. */
  1093. #define KUNIT_EXPECT_MEMNEQ(test, left, right, size) \
  1094. KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, NULL)
  1095. #define KUNIT_EXPECT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
  1096. KUNIT_MEM_ASSERTION(test, \
  1097. KUNIT_EXPECTATION, \
  1098. left, !=, right, \
  1099. size, \
  1100. fmt, \
  1101. ##__VA_ARGS__)
  1102. /**
  1103. * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
  1104. * @test: The test context object.
  1105. * @ptr: an arbitrary pointer.
  1106. *
  1107. * Sets an expectation that the value that @ptr evaluates to is null. This is
  1108. * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
  1109. * See KUNIT_EXPECT_TRUE() for more information.
  1110. */
  1111. #define KUNIT_EXPECT_NULL(test, ptr) \
  1112. KUNIT_EXPECT_NULL_MSG(test, \
  1113. ptr, \
  1114. NULL)
  1115. #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...) \
  1116. KUNIT_BINARY_PTR_ASSERTION(test, \
  1117. KUNIT_EXPECTATION, \
  1118. ptr, ==, NULL, \
  1119. fmt, \
  1120. ##__VA_ARGS__)
  1121. /**
  1122. * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
  1123. * @test: The test context object.
  1124. * @ptr: an arbitrary pointer.
  1125. *
  1126. * Sets an expectation that the value that @ptr evaluates to is not null. This
  1127. * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
  1128. * See KUNIT_EXPECT_TRUE() for more information.
  1129. */
  1130. #define KUNIT_EXPECT_NOT_NULL(test, ptr) \
  1131. KUNIT_EXPECT_NOT_NULL_MSG(test, \
  1132. ptr, \
  1133. NULL)
  1134. #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...) \
  1135. KUNIT_BINARY_PTR_ASSERTION(test, \
  1136. KUNIT_EXPECTATION, \
  1137. ptr, !=, NULL, \
  1138. fmt, \
  1139. ##__VA_ARGS__)
  1140. /**
  1141. * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
  1142. * @test: The test context object.
  1143. * @ptr: an arbitrary pointer.
  1144. *
  1145. * Sets an expectation that the value that @ptr evaluates to is not null and not
  1146. * an errno stored in a pointer. This is semantically equivalent to
  1147. * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
  1148. * more information.
  1149. */
  1150. #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
  1151. KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
  1152. #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
  1153. KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
  1154. KUNIT_EXPECTATION, \
  1155. ptr, \
  1156. fmt, \
  1157. ##__VA_ARGS__)
  1158. /**
  1159. * KUNIT_FAIL_AND_ABORT() - Always causes a test to fail and abort when evaluated.
  1160. * @test: The test context object.
  1161. * @fmt: an informational message to be printed when the assertion is made.
  1162. * @...: string format arguments.
  1163. *
  1164. * The opposite of KUNIT_SUCCEED(), it is an assertion that always fails. In
  1165. * other words, it always results in a failed assertion, and consequently
  1166. * always causes the test case to fail and abort when evaluated.
  1167. * See KUNIT_ASSERT_TRUE() for more information.
  1168. */
  1169. #define KUNIT_FAIL_AND_ABORT(test, fmt, ...) \
  1170. KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
  1171. /**
  1172. * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
  1173. * @test: The test context object.
  1174. * @condition: an arbitrary boolean expression. The test fails and aborts when
  1175. * this does not evaluate to true.
  1176. *
  1177. * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
  1178. * fail *and immediately abort* when the specified condition is not met. Unlike
  1179. * an expectation failure, it will prevent the test case from continuing to run;
  1180. * this is otherwise known as an *assertion failure*.
  1181. */
  1182. #define KUNIT_ASSERT_TRUE(test, condition) \
  1183. KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
  1184. #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
  1185. KUNIT_TRUE_MSG_ASSERTION(test, \
  1186. KUNIT_ASSERTION, \
  1187. condition, \
  1188. fmt, \
  1189. ##__VA_ARGS__)
  1190. /**
  1191. * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
  1192. * @test: The test context object.
  1193. * @condition: an arbitrary boolean expression.
  1194. *
  1195. * Sets an assertion that the value that @condition evaluates to is false. This
  1196. * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
  1197. * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1198. */
  1199. #define KUNIT_ASSERT_FALSE(test, condition) \
  1200. KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
  1201. #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
  1202. KUNIT_FALSE_MSG_ASSERTION(test, \
  1203. KUNIT_ASSERTION, \
  1204. condition, \
  1205. fmt, \
  1206. ##__VA_ARGS__)
  1207. /**
  1208. * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
  1209. * @test: The test context object.
  1210. * @left: an arbitrary expression that evaluates to a primitive C type.
  1211. * @right: an arbitrary expression that evaluates to a primitive C type.
  1212. *
  1213. * Sets an assertion that the values that @left and @right evaluate to are
  1214. * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
  1215. * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1216. */
  1217. #define KUNIT_ASSERT_EQ(test, left, right) \
  1218. KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
  1219. #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
  1220. KUNIT_BINARY_INT_ASSERTION(test, \
  1221. KUNIT_ASSERTION, \
  1222. left, ==, right, \
  1223. fmt, \
  1224. ##__VA_ARGS__)
  1225. /**
  1226. * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
  1227. * @test: The test context object.
  1228. * @left: an arbitrary expression that evaluates to a pointer.
  1229. * @right: an arbitrary expression that evaluates to a pointer.
  1230. *
  1231. * Sets an assertion that the values that @left and @right evaluate to are
  1232. * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
  1233. * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1234. */
  1235. #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
  1236. KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
  1237. #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
  1238. KUNIT_BINARY_PTR_ASSERTION(test, \
  1239. KUNIT_ASSERTION, \
  1240. left, ==, right, \
  1241. fmt, \
  1242. ##__VA_ARGS__)
  1243. /**
  1244. * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
  1245. * @test: The test context object.
  1246. * @left: an arbitrary expression that evaluates to a primitive C type.
  1247. * @right: an arbitrary expression that evaluates to a primitive C type.
  1248. *
  1249. * Sets an assertion that the values that @left and @right evaluate to are not
  1250. * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
  1251. * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1252. */
  1253. #define KUNIT_ASSERT_NE(test, left, right) \
  1254. KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
  1255. #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
  1256. KUNIT_BINARY_INT_ASSERTION(test, \
  1257. KUNIT_ASSERTION, \
  1258. left, !=, right, \
  1259. fmt, \
  1260. ##__VA_ARGS__)
  1261. /**
  1262. * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
  1263. * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
  1264. * @test: The test context object.
  1265. * @left: an arbitrary expression that evaluates to a pointer.
  1266. * @right: an arbitrary expression that evaluates to a pointer.
  1267. *
  1268. * Sets an assertion that the values that @left and @right evaluate to are not
  1269. * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
  1270. * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1271. */
  1272. #define KUNIT_ASSERT_PTR_NE(test, left, right) \
  1273. KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
  1274. #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
  1275. KUNIT_BINARY_PTR_ASSERTION(test, \
  1276. KUNIT_ASSERTION, \
  1277. left, !=, right, \
  1278. fmt, \
  1279. ##__VA_ARGS__)
  1280. /**
  1281. * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
  1282. * @test: The test context object.
  1283. * @left: an arbitrary expression that evaluates to a primitive C type.
  1284. * @right: an arbitrary expression that evaluates to a primitive C type.
  1285. *
  1286. * Sets an assertion that the value that @left evaluates to is less than the
  1287. * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
  1288. * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
  1289. * is not met.
  1290. */
  1291. #define KUNIT_ASSERT_LT(test, left, right) \
  1292. KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
  1293. #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
  1294. KUNIT_BINARY_INT_ASSERTION(test, \
  1295. KUNIT_ASSERTION, \
  1296. left, <, right, \
  1297. fmt, \
  1298. ##__VA_ARGS__)
  1299. /**
  1300. * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
  1301. * @test: The test context object.
  1302. * @left: an arbitrary expression that evaluates to a primitive C type.
  1303. * @right: an arbitrary expression that evaluates to a primitive C type.
  1304. *
  1305. * Sets an assertion that the value that @left evaluates to is less than or
  1306. * equal to the value that @right evaluates to. This is the same as
  1307. * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
  1308. * KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1309. */
  1310. #define KUNIT_ASSERT_LE(test, left, right) \
  1311. KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
  1312. #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
  1313. KUNIT_BINARY_INT_ASSERTION(test, \
  1314. KUNIT_ASSERTION, \
  1315. left, <=, right, \
  1316. fmt, \
  1317. ##__VA_ARGS__)
  1318. /**
  1319. * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
  1320. * @test: The test context object.
  1321. * @left: an arbitrary expression that evaluates to a primitive C type.
  1322. * @right: an arbitrary expression that evaluates to a primitive C type.
  1323. *
  1324. * Sets an assertion that the value that @left evaluates to is greater than the
  1325. * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
  1326. * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
  1327. * is not met.
  1328. */
  1329. #define KUNIT_ASSERT_GT(test, left, right) \
  1330. KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
  1331. #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
  1332. KUNIT_BINARY_INT_ASSERTION(test, \
  1333. KUNIT_ASSERTION, \
  1334. left, >, right, \
  1335. fmt, \
  1336. ##__VA_ARGS__)
  1337. /**
  1338. * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
  1339. * @test: The test context object.
  1340. * @left: an arbitrary expression that evaluates to a primitive C type.
  1341. * @right: an arbitrary expression that evaluates to a primitive C type.
  1342. *
  1343. * Sets an assertion that the value that @left evaluates to is greater than the
  1344. * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
  1345. * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
  1346. * is not met.
  1347. */
  1348. #define KUNIT_ASSERT_GE(test, left, right) \
  1349. KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
  1350. #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
  1351. KUNIT_BINARY_INT_ASSERTION(test, \
  1352. KUNIT_ASSERTION, \
  1353. left, >=, right, \
  1354. fmt, \
  1355. ##__VA_ARGS__)
  1356. /**
  1357. * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
  1358. * @test: The test context object.
  1359. * @left: an arbitrary expression that evaluates to a null terminated string.
  1360. * @right: an arbitrary expression that evaluates to a null terminated string.
  1361. *
  1362. * Sets an assertion that the values that @left and @right evaluate to are
  1363. * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
  1364. * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1365. */
  1366. #define KUNIT_ASSERT_STREQ(test, left, right) \
  1367. KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
  1368. #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
  1369. KUNIT_BINARY_STR_ASSERTION(test, \
  1370. KUNIT_ASSERTION, \
  1371. left, ==, right, \
  1372. fmt, \
  1373. ##__VA_ARGS__)
  1374. /**
  1375. * KUNIT_ASSERT_STRNEQ() - An assertion that strings @left and @right are not equal.
  1376. * @test: The test context object.
  1377. * @left: an arbitrary expression that evaluates to a null terminated string.
  1378. * @right: an arbitrary expression that evaluates to a null terminated string.
  1379. *
  1380. * Sets an assertion that the values that @left and @right evaluate to are
  1381. * not equal. This is semantically equivalent to
  1382. * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
  1383. * for more information.
  1384. */
  1385. #define KUNIT_ASSERT_STRNEQ(test, left, right) \
  1386. KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
  1387. #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
  1388. KUNIT_BINARY_STR_ASSERTION(test, \
  1389. KUNIT_ASSERTION, \
  1390. left, !=, right, \
  1391. fmt, \
  1392. ##__VA_ARGS__)
  1393. /**
  1394. * KUNIT_ASSERT_MEMEQ() - Asserts that the first @size bytes of @left and @right are equal.
  1395. * @test: The test context object.
  1396. * @left: An arbitrary expression that evaluates to the specified size.
  1397. * @right: An arbitrary expression that evaluates to the specified size.
  1398. * @size: Number of bytes compared.
  1399. *
  1400. * Sets an assertion that the values that @left and @right evaluate to are
  1401. * equal. This is semantically equivalent to
  1402. * KUNIT_ASSERT_TRUE(@test, !memcmp((@left), (@right), (@size))). See
  1403. * KUNIT_ASSERT_TRUE() for more information.
  1404. *
  1405. * Although this assertion works for any memory block, it is not recommended
  1406. * for comparing more structured data, such as structs. This assertion is
  1407. * recommended for comparing, for example, data arrays.
  1408. */
  1409. #define KUNIT_ASSERT_MEMEQ(test, left, right, size) \
  1410. KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, NULL)
  1411. #define KUNIT_ASSERT_MEMEQ_MSG(test, left, right, size, fmt, ...) \
  1412. KUNIT_MEM_ASSERTION(test, \
  1413. KUNIT_ASSERTION, \
  1414. left, ==, right, \
  1415. size, \
  1416. fmt, \
  1417. ##__VA_ARGS__)
  1418. /**
  1419. * KUNIT_ASSERT_MEMNEQ() - Asserts that the first @size bytes of @left and @right are not equal.
  1420. * @test: The test context object.
  1421. * @left: An arbitrary expression that evaluates to the specified size.
  1422. * @right: An arbitrary expression that evaluates to the specified size.
  1423. * @size: Number of bytes compared.
  1424. *
  1425. * Sets an assertion that the values that @left and @right evaluate to are
  1426. * not equal. This is semantically equivalent to
  1427. * KUNIT_ASSERT_TRUE(@test, memcmp((@left), (@right), (@size))). See
  1428. * KUNIT_ASSERT_TRUE() for more information.
  1429. *
  1430. * Although this assertion works for any memory block, it is not recommended
  1431. * for comparing more structured data, such as structs. This assertion is
  1432. * recommended for comparing, for example, data arrays.
  1433. */
  1434. #define KUNIT_ASSERT_MEMNEQ(test, left, right, size) \
  1435. KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, NULL)
  1436. #define KUNIT_ASSERT_MEMNEQ_MSG(test, left, right, size, fmt, ...) \
  1437. KUNIT_MEM_ASSERTION(test, \
  1438. KUNIT_ASSERTION, \
  1439. left, !=, right, \
  1440. size, \
  1441. fmt, \
  1442. ##__VA_ARGS__)
  1443. /**
  1444. * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
  1445. * @test: The test context object.
  1446. * @ptr: an arbitrary pointer.
  1447. *
  1448. * Sets an assertion that the values that @ptr evaluates to is null. This is
  1449. * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
  1450. * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1451. */
  1452. #define KUNIT_ASSERT_NULL(test, ptr) \
  1453. KUNIT_ASSERT_NULL_MSG(test, \
  1454. ptr, \
  1455. NULL)
  1456. #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
  1457. KUNIT_BINARY_PTR_ASSERTION(test, \
  1458. KUNIT_ASSERTION, \
  1459. ptr, ==, NULL, \
  1460. fmt, \
  1461. ##__VA_ARGS__)
  1462. /**
  1463. * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
  1464. * @test: The test context object.
  1465. * @ptr: an arbitrary pointer.
  1466. *
  1467. * Sets an assertion that the values that @ptr evaluates to is not null. This
  1468. * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
  1469. * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1470. */
  1471. #define KUNIT_ASSERT_NOT_NULL(test, ptr) \
  1472. KUNIT_ASSERT_NOT_NULL_MSG(test, \
  1473. ptr, \
  1474. NULL)
  1475. #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
  1476. KUNIT_BINARY_PTR_ASSERTION(test, \
  1477. KUNIT_ASSERTION, \
  1478. ptr, !=, NULL, \
  1479. fmt, \
  1480. ##__VA_ARGS__)
  1481. /**
  1482. * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
  1483. * @test: The test context object.
  1484. * @ptr: an arbitrary pointer.
  1485. *
  1486. * Sets an assertion that the value that @ptr evaluates to is not null and not
  1487. * an errno stored in a pointer. This is the same as
  1488. * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
  1489. * KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1490. */
  1491. #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
  1492. KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
  1493. #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
  1494. KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
  1495. KUNIT_ASSERTION, \
  1496. ptr, \
  1497. fmt, \
  1498. ##__VA_ARGS__)
  1499. /**
  1500. * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
  1501. * @name: prefix for the test parameter generator function.
  1502. * @array: array of test parameters.
  1503. * @get_desc: function to convert param to description; NULL to use default
  1504. *
  1505. * Define function @name_gen_params which uses @array to generate parameters.
  1506. */
  1507. #define KUNIT_ARRAY_PARAM(name, array, get_desc) \
  1508. static const void *name##_gen_params(const void *prev, char *desc) \
  1509. { \
  1510. typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
  1511. if (__next - (array) < ARRAY_SIZE((array))) { \
  1512. void (*__get_desc)(typeof(__next), char *) = get_desc; \
  1513. if (__get_desc) \
  1514. __get_desc(__next, desc); \
  1515. return __next; \
  1516. } \
  1517. return NULL; \
  1518. }
  1519. /**
  1520. * KUNIT_ARRAY_PARAM_DESC() - Define test parameter generator from an array.
  1521. * @name: prefix for the test parameter generator function.
  1522. * @array: array of test parameters.
  1523. * @desc_member: structure member from array element to use as description
  1524. *
  1525. * Define function @name_gen_params which uses @array to generate parameters.
  1526. */
  1527. #define KUNIT_ARRAY_PARAM_DESC(name, array, desc_member) \
  1528. static const void *name##_gen_params(const void *prev, char *desc) \
  1529. { \
  1530. typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
  1531. if (__next - (array) < ARRAY_SIZE((array))) { \
  1532. strscpy(desc, __next->desc_member, KUNIT_PARAM_DESC_SIZE); \
  1533. return __next; \
  1534. } \
  1535. return NULL; \
  1536. }
  1537. // TODO(dlatypov@google.com): consider eventually migrating users to explicitly
  1538. // include resource.h themselves if they need it.
  1539. #include <kunit/resource.h>
  1540. #endif /* _KUNIT_TEST_H */