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