test.h 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745
  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/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/types.h>
  16. #include <linux/kref.h>
  17. struct kunit_resource;
  18. typedef int (*kunit_resource_init_t)(struct kunit_resource *, void *);
  19. typedef void (*kunit_resource_free_t)(struct kunit_resource *);
  20. /**
  21. * struct kunit_resource - represents a *test managed resource*
  22. * @data: for the user to store arbitrary data.
  23. * @name: optional name
  24. * @free: a user supplied function to free the resource. Populated by
  25. * kunit_resource_alloc().
  26. *
  27. * Represents a *test managed resource*, a resource which will automatically be
  28. * cleaned up at the end of a test case.
  29. *
  30. * Resources are reference counted so if a resource is retrieved via
  31. * kunit_alloc_and_get_resource() or kunit_find_resource(), we need
  32. * to call kunit_put_resource() to reduce the resource reference count
  33. * when finished with it. Note that kunit_alloc_resource() does not require a
  34. * kunit_resource_put() because it does not retrieve the resource itself.
  35. *
  36. * Example:
  37. *
  38. * .. code-block:: c
  39. *
  40. * struct kunit_kmalloc_params {
  41. * size_t size;
  42. * gfp_t gfp;
  43. * };
  44. *
  45. * static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
  46. * {
  47. * struct kunit_kmalloc_params *params = context;
  48. * res->data = kmalloc(params->size, params->gfp);
  49. *
  50. * if (!res->data)
  51. * return -ENOMEM;
  52. *
  53. * return 0;
  54. * }
  55. *
  56. * static void kunit_kmalloc_free(struct kunit_resource *res)
  57. * {
  58. * kfree(res->data);
  59. * }
  60. *
  61. * void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
  62. * {
  63. * struct kunit_kmalloc_params params;
  64. *
  65. * params.size = size;
  66. * params.gfp = gfp;
  67. *
  68. * return kunit_alloc_resource(test, kunit_kmalloc_init,
  69. * kunit_kmalloc_free, &params);
  70. * }
  71. *
  72. * Resources can also be named, with lookup/removal done on a name
  73. * basis also. kunit_add_named_resource(), kunit_find_named_resource()
  74. * and kunit_destroy_named_resource(). Resource names must be
  75. * unique within the test instance.
  76. */
  77. struct kunit_resource {
  78. void *data;
  79. const char *name;
  80. kunit_resource_free_t free;
  81. /* private: internal use only. */
  82. struct kref refcount;
  83. struct list_head node;
  84. };
  85. struct kunit;
  86. /* Size of log associated with test. */
  87. #define KUNIT_LOG_SIZE 512
  88. /*
  89. * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
  90. * sub-subtest. See the "Subtests" section in
  91. * https://node-tap.org/tap-protocol/
  92. */
  93. #define KUNIT_SUBTEST_INDENT " "
  94. #define KUNIT_SUBSUBTEST_INDENT " "
  95. /**
  96. * struct kunit_case - represents an individual test case.
  97. *
  98. * @run_case: the function representing the actual test case.
  99. * @name: the name of the test case.
  100. *
  101. * A test case is a function with the signature,
  102. * ``void (*)(struct kunit *)``
  103. * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
  104. * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
  105. * with a &struct kunit_suite and will be run after the suite's init
  106. * function and followed by the suite's exit function.
  107. *
  108. * A test case should be static and should only be created with the
  109. * KUNIT_CASE() macro; additionally, every array of test cases should be
  110. * terminated with an empty test case.
  111. *
  112. * Example:
  113. *
  114. * .. code-block:: c
  115. *
  116. * void add_test_basic(struct kunit *test)
  117. * {
  118. * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
  119. * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
  120. * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
  121. * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
  122. * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
  123. * }
  124. *
  125. * static struct kunit_case example_test_cases[] = {
  126. * KUNIT_CASE(add_test_basic),
  127. * {}
  128. * };
  129. *
  130. */
  131. struct kunit_case {
  132. void (*run_case)(struct kunit *test);
  133. const char *name;
  134. /* private: internal use only. */
  135. bool success;
  136. char *log;
  137. };
  138. static inline char *kunit_status_to_string(bool status)
  139. {
  140. return status ? "ok" : "not ok";
  141. }
  142. /**
  143. * KUNIT_CASE - A helper for creating a &struct kunit_case
  144. *
  145. * @test_name: a reference to a test case function.
  146. *
  147. * Takes a symbol for a function representing a test case and creates a
  148. * &struct kunit_case object from it. See the documentation for
  149. * &struct kunit_case for an example on how to use it.
  150. */
  151. #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
  152. /**
  153. * struct kunit_suite - describes a related collection of &struct kunit_case
  154. *
  155. * @name: the name of the test. Purely informational.
  156. * @init: called before every test case.
  157. * @exit: called after every test case.
  158. * @test_cases: a null terminated array of test cases.
  159. *
  160. * A kunit_suite is a collection of related &struct kunit_case s, such that
  161. * @init is called before every test case and @exit is called after every
  162. * test case, similar to the notion of a *test fixture* or a *test class*
  163. * in other unit testing frameworks like JUnit or Googletest.
  164. *
  165. * Every &struct kunit_case must be associated with a kunit_suite for KUnit
  166. * to run it.
  167. */
  168. struct kunit_suite {
  169. const char name[256];
  170. int (*init)(struct kunit *test);
  171. void (*exit)(struct kunit *test);
  172. struct kunit_case *test_cases;
  173. /* private: internal use only */
  174. struct dentry *debugfs;
  175. char *log;
  176. };
  177. /**
  178. * struct kunit - represents a running instance of a test.
  179. *
  180. * @priv: for user to store arbitrary data. Commonly used to pass data
  181. * created in the init function (see &struct kunit_suite).
  182. *
  183. * Used to store information about the current context under which the test
  184. * is running. Most of this data is private and should only be accessed
  185. * indirectly via public functions; the one exception is @priv which can be
  186. * used by the test writer to store arbitrary data.
  187. */
  188. struct kunit {
  189. void *priv;
  190. /* private: internal use only. */
  191. const char *name; /* Read only after initialization! */
  192. char *log; /* Points at case log after initialization */
  193. struct kunit_try_catch try_catch;
  194. /*
  195. * success starts as true, and may only be set to false during a
  196. * test case; thus, it is safe to update this across multiple
  197. * threads using WRITE_ONCE; however, as a consequence, it may only
  198. * be read after the test case finishes once all threads associated
  199. * with the test case have terminated.
  200. */
  201. bool success; /* Read only after test_case finishes! */
  202. spinlock_t lock; /* Guards all mutable test state. */
  203. /*
  204. * Because resources is a list that may be updated multiple times (with
  205. * new resources) from any thread associated with a test case, we must
  206. * protect it with some type of lock.
  207. */
  208. struct list_head resources; /* Protected by lock. */
  209. };
  210. static inline void kunit_set_failure(struct kunit *test)
  211. {
  212. WRITE_ONCE(test->success, false);
  213. }
  214. void kunit_init_test(struct kunit *test, const char *name, char *log);
  215. int kunit_run_tests(struct kunit_suite *suite);
  216. size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
  217. unsigned int kunit_test_case_num(struct kunit_suite *suite,
  218. struct kunit_case *test_case);
  219. int __kunit_test_suites_init(struct kunit_suite * const * const suites);
  220. void __kunit_test_suites_exit(struct kunit_suite **suites);
  221. #if IS_BUILTIN(CONFIG_KUNIT)
  222. int kunit_run_all_tests(void);
  223. #else
  224. static inline int kunit_run_all_tests(void)
  225. {
  226. return 0;
  227. }
  228. #endif /* IS_BUILTIN(CONFIG_KUNIT) */
  229. #ifdef MODULE
  230. /**
  231. * kunit_test_suites_for_module() - used to register one or more
  232. * &struct kunit_suite with KUnit.
  233. *
  234. * @__suites: a statically allocated list of &struct kunit_suite.
  235. *
  236. * Registers @__suites with the test framework. See &struct kunit_suite for
  237. * more information.
  238. *
  239. * If a test suite is built-in, module_init() gets translated into
  240. * an initcall which we don't want as the idea is that for builtins
  241. * the executor will manage execution. So ensure we do not define
  242. * module_{init|exit} functions for the builtin case when registering
  243. * suites via kunit_test_suites() below.
  244. */
  245. #define kunit_test_suites_for_module(__suites) \
  246. static int __init kunit_test_suites_init(void) \
  247. { \
  248. return __kunit_test_suites_init(__suites); \
  249. } \
  250. module_init(kunit_test_suites_init); \
  251. \
  252. static void __exit kunit_test_suites_exit(void) \
  253. { \
  254. return __kunit_test_suites_exit(__suites); \
  255. } \
  256. module_exit(kunit_test_suites_exit)
  257. #else
  258. #define kunit_test_suites_for_module(__suites)
  259. #endif /* MODULE */
  260. #define __kunit_test_suites(unique_array, unique_suites, ...) \
  261. static struct kunit_suite *unique_array[] = { __VA_ARGS__, NULL }; \
  262. kunit_test_suites_for_module(unique_array); \
  263. static struct kunit_suite **unique_suites \
  264. __used __section(".kunit_test_suites") = unique_array
  265. /**
  266. * kunit_test_suites() - used to register one or more &struct kunit_suite
  267. * with KUnit.
  268. *
  269. * @__suites: a statically allocated list of &struct kunit_suite.
  270. *
  271. * Registers @suites with the test framework. See &struct kunit_suite for
  272. * more information.
  273. *
  274. * When builtin, KUnit tests are all run via executor; this is done
  275. * by placing the array of struct kunit_suite * in the .kunit_test_suites
  276. * ELF section.
  277. *
  278. * An alternative is to build the tests as a module. Because modules do not
  279. * support multiple initcall()s, we need to initialize an array of suites for a
  280. * module.
  281. *
  282. */
  283. #define kunit_test_suites(__suites...) \
  284. __kunit_test_suites(__UNIQUE_ID(array), \
  285. __UNIQUE_ID(suites), \
  286. ##__suites)
  287. #define kunit_test_suite(suite) kunit_test_suites(&suite)
  288. #define kunit_suite_for_each_test_case(suite, test_case) \
  289. for (test_case = suite->test_cases; test_case->run_case; test_case++)
  290. bool kunit_suite_has_succeeded(struct kunit_suite *suite);
  291. /*
  292. * Like kunit_alloc_resource() below, but returns the struct kunit_resource
  293. * object that contains the allocation. This is mostly for testing purposes.
  294. */
  295. struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
  296. kunit_resource_init_t init,
  297. kunit_resource_free_t free,
  298. gfp_t internal_gfp,
  299. void *context);
  300. /**
  301. * kunit_get_resource() - Hold resource for use. Should not need to be used
  302. * by most users as we automatically get resources
  303. * retrieved by kunit_find_resource*().
  304. * @res: resource
  305. */
  306. static inline void kunit_get_resource(struct kunit_resource *res)
  307. {
  308. kref_get(&res->refcount);
  309. }
  310. /*
  311. * Called when refcount reaches zero via kunit_put_resources();
  312. * should not be called directly.
  313. */
  314. static inline void kunit_release_resource(struct kref *kref)
  315. {
  316. struct kunit_resource *res = container_of(kref, struct kunit_resource,
  317. refcount);
  318. /* If free function is defined, resource was dynamically allocated. */
  319. if (res->free) {
  320. res->free(res);
  321. kfree(res);
  322. }
  323. }
  324. /**
  325. * kunit_put_resource() - When caller is done with retrieved resource,
  326. * kunit_put_resource() should be called to drop
  327. * reference count. The resource list maintains
  328. * a reference count on resources, so if no users
  329. * are utilizing a resource and it is removed from
  330. * the resource list, it will be freed via the
  331. * associated free function (if any). Only
  332. * needs to be used if we alloc_and_get() or
  333. * find() resource.
  334. * @res: resource
  335. */
  336. static inline void kunit_put_resource(struct kunit_resource *res)
  337. {
  338. kref_put(&res->refcount, kunit_release_resource);
  339. }
  340. /**
  341. * kunit_add_resource() - Add a *test managed resource*.
  342. * @test: The test context object.
  343. * @init: a user-supplied function to initialize the result (if needed). If
  344. * none is supplied, the resource data value is simply set to @data.
  345. * If an init function is supplied, @data is passed to it instead.
  346. * @free: a user-supplied function to free the resource (if needed).
  347. * @res: The resource.
  348. * @data: value to pass to init function or set in resource data field.
  349. */
  350. int kunit_add_resource(struct kunit *test,
  351. kunit_resource_init_t init,
  352. kunit_resource_free_t free,
  353. struct kunit_resource *res,
  354. void *data);
  355. /**
  356. * kunit_add_named_resource() - Add a named *test managed resource*.
  357. * @test: The test context object.
  358. * @init: a user-supplied function to initialize the resource data, if needed.
  359. * @free: a user-supplied function to free the resource data, if needed.
  360. * @res: The resource.
  361. * @name: name to be set for resource.
  362. * @data: value to pass to init function or set in resource data field.
  363. */
  364. int kunit_add_named_resource(struct kunit *test,
  365. kunit_resource_init_t init,
  366. kunit_resource_free_t free,
  367. struct kunit_resource *res,
  368. const char *name,
  369. void *data);
  370. /**
  371. * kunit_alloc_resource() - Allocates a *test managed resource*.
  372. * @test: The test context object.
  373. * @init: a user supplied function to initialize the resource.
  374. * @free: a user supplied function to free the resource.
  375. * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL
  376. * @context: for the user to pass in arbitrary data to the init function.
  377. *
  378. * Allocates a *test managed resource*, a resource which will automatically be
  379. * cleaned up at the end of a test case. See &struct kunit_resource for an
  380. * example.
  381. *
  382. * Note: KUnit needs to allocate memory for a kunit_resource object. You must
  383. * specify an @internal_gfp that is compatible with the use context of your
  384. * resource.
  385. */
  386. static inline void *kunit_alloc_resource(struct kunit *test,
  387. kunit_resource_init_t init,
  388. kunit_resource_free_t free,
  389. gfp_t internal_gfp,
  390. void *context)
  391. {
  392. struct kunit_resource *res;
  393. res = kzalloc(sizeof(*res), internal_gfp);
  394. if (!res)
  395. return NULL;
  396. if (!kunit_add_resource(test, init, free, res, context))
  397. return res->data;
  398. return NULL;
  399. }
  400. typedef bool (*kunit_resource_match_t)(struct kunit *test,
  401. struct kunit_resource *res,
  402. void *match_data);
  403. /**
  404. * kunit_resource_instance_match() - Match a resource with the same instance.
  405. * @test: Test case to which the resource belongs.
  406. * @res: The resource.
  407. * @match_data: The resource pointer to match against.
  408. *
  409. * An instance of kunit_resource_match_t that matches a resource whose
  410. * allocation matches @match_data.
  411. */
  412. static inline bool kunit_resource_instance_match(struct kunit *test,
  413. struct kunit_resource *res,
  414. void *match_data)
  415. {
  416. return res->data == match_data;
  417. }
  418. /**
  419. * kunit_resource_name_match() - Match a resource with the same name.
  420. * @test: Test case to which the resource belongs.
  421. * @res: The resource.
  422. * @match_name: The name to match against.
  423. */
  424. static inline bool kunit_resource_name_match(struct kunit *test,
  425. struct kunit_resource *res,
  426. void *match_name)
  427. {
  428. return res->name && strcmp(res->name, match_name) == 0;
  429. }
  430. /**
  431. * kunit_find_resource() - Find a resource using match function/data.
  432. * @test: Test case to which the resource belongs.
  433. * @match: match function to be applied to resources/match data.
  434. * @match_data: data to be used in matching.
  435. */
  436. static inline struct kunit_resource *
  437. kunit_find_resource(struct kunit *test,
  438. kunit_resource_match_t match,
  439. void *match_data)
  440. {
  441. struct kunit_resource *res, *found = NULL;
  442. spin_lock(&test->lock);
  443. list_for_each_entry_reverse(res, &test->resources, node) {
  444. if (match(test, res, (void *)match_data)) {
  445. found = res;
  446. kunit_get_resource(found);
  447. break;
  448. }
  449. }
  450. spin_unlock(&test->lock);
  451. return found;
  452. }
  453. /**
  454. * kunit_find_named_resource() - Find a resource using match name.
  455. * @test: Test case to which the resource belongs.
  456. * @name: match name.
  457. */
  458. static inline struct kunit_resource *
  459. kunit_find_named_resource(struct kunit *test,
  460. const char *name)
  461. {
  462. return kunit_find_resource(test, kunit_resource_name_match,
  463. (void *)name);
  464. }
  465. /**
  466. * kunit_destroy_resource() - Find a kunit_resource and destroy it.
  467. * @test: Test case to which the resource belongs.
  468. * @match: Match function. Returns whether a given resource matches @match_data.
  469. * @match_data: Data passed into @match.
  470. *
  471. * RETURNS:
  472. * 0 if kunit_resource is found and freed, -ENOENT if not found.
  473. */
  474. int kunit_destroy_resource(struct kunit *test,
  475. kunit_resource_match_t match,
  476. void *match_data);
  477. static inline int kunit_destroy_named_resource(struct kunit *test,
  478. const char *name)
  479. {
  480. return kunit_destroy_resource(test, kunit_resource_name_match,
  481. (void *)name);
  482. }
  483. /**
  484. * kunit_remove_resource() - remove resource from resource list associated with
  485. * test.
  486. * @test: The test context object.
  487. * @res: The resource to be removed.
  488. *
  489. * Note that the resource will not be immediately freed since it is likely
  490. * the caller has a reference to it via alloc_and_get() or find();
  491. * in this case a final call to kunit_put_resource() is required.
  492. */
  493. void kunit_remove_resource(struct kunit *test, struct kunit_resource *res);
  494. /**
  495. * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
  496. * @test: The test context object.
  497. * @size: The size in bytes of the desired memory.
  498. * @gfp: flags passed to underlying kmalloc().
  499. *
  500. * Just like `kmalloc(...)`, except the allocation is managed by the test case
  501. * and is automatically cleaned up after the test case concludes. See &struct
  502. * kunit_resource for more information.
  503. */
  504. void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp);
  505. /**
  506. * kunit_kfree() - Like kfree except for allocations managed by KUnit.
  507. * @test: The test case to which the resource belongs.
  508. * @ptr: The memory allocation to free.
  509. */
  510. void kunit_kfree(struct kunit *test, const void *ptr);
  511. /**
  512. * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
  513. * @test: The test context object.
  514. * @size: The size in bytes of the desired memory.
  515. * @gfp: flags passed to underlying kmalloc().
  516. *
  517. * See kzalloc() and kunit_kmalloc() for more information.
  518. */
  519. static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
  520. {
  521. return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
  522. }
  523. void kunit_cleanup(struct kunit *test);
  524. void kunit_log_append(char *log, const char *fmt, ...);
  525. /*
  526. * printk and log to per-test or per-suite log buffer. Logging only done
  527. * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
  528. */
  529. #define kunit_log(lvl, test_or_suite, fmt, ...) \
  530. do { \
  531. printk(lvl fmt, ##__VA_ARGS__); \
  532. kunit_log_append((test_or_suite)->log, fmt "\n", \
  533. ##__VA_ARGS__); \
  534. } while (0)
  535. #define kunit_printk(lvl, test, fmt, ...) \
  536. kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
  537. (test)->name, ##__VA_ARGS__)
  538. /**
  539. * kunit_info() - Prints an INFO level message associated with @test.
  540. *
  541. * @test: The test context object.
  542. * @fmt: A printk() style format string.
  543. *
  544. * Prints an info level message associated with the test suite being run.
  545. * Takes a variable number of format parameters just like printk().
  546. */
  547. #define kunit_info(test, fmt, ...) \
  548. kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
  549. /**
  550. * kunit_warn() - Prints a WARN level message associated with @test.
  551. *
  552. * @test: The test context object.
  553. * @fmt: A printk() style format string.
  554. *
  555. * Prints a warning level message.
  556. */
  557. #define kunit_warn(test, fmt, ...) \
  558. kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
  559. /**
  560. * kunit_err() - Prints an ERROR level message associated with @test.
  561. *
  562. * @test: The test context object.
  563. * @fmt: A printk() style format string.
  564. *
  565. * Prints an error level message.
  566. */
  567. #define kunit_err(test, fmt, ...) \
  568. kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
  569. /**
  570. * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
  571. * @test: The test context object.
  572. *
  573. * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
  574. * words, it does nothing and only exists for code clarity. See
  575. * KUNIT_EXPECT_TRUE() for more information.
  576. */
  577. #define KUNIT_SUCCEED(test) do {} while (0)
  578. void kunit_do_assertion(struct kunit *test,
  579. struct kunit_assert *assert,
  580. bool pass,
  581. const char *fmt, ...);
  582. #define KUNIT_ASSERTION(test, pass, assert_class, INITIALIZER, fmt, ...) do { \
  583. struct assert_class __assertion = INITIALIZER; \
  584. kunit_do_assertion(test, \
  585. &__assertion.assert, \
  586. pass, \
  587. fmt, \
  588. ##__VA_ARGS__); \
  589. } while (0)
  590. #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
  591. KUNIT_ASSERTION(test, \
  592. false, \
  593. kunit_fail_assert, \
  594. KUNIT_INIT_FAIL_ASSERT_STRUCT(test, assert_type), \
  595. fmt, \
  596. ##__VA_ARGS__)
  597. /**
  598. * KUNIT_FAIL() - Always causes a test to fail when evaluated.
  599. * @test: The test context object.
  600. * @fmt: an informational message to be printed when the assertion is made.
  601. * @...: string format arguments.
  602. *
  603. * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
  604. * other words, it always results in a failed expectation, and consequently
  605. * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
  606. * for more information.
  607. */
  608. #define KUNIT_FAIL(test, fmt, ...) \
  609. KUNIT_FAIL_ASSERTION(test, \
  610. KUNIT_EXPECTATION, \
  611. fmt, \
  612. ##__VA_ARGS__)
  613. #define KUNIT_UNARY_ASSERTION(test, \
  614. assert_type, \
  615. condition, \
  616. expected_true, \
  617. fmt, \
  618. ...) \
  619. KUNIT_ASSERTION(test, \
  620. !!(condition) == !!expected_true, \
  621. kunit_unary_assert, \
  622. KUNIT_INIT_UNARY_ASSERT_STRUCT(test, \
  623. assert_type, \
  624. #condition, \
  625. expected_true), \
  626. fmt, \
  627. ##__VA_ARGS__)
  628. #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
  629. KUNIT_UNARY_ASSERTION(test, \
  630. assert_type, \
  631. condition, \
  632. true, \
  633. fmt, \
  634. ##__VA_ARGS__)
  635. #define KUNIT_TRUE_ASSERTION(test, assert_type, condition) \
  636. KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, NULL)
  637. #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
  638. KUNIT_UNARY_ASSERTION(test, \
  639. assert_type, \
  640. condition, \
  641. false, \
  642. fmt, \
  643. ##__VA_ARGS__)
  644. #define KUNIT_FALSE_ASSERTION(test, assert_type, condition) \
  645. KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, NULL)
  646. /*
  647. * A factory macro for defining the assertions and expectations for the basic
  648. * comparisons defined for the built in types.
  649. *
  650. * Unfortunately, there is no common type that all types can be promoted to for
  651. * which all the binary operators behave the same way as for the actual types
  652. * (for example, there is no type that long long and unsigned long long can
  653. * both be cast to where the comparison result is preserved for all values). So
  654. * the best we can do is do the comparison in the original types and then coerce
  655. * everything to long long for printing; this way, the comparison behaves
  656. * correctly and the printed out value usually makes sense without
  657. * interpretation, but can always be interpreted to figure out the actual
  658. * value.
  659. */
  660. #define KUNIT_BASE_BINARY_ASSERTION(test, \
  661. assert_class, \
  662. ASSERT_CLASS_INIT, \
  663. assert_type, \
  664. left, \
  665. op, \
  666. right, \
  667. fmt, \
  668. ...) \
  669. do { \
  670. typeof(left) __left = (left); \
  671. typeof(right) __right = (right); \
  672. ((void)__typecheck(__left, __right)); \
  673. \
  674. KUNIT_ASSERTION(test, \
  675. __left op __right, \
  676. assert_class, \
  677. ASSERT_CLASS_INIT(test, \
  678. assert_type, \
  679. #op, \
  680. #left, \
  681. __left, \
  682. #right, \
  683. __right), \
  684. fmt, \
  685. ##__VA_ARGS__); \
  686. } while (0)
  687. #define KUNIT_BASE_EQ_MSG_ASSERTION(test, \
  688. assert_class, \
  689. ASSERT_CLASS_INIT, \
  690. assert_type, \
  691. left, \
  692. right, \
  693. fmt, \
  694. ...) \
  695. KUNIT_BASE_BINARY_ASSERTION(test, \
  696. assert_class, \
  697. ASSERT_CLASS_INIT, \
  698. assert_type, \
  699. left, ==, right, \
  700. fmt, \
  701. ##__VA_ARGS__)
  702. #define KUNIT_BASE_NE_MSG_ASSERTION(test, \
  703. assert_class, \
  704. ASSERT_CLASS_INIT, \
  705. assert_type, \
  706. left, \
  707. right, \
  708. fmt, \
  709. ...) \
  710. KUNIT_BASE_BINARY_ASSERTION(test, \
  711. assert_class, \
  712. ASSERT_CLASS_INIT, \
  713. assert_type, \
  714. left, !=, right, \
  715. fmt, \
  716. ##__VA_ARGS__)
  717. #define KUNIT_BASE_LT_MSG_ASSERTION(test, \
  718. assert_class, \
  719. ASSERT_CLASS_INIT, \
  720. assert_type, \
  721. left, \
  722. right, \
  723. fmt, \
  724. ...) \
  725. KUNIT_BASE_BINARY_ASSERTION(test, \
  726. assert_class, \
  727. ASSERT_CLASS_INIT, \
  728. assert_type, \
  729. left, <, right, \
  730. fmt, \
  731. ##__VA_ARGS__)
  732. #define KUNIT_BASE_LE_MSG_ASSERTION(test, \
  733. assert_class, \
  734. ASSERT_CLASS_INIT, \
  735. assert_type, \
  736. left, \
  737. right, \
  738. fmt, \
  739. ...) \
  740. KUNIT_BASE_BINARY_ASSERTION(test, \
  741. assert_class, \
  742. ASSERT_CLASS_INIT, \
  743. assert_type, \
  744. left, <=, right, \
  745. fmt, \
  746. ##__VA_ARGS__)
  747. #define KUNIT_BASE_GT_MSG_ASSERTION(test, \
  748. assert_class, \
  749. ASSERT_CLASS_INIT, \
  750. assert_type, \
  751. left, \
  752. right, \
  753. fmt, \
  754. ...) \
  755. KUNIT_BASE_BINARY_ASSERTION(test, \
  756. assert_class, \
  757. ASSERT_CLASS_INIT, \
  758. assert_type, \
  759. left, >, right, \
  760. fmt, \
  761. ##__VA_ARGS__)
  762. #define KUNIT_BASE_GE_MSG_ASSERTION(test, \
  763. assert_class, \
  764. ASSERT_CLASS_INIT, \
  765. assert_type, \
  766. left, \
  767. right, \
  768. fmt, \
  769. ...) \
  770. KUNIT_BASE_BINARY_ASSERTION(test, \
  771. assert_class, \
  772. ASSERT_CLASS_INIT, \
  773. assert_type, \
  774. left, >=, right, \
  775. fmt, \
  776. ##__VA_ARGS__)
  777. #define KUNIT_BINARY_EQ_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
  778. KUNIT_BASE_EQ_MSG_ASSERTION(test, \
  779. kunit_binary_assert, \
  780. KUNIT_INIT_BINARY_ASSERT_STRUCT, \
  781. assert_type, \
  782. left, \
  783. right, \
  784. fmt, \
  785. ##__VA_ARGS__)
  786. #define KUNIT_BINARY_EQ_ASSERTION(test, assert_type, left, right) \
  787. KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
  788. assert_type, \
  789. left, \
  790. right, \
  791. NULL)
  792. #define KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
  793. assert_type, \
  794. left, \
  795. right, \
  796. fmt, \
  797. ...) \
  798. KUNIT_BASE_EQ_MSG_ASSERTION(test, \
  799. kunit_binary_ptr_assert, \
  800. KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
  801. assert_type, \
  802. left, \
  803. right, \
  804. fmt, \
  805. ##__VA_ARGS__)
  806. #define KUNIT_BINARY_PTR_EQ_ASSERTION(test, assert_type, left, right) \
  807. KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
  808. assert_type, \
  809. left, \
  810. right, \
  811. NULL)
  812. #define KUNIT_BINARY_NE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
  813. KUNIT_BASE_NE_MSG_ASSERTION(test, \
  814. kunit_binary_assert, \
  815. KUNIT_INIT_BINARY_ASSERT_STRUCT, \
  816. assert_type, \
  817. left, \
  818. right, \
  819. fmt, \
  820. ##__VA_ARGS__)
  821. #define KUNIT_BINARY_NE_ASSERTION(test, assert_type, left, right) \
  822. KUNIT_BINARY_NE_MSG_ASSERTION(test, \
  823. assert_type, \
  824. left, \
  825. right, \
  826. NULL)
  827. #define KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
  828. assert_type, \
  829. left, \
  830. right, \
  831. fmt, \
  832. ...) \
  833. KUNIT_BASE_NE_MSG_ASSERTION(test, \
  834. kunit_binary_ptr_assert, \
  835. KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
  836. assert_type, \
  837. left, \
  838. right, \
  839. fmt, \
  840. ##__VA_ARGS__)
  841. #define KUNIT_BINARY_PTR_NE_ASSERTION(test, assert_type, left, right) \
  842. KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
  843. assert_type, \
  844. left, \
  845. right, \
  846. NULL)
  847. #define KUNIT_BINARY_LT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
  848. KUNIT_BASE_LT_MSG_ASSERTION(test, \
  849. kunit_binary_assert, \
  850. KUNIT_INIT_BINARY_ASSERT_STRUCT, \
  851. assert_type, \
  852. left, \
  853. right, \
  854. fmt, \
  855. ##__VA_ARGS__)
  856. #define KUNIT_BINARY_LT_ASSERTION(test, assert_type, left, right) \
  857. KUNIT_BINARY_LT_MSG_ASSERTION(test, \
  858. assert_type, \
  859. left, \
  860. right, \
  861. NULL)
  862. #define KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \
  863. assert_type, \
  864. left, \
  865. right, \
  866. fmt, \
  867. ...) \
  868. KUNIT_BASE_LT_MSG_ASSERTION(test, \
  869. kunit_binary_ptr_assert, \
  870. KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
  871. assert_type, \
  872. left, \
  873. right, \
  874. fmt, \
  875. ##__VA_ARGS__)
  876. #define KUNIT_BINARY_PTR_LT_ASSERTION(test, assert_type, left, right) \
  877. KUNIT_BINARY_PTR_LT_MSG_ASSERTION(test, \
  878. assert_type, \
  879. left, \
  880. right, \
  881. NULL)
  882. #define KUNIT_BINARY_LE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
  883. KUNIT_BASE_LE_MSG_ASSERTION(test, \
  884. kunit_binary_assert, \
  885. KUNIT_INIT_BINARY_ASSERT_STRUCT, \
  886. assert_type, \
  887. left, \
  888. right, \
  889. fmt, \
  890. ##__VA_ARGS__)
  891. #define KUNIT_BINARY_LE_ASSERTION(test, assert_type, left, right) \
  892. KUNIT_BINARY_LE_MSG_ASSERTION(test, \
  893. assert_type, \
  894. left, \
  895. right, \
  896. NULL)
  897. #define KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \
  898. assert_type, \
  899. left, \
  900. right, \
  901. fmt, \
  902. ...) \
  903. KUNIT_BASE_LE_MSG_ASSERTION(test, \
  904. kunit_binary_ptr_assert, \
  905. KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
  906. assert_type, \
  907. left, \
  908. right, \
  909. fmt, \
  910. ##__VA_ARGS__)
  911. #define KUNIT_BINARY_PTR_LE_ASSERTION(test, assert_type, left, right) \
  912. KUNIT_BINARY_PTR_LE_MSG_ASSERTION(test, \
  913. assert_type, \
  914. left, \
  915. right, \
  916. NULL)
  917. #define KUNIT_BINARY_GT_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
  918. KUNIT_BASE_GT_MSG_ASSERTION(test, \
  919. kunit_binary_assert, \
  920. KUNIT_INIT_BINARY_ASSERT_STRUCT, \
  921. assert_type, \
  922. left, \
  923. right, \
  924. fmt, \
  925. ##__VA_ARGS__)
  926. #define KUNIT_BINARY_GT_ASSERTION(test, assert_type, left, right) \
  927. KUNIT_BINARY_GT_MSG_ASSERTION(test, \
  928. assert_type, \
  929. left, \
  930. right, \
  931. NULL)
  932. #define KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \
  933. assert_type, \
  934. left, \
  935. right, \
  936. fmt, \
  937. ...) \
  938. KUNIT_BASE_GT_MSG_ASSERTION(test, \
  939. kunit_binary_ptr_assert, \
  940. KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
  941. assert_type, \
  942. left, \
  943. right, \
  944. fmt, \
  945. ##__VA_ARGS__)
  946. #define KUNIT_BINARY_PTR_GT_ASSERTION(test, assert_type, left, right) \
  947. KUNIT_BINARY_PTR_GT_MSG_ASSERTION(test, \
  948. assert_type, \
  949. left, \
  950. right, \
  951. NULL)
  952. #define KUNIT_BINARY_GE_MSG_ASSERTION(test, assert_type, left, right, fmt, ...)\
  953. KUNIT_BASE_GE_MSG_ASSERTION(test, \
  954. kunit_binary_assert, \
  955. KUNIT_INIT_BINARY_ASSERT_STRUCT, \
  956. assert_type, \
  957. left, \
  958. right, \
  959. fmt, \
  960. ##__VA_ARGS__)
  961. #define KUNIT_BINARY_GE_ASSERTION(test, assert_type, left, right) \
  962. KUNIT_BINARY_GE_MSG_ASSERTION(test, \
  963. assert_type, \
  964. left, \
  965. right, \
  966. NULL)
  967. #define KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \
  968. assert_type, \
  969. left, \
  970. right, \
  971. fmt, \
  972. ...) \
  973. KUNIT_BASE_GE_MSG_ASSERTION(test, \
  974. kunit_binary_ptr_assert, \
  975. KUNIT_INIT_BINARY_PTR_ASSERT_STRUCT, \
  976. assert_type, \
  977. left, \
  978. right, \
  979. fmt, \
  980. ##__VA_ARGS__)
  981. #define KUNIT_BINARY_PTR_GE_ASSERTION(test, assert_type, left, right) \
  982. KUNIT_BINARY_PTR_GE_MSG_ASSERTION(test, \
  983. assert_type, \
  984. left, \
  985. right, \
  986. NULL)
  987. #define KUNIT_BINARY_STR_ASSERTION(test, \
  988. assert_type, \
  989. left, \
  990. op, \
  991. right, \
  992. fmt, \
  993. ...) \
  994. do { \
  995. typeof(left) __left = (left); \
  996. typeof(right) __right = (right); \
  997. \
  998. KUNIT_ASSERTION(test, \
  999. strcmp(__left, __right) op 0, \
  1000. kunit_binary_str_assert, \
  1001. KUNIT_INIT_BINARY_STR_ASSERT_STRUCT(test, \
  1002. assert_type, \
  1003. #op, \
  1004. #left, \
  1005. __left, \
  1006. #right, \
  1007. __right), \
  1008. fmt, \
  1009. ##__VA_ARGS__); \
  1010. } while (0)
  1011. #define KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
  1012. assert_type, \
  1013. left, \
  1014. right, \
  1015. fmt, \
  1016. ...) \
  1017. KUNIT_BINARY_STR_ASSERTION(test, \
  1018. assert_type, \
  1019. left, ==, right, \
  1020. fmt, \
  1021. ##__VA_ARGS__)
  1022. #define KUNIT_BINARY_STR_EQ_ASSERTION(test, assert_type, left, right) \
  1023. KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
  1024. assert_type, \
  1025. left, \
  1026. right, \
  1027. NULL)
  1028. #define KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
  1029. assert_type, \
  1030. left, \
  1031. right, \
  1032. fmt, \
  1033. ...) \
  1034. KUNIT_BINARY_STR_ASSERTION(test, \
  1035. assert_type, \
  1036. left, !=, right, \
  1037. fmt, \
  1038. ##__VA_ARGS__)
  1039. #define KUNIT_BINARY_STR_NE_ASSERTION(test, assert_type, left, right) \
  1040. KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
  1041. assert_type, \
  1042. left, \
  1043. right, \
  1044. NULL)
  1045. #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
  1046. assert_type, \
  1047. ptr, \
  1048. fmt, \
  1049. ...) \
  1050. do { \
  1051. typeof(ptr) __ptr = (ptr); \
  1052. \
  1053. KUNIT_ASSERTION(test, \
  1054. !IS_ERR_OR_NULL(__ptr), \
  1055. kunit_ptr_not_err_assert, \
  1056. KUNIT_INIT_PTR_NOT_ERR_STRUCT(test, \
  1057. assert_type, \
  1058. #ptr, \
  1059. __ptr), \
  1060. fmt, \
  1061. ##__VA_ARGS__); \
  1062. } while (0)
  1063. #define KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, assert_type, ptr) \
  1064. KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
  1065. assert_type, \
  1066. ptr, \
  1067. NULL)
  1068. /**
  1069. * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
  1070. * @test: The test context object.
  1071. * @condition: an arbitrary boolean expression. The test fails when this does
  1072. * not evaluate to true.
  1073. *
  1074. * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
  1075. * to fail when the specified condition is not met; however, it will not prevent
  1076. * the test case from continuing to run; this is otherwise known as an
  1077. * *expectation failure*.
  1078. */
  1079. #define KUNIT_EXPECT_TRUE(test, condition) \
  1080. KUNIT_TRUE_ASSERTION(test, KUNIT_EXPECTATION, condition)
  1081. #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
  1082. KUNIT_TRUE_MSG_ASSERTION(test, \
  1083. KUNIT_EXPECTATION, \
  1084. condition, \
  1085. fmt, \
  1086. ##__VA_ARGS__)
  1087. /**
  1088. * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
  1089. * @test: The test context object.
  1090. * @condition: an arbitrary boolean expression. The test fails when this does
  1091. * not evaluate to false.
  1092. *
  1093. * Sets an expectation that @condition evaluates to false. See
  1094. * KUNIT_EXPECT_TRUE() for more information.
  1095. */
  1096. #define KUNIT_EXPECT_FALSE(test, condition) \
  1097. KUNIT_FALSE_ASSERTION(test, KUNIT_EXPECTATION, condition)
  1098. #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
  1099. KUNIT_FALSE_MSG_ASSERTION(test, \
  1100. KUNIT_EXPECTATION, \
  1101. condition, \
  1102. fmt, \
  1103. ##__VA_ARGS__)
  1104. /**
  1105. * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
  1106. * @test: The test context object.
  1107. * @left: an arbitrary expression that evaluates to a primitive C type.
  1108. * @right: an arbitrary expression that evaluates to a primitive C type.
  1109. *
  1110. * Sets an expectation that the values that @left and @right evaluate to are
  1111. * equal. This is semantically equivalent to
  1112. * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
  1113. * more information.
  1114. */
  1115. #define KUNIT_EXPECT_EQ(test, left, right) \
  1116. KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
  1117. #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
  1118. KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
  1119. KUNIT_EXPECTATION, \
  1120. left, \
  1121. right, \
  1122. fmt, \
  1123. ##__VA_ARGS__)
  1124. /**
  1125. * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
  1126. * @test: The test context object.
  1127. * @left: an arbitrary expression that evaluates to a pointer.
  1128. * @right: an arbitrary expression that evaluates to a pointer.
  1129. *
  1130. * Sets an expectation that the values that @left and @right evaluate to are
  1131. * equal. This is semantically equivalent to
  1132. * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
  1133. * more information.
  1134. */
  1135. #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
  1136. KUNIT_BINARY_PTR_EQ_ASSERTION(test, \
  1137. KUNIT_EXPECTATION, \
  1138. left, \
  1139. right)
  1140. #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
  1141. KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
  1142. KUNIT_EXPECTATION, \
  1143. left, \
  1144. right, \
  1145. fmt, \
  1146. ##__VA_ARGS__)
  1147. /**
  1148. * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
  1149. * @test: The test context object.
  1150. * @left: an arbitrary expression that evaluates to a primitive C type.
  1151. * @right: an arbitrary expression that evaluates to a primitive C type.
  1152. *
  1153. * Sets an expectation that the values that @left and @right evaluate to are not
  1154. * equal. This is semantically equivalent to
  1155. * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
  1156. * more information.
  1157. */
  1158. #define KUNIT_EXPECT_NE(test, left, right) \
  1159. KUNIT_BINARY_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
  1160. #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
  1161. KUNIT_BINARY_NE_MSG_ASSERTION(test, \
  1162. KUNIT_EXPECTATION, \
  1163. left, \
  1164. right, \
  1165. fmt, \
  1166. ##__VA_ARGS__)
  1167. /**
  1168. * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
  1169. * @test: The test context object.
  1170. * @left: an arbitrary expression that evaluates to a pointer.
  1171. * @right: an arbitrary expression that evaluates to a pointer.
  1172. *
  1173. * Sets an expectation that the values that @left and @right evaluate to are not
  1174. * equal. This is semantically equivalent to
  1175. * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
  1176. * more information.
  1177. */
  1178. #define KUNIT_EXPECT_PTR_NE(test, left, right) \
  1179. KUNIT_BINARY_PTR_NE_ASSERTION(test, \
  1180. KUNIT_EXPECTATION, \
  1181. left, \
  1182. right)
  1183. #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
  1184. KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
  1185. KUNIT_EXPECTATION, \
  1186. left, \
  1187. right, \
  1188. fmt, \
  1189. ##__VA_ARGS__)
  1190. /**
  1191. * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
  1192. * @test: The test context object.
  1193. * @left: an arbitrary expression that evaluates to a primitive C type.
  1194. * @right: an arbitrary expression that evaluates to a primitive C type.
  1195. *
  1196. * Sets an expectation that the value that @left evaluates to is less than the
  1197. * value that @right evaluates to. This is semantically equivalent to
  1198. * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
  1199. * more information.
  1200. */
  1201. #define KUNIT_EXPECT_LT(test, left, right) \
  1202. KUNIT_BINARY_LT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
  1203. #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
  1204. KUNIT_BINARY_LT_MSG_ASSERTION(test, \
  1205. KUNIT_EXPECTATION, \
  1206. left, \
  1207. right, \
  1208. fmt, \
  1209. ##__VA_ARGS__)
  1210. /**
  1211. * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
  1212. * @test: The test context object.
  1213. * @left: an arbitrary expression that evaluates to a primitive C type.
  1214. * @right: an arbitrary expression that evaluates to a primitive C type.
  1215. *
  1216. * Sets an expectation that the value that @left evaluates to is less than or
  1217. * equal to the value that @right evaluates to. Semantically this is equivalent
  1218. * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
  1219. * more information.
  1220. */
  1221. #define KUNIT_EXPECT_LE(test, left, right) \
  1222. KUNIT_BINARY_LE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
  1223. #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
  1224. KUNIT_BINARY_LE_MSG_ASSERTION(test, \
  1225. KUNIT_EXPECTATION, \
  1226. left, \
  1227. right, \
  1228. fmt, \
  1229. ##__VA_ARGS__)
  1230. /**
  1231. * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
  1232. * @test: The test context object.
  1233. * @left: an arbitrary expression that evaluates to a primitive C type.
  1234. * @right: an arbitrary expression that evaluates to a primitive C type.
  1235. *
  1236. * Sets an expectation that the value that @left evaluates to is greater than
  1237. * the value that @right evaluates to. This is semantically equivalent to
  1238. * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
  1239. * more information.
  1240. */
  1241. #define KUNIT_EXPECT_GT(test, left, right) \
  1242. KUNIT_BINARY_GT_ASSERTION(test, KUNIT_EXPECTATION, left, right)
  1243. #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
  1244. KUNIT_BINARY_GT_MSG_ASSERTION(test, \
  1245. KUNIT_EXPECTATION, \
  1246. left, \
  1247. right, \
  1248. fmt, \
  1249. ##__VA_ARGS__)
  1250. /**
  1251. * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
  1252. * @test: The test context object.
  1253. * @left: an arbitrary expression that evaluates to a primitive C type.
  1254. * @right: an arbitrary expression that evaluates to a primitive C type.
  1255. *
  1256. * Sets an expectation that the value that @left evaluates to is greater than
  1257. * the value that @right evaluates to. This is semantically equivalent to
  1258. * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
  1259. * more information.
  1260. */
  1261. #define KUNIT_EXPECT_GE(test, left, right) \
  1262. KUNIT_BINARY_GE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
  1263. #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
  1264. KUNIT_BINARY_GE_MSG_ASSERTION(test, \
  1265. KUNIT_EXPECTATION, \
  1266. left, \
  1267. right, \
  1268. fmt, \
  1269. ##__VA_ARGS__)
  1270. /**
  1271. * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
  1272. * @test: The test context object.
  1273. * @left: an arbitrary expression that evaluates to a null terminated string.
  1274. * @right: an arbitrary expression that evaluates to a null terminated string.
  1275. *
  1276. * Sets an expectation that the values that @left and @right evaluate to are
  1277. * equal. This is semantically equivalent to
  1278. * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
  1279. * for more information.
  1280. */
  1281. #define KUNIT_EXPECT_STREQ(test, left, right) \
  1282. KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_EXPECTATION, left, right)
  1283. #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
  1284. KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
  1285. KUNIT_EXPECTATION, \
  1286. left, \
  1287. right, \
  1288. fmt, \
  1289. ##__VA_ARGS__)
  1290. /**
  1291. * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
  1292. * @test: The test context object.
  1293. * @left: an arbitrary expression that evaluates to a null terminated string.
  1294. * @right: an arbitrary expression that evaluates to a null terminated string.
  1295. *
  1296. * Sets an expectation that the values that @left and @right evaluate to are
  1297. * not equal. This is semantically equivalent to
  1298. * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
  1299. * for more information.
  1300. */
  1301. #define KUNIT_EXPECT_STRNEQ(test, left, right) \
  1302. KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_EXPECTATION, left, right)
  1303. #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
  1304. KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
  1305. KUNIT_EXPECTATION, \
  1306. left, \
  1307. right, \
  1308. fmt, \
  1309. ##__VA_ARGS__)
  1310. /**
  1311. * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
  1312. * @test: The test context object.
  1313. * @ptr: an arbitrary pointer.
  1314. *
  1315. * Sets an expectation that the value that @ptr evaluates to is not null and not
  1316. * an errno stored in a pointer. This is semantically equivalent to
  1317. * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
  1318. * more information.
  1319. */
  1320. #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
  1321. KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_EXPECTATION, ptr)
  1322. #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
  1323. KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
  1324. KUNIT_EXPECTATION, \
  1325. ptr, \
  1326. fmt, \
  1327. ##__VA_ARGS__)
  1328. #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
  1329. KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
  1330. /**
  1331. * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
  1332. * @test: The test context object.
  1333. * @condition: an arbitrary boolean expression. The test fails and aborts when
  1334. * this does not evaluate to true.
  1335. *
  1336. * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
  1337. * fail *and immediately abort* when the specified condition is not met. Unlike
  1338. * an expectation failure, it will prevent the test case from continuing to run;
  1339. * this is otherwise known as an *assertion failure*.
  1340. */
  1341. #define KUNIT_ASSERT_TRUE(test, condition) \
  1342. KUNIT_TRUE_ASSERTION(test, KUNIT_ASSERTION, condition)
  1343. #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
  1344. KUNIT_TRUE_MSG_ASSERTION(test, \
  1345. KUNIT_ASSERTION, \
  1346. condition, \
  1347. fmt, \
  1348. ##__VA_ARGS__)
  1349. /**
  1350. * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
  1351. * @test: The test context object.
  1352. * @condition: an arbitrary boolean expression.
  1353. *
  1354. * Sets an assertion that the value that @condition evaluates to is false. This
  1355. * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
  1356. * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1357. */
  1358. #define KUNIT_ASSERT_FALSE(test, condition) \
  1359. KUNIT_FALSE_ASSERTION(test, KUNIT_ASSERTION, condition)
  1360. #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
  1361. KUNIT_FALSE_MSG_ASSERTION(test, \
  1362. KUNIT_ASSERTION, \
  1363. condition, \
  1364. fmt, \
  1365. ##__VA_ARGS__)
  1366. /**
  1367. * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
  1368. * @test: The test context object.
  1369. * @left: an arbitrary expression that evaluates to a primitive C type.
  1370. * @right: an arbitrary expression that evaluates to a primitive C type.
  1371. *
  1372. * Sets an assertion that the values that @left and @right evaluate to are
  1373. * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
  1374. * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1375. */
  1376. #define KUNIT_ASSERT_EQ(test, left, right) \
  1377. KUNIT_BINARY_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
  1378. #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
  1379. KUNIT_BINARY_EQ_MSG_ASSERTION(test, \
  1380. KUNIT_ASSERTION, \
  1381. left, \
  1382. right, \
  1383. fmt, \
  1384. ##__VA_ARGS__)
  1385. /**
  1386. * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
  1387. * @test: The test context object.
  1388. * @left: an arbitrary expression that evaluates to a pointer.
  1389. * @right: an arbitrary expression that evaluates to a pointer.
  1390. *
  1391. * Sets an assertion that the values that @left and @right evaluate to are
  1392. * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
  1393. * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1394. */
  1395. #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
  1396. KUNIT_BINARY_PTR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
  1397. #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
  1398. KUNIT_BINARY_PTR_EQ_MSG_ASSERTION(test, \
  1399. KUNIT_ASSERTION, \
  1400. left, \
  1401. right, \
  1402. fmt, \
  1403. ##__VA_ARGS__)
  1404. /**
  1405. * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
  1406. * @test: The test context object.
  1407. * @left: an arbitrary expression that evaluates to a primitive C type.
  1408. * @right: an arbitrary expression that evaluates to a primitive C type.
  1409. *
  1410. * Sets an assertion that the values that @left and @right evaluate to are not
  1411. * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
  1412. * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1413. */
  1414. #define KUNIT_ASSERT_NE(test, left, right) \
  1415. KUNIT_BINARY_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
  1416. #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
  1417. KUNIT_BINARY_NE_MSG_ASSERTION(test, \
  1418. KUNIT_ASSERTION, \
  1419. left, \
  1420. right, \
  1421. fmt, \
  1422. ##__VA_ARGS__)
  1423. /**
  1424. * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
  1425. * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
  1426. * @test: The test context object.
  1427. * @left: an arbitrary expression that evaluates to a pointer.
  1428. * @right: an arbitrary expression that evaluates to a pointer.
  1429. *
  1430. * Sets an assertion that the values that @left and @right evaluate to are not
  1431. * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
  1432. * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1433. */
  1434. #define KUNIT_ASSERT_PTR_NE(test, left, right) \
  1435. KUNIT_BINARY_PTR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
  1436. #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
  1437. KUNIT_BINARY_PTR_NE_MSG_ASSERTION(test, \
  1438. KUNIT_ASSERTION, \
  1439. left, \
  1440. right, \
  1441. fmt, \
  1442. ##__VA_ARGS__)
  1443. /**
  1444. * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
  1445. * @test: The test context object.
  1446. * @left: an arbitrary expression that evaluates to a primitive C type.
  1447. * @right: an arbitrary expression that evaluates to a primitive C type.
  1448. *
  1449. * Sets an assertion that the value that @left evaluates to is less than the
  1450. * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
  1451. * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
  1452. * is not met.
  1453. */
  1454. #define KUNIT_ASSERT_LT(test, left, right) \
  1455. KUNIT_BINARY_LT_ASSERTION(test, KUNIT_ASSERTION, left, right)
  1456. #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
  1457. KUNIT_BINARY_LT_MSG_ASSERTION(test, \
  1458. KUNIT_ASSERTION, \
  1459. left, \
  1460. right, \
  1461. fmt, \
  1462. ##__VA_ARGS__)
  1463. /**
  1464. * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
  1465. * @test: The test context object.
  1466. * @left: an arbitrary expression that evaluates to a primitive C type.
  1467. * @right: an arbitrary expression that evaluates to a primitive C type.
  1468. *
  1469. * Sets an assertion that the value that @left evaluates to is less than or
  1470. * equal to the value that @right evaluates to. This is the same as
  1471. * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
  1472. * KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1473. */
  1474. #define KUNIT_ASSERT_LE(test, left, right) \
  1475. KUNIT_BINARY_LE_ASSERTION(test, KUNIT_ASSERTION, left, right)
  1476. #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
  1477. KUNIT_BINARY_LE_MSG_ASSERTION(test, \
  1478. KUNIT_ASSERTION, \
  1479. left, \
  1480. right, \
  1481. fmt, \
  1482. ##__VA_ARGS__)
  1483. /**
  1484. * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
  1485. * @test: The test context object.
  1486. * @left: an arbitrary expression that evaluates to a primitive C type.
  1487. * @right: an arbitrary expression that evaluates to a primitive C type.
  1488. *
  1489. * Sets an assertion that the value that @left evaluates to is greater than the
  1490. * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
  1491. * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
  1492. * is not met.
  1493. */
  1494. #define KUNIT_ASSERT_GT(test, left, right) \
  1495. KUNIT_BINARY_GT_ASSERTION(test, KUNIT_ASSERTION, left, right)
  1496. #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
  1497. KUNIT_BINARY_GT_MSG_ASSERTION(test, \
  1498. KUNIT_ASSERTION, \
  1499. left, \
  1500. right, \
  1501. fmt, \
  1502. ##__VA_ARGS__)
  1503. /**
  1504. * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
  1505. * @test: The test context object.
  1506. * @left: an arbitrary expression that evaluates to a primitive C type.
  1507. * @right: an arbitrary expression that evaluates to a primitive C type.
  1508. *
  1509. * Sets an assertion that the value that @left evaluates to is greater than the
  1510. * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
  1511. * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
  1512. * is not met.
  1513. */
  1514. #define KUNIT_ASSERT_GE(test, left, right) \
  1515. KUNIT_BINARY_GE_ASSERTION(test, KUNIT_ASSERTION, left, right)
  1516. #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
  1517. KUNIT_BINARY_GE_MSG_ASSERTION(test, \
  1518. KUNIT_ASSERTION, \
  1519. left, \
  1520. right, \
  1521. fmt, \
  1522. ##__VA_ARGS__)
  1523. /**
  1524. * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
  1525. * @test: The test context object.
  1526. * @left: an arbitrary expression that evaluates to a null terminated string.
  1527. * @right: an arbitrary expression that evaluates to a null terminated string.
  1528. *
  1529. * Sets an assertion that the values that @left and @right evaluate to are
  1530. * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
  1531. * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1532. */
  1533. #define KUNIT_ASSERT_STREQ(test, left, right) \
  1534. KUNIT_BINARY_STR_EQ_ASSERTION(test, KUNIT_ASSERTION, left, right)
  1535. #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
  1536. KUNIT_BINARY_STR_EQ_MSG_ASSERTION(test, \
  1537. KUNIT_ASSERTION, \
  1538. left, \
  1539. right, \
  1540. fmt, \
  1541. ##__VA_ARGS__)
  1542. /**
  1543. * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
  1544. * @test: The test context object.
  1545. * @left: an arbitrary expression that evaluates to a null terminated string.
  1546. * @right: an arbitrary expression that evaluates to a null terminated string.
  1547. *
  1548. * Sets an expectation that the values that @left and @right evaluate to are
  1549. * not equal. This is semantically equivalent to
  1550. * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
  1551. * for more information.
  1552. */
  1553. #define KUNIT_ASSERT_STRNEQ(test, left, right) \
  1554. KUNIT_BINARY_STR_NE_ASSERTION(test, KUNIT_ASSERTION, left, right)
  1555. #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
  1556. KUNIT_BINARY_STR_NE_MSG_ASSERTION(test, \
  1557. KUNIT_ASSERTION, \
  1558. left, \
  1559. right, \
  1560. fmt, \
  1561. ##__VA_ARGS__)
  1562. /**
  1563. * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
  1564. * @test: The test context object.
  1565. * @ptr: an arbitrary pointer.
  1566. *
  1567. * Sets an assertion that the value that @ptr evaluates to is not null and not
  1568. * an errno stored in a pointer. This is the same as
  1569. * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
  1570. * KUNIT_ASSERT_TRUE()) when the assertion is not met.
  1571. */
  1572. #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
  1573. KUNIT_PTR_NOT_ERR_OR_NULL_ASSERTION(test, KUNIT_ASSERTION, ptr)
  1574. #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
  1575. KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
  1576. KUNIT_ASSERTION, \
  1577. ptr, \
  1578. fmt, \
  1579. ##__VA_ARGS__)
  1580. #endif /* _KUNIT_TEST_H */