Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Base unit test (KUnit) API.
0004  *
0005  * Copyright (C) 2019, Google LLC.
0006  * Author: Brendan Higgins <brendanhiggins@google.com>
0007  */
0008 
0009 #ifndef _KUNIT_TEST_H
0010 #define _KUNIT_TEST_H
0011 
0012 #include <kunit/assert.h>
0013 #include <kunit/try-catch.h>
0014 
0015 #include <linux/compiler.h>
0016 #include <linux/container_of.h>
0017 #include <linux/err.h>
0018 #include <linux/init.h>
0019 #include <linux/kconfig.h>
0020 #include <linux/kref.h>
0021 #include <linux/list.h>
0022 #include <linux/module.h>
0023 #include <linux/slab.h>
0024 #include <linux/spinlock.h>
0025 #include <linux/string.h>
0026 #include <linux/types.h>
0027 
0028 #include <asm/rwonce.h>
0029 
0030 struct kunit;
0031 
0032 /* Size of log associated with test. */
0033 #define KUNIT_LOG_SIZE  512
0034 
0035 /* Maximum size of parameter description string. */
0036 #define KUNIT_PARAM_DESC_SIZE 128
0037 
0038 /* Maximum size of a status comment. */
0039 #define KUNIT_STATUS_COMMENT_SIZE 256
0040 
0041 /*
0042  * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
0043  * sub-subtest.  See the "Subtests" section in
0044  * https://node-tap.org/tap-protocol/
0045  */
0046 #define KUNIT_SUBTEST_INDENT        "    "
0047 #define KUNIT_SUBSUBTEST_INDENT     "        "
0048 
0049 /**
0050  * enum kunit_status - Type of result for a test or test suite
0051  * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
0052  * @KUNIT_FAILURE: Denotes the test has failed.
0053  * @KUNIT_SKIPPED: Denotes the test has been skipped.
0054  */
0055 enum kunit_status {
0056     KUNIT_SUCCESS,
0057     KUNIT_FAILURE,
0058     KUNIT_SKIPPED,
0059 };
0060 
0061 /**
0062  * struct kunit_case - represents an individual test case.
0063  *
0064  * @run_case: the function representing the actual test case.
0065  * @name:     the name of the test case.
0066  * @generate_params: the generator function for parameterized tests.
0067  *
0068  * A test case is a function with the signature,
0069  * ``void (*)(struct kunit *)``
0070  * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
0071  * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
0072  * with a &struct kunit_suite and will be run after the suite's init
0073  * function and followed by the suite's exit function.
0074  *
0075  * A test case should be static and should only be created with the
0076  * KUNIT_CASE() macro; additionally, every array of test cases should be
0077  * terminated with an empty test case.
0078  *
0079  * Example:
0080  *
0081  * .. code-block:: c
0082  *
0083  *  void add_test_basic(struct kunit *test)
0084  *  {
0085  *      KUNIT_EXPECT_EQ(test, 1, add(1, 0));
0086  *      KUNIT_EXPECT_EQ(test, 2, add(1, 1));
0087  *      KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
0088  *      KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
0089  *      KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
0090  *  }
0091  *
0092  *  static struct kunit_case example_test_cases[] = {
0093  *      KUNIT_CASE(add_test_basic),
0094  *      {}
0095  *  };
0096  *
0097  */
0098 struct kunit_case {
0099     void (*run_case)(struct kunit *test);
0100     const char *name;
0101     const void* (*generate_params)(const void *prev, char *desc);
0102 
0103     /* private: internal use only. */
0104     enum kunit_status status;
0105     char *log;
0106 };
0107 
0108 static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
0109 {
0110     switch (status) {
0111     case KUNIT_SKIPPED:
0112     case KUNIT_SUCCESS:
0113         return "ok";
0114     case KUNIT_FAILURE:
0115         return "not ok";
0116     }
0117     return "invalid";
0118 }
0119 
0120 /**
0121  * KUNIT_CASE - A helper for creating a &struct kunit_case
0122  *
0123  * @test_name: a reference to a test case function.
0124  *
0125  * Takes a symbol for a function representing a test case and creates a
0126  * &struct kunit_case object from it. See the documentation for
0127  * &struct kunit_case for an example on how to use it.
0128  */
0129 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
0130 
0131 /**
0132  * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
0133  *
0134  * @test_name: a reference to a test case function.
0135  * @gen_params: a reference to a parameter generator function.
0136  *
0137  * The generator function::
0138  *
0139  *  const void* gen_params(const void *prev, char *desc)
0140  *
0141  * is used to lazily generate a series of arbitrarily typed values that fit into
0142  * a void*. The argument @prev is the previously returned value, which should be
0143  * used to derive the next value; @prev is set to NULL on the initial generator
0144  * call. When no more values are available, the generator must return NULL.
0145  * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
0146  * describing the parameter.
0147  */
0148 #define KUNIT_CASE_PARAM(test_name, gen_params)         \
0149         { .run_case = test_name, .name = #test_name,    \
0150           .generate_params = gen_params }
0151 
0152 /**
0153  * struct kunit_suite - describes a related collection of &struct kunit_case
0154  *
0155  * @name:   the name of the test. Purely informational.
0156  * @suite_init: called once per test suite before the test cases.
0157  * @suite_exit: called once per test suite after all test cases.
0158  * @init:   called before every test case.
0159  * @exit:   called after every test case.
0160  * @test_cases: a null terminated array of test cases.
0161  *
0162  * A kunit_suite is a collection of related &struct kunit_case s, such that
0163  * @init is called before every test case and @exit is called after every
0164  * test case, similar to the notion of a *test fixture* or a *test class*
0165  * in other unit testing frameworks like JUnit or Googletest.
0166  *
0167  * Every &struct kunit_case must be associated with a kunit_suite for KUnit
0168  * to run it.
0169  */
0170 struct kunit_suite {
0171     const char name[256];
0172     int (*suite_init)(struct kunit_suite *suite);
0173     void (*suite_exit)(struct kunit_suite *suite);
0174     int (*init)(struct kunit *test);
0175     void (*exit)(struct kunit *test);
0176     struct kunit_case *test_cases;
0177 
0178     /* private: internal use only */
0179     char status_comment[KUNIT_STATUS_COMMENT_SIZE];
0180     struct dentry *debugfs;
0181     char *log;
0182     int suite_init_err;
0183 };
0184 
0185 /**
0186  * struct kunit - represents a running instance of a test.
0187  *
0188  * @priv: for user to store arbitrary data. Commonly used to pass data
0189  *    created in the init function (see &struct kunit_suite).
0190  *
0191  * Used to store information about the current context under which the test
0192  * is running. Most of this data is private and should only be accessed
0193  * indirectly via public functions; the one exception is @priv which can be
0194  * used by the test writer to store arbitrary data.
0195  */
0196 struct kunit {
0197     void *priv;
0198 
0199     /* private: internal use only. */
0200     const char *name; /* Read only after initialization! */
0201     char *log; /* Points at case log after initialization */
0202     struct kunit_try_catch try_catch;
0203     /* param_value is the current parameter value for a test case. */
0204     const void *param_value;
0205     /* param_index stores the index of the parameter in parameterized tests. */
0206     int param_index;
0207     /*
0208      * success starts as true, and may only be set to false during a
0209      * test case; thus, it is safe to update this across multiple
0210      * threads using WRITE_ONCE; however, as a consequence, it may only
0211      * be read after the test case finishes once all threads associated
0212      * with the test case have terminated.
0213      */
0214     spinlock_t lock; /* Guards all mutable test state. */
0215     enum kunit_status status; /* Read only after test_case finishes! */
0216     /*
0217      * Because resources is a list that may be updated multiple times (with
0218      * new resources) from any thread associated with a test case, we must
0219      * protect it with some type of lock.
0220      */
0221     struct list_head resources; /* Protected by lock. */
0222 
0223     char status_comment[KUNIT_STATUS_COMMENT_SIZE];
0224 };
0225 
0226 static inline void kunit_set_failure(struct kunit *test)
0227 {
0228     WRITE_ONCE(test->status, KUNIT_FAILURE);
0229 }
0230 
0231 void kunit_init_test(struct kunit *test, const char *name, char *log);
0232 
0233 int kunit_run_tests(struct kunit_suite *suite);
0234 
0235 size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
0236 
0237 unsigned int kunit_test_case_num(struct kunit_suite *suite,
0238                  struct kunit_case *test_case);
0239 
0240 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
0241 
0242 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
0243 
0244 #if IS_BUILTIN(CONFIG_KUNIT)
0245 int kunit_run_all_tests(void);
0246 #else
0247 static inline int kunit_run_all_tests(void)
0248 {
0249     return 0;
0250 }
0251 #endif /* IS_BUILTIN(CONFIG_KUNIT) */
0252 
0253 #define __kunit_test_suites(unique_array, ...)                     \
0254     MODULE_INFO(test, "Y");                            \
0255     static struct kunit_suite *unique_array[]                  \
0256     __aligned(sizeof(struct kunit_suite *))                    \
0257     __used __section(".kunit_test_suites") = { __VA_ARGS__ }
0258 
0259 /**
0260  * kunit_test_suites() - used to register one or more &struct kunit_suite
0261  *           with KUnit.
0262  *
0263  * @__suites: a statically allocated list of &struct kunit_suite.
0264  *
0265  * Registers @suites with the test framework.
0266  * This is done by placing the array of struct kunit_suite * in the
0267  * .kunit_test_suites ELF section.
0268  *
0269  * When builtin, KUnit tests are all run via the executor at boot, and when
0270  * built as a module, they run on module load.
0271  *
0272  */
0273 #define kunit_test_suites(__suites...)                      \
0274     __kunit_test_suites(__UNIQUE_ID(array),             \
0275                 ##__suites)
0276 
0277 #define kunit_test_suite(suite) kunit_test_suites(&suite)
0278 
0279 /**
0280  * kunit_test_init_section_suites() - used to register one or more &struct
0281  *                    kunit_suite containing init functions or
0282  *                    init data.
0283  *
0284  * @__suites: a statically allocated list of &struct kunit_suite.
0285  *
0286  * This functions identically as kunit_test_suites() except that it suppresses
0287  * modpost warnings for referencing functions marked __init or data marked
0288  * __initdata; this is OK because currently KUnit only runs tests upon boot
0289  * during the init phase or upon loading a module during the init phase.
0290  *
0291  * NOTE TO KUNIT DEVS: If we ever allow KUnit tests to be run after boot, these
0292  * tests must be excluded.
0293  *
0294  * The only thing this macro does that's different from kunit_test_suites is
0295  * that it suffixes the array and suite declarations it makes with _probe;
0296  * modpost suppresses warnings about referencing init data for symbols named in
0297  * this manner.
0298  */
0299 #define kunit_test_init_section_suites(__suites...)         \
0300     __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe),    \
0301                 CONCATENATE(__UNIQUE_ID(suites), _probe),   \
0302                 ##__suites)
0303 
0304 #define kunit_test_init_section_suite(suite)    \
0305     kunit_test_init_section_suites(&suite)
0306 
0307 #define kunit_suite_for_each_test_case(suite, test_case)        \
0308     for (test_case = suite->test_cases; test_case->run_case; test_case++)
0309 
0310 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
0311 
0312 /**
0313  * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
0314  * @test: The test context object.
0315  * @n: number of elements.
0316  * @size: The size in bytes of the desired memory.
0317  * @gfp: flags passed to underlying kmalloc().
0318  *
0319  * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
0320  * and is automatically cleaned up after the test case concludes. See &struct
0321  * kunit_resource for more information.
0322  */
0323 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
0324 
0325 /**
0326  * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
0327  * @test: The test context object.
0328  * @size: The size in bytes of the desired memory.
0329  * @gfp: flags passed to underlying kmalloc().
0330  *
0331  * See kmalloc() and kunit_kmalloc_array() for more information.
0332  */
0333 static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
0334 {
0335     return kunit_kmalloc_array(test, 1, size, gfp);
0336 }
0337 
0338 /**
0339  * kunit_kfree() - Like kfree except for allocations managed by KUnit.
0340  * @test: The test case to which the resource belongs.
0341  * @ptr: The memory allocation to free.
0342  */
0343 void kunit_kfree(struct kunit *test, const void *ptr);
0344 
0345 /**
0346  * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
0347  * @test: The test context object.
0348  * @size: The size in bytes of the desired memory.
0349  * @gfp: flags passed to underlying kmalloc().
0350  *
0351  * See kzalloc() and kunit_kmalloc_array() for more information.
0352  */
0353 static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
0354 {
0355     return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
0356 }
0357 
0358 /**
0359  * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
0360  * @test: The test context object.
0361  * @n: number of elements.
0362  * @size: The size in bytes of the desired memory.
0363  * @gfp: flags passed to underlying kmalloc().
0364  *
0365  * See kcalloc() and kunit_kmalloc_array() for more information.
0366  */
0367 static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
0368 {
0369     return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
0370 }
0371 
0372 void kunit_cleanup(struct kunit *test);
0373 
0374 void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
0375 
0376 /**
0377  * kunit_mark_skipped() - Marks @test_or_suite as skipped
0378  *
0379  * @test_or_suite: The test context object.
0380  * @fmt:  A printk() style format string.
0381  *
0382  * Marks the test as skipped. @fmt is given output as the test status
0383  * comment, typically the reason the test was skipped.
0384  *
0385  * Test execution continues after kunit_mark_skipped() is called.
0386  */
0387 #define kunit_mark_skipped(test_or_suite, fmt, ...)         \
0388     do {                                \
0389         WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
0390         scnprintf((test_or_suite)->status_comment,      \
0391               KUNIT_STATUS_COMMENT_SIZE,            \
0392               fmt, ##__VA_ARGS__);              \
0393     } while (0)
0394 
0395 /**
0396  * kunit_skip() - Marks @test_or_suite as skipped
0397  *
0398  * @test_or_suite: The test context object.
0399  * @fmt:  A printk() style format string.
0400  *
0401  * Skips the test. @fmt is given output as the test status
0402  * comment, typically the reason the test was skipped.
0403  *
0404  * Test execution is halted after kunit_skip() is called.
0405  */
0406 #define kunit_skip(test_or_suite, fmt, ...)             \
0407     do {                                \
0408         kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
0409         kunit_try_catch_throw(&((test_or_suite)->try_catch));   \
0410     } while (0)
0411 
0412 /*
0413  * printk and log to per-test or per-suite log buffer.  Logging only done
0414  * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
0415  */
0416 #define kunit_log(lvl, test_or_suite, fmt, ...)             \
0417     do {                                \
0418         printk(lvl fmt, ##__VA_ARGS__);             \
0419         kunit_log_append((test_or_suite)->log,  fmt "\n",   \
0420                  ##__VA_ARGS__);            \
0421     } while (0)
0422 
0423 #define kunit_printk(lvl, test, fmt, ...)               \
0424     kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt,     \
0425           (test)->name, ##__VA_ARGS__)
0426 
0427 /**
0428  * kunit_info() - Prints an INFO level message associated with @test.
0429  *
0430  * @test: The test context object.
0431  * @fmt:  A printk() style format string.
0432  *
0433  * Prints an info level message associated with the test suite being run.
0434  * Takes a variable number of format parameters just like printk().
0435  */
0436 #define kunit_info(test, fmt, ...) \
0437     kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
0438 
0439 /**
0440  * kunit_warn() - Prints a WARN level message associated with @test.
0441  *
0442  * @test: The test context object.
0443  * @fmt:  A printk() style format string.
0444  *
0445  * Prints a warning level message.
0446  */
0447 #define kunit_warn(test, fmt, ...) \
0448     kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
0449 
0450 /**
0451  * kunit_err() - Prints an ERROR level message associated with @test.
0452  *
0453  * @test: The test context object.
0454  * @fmt:  A printk() style format string.
0455  *
0456  * Prints an error level message.
0457  */
0458 #define kunit_err(test, fmt, ...) \
0459     kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
0460 
0461 /**
0462  * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
0463  * @test: The test context object.
0464  *
0465  * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
0466  * words, it does nothing and only exists for code clarity. See
0467  * KUNIT_EXPECT_TRUE() for more information.
0468  */
0469 #define KUNIT_SUCCEED(test) do {} while (0)
0470 
0471 void kunit_do_failed_assertion(struct kunit *test,
0472                    const struct kunit_loc *loc,
0473                    enum kunit_assert_type type,
0474                    const struct kunit_assert *assert,
0475                    const char *fmt, ...);
0476 
0477 #define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \
0478     if (unlikely(!(pass))) {                           \
0479         static const struct kunit_loc __loc = KUNIT_CURRENT_LOC;       \
0480         struct assert_class __assertion = INITIALIZER;             \
0481         kunit_do_failed_assertion(test,                    \
0482                       &__loc,                  \
0483                       assert_type,                 \
0484                       &__assertion.assert,             \
0485                       fmt,                     \
0486                       ##__VA_ARGS__);              \
0487     }                                      \
0488 } while (0)
0489 
0490 
0491 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...)              \
0492     KUNIT_ASSERTION(test,                              \
0493             assert_type,                           \
0494             false,                             \
0495             kunit_fail_assert,                     \
0496             KUNIT_INIT_FAIL_ASSERT_STRUCT,                 \
0497             fmt,                               \
0498             ##__VA_ARGS__)
0499 
0500 /**
0501  * KUNIT_FAIL() - Always causes a test to fail when evaluated.
0502  * @test: The test context object.
0503  * @fmt: an informational message to be printed when the assertion is made.
0504  * @...: string format arguments.
0505  *
0506  * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
0507  * other words, it always results in a failed expectation, and consequently
0508  * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
0509  * for more information.
0510  */
0511 #define KUNIT_FAIL(test, fmt, ...)                         \
0512     KUNIT_FAIL_ASSERTION(test,                         \
0513                  KUNIT_EXPECTATION,                    \
0514                  fmt,                          \
0515                  ##__VA_ARGS__)
0516 
0517 #define KUNIT_UNARY_ASSERTION(test,                        \
0518                   assert_type,                     \
0519                   condition,                       \
0520                   expected_true,                       \
0521                   fmt,                         \
0522                   ...)                         \
0523     KUNIT_ASSERTION(test,                              \
0524             assert_type,                           \
0525             !!(condition) == !!expected_true,              \
0526             kunit_unary_assert,                    \
0527             KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition,         \
0528                                expected_true),         \
0529             fmt,                               \
0530             ##__VA_ARGS__)
0531 
0532 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)       \
0533     KUNIT_UNARY_ASSERTION(test,                        \
0534                   assert_type,                     \
0535                   condition,                       \
0536                   true,                        \
0537                   fmt,                         \
0538                   ##__VA_ARGS__)
0539 
0540 #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...)      \
0541     KUNIT_UNARY_ASSERTION(test,                        \
0542                   assert_type,                     \
0543                   condition,                       \
0544                   false,                           \
0545                   fmt,                         \
0546                   ##__VA_ARGS__)
0547 
0548 /*
0549  * A factory macro for defining the assertions and expectations for the basic
0550  * comparisons defined for the built in types.
0551  *
0552  * Unfortunately, there is no common type that all types can be promoted to for
0553  * which all the binary operators behave the same way as for the actual types
0554  * (for example, there is no type that long long and unsigned long long can
0555  * both be cast to where the comparison result is preserved for all values). So
0556  * the best we can do is do the comparison in the original types and then coerce
0557  * everything to long long for printing; this way, the comparison behaves
0558  * correctly and the printed out value usually makes sense without
0559  * interpretation, but can always be interpreted to figure out the actual
0560  * value.
0561  */
0562 #define KUNIT_BASE_BINARY_ASSERTION(test,                      \
0563                     assert_class,                  \
0564                     format_func,                   \
0565                     assert_type,                   \
0566                     left,                      \
0567                     op,                        \
0568                     right,                     \
0569                     fmt,                       \
0570                     ...)                       \
0571 do {                                           \
0572     const typeof(left) __left = (left);                    \
0573     const typeof(right) __right = (right);                     \
0574     static const struct kunit_binary_assert_text __text = {            \
0575         .operation = #op,                          \
0576         .left_text = #left,                        \
0577         .right_text = #right,                          \
0578     };                                     \
0579                                            \
0580     KUNIT_ASSERTION(test,                              \
0581             assert_type,                           \
0582             __left op __right,                     \
0583             assert_class,                          \
0584             KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func,           \
0585                             &__text,           \
0586                             __left,            \
0587                             __right),          \
0588             fmt,                               \
0589             ##__VA_ARGS__);                        \
0590 } while (0)
0591 
0592 #define KUNIT_BINARY_INT_ASSERTION(test,                       \
0593                    assert_type,                    \
0594                    left,                       \
0595                    op,                         \
0596                    right,                      \
0597                    fmt,                        \
0598                     ...)                       \
0599     KUNIT_BASE_BINARY_ASSERTION(test,                      \
0600                     kunit_binary_assert,               \
0601                     kunit_binary_assert_format,            \
0602                     assert_type,                   \
0603                     left, op, right,                   \
0604                     fmt,                       \
0605                     ##__VA_ARGS__)
0606 
0607 #define KUNIT_BINARY_PTR_ASSERTION(test,                       \
0608                    assert_type,                    \
0609                    left,                       \
0610                    op,                         \
0611                    right,                      \
0612                    fmt,                        \
0613                     ...)                       \
0614     KUNIT_BASE_BINARY_ASSERTION(test,                      \
0615                     kunit_binary_ptr_assert,               \
0616                     kunit_binary_ptr_assert_format,        \
0617                     assert_type,                   \
0618                     left, op, right,                   \
0619                     fmt,                       \
0620                     ##__VA_ARGS__)
0621 
0622 #define KUNIT_BINARY_STR_ASSERTION(test,                       \
0623                    assert_type,                    \
0624                    left,                       \
0625                    op,                         \
0626                    right,                      \
0627                    fmt,                        \
0628                    ...)                        \
0629 do {                                           \
0630     const char *__left = (left);                           \
0631     const char *__right = (right);                         \
0632     static const struct kunit_binary_assert_text __text = {            \
0633         .operation = #op,                          \
0634         .left_text = #left,                        \
0635         .right_text = #right,                          \
0636     };                                     \
0637                                            \
0638     KUNIT_ASSERTION(test,                              \
0639             assert_type,                           \
0640             strcmp(__left, __right) op 0,                  \
0641             kunit_binary_str_assert,                   \
0642             KUNIT_INIT_BINARY_ASSERT_STRUCT(kunit_binary_str_assert_format,\
0643                             &__text,           \
0644                             __left,            \
0645                             __right),          \
0646             fmt,                               \
0647             ##__VA_ARGS__);                        \
0648 } while (0)
0649 
0650 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,                  \
0651                         assert_type,               \
0652                         ptr,                   \
0653                         fmt,                   \
0654                         ...)                   \
0655 do {                                           \
0656     const typeof(ptr) __ptr = (ptr);                       \
0657                                            \
0658     KUNIT_ASSERTION(test,                              \
0659             assert_type,                           \
0660             !IS_ERR_OR_NULL(__ptr),                    \
0661             kunit_ptr_not_err_assert,                  \
0662             KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr,            \
0663                               __ptr),              \
0664             fmt,                               \
0665             ##__VA_ARGS__);                        \
0666 } while (0)
0667 
0668 /**
0669  * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
0670  * @test: The test context object.
0671  * @condition: an arbitrary boolean expression. The test fails when this does
0672  * not evaluate to true.
0673  *
0674  * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
0675  * to fail when the specified condition is not met; however, it will not prevent
0676  * the test case from continuing to run; this is otherwise known as an
0677  * *expectation failure*.
0678  */
0679 #define KUNIT_EXPECT_TRUE(test, condition) \
0680     KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
0681 
0682 #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...)               \
0683     KUNIT_TRUE_MSG_ASSERTION(test,                         \
0684                  KUNIT_EXPECTATION,                \
0685                  condition,                    \
0686                  fmt,                          \
0687                  ##__VA_ARGS__)
0688 
0689 /**
0690  * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
0691  * @test: The test context object.
0692  * @condition: an arbitrary boolean expression. The test fails when this does
0693  * not evaluate to false.
0694  *
0695  * Sets an expectation that @condition evaluates to false. See
0696  * KUNIT_EXPECT_TRUE() for more information.
0697  */
0698 #define KUNIT_EXPECT_FALSE(test, condition) \
0699     KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
0700 
0701 #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...)              \
0702     KUNIT_FALSE_MSG_ASSERTION(test,                        \
0703                   KUNIT_EXPECTATION,                   \
0704                   condition,                       \
0705                   fmt,                         \
0706                   ##__VA_ARGS__)
0707 
0708 /**
0709  * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
0710  * @test: The test context object.
0711  * @left: an arbitrary expression that evaluates to a primitive C type.
0712  * @right: an arbitrary expression that evaluates to a primitive C type.
0713  *
0714  * Sets an expectation that the values that @left and @right evaluate to are
0715  * equal. This is semantically equivalent to
0716  * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
0717  * more information.
0718  */
0719 #define KUNIT_EXPECT_EQ(test, left, right) \
0720     KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
0721 
0722 #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...)               \
0723     KUNIT_BINARY_INT_ASSERTION(test,                       \
0724                    KUNIT_EXPECTATION,                  \
0725                    left, ==, right,                \
0726                    fmt,                        \
0727                     ##__VA_ARGS__)
0728 
0729 /**
0730  * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
0731  * @test: The test context object.
0732  * @left: an arbitrary expression that evaluates to a pointer.
0733  * @right: an arbitrary expression that evaluates to a pointer.
0734  *
0735  * Sets an expectation that the values that @left and @right evaluate to are
0736  * equal. This is semantically equivalent to
0737  * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
0738  * more information.
0739  */
0740 #define KUNIT_EXPECT_PTR_EQ(test, left, right)                     \
0741     KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
0742 
0743 #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...)               \
0744     KUNIT_BINARY_PTR_ASSERTION(test,                       \
0745                    KUNIT_EXPECTATION,                  \
0746                    left, ==, right,                \
0747                    fmt,                        \
0748                    ##__VA_ARGS__)
0749 
0750 /**
0751  * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
0752  * @test: The test context object.
0753  * @left: an arbitrary expression that evaluates to a primitive C type.
0754  * @right: an arbitrary expression that evaluates to a primitive C type.
0755  *
0756  * Sets an expectation that the values that @left and @right evaluate to are not
0757  * equal. This is semantically equivalent to
0758  * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
0759  * more information.
0760  */
0761 #define KUNIT_EXPECT_NE(test, left, right) \
0762     KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
0763 
0764 #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...)               \
0765     KUNIT_BINARY_INT_ASSERTION(test,                       \
0766                    KUNIT_EXPECTATION,                  \
0767                    left, !=, right,                \
0768                    fmt,                        \
0769                     ##__VA_ARGS__)
0770 
0771 /**
0772  * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
0773  * @test: The test context object.
0774  * @left: an arbitrary expression that evaluates to a pointer.
0775  * @right: an arbitrary expression that evaluates to a pointer.
0776  *
0777  * Sets an expectation that the values that @left and @right evaluate to are not
0778  * equal. This is semantically equivalent to
0779  * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
0780  * more information.
0781  */
0782 #define KUNIT_EXPECT_PTR_NE(test, left, right)                     \
0783     KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
0784 
0785 #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...)               \
0786     KUNIT_BINARY_PTR_ASSERTION(test,                       \
0787                    KUNIT_EXPECTATION,                  \
0788                    left, !=, right,                \
0789                    fmt,                        \
0790                    ##__VA_ARGS__)
0791 
0792 /**
0793  * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
0794  * @test: The test context object.
0795  * @left: an arbitrary expression that evaluates to a primitive C type.
0796  * @right: an arbitrary expression that evaluates to a primitive C type.
0797  *
0798  * Sets an expectation that the value that @left evaluates to is less than the
0799  * value that @right evaluates to. This is semantically equivalent to
0800  * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
0801  * more information.
0802  */
0803 #define KUNIT_EXPECT_LT(test, left, right) \
0804     KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
0805 
0806 #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...)               \
0807     KUNIT_BINARY_INT_ASSERTION(test,                       \
0808                    KUNIT_EXPECTATION,                  \
0809                    left, <, right,                 \
0810                    fmt,                        \
0811                     ##__VA_ARGS__)
0812 
0813 /**
0814  * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
0815  * @test: The test context object.
0816  * @left: an arbitrary expression that evaluates to a primitive C type.
0817  * @right: an arbitrary expression that evaluates to a primitive C type.
0818  *
0819  * Sets an expectation that the value that @left evaluates to is less than or
0820  * equal to the value that @right evaluates to. Semantically this is equivalent
0821  * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
0822  * more information.
0823  */
0824 #define KUNIT_EXPECT_LE(test, left, right) \
0825     KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
0826 
0827 #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...)               \
0828     KUNIT_BINARY_INT_ASSERTION(test,                       \
0829                    KUNIT_EXPECTATION,                  \
0830                    left, <=, right,                \
0831                    fmt,                        \
0832                     ##__VA_ARGS__)
0833 
0834 /**
0835  * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
0836  * @test: The test context object.
0837  * @left: an arbitrary expression that evaluates to a primitive C type.
0838  * @right: an arbitrary expression that evaluates to a primitive C type.
0839  *
0840  * Sets an expectation that the value that @left evaluates to is greater than
0841  * the value that @right evaluates to. This is semantically equivalent to
0842  * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
0843  * more information.
0844  */
0845 #define KUNIT_EXPECT_GT(test, left, right) \
0846     KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
0847 
0848 #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...)               \
0849     KUNIT_BINARY_INT_ASSERTION(test,                       \
0850                    KUNIT_EXPECTATION,                  \
0851                    left, >, right,                 \
0852                    fmt,                        \
0853                     ##__VA_ARGS__)
0854 
0855 /**
0856  * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
0857  * @test: The test context object.
0858  * @left: an arbitrary expression that evaluates to a primitive C type.
0859  * @right: an arbitrary expression that evaluates to a primitive C type.
0860  *
0861  * Sets an expectation that the value that @left evaluates to is greater than
0862  * the value that @right evaluates to. This is semantically equivalent to
0863  * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
0864  * more information.
0865  */
0866 #define KUNIT_EXPECT_GE(test, left, right) \
0867     KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
0868 
0869 #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...)               \
0870     KUNIT_BINARY_INT_ASSERTION(test,                       \
0871                    KUNIT_EXPECTATION,                  \
0872                    left, >=, right,                \
0873                    fmt,                        \
0874                     ##__VA_ARGS__)
0875 
0876 /**
0877  * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
0878  * @test: The test context object.
0879  * @left: an arbitrary expression that evaluates to a null terminated string.
0880  * @right: an arbitrary expression that evaluates to a null terminated string.
0881  *
0882  * Sets an expectation that the values that @left and @right evaluate to are
0883  * equal. This is semantically equivalent to
0884  * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
0885  * for more information.
0886  */
0887 #define KUNIT_EXPECT_STREQ(test, left, right) \
0888     KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
0889 
0890 #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...)            \
0891     KUNIT_BINARY_STR_ASSERTION(test,                       \
0892                    KUNIT_EXPECTATION,                  \
0893                    left, ==, right,                \
0894                    fmt,                        \
0895                    ##__VA_ARGS__)
0896 
0897 /**
0898  * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
0899  * @test: The test context object.
0900  * @left: an arbitrary expression that evaluates to a null terminated string.
0901  * @right: an arbitrary expression that evaluates to a null terminated string.
0902  *
0903  * Sets an expectation that the values that @left and @right evaluate to are
0904  * not equal. This is semantically equivalent to
0905  * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
0906  * for more information.
0907  */
0908 #define KUNIT_EXPECT_STRNEQ(test, left, right) \
0909     KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
0910 
0911 #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...)               \
0912     KUNIT_BINARY_STR_ASSERTION(test,                       \
0913                    KUNIT_EXPECTATION,                  \
0914                    left, !=, right,                \
0915                    fmt,                        \
0916                    ##__VA_ARGS__)
0917 
0918 /**
0919  * KUNIT_EXPECT_NULL() - Expects that @ptr is null.
0920  * @test: The test context object.
0921  * @ptr: an arbitrary pointer.
0922  *
0923  * Sets an expectation that the value that @ptr evaluates to is null. This is
0924  * semantically equivalent to KUNIT_EXPECT_PTR_EQ(@test, ptr, NULL).
0925  * See KUNIT_EXPECT_TRUE() for more information.
0926  */
0927 #define KUNIT_EXPECT_NULL(test, ptr)                               \
0928     KUNIT_EXPECT_NULL_MSG(test,                        \
0929                   ptr,                         \
0930                   NULL)
0931 
0932 #define KUNIT_EXPECT_NULL_MSG(test, ptr, fmt, ...)                         \
0933     KUNIT_BINARY_PTR_ASSERTION(test,                       \
0934                    KUNIT_EXPECTATION,                  \
0935                    ptr, ==, NULL,                  \
0936                    fmt,                        \
0937                    ##__VA_ARGS__)
0938 
0939 /**
0940  * KUNIT_EXPECT_NOT_NULL() - Expects that @ptr is not null.
0941  * @test: The test context object.
0942  * @ptr: an arbitrary pointer.
0943  *
0944  * Sets an expectation that the value that @ptr evaluates to is not null. This
0945  * is semantically equivalent to KUNIT_EXPECT_PTR_NE(@test, ptr, NULL).
0946  * See KUNIT_EXPECT_TRUE() for more information.
0947  */
0948 #define KUNIT_EXPECT_NOT_NULL(test, ptr)                           \
0949     KUNIT_EXPECT_NOT_NULL_MSG(test,                        \
0950                   ptr,                         \
0951                   NULL)
0952 
0953 #define KUNIT_EXPECT_NOT_NULL_MSG(test, ptr, fmt, ...)                         \
0954     KUNIT_BINARY_PTR_ASSERTION(test,                       \
0955                    KUNIT_EXPECTATION,                  \
0956                    ptr, !=, NULL,                  \
0957                    fmt,                        \
0958                    ##__VA_ARGS__)
0959 
0960 /**
0961  * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
0962  * @test: The test context object.
0963  * @ptr: an arbitrary pointer.
0964  *
0965  * Sets an expectation that the value that @ptr evaluates to is not null and not
0966  * an errno stored in a pointer. This is semantically equivalent to
0967  * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
0968  * more information.
0969  */
0970 #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
0971     KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
0972 
0973 #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)              \
0974     KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,                  \
0975                         KUNIT_EXPECTATION,         \
0976                         ptr,                   \
0977                         fmt,                   \
0978                         ##__VA_ARGS__)
0979 
0980 #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
0981     KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
0982 
0983 /**
0984  * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
0985  * @test: The test context object.
0986  * @condition: an arbitrary boolean expression. The test fails and aborts when
0987  * this does not evaluate to true.
0988  *
0989  * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
0990  * fail *and immediately abort* when the specified condition is not met. Unlike
0991  * an expectation failure, it will prevent the test case from continuing to run;
0992  * this is otherwise known as an *assertion failure*.
0993  */
0994 #define KUNIT_ASSERT_TRUE(test, condition) \
0995     KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
0996 
0997 #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...)               \
0998     KUNIT_TRUE_MSG_ASSERTION(test,                         \
0999                  KUNIT_ASSERTION,                  \
1000                  condition,                    \
1001                  fmt,                          \
1002                  ##__VA_ARGS__)
1003 
1004 /**
1005  * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
1006  * @test: The test context object.
1007  * @condition: an arbitrary boolean expression.
1008  *
1009  * Sets an assertion that the value that @condition evaluates to is false. This
1010  * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
1011  * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1012  */
1013 #define KUNIT_ASSERT_FALSE(test, condition) \
1014     KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
1015 
1016 #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...)              \
1017     KUNIT_FALSE_MSG_ASSERTION(test,                        \
1018                   KUNIT_ASSERTION,                 \
1019                   condition,                       \
1020                   fmt,                         \
1021                   ##__VA_ARGS__)
1022 
1023 /**
1024  * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
1025  * @test: The test context object.
1026  * @left: an arbitrary expression that evaluates to a primitive C type.
1027  * @right: an arbitrary expression that evaluates to a primitive C type.
1028  *
1029  * Sets an assertion that the values that @left and @right evaluate to are
1030  * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1031  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1032  */
1033 #define KUNIT_ASSERT_EQ(test, left, right) \
1034     KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
1035 
1036 #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...)               \
1037     KUNIT_BINARY_INT_ASSERTION(test,                       \
1038                    KUNIT_ASSERTION,                \
1039                    left, ==, right,                \
1040                    fmt,                        \
1041                     ##__VA_ARGS__)
1042 
1043 /**
1044  * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1045  * @test: The test context object.
1046  * @left: an arbitrary expression that evaluates to a pointer.
1047  * @right: an arbitrary expression that evaluates to a pointer.
1048  *
1049  * Sets an assertion that the values that @left and @right evaluate to are
1050  * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
1051  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1052  */
1053 #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
1054     KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
1055 
1056 #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...)               \
1057     KUNIT_BINARY_PTR_ASSERTION(test,                       \
1058                    KUNIT_ASSERTION,                \
1059                    left, ==, right,                \
1060                    fmt,                        \
1061                    ##__VA_ARGS__)
1062 
1063 /**
1064  * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
1065  * @test: The test context object.
1066  * @left: an arbitrary expression that evaluates to a primitive C type.
1067  * @right: an arbitrary expression that evaluates to a primitive C type.
1068  *
1069  * Sets an assertion that the values that @left and @right evaluate to are not
1070  * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1071  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1072  */
1073 #define KUNIT_ASSERT_NE(test, left, right) \
1074     KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
1075 
1076 #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...)               \
1077     KUNIT_BINARY_INT_ASSERTION(test,                       \
1078                    KUNIT_ASSERTION,                \
1079                    left, !=, right,                \
1080                    fmt,                        \
1081                     ##__VA_ARGS__)
1082 
1083 /**
1084  * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1085  * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1086  * @test: The test context object.
1087  * @left: an arbitrary expression that evaluates to a pointer.
1088  * @right: an arbitrary expression that evaluates to a pointer.
1089  *
1090  * Sets an assertion that the values that @left and @right evaluate to are not
1091  * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1092  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1093  */
1094 #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1095     KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
1096 
1097 #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...)               \
1098     KUNIT_BINARY_PTR_ASSERTION(test,                       \
1099                    KUNIT_ASSERTION,                \
1100                    left, !=, right,                \
1101                    fmt,                        \
1102                    ##__VA_ARGS__)
1103 /**
1104  * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1105  * @test: The test context object.
1106  * @left: an arbitrary expression that evaluates to a primitive C type.
1107  * @right: an arbitrary expression that evaluates to a primitive C type.
1108  *
1109  * Sets an assertion that the value that @left evaluates to is less than the
1110  * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1111  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1112  * is not met.
1113  */
1114 #define KUNIT_ASSERT_LT(test, left, right) \
1115     KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
1116 
1117 #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...)               \
1118     KUNIT_BINARY_INT_ASSERTION(test,                       \
1119                    KUNIT_ASSERTION,                \
1120                    left, <, right,                 \
1121                    fmt,                        \
1122                     ##__VA_ARGS__)
1123 /**
1124  * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1125  * @test: The test context object.
1126  * @left: an arbitrary expression that evaluates to a primitive C type.
1127  * @right: an arbitrary expression that evaluates to a primitive C type.
1128  *
1129  * Sets an assertion that the value that @left evaluates to is less than or
1130  * equal to the value that @right evaluates to. This is the same as
1131  * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1132  * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1133  */
1134 #define KUNIT_ASSERT_LE(test, left, right) \
1135     KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
1136 
1137 #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...)               \
1138     KUNIT_BINARY_INT_ASSERTION(test,                       \
1139                    KUNIT_ASSERTION,                \
1140                    left, <=, right,                \
1141                    fmt,                        \
1142                     ##__VA_ARGS__)
1143 
1144 /**
1145  * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1146  * @test: The test context object.
1147  * @left: an arbitrary expression that evaluates to a primitive C type.
1148  * @right: an arbitrary expression that evaluates to a primitive C type.
1149  *
1150  * Sets an assertion that the value that @left evaluates to is greater than the
1151  * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1152  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1153  * is not met.
1154  */
1155 #define KUNIT_ASSERT_GT(test, left, right) \
1156     KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
1157 
1158 #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...)               \
1159     KUNIT_BINARY_INT_ASSERTION(test,                       \
1160                    KUNIT_ASSERTION,                \
1161                    left, >, right,                 \
1162                    fmt,                        \
1163                     ##__VA_ARGS__)
1164 
1165 /**
1166  * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1167  * @test: The test context object.
1168  * @left: an arbitrary expression that evaluates to a primitive C type.
1169  * @right: an arbitrary expression that evaluates to a primitive C type.
1170  *
1171  * Sets an assertion that the value that @left evaluates to is greater than the
1172  * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1173  * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1174  * is not met.
1175  */
1176 #define KUNIT_ASSERT_GE(test, left, right) \
1177     KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
1178 
1179 #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...)               \
1180     KUNIT_BINARY_INT_ASSERTION(test,                       \
1181                    KUNIT_ASSERTION,                \
1182                    left, >=, right,                \
1183                    fmt,                        \
1184                     ##__VA_ARGS__)
1185 
1186 /**
1187  * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1188  * @test: The test context object.
1189  * @left: an arbitrary expression that evaluates to a null terminated string.
1190  * @right: an arbitrary expression that evaluates to a null terminated string.
1191  *
1192  * Sets an assertion that the values that @left and @right evaluate to are
1193  * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1194  * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1195  */
1196 #define KUNIT_ASSERT_STREQ(test, left, right) \
1197     KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
1198 
1199 #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...)            \
1200     KUNIT_BINARY_STR_ASSERTION(test,                       \
1201                    KUNIT_ASSERTION,                \
1202                    left, ==, right,                \
1203                    fmt,                        \
1204                    ##__VA_ARGS__)
1205 
1206 /**
1207  * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1208  * @test: The test context object.
1209  * @left: an arbitrary expression that evaluates to a null terminated string.
1210  * @right: an arbitrary expression that evaluates to a null terminated string.
1211  *
1212  * Sets an expectation that the values that @left and @right evaluate to are
1213  * not equal. This is semantically equivalent to
1214  * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1215  * for more information.
1216  */
1217 #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1218     KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
1219 
1220 #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...)               \
1221     KUNIT_BINARY_STR_ASSERTION(test,                       \
1222                    KUNIT_ASSERTION,                \
1223                    left, !=, right,                \
1224                    fmt,                        \
1225                    ##__VA_ARGS__)
1226 
1227 /**
1228  * KUNIT_ASSERT_NULL() - Asserts that pointers @ptr is null.
1229  * @test: The test context object.
1230  * @ptr: an arbitrary pointer.
1231  *
1232  * Sets an assertion that the values that @ptr evaluates to is null. This is
1233  * the same as KUNIT_EXPECT_NULL(), except it causes an assertion
1234  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1235  */
1236 #define KUNIT_ASSERT_NULL(test, ptr) \
1237     KUNIT_ASSERT_NULL_MSG(test,                        \
1238                   ptr,                         \
1239                   NULL)
1240 
1241 #define KUNIT_ASSERT_NULL_MSG(test, ptr, fmt, ...) \
1242     KUNIT_BINARY_PTR_ASSERTION(test,                       \
1243                    KUNIT_ASSERTION,                \
1244                    ptr, ==, NULL,                  \
1245                    fmt,                        \
1246                    ##__VA_ARGS__)
1247 
1248 /**
1249  * KUNIT_ASSERT_NOT_NULL() - Asserts that pointers @ptr is not null.
1250  * @test: The test context object.
1251  * @ptr: an arbitrary pointer.
1252  *
1253  * Sets an assertion that the values that @ptr evaluates to is not null. This
1254  * is the same as KUNIT_EXPECT_NOT_NULL(), except it causes an assertion
1255  * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1256  */
1257 #define KUNIT_ASSERT_NOT_NULL(test, ptr) \
1258     KUNIT_ASSERT_NOT_NULL_MSG(test,                        \
1259                   ptr,                         \
1260                   NULL)
1261 
1262 #define KUNIT_ASSERT_NOT_NULL_MSG(test, ptr, fmt, ...) \
1263     KUNIT_BINARY_PTR_ASSERTION(test,                       \
1264                    KUNIT_ASSERTION,                \
1265                    ptr, !=, NULL,                  \
1266                    fmt,                        \
1267                    ##__VA_ARGS__)
1268 
1269 /**
1270  * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1271  * @test: The test context object.
1272  * @ptr: an arbitrary pointer.
1273  *
1274  * Sets an assertion that the value that @ptr evaluates to is not null and not
1275  * an errno stored in a pointer. This is the same as
1276  * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1277  * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1278  */
1279 #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1280     KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1281 
1282 #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...)              \
1283     KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test,                  \
1284                         KUNIT_ASSERTION,           \
1285                         ptr,                   \
1286                         fmt,                   \
1287                         ##__VA_ARGS__)
1288 
1289 /**
1290  * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1291  * @name:  prefix for the test parameter generator function.
1292  * @array: array of test parameters.
1293  * @get_desc: function to convert param to description; NULL to use default
1294  *
1295  * Define function @name_gen_params which uses @array to generate parameters.
1296  */
1297 #define KUNIT_ARRAY_PARAM(name, array, get_desc)                        \
1298     static const void *name##_gen_params(const void *prev, char *desc)          \
1299     {                                           \
1300         typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array);  \
1301         if (__next - (array) < ARRAY_SIZE((array))) {                   \
1302             void (*__get_desc)(typeof(__next), char *) = get_desc;          \
1303             if (__get_desc)                             \
1304                 __get_desc(__next, desc);                   \
1305             return __next;                              \
1306         }                                       \
1307         return NULL;                                    \
1308     }
1309 
1310 // TODO(dlatypov@google.com): consider eventually migrating users to explicitly
1311 // include resource.h themselves if they need it.
1312 #include <kunit/resource.h>
1313 
1314 #endif /* _KUNIT_TEST_H */