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