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