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