Line data Source code
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 :
15 : #include <linux/compiler.h>
16 : #include <linux/container_of.h>
17 : #include <linux/err.h>
18 : #include <linux/init.h>
19 : #include <linux/kconfig.h>
20 : #include <linux/kref.h>
21 : #include <linux/list.h>
22 : #include <linux/module.h>
23 : #include <linux/slab.h>
24 : #include <linux/spinlock.h>
25 : #include <linux/string.h>
26 : #include <linux/types.h>
27 :
28 : #include <asm/rwonce.h>
29 :
30 : struct kunit;
31 :
32 : /* Size of log associated with test. */
33 : #define KUNIT_LOG_SIZE 512
34 :
35 : /* Maximum size of parameter description string. */
36 : #define KUNIT_PARAM_DESC_SIZE 128
37 :
38 : /* Maximum size of a status comment. */
39 : #define KUNIT_STATUS_COMMENT_SIZE 256
40 :
41 : /*
42 : * TAP specifies subtest stream indentation of 4 spaces, 8 spaces for a
43 : * sub-subtest. See the "Subtests" section in
44 : * https://node-tap.org/tap-protocol/
45 : */
46 : #define KUNIT_SUBTEST_INDENT " "
47 : #define KUNIT_SUBSUBTEST_INDENT " "
48 :
49 : /**
50 : * enum kunit_status - Type of result for a test or test suite
51 : * @KUNIT_SUCCESS: Denotes the test suite has not failed nor been skipped
52 : * @KUNIT_FAILURE: Denotes the test has failed.
53 : * @KUNIT_SKIPPED: Denotes the test has been skipped.
54 : */
55 : enum kunit_status {
56 : KUNIT_SUCCESS,
57 : KUNIT_FAILURE,
58 : KUNIT_SKIPPED,
59 : };
60 :
61 : /**
62 : * struct kunit_case - represents an individual test case.
63 : *
64 : * @run_case: the function representing the actual test case.
65 : * @name: the name of the test case.
66 : * @generate_params: the generator function for parameterized tests.
67 : *
68 : * A test case is a function with the signature,
69 : * ``void (*)(struct kunit *)``
70 : * that makes expectations and assertions (see KUNIT_EXPECT_TRUE() and
71 : * KUNIT_ASSERT_TRUE()) about code under test. Each test case is associated
72 : * with a &struct kunit_suite and will be run after the suite's init
73 : * function and followed by the suite's exit function.
74 : *
75 : * A test case should be static and should only be created with the
76 : * KUNIT_CASE() macro; additionally, every array of test cases should be
77 : * terminated with an empty test case.
78 : *
79 : * Example:
80 : *
81 : * .. code-block:: c
82 : *
83 : * void add_test_basic(struct kunit *test)
84 : * {
85 : * KUNIT_EXPECT_EQ(test, 1, add(1, 0));
86 : * KUNIT_EXPECT_EQ(test, 2, add(1, 1));
87 : * KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
88 : * KUNIT_EXPECT_EQ(test, INT_MAX, add(0, INT_MAX));
89 : * KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
90 : * }
91 : *
92 : * static struct kunit_case example_test_cases[] = {
93 : * KUNIT_CASE(add_test_basic),
94 : * {}
95 : * };
96 : *
97 : */
98 : struct kunit_case {
99 : void (*run_case)(struct kunit *test);
100 : const char *name;
101 : const void* (*generate_params)(const void *prev, char *desc);
102 :
103 : /* private: internal use only. */
104 : enum kunit_status status;
105 : char *log;
106 : };
107 :
108 : static inline char *kunit_status_to_ok_not_ok(enum kunit_status status)
109 : {
110 210 : switch (status) {
111 : case KUNIT_SKIPPED:
112 : case KUNIT_SUCCESS:
113 : return "ok";
114 : case KUNIT_FAILURE:
115 : return "not ok";
116 : }
117 : return "invalid";
118 : }
119 :
120 : /**
121 : * KUNIT_CASE - A helper for creating a &struct kunit_case
122 : *
123 : * @test_name: a reference to a test case function.
124 : *
125 : * Takes a symbol for a function representing a test case and creates a
126 : * &struct kunit_case object from it. See the documentation for
127 : * &struct kunit_case for an example on how to use it.
128 : */
129 : #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
130 :
131 : /**
132 : * KUNIT_CASE_PARAM - A helper for creation a parameterized &struct kunit_case
133 : *
134 : * @test_name: a reference to a test case function.
135 : * @gen_params: a reference to a parameter generator function.
136 : *
137 : * The generator function::
138 : *
139 : * const void* gen_params(const void *prev, char *desc)
140 : *
141 : * is used to lazily generate a series of arbitrarily typed values that fit into
142 : * a void*. The argument @prev is the previously returned value, which should be
143 : * used to derive the next value; @prev is set to NULL on the initial generator
144 : * call. When no more values are available, the generator must return NULL.
145 : * Optionally write a string into @desc (size of KUNIT_PARAM_DESC_SIZE)
146 : * describing the parameter.
147 : */
148 : #define KUNIT_CASE_PARAM(test_name, gen_params) \
149 : { .run_case = test_name, .name = #test_name, \
150 : .generate_params = gen_params }
151 :
152 : /**
153 : * struct kunit_suite - describes a related collection of &struct kunit_case
154 : *
155 : * @name: the name of the test. Purely informational.
156 : * @init: called before every test case.
157 : * @exit: called after every test case.
158 : * @test_cases: a null terminated array of test cases.
159 : *
160 : * A kunit_suite is a collection of related &struct kunit_case s, such that
161 : * @init is called before every test case and @exit is called after every
162 : * test case, similar to the notion of a *test fixture* or a *test class*
163 : * in other unit testing frameworks like JUnit or Googletest.
164 : *
165 : * Every &struct kunit_case must be associated with a kunit_suite for KUnit
166 : * to run it.
167 : */
168 : struct kunit_suite {
169 : const char name[256];
170 : int (*init)(struct kunit *test);
171 : void (*exit)(struct kunit *test);
172 : struct kunit_case *test_cases;
173 :
174 : /* private: internal use only */
175 : char status_comment[KUNIT_STATUS_COMMENT_SIZE];
176 : struct dentry *debugfs;
177 : char *log;
178 : };
179 :
180 : /**
181 : * struct kunit - represents a running instance of a test.
182 : *
183 : * @priv: for user to store arbitrary data. Commonly used to pass data
184 : * created in the init function (see &struct kunit_suite).
185 : *
186 : * Used to store information about the current context under which the test
187 : * is running. Most of this data is private and should only be accessed
188 : * indirectly via public functions; the one exception is @priv which can be
189 : * used by the test writer to store arbitrary data.
190 : */
191 : struct kunit {
192 : void *priv;
193 :
194 : /* private: internal use only. */
195 : const char *name; /* Read only after initialization! */
196 : char *log; /* Points at case log after initialization */
197 : struct kunit_try_catch try_catch;
198 : /* param_value is the current parameter value for a test case. */
199 : const void *param_value;
200 : /* param_index stores the index of the parameter in parameterized tests. */
201 : int param_index;
202 : /*
203 : * success starts as true, and may only be set to false during a
204 : * test case; thus, it is safe to update this across multiple
205 : * threads using WRITE_ONCE; however, as a consequence, it may only
206 : * be read after the test case finishes once all threads associated
207 : * with the test case have terminated.
208 : */
209 : spinlock_t lock; /* Guards all mutable test state. */
210 : enum kunit_status status; /* Read only after test_case finishes! */
211 : /*
212 : * Because resources is a list that may be updated multiple times (with
213 : * new resources) from any thread associated with a test case, we must
214 : * protect it with some type of lock.
215 : */
216 : struct list_head resources; /* Protected by lock. */
217 :
218 : char status_comment[KUNIT_STATUS_COMMENT_SIZE];
219 : };
220 :
221 : static inline void kunit_set_failure(struct kunit *test)
222 : {
223 10 : WRITE_ONCE(test->status, KUNIT_FAILURE);
224 : }
225 :
226 : void kunit_init_test(struct kunit *test, const char *name, char *log);
227 :
228 : int kunit_run_tests(struct kunit_suite *suite);
229 :
230 : size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
231 :
232 : unsigned int kunit_test_case_num(struct kunit_suite *suite,
233 : struct kunit_case *test_case);
234 :
235 : int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
236 :
237 : void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);
238 :
239 : #if IS_BUILTIN(CONFIG_KUNIT)
240 : int kunit_run_all_tests(void);
241 : #else
242 : static inline int kunit_run_all_tests(void)
243 : {
244 : return 0;
245 : }
246 : #endif /* IS_BUILTIN(CONFIG_KUNIT) */
247 :
248 : #define __kunit_test_suites(unique_array, ...) \
249 : static struct kunit_suite *unique_array[] \
250 : __aligned(sizeof(struct kunit_suite *)) \
251 : __used __section(".kunit_test_suites") = { __VA_ARGS__ }
252 :
253 : /**
254 : * kunit_test_suites() - used to register one or more &struct kunit_suite
255 : * with KUnit.
256 : *
257 : * @__suites: a statically allocated list of &struct kunit_suite.
258 : *
259 : * Registers @suites with the test framework.
260 : * This is done by placing the array of struct kunit_suite * in the
261 : * .kunit_test_suites ELF section.
262 : *
263 : * When builtin, KUnit tests are all run via the executor at boot, and when
264 : * built as a module, they run on module load.
265 : *
266 : */
267 : #define kunit_test_suites(__suites...) \
268 : __kunit_test_suites(__UNIQUE_ID(array), \
269 : ##__suites)
270 :
271 : #define kunit_test_suite(suite) kunit_test_suites(&suite)
272 :
273 : #define kunit_suite_for_each_test_case(suite, test_case) \
274 : for (test_case = suite->test_cases; test_case->run_case; test_case++)
275 :
276 : enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
277 :
278 : /**
279 : * kunit_kmalloc_array() - Like kmalloc_array() except the allocation is *test managed*.
280 : * @test: The test context object.
281 : * @n: number of elements.
282 : * @size: The size in bytes of the desired memory.
283 : * @gfp: flags passed to underlying kmalloc().
284 : *
285 : * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
286 : * and is automatically cleaned up after the test case concludes. See &struct
287 : * kunit_resource for more information.
288 : */
289 : void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
290 :
291 : /**
292 : * kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
293 : * @test: The test context object.
294 : * @size: The size in bytes of the desired memory.
295 : * @gfp: flags passed to underlying kmalloc().
296 : *
297 : * See kmalloc() and kunit_kmalloc_array() for more information.
298 : */
299 : static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
300 : {
301 221 : return kunit_kmalloc_array(test, 1, size, gfp);
302 : }
303 :
304 : /**
305 : * kunit_kfree() - Like kfree except for allocations managed by KUnit.
306 : * @test: The test case to which the resource belongs.
307 : * @ptr: The memory allocation to free.
308 : */
309 : void kunit_kfree(struct kunit *test, const void *ptr);
310 :
311 : /**
312 : * kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
313 : * @test: The test context object.
314 : * @size: The size in bytes of the desired memory.
315 : * @gfp: flags passed to underlying kmalloc().
316 : *
317 : * See kzalloc() and kunit_kmalloc_array() for more information.
318 : */
319 : static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
320 : {
321 362 : return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
322 : }
323 :
324 : /**
325 : * kunit_kcalloc() - Just like kunit_kmalloc_array(), but zeroes the allocation.
326 : * @test: The test context object.
327 : * @n: number of elements.
328 : * @size: The size in bytes of the desired memory.
329 : * @gfp: flags passed to underlying kmalloc().
330 : *
331 : * See kcalloc() and kunit_kmalloc_array() for more information.
332 : */
333 : static inline void *kunit_kcalloc(struct kunit *test, size_t n, size_t size, gfp_t gfp)
334 : {
335 : return kunit_kmalloc_array(test, n, size, gfp | __GFP_ZERO);
336 : }
337 :
338 : void kunit_cleanup(struct kunit *test);
339 :
340 : void __printf(2, 3) kunit_log_append(char *log, const char *fmt, ...);
341 :
342 : /**
343 : * kunit_mark_skipped() - Marks @test_or_suite as skipped
344 : *
345 : * @test_or_suite: The test context object.
346 : * @fmt: A printk() style format string.
347 : *
348 : * Marks the test as skipped. @fmt is given output as the test status
349 : * comment, typically the reason the test was skipped.
350 : *
351 : * Test execution continues after kunit_mark_skipped() is called.
352 : */
353 : #define kunit_mark_skipped(test_or_suite, fmt, ...) \
354 : do { \
355 : WRITE_ONCE((test_or_suite)->status, KUNIT_SKIPPED); \
356 : scnprintf((test_or_suite)->status_comment, \
357 : KUNIT_STATUS_COMMENT_SIZE, \
358 : fmt, ##__VA_ARGS__); \
359 : } while (0)
360 :
361 : /**
362 : * kunit_skip() - Marks @test_or_suite as skipped
363 : *
364 : * @test_or_suite: The test context object.
365 : * @fmt: A printk() style format string.
366 : *
367 : * Skips the test. @fmt is given output as the test status
368 : * comment, typically the reason the test was skipped.
369 : *
370 : * Test execution is halted after kunit_skip() is called.
371 : */
372 : #define kunit_skip(test_or_suite, fmt, ...) \
373 : do { \
374 : kunit_mark_skipped((test_or_suite), fmt, ##__VA_ARGS__);\
375 : kunit_try_catch_throw(&((test_or_suite)->try_catch)); \
376 : } while (0)
377 :
378 : /*
379 : * printk and log to per-test or per-suite log buffer. Logging only done
380 : * if CONFIG_KUNIT_DEBUGFS is 'y'; if it is 'n', no log is allocated/used.
381 : */
382 : #define kunit_log(lvl, test_or_suite, fmt, ...) \
383 : do { \
384 : printk(lvl fmt, ##__VA_ARGS__); \
385 : kunit_log_append((test_or_suite)->log, fmt "\n", \
386 : ##__VA_ARGS__); \
387 : } while (0)
388 :
389 : #define kunit_printk(lvl, test, fmt, ...) \
390 : kunit_log(lvl, test, KUNIT_SUBTEST_INDENT "# %s: " fmt, \
391 : (test)->name, ##__VA_ARGS__)
392 :
393 : /**
394 : * kunit_info() - Prints an INFO level message associated with @test.
395 : *
396 : * @test: The test context object.
397 : * @fmt: A printk() style format string.
398 : *
399 : * Prints an info level message associated with the test suite being run.
400 : * Takes a variable number of format parameters just like printk().
401 : */
402 : #define kunit_info(test, fmt, ...) \
403 : kunit_printk(KERN_INFO, test, fmt, ##__VA_ARGS__)
404 :
405 : /**
406 : * kunit_warn() - Prints a WARN level message associated with @test.
407 : *
408 : * @test: The test context object.
409 : * @fmt: A printk() style format string.
410 : *
411 : * Prints a warning level message.
412 : */
413 : #define kunit_warn(test, fmt, ...) \
414 : kunit_printk(KERN_WARNING, test, fmt, ##__VA_ARGS__)
415 :
416 : /**
417 : * kunit_err() - Prints an ERROR level message associated with @test.
418 : *
419 : * @test: The test context object.
420 : * @fmt: A printk() style format string.
421 : *
422 : * Prints an error level message.
423 : */
424 : #define kunit_err(test, fmt, ...) \
425 : kunit_printk(KERN_ERR, test, fmt, ##__VA_ARGS__)
426 :
427 : /**
428 : * KUNIT_SUCCEED() - A no-op expectation. Only exists for code clarity.
429 : * @test: The test context object.
430 : *
431 : * The opposite of KUNIT_FAIL(), it is an expectation that cannot fail. In other
432 : * words, it does nothing and only exists for code clarity. See
433 : * KUNIT_EXPECT_TRUE() for more information.
434 : */
435 : #define KUNIT_SUCCEED(test) do {} while (0)
436 :
437 : void kunit_do_failed_assertion(struct kunit *test,
438 : const struct kunit_loc *loc,
439 : enum kunit_assert_type type,
440 : struct kunit_assert *assert,
441 : const char *fmt, ...);
442 :
443 : #define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER, fmt, ...) do { \
444 : if (unlikely(!(pass))) { \
445 : static const struct kunit_loc __loc = KUNIT_CURRENT_LOC; \
446 : struct assert_class __assertion = INITIALIZER; \
447 : kunit_do_failed_assertion(test, \
448 : &__loc, \
449 : assert_type, \
450 : &__assertion.assert, \
451 : fmt, \
452 : ##__VA_ARGS__); \
453 : } \
454 : } while (0)
455 :
456 :
457 : #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \
458 : KUNIT_ASSERTION(test, \
459 : assert_type, \
460 : false, \
461 : kunit_fail_assert, \
462 : KUNIT_INIT_FAIL_ASSERT_STRUCT, \
463 : fmt, \
464 : ##__VA_ARGS__)
465 :
466 : /**
467 : * KUNIT_FAIL() - Always causes a test to fail when evaluated.
468 : * @test: The test context object.
469 : * @fmt: an informational message to be printed when the assertion is made.
470 : * @...: string format arguments.
471 : *
472 : * The opposite of KUNIT_SUCCEED(), it is an expectation that always fails. In
473 : * other words, it always results in a failed expectation, and consequently
474 : * always causes the test case to fail when evaluated. See KUNIT_EXPECT_TRUE()
475 : * for more information.
476 : */
477 : #define KUNIT_FAIL(test, fmt, ...) \
478 : KUNIT_FAIL_ASSERTION(test, \
479 : KUNIT_EXPECTATION, \
480 : fmt, \
481 : ##__VA_ARGS__)
482 :
483 : #define KUNIT_UNARY_ASSERTION(test, \
484 : assert_type, \
485 : condition, \
486 : expected_true, \
487 : fmt, \
488 : ...) \
489 : KUNIT_ASSERTION(test, \
490 : assert_type, \
491 : !!(condition) == !!expected_true, \
492 : kunit_unary_assert, \
493 : KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \
494 : expected_true), \
495 : fmt, \
496 : ##__VA_ARGS__)
497 :
498 : #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
499 : KUNIT_UNARY_ASSERTION(test, \
500 : assert_type, \
501 : condition, \
502 : true, \
503 : fmt, \
504 : ##__VA_ARGS__)
505 :
506 : #define KUNIT_FALSE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) \
507 : KUNIT_UNARY_ASSERTION(test, \
508 : assert_type, \
509 : condition, \
510 : false, \
511 : fmt, \
512 : ##__VA_ARGS__)
513 :
514 : /*
515 : * A factory macro for defining the assertions and expectations for the basic
516 : * comparisons defined for the built in types.
517 : *
518 : * Unfortunately, there is no common type that all types can be promoted to for
519 : * which all the binary operators behave the same way as for the actual types
520 : * (for example, there is no type that long long and unsigned long long can
521 : * both be cast to where the comparison result is preserved for all values). So
522 : * the best we can do is do the comparison in the original types and then coerce
523 : * everything to long long for printing; this way, the comparison behaves
524 : * correctly and the printed out value usually makes sense without
525 : * interpretation, but can always be interpreted to figure out the actual
526 : * value.
527 : */
528 : #define KUNIT_BASE_BINARY_ASSERTION(test, \
529 : assert_class, \
530 : format_func, \
531 : assert_type, \
532 : left, \
533 : op, \
534 : right, \
535 : fmt, \
536 : ...) \
537 : do { \
538 : const typeof(left) __left = (left); \
539 : const typeof(right) __right = (right); \
540 : static const struct kunit_binary_assert_text __text = { \
541 : .operation = #op, \
542 : .left_text = #left, \
543 : .right_text = #right, \
544 : }; \
545 : \
546 : KUNIT_ASSERTION(test, \
547 : assert_type, \
548 : __left op __right, \
549 : assert_class, \
550 : KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func, \
551 : &__text, \
552 : __left, \
553 : __right), \
554 : fmt, \
555 : ##__VA_ARGS__); \
556 : } while (0)
557 :
558 : #define KUNIT_BINARY_INT_ASSERTION(test, \
559 : assert_type, \
560 : left, \
561 : op, \
562 : right, \
563 : fmt, \
564 : ...) \
565 : KUNIT_BASE_BINARY_ASSERTION(test, \
566 : kunit_binary_assert, \
567 : kunit_binary_assert_format, \
568 : assert_type, \
569 : left, op, right, \
570 : fmt, \
571 : ##__VA_ARGS__)
572 :
573 : #define KUNIT_BINARY_PTR_ASSERTION(test, \
574 : assert_type, \
575 : left, \
576 : op, \
577 : right, \
578 : fmt, \
579 : ...) \
580 : KUNIT_BASE_BINARY_ASSERTION(test, \
581 : kunit_binary_ptr_assert, \
582 : kunit_binary_ptr_assert_format, \
583 : assert_type, \
584 : left, op, right, \
585 : fmt, \
586 : ##__VA_ARGS__)
587 :
588 : #define KUNIT_BINARY_STR_ASSERTION(test, \
589 : assert_type, \
590 : left, \
591 : op, \
592 : right, \
593 : fmt, \
594 : ...) \
595 : do { \
596 : const char *__left = (left); \
597 : const char *__right = (right); \
598 : static const struct kunit_binary_assert_text __text = { \
599 : .operation = #op, \
600 : .left_text = #left, \
601 : .right_text = #right, \
602 : }; \
603 : \
604 : KUNIT_ASSERTION(test, \
605 : assert_type, \
606 : strcmp(__left, __right) op 0, \
607 : kunit_binary_str_assert, \
608 : KUNIT_INIT_BINARY_ASSERT_STRUCT(kunit_binary_str_assert_format,\
609 : &__text, \
610 : __left, \
611 : __right), \
612 : fmt, \
613 : ##__VA_ARGS__); \
614 : } while (0)
615 :
616 : #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
617 : assert_type, \
618 : ptr, \
619 : fmt, \
620 : ...) \
621 : do { \
622 : const typeof(ptr) __ptr = (ptr); \
623 : \
624 : KUNIT_ASSERTION(test, \
625 : assert_type, \
626 : !IS_ERR_OR_NULL(__ptr), \
627 : kunit_ptr_not_err_assert, \
628 : KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, \
629 : __ptr), \
630 : fmt, \
631 : ##__VA_ARGS__); \
632 : } while (0)
633 :
634 : /**
635 : * KUNIT_EXPECT_TRUE() - Causes a test failure when the expression is not true.
636 : * @test: The test context object.
637 : * @condition: an arbitrary boolean expression. The test fails when this does
638 : * not evaluate to true.
639 : *
640 : * This and expectations of the form `KUNIT_EXPECT_*` will cause the test case
641 : * to fail when the specified condition is not met; however, it will not prevent
642 : * the test case from continuing to run; this is otherwise known as an
643 : * *expectation failure*.
644 : */
645 : #define KUNIT_EXPECT_TRUE(test, condition) \
646 : KUNIT_EXPECT_TRUE_MSG(test, condition, NULL)
647 :
648 : #define KUNIT_EXPECT_TRUE_MSG(test, condition, fmt, ...) \
649 : KUNIT_TRUE_MSG_ASSERTION(test, \
650 : KUNIT_EXPECTATION, \
651 : condition, \
652 : fmt, \
653 : ##__VA_ARGS__)
654 :
655 : /**
656 : * KUNIT_EXPECT_FALSE() - Makes a test failure when the expression is not false.
657 : * @test: The test context object.
658 : * @condition: an arbitrary boolean expression. The test fails when this does
659 : * not evaluate to false.
660 : *
661 : * Sets an expectation that @condition evaluates to false. See
662 : * KUNIT_EXPECT_TRUE() for more information.
663 : */
664 : #define KUNIT_EXPECT_FALSE(test, condition) \
665 : KUNIT_EXPECT_FALSE_MSG(test, condition, NULL)
666 :
667 : #define KUNIT_EXPECT_FALSE_MSG(test, condition, fmt, ...) \
668 : KUNIT_FALSE_MSG_ASSERTION(test, \
669 : KUNIT_EXPECTATION, \
670 : condition, \
671 : fmt, \
672 : ##__VA_ARGS__)
673 :
674 : /**
675 : * KUNIT_EXPECT_EQ() - Sets an expectation that @left and @right are equal.
676 : * @test: The test context object.
677 : * @left: an arbitrary expression that evaluates to a primitive C type.
678 : * @right: an arbitrary expression that evaluates to a primitive C type.
679 : *
680 : * Sets an expectation that the values that @left and @right evaluate to are
681 : * equal. This is semantically equivalent to
682 : * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
683 : * more information.
684 : */
685 : #define KUNIT_EXPECT_EQ(test, left, right) \
686 : KUNIT_EXPECT_EQ_MSG(test, left, right, NULL)
687 :
688 : #define KUNIT_EXPECT_EQ_MSG(test, left, right, fmt, ...) \
689 : KUNIT_BINARY_INT_ASSERTION(test, \
690 : KUNIT_EXPECTATION, \
691 : left, ==, right, \
692 : fmt, \
693 : ##__VA_ARGS__)
694 :
695 : /**
696 : * KUNIT_EXPECT_PTR_EQ() - Expects that pointers @left and @right are equal.
697 : * @test: The test context object.
698 : * @left: an arbitrary expression that evaluates to a pointer.
699 : * @right: an arbitrary expression that evaluates to a pointer.
700 : *
701 : * Sets an expectation that the values that @left and @right evaluate to are
702 : * equal. This is semantically equivalent to
703 : * KUNIT_EXPECT_TRUE(@test, (@left) == (@right)). See KUNIT_EXPECT_TRUE() for
704 : * more information.
705 : */
706 : #define KUNIT_EXPECT_PTR_EQ(test, left, right) \
707 : KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, NULL)
708 :
709 : #define KUNIT_EXPECT_PTR_EQ_MSG(test, left, right, fmt, ...) \
710 : KUNIT_BINARY_PTR_ASSERTION(test, \
711 : KUNIT_EXPECTATION, \
712 : left, ==, right, \
713 : fmt, \
714 : ##__VA_ARGS__)
715 :
716 : /**
717 : * KUNIT_EXPECT_NE() - An expectation that @left and @right are not equal.
718 : * @test: The test context object.
719 : * @left: an arbitrary expression that evaluates to a primitive C type.
720 : * @right: an arbitrary expression that evaluates to a primitive C type.
721 : *
722 : * Sets an expectation that the values that @left and @right evaluate to are not
723 : * equal. This is semantically equivalent to
724 : * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
725 : * more information.
726 : */
727 : #define KUNIT_EXPECT_NE(test, left, right) \
728 : KUNIT_EXPECT_NE_MSG(test, left, right, NULL)
729 :
730 : #define KUNIT_EXPECT_NE_MSG(test, left, right, fmt, ...) \
731 : KUNIT_BINARY_INT_ASSERTION(test, \
732 : KUNIT_EXPECTATION, \
733 : left, !=, right, \
734 : fmt, \
735 : ##__VA_ARGS__)
736 :
737 : /**
738 : * KUNIT_EXPECT_PTR_NE() - Expects that pointers @left and @right are not equal.
739 : * @test: The test context object.
740 : * @left: an arbitrary expression that evaluates to a pointer.
741 : * @right: an arbitrary expression that evaluates to a pointer.
742 : *
743 : * Sets an expectation that the values that @left and @right evaluate to are not
744 : * equal. This is semantically equivalent to
745 : * KUNIT_EXPECT_TRUE(@test, (@left) != (@right)). See KUNIT_EXPECT_TRUE() for
746 : * more information.
747 : */
748 : #define KUNIT_EXPECT_PTR_NE(test, left, right) \
749 : KUNIT_EXPECT_PTR_NE_MSG(test, left, right, NULL)
750 :
751 : #define KUNIT_EXPECT_PTR_NE_MSG(test, left, right, fmt, ...) \
752 : KUNIT_BINARY_PTR_ASSERTION(test, \
753 : KUNIT_EXPECTATION, \
754 : left, !=, right, \
755 : fmt, \
756 : ##__VA_ARGS__)
757 :
758 : /**
759 : * KUNIT_EXPECT_LT() - An expectation that @left is less than @right.
760 : * @test: The test context object.
761 : * @left: an arbitrary expression that evaluates to a primitive C type.
762 : * @right: an arbitrary expression that evaluates to a primitive C type.
763 : *
764 : * Sets an expectation that the value that @left evaluates to is less than the
765 : * value that @right evaluates to. This is semantically equivalent to
766 : * KUNIT_EXPECT_TRUE(@test, (@left) < (@right)). See KUNIT_EXPECT_TRUE() for
767 : * more information.
768 : */
769 : #define KUNIT_EXPECT_LT(test, left, right) \
770 : KUNIT_EXPECT_LT_MSG(test, left, right, NULL)
771 :
772 : #define KUNIT_EXPECT_LT_MSG(test, left, right, fmt, ...) \
773 : KUNIT_BINARY_INT_ASSERTION(test, \
774 : KUNIT_EXPECTATION, \
775 : left, <, right, \
776 : fmt, \
777 : ##__VA_ARGS__)
778 :
779 : /**
780 : * KUNIT_EXPECT_LE() - Expects that @left is less than or equal to @right.
781 : * @test: The test context object.
782 : * @left: an arbitrary expression that evaluates to a primitive C type.
783 : * @right: an arbitrary expression that evaluates to a primitive C type.
784 : *
785 : * Sets an expectation that the value that @left evaluates to is less than or
786 : * equal to the value that @right evaluates to. Semantically this is equivalent
787 : * to KUNIT_EXPECT_TRUE(@test, (@left) <= (@right)). See KUNIT_EXPECT_TRUE() for
788 : * more information.
789 : */
790 : #define KUNIT_EXPECT_LE(test, left, right) \
791 : KUNIT_EXPECT_LE_MSG(test, left, right, NULL)
792 :
793 : #define KUNIT_EXPECT_LE_MSG(test, left, right, fmt, ...) \
794 : KUNIT_BINARY_INT_ASSERTION(test, \
795 : KUNIT_ASSERTION, \
796 : left, <=, right, \
797 : fmt, \
798 : ##__VA_ARGS__)
799 :
800 : /**
801 : * KUNIT_EXPECT_GT() - An expectation that @left is greater than @right.
802 : * @test: The test context object.
803 : * @left: an arbitrary expression that evaluates to a primitive C type.
804 : * @right: an arbitrary expression that evaluates to a primitive C type.
805 : *
806 : * Sets an expectation that the value that @left evaluates to is greater than
807 : * the value that @right evaluates to. This is semantically equivalent to
808 : * KUNIT_EXPECT_TRUE(@test, (@left) > (@right)). See KUNIT_EXPECT_TRUE() for
809 : * more information.
810 : */
811 : #define KUNIT_EXPECT_GT(test, left, right) \
812 : KUNIT_EXPECT_GT_MSG(test, left, right, NULL)
813 :
814 : #define KUNIT_EXPECT_GT_MSG(test, left, right, fmt, ...) \
815 : KUNIT_BINARY_INT_ASSERTION(test, \
816 : KUNIT_EXPECTATION, \
817 : left, >, right, \
818 : fmt, \
819 : ##__VA_ARGS__)
820 :
821 : /**
822 : * KUNIT_EXPECT_GE() - Expects that @left is greater than or equal to @right.
823 : * @test: The test context object.
824 : * @left: an arbitrary expression that evaluates to a primitive C type.
825 : * @right: an arbitrary expression that evaluates to a primitive C type.
826 : *
827 : * Sets an expectation that the value that @left evaluates to is greater than
828 : * the value that @right evaluates to. This is semantically equivalent to
829 : * KUNIT_EXPECT_TRUE(@test, (@left) >= (@right)). See KUNIT_EXPECT_TRUE() for
830 : * more information.
831 : */
832 : #define KUNIT_EXPECT_GE(test, left, right) \
833 : KUNIT_EXPECT_GE_MSG(test, left, right, NULL)
834 :
835 : #define KUNIT_EXPECT_GE_MSG(test, left, right, fmt, ...) \
836 : KUNIT_BINARY_INT_ASSERTION(test, \
837 : KUNIT_EXPECTATION, \
838 : left, >=, right, \
839 : fmt, \
840 : ##__VA_ARGS__)
841 :
842 : /**
843 : * KUNIT_EXPECT_STREQ() - Expects that strings @left and @right are equal.
844 : * @test: The test context object.
845 : * @left: an arbitrary expression that evaluates to a null terminated string.
846 : * @right: an arbitrary expression that evaluates to a null terminated string.
847 : *
848 : * Sets an expectation that the values that @left and @right evaluate to are
849 : * equal. This is semantically equivalent to
850 : * KUNIT_EXPECT_TRUE(@test, !strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
851 : * for more information.
852 : */
853 : #define KUNIT_EXPECT_STREQ(test, left, right) \
854 : KUNIT_EXPECT_STREQ_MSG(test, left, right, NULL)
855 :
856 : #define KUNIT_EXPECT_STREQ_MSG(test, left, right, fmt, ...) \
857 : KUNIT_BINARY_STR_ASSERTION(test, \
858 : KUNIT_EXPECTATION, \
859 : left, ==, right, \
860 : fmt, \
861 : ##__VA_ARGS__)
862 :
863 : /**
864 : * KUNIT_EXPECT_STRNEQ() - Expects that strings @left and @right are not equal.
865 : * @test: The test context object.
866 : * @left: an arbitrary expression that evaluates to a null terminated string.
867 : * @right: an arbitrary expression that evaluates to a null terminated string.
868 : *
869 : * Sets an expectation that the values that @left and @right evaluate to are
870 : * not equal. This is semantically equivalent to
871 : * KUNIT_EXPECT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_EXPECT_TRUE()
872 : * for more information.
873 : */
874 : #define KUNIT_EXPECT_STRNEQ(test, left, right) \
875 : KUNIT_EXPECT_STRNEQ_MSG(test, left, right, NULL)
876 :
877 : #define KUNIT_EXPECT_STRNEQ_MSG(test, left, right, fmt, ...) \
878 : KUNIT_BINARY_STR_ASSERTION(test, \
879 : KUNIT_EXPECTATION, \
880 : left, !=, right, \
881 : fmt, \
882 : ##__VA_ARGS__)
883 :
884 : /**
885 : * KUNIT_EXPECT_NOT_ERR_OR_NULL() - Expects that @ptr is not null and not err.
886 : * @test: The test context object.
887 : * @ptr: an arbitrary pointer.
888 : *
889 : * Sets an expectation that the value that @ptr evaluates to is not null and not
890 : * an errno stored in a pointer. This is semantically equivalent to
891 : * KUNIT_EXPECT_TRUE(@test, !IS_ERR_OR_NULL(@ptr)). See KUNIT_EXPECT_TRUE() for
892 : * more information.
893 : */
894 : #define KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ptr) \
895 : KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
896 :
897 : #define KUNIT_EXPECT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
898 : KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
899 : KUNIT_EXPECTATION, \
900 : ptr, \
901 : fmt, \
902 : ##__VA_ARGS__)
903 :
904 : #define KUNIT_ASSERT_FAILURE(test, fmt, ...) \
905 : KUNIT_FAIL_ASSERTION(test, KUNIT_ASSERTION, fmt, ##__VA_ARGS__)
906 :
907 : /**
908 : * KUNIT_ASSERT_TRUE() - Sets an assertion that @condition is true.
909 : * @test: The test context object.
910 : * @condition: an arbitrary boolean expression. The test fails and aborts when
911 : * this does not evaluate to true.
912 : *
913 : * This and assertions of the form `KUNIT_ASSERT_*` will cause the test case to
914 : * fail *and immediately abort* when the specified condition is not met. Unlike
915 : * an expectation failure, it will prevent the test case from continuing to run;
916 : * this is otherwise known as an *assertion failure*.
917 : */
918 : #define KUNIT_ASSERT_TRUE(test, condition) \
919 : KUNIT_ASSERT_TRUE_MSG(test, condition, NULL)
920 :
921 : #define KUNIT_ASSERT_TRUE_MSG(test, condition, fmt, ...) \
922 : KUNIT_TRUE_MSG_ASSERTION(test, \
923 : KUNIT_ASSERTION, \
924 : condition, \
925 : fmt, \
926 : ##__VA_ARGS__)
927 :
928 : /**
929 : * KUNIT_ASSERT_FALSE() - Sets an assertion that @condition is false.
930 : * @test: The test context object.
931 : * @condition: an arbitrary boolean expression.
932 : *
933 : * Sets an assertion that the value that @condition evaluates to is false. This
934 : * is the same as KUNIT_EXPECT_FALSE(), except it causes an assertion failure
935 : * (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
936 : */
937 : #define KUNIT_ASSERT_FALSE(test, condition) \
938 : KUNIT_ASSERT_FALSE_MSG(test, condition, NULL)
939 :
940 : #define KUNIT_ASSERT_FALSE_MSG(test, condition, fmt, ...) \
941 : KUNIT_FALSE_MSG_ASSERTION(test, \
942 : KUNIT_ASSERTION, \
943 : condition, \
944 : fmt, \
945 : ##__VA_ARGS__)
946 :
947 : /**
948 : * KUNIT_ASSERT_EQ() - Sets an assertion that @left and @right are equal.
949 : * @test: The test context object.
950 : * @left: an arbitrary expression that evaluates to a primitive C type.
951 : * @right: an arbitrary expression that evaluates to a primitive C type.
952 : *
953 : * Sets an assertion that the values that @left and @right evaluate to are
954 : * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
955 : * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
956 : */
957 : #define KUNIT_ASSERT_EQ(test, left, right) \
958 : KUNIT_ASSERT_EQ_MSG(test, left, right, NULL)
959 :
960 : #define KUNIT_ASSERT_EQ_MSG(test, left, right, fmt, ...) \
961 : KUNIT_BINARY_INT_ASSERTION(test, \
962 : KUNIT_ASSERTION, \
963 : left, ==, right, \
964 : fmt, \
965 : ##__VA_ARGS__)
966 :
967 : /**
968 : * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
969 : * @test: The test context object.
970 : * @left: an arbitrary expression that evaluates to a pointer.
971 : * @right: an arbitrary expression that evaluates to a pointer.
972 : *
973 : * Sets an assertion that the values that @left and @right evaluate to are
974 : * equal. This is the same as KUNIT_EXPECT_EQ(), except it causes an assertion
975 : * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
976 : */
977 : #define KUNIT_ASSERT_PTR_EQ(test, left, right) \
978 : KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, NULL)
979 :
980 : #define KUNIT_ASSERT_PTR_EQ_MSG(test, left, right, fmt, ...) \
981 : KUNIT_BINARY_PTR_ASSERTION(test, \
982 : KUNIT_ASSERTION, \
983 : left, ==, right, \
984 : fmt, \
985 : ##__VA_ARGS__)
986 :
987 : /**
988 : * KUNIT_ASSERT_NE() - An assertion that @left and @right are not equal.
989 : * @test: The test context object.
990 : * @left: an arbitrary expression that evaluates to a primitive C type.
991 : * @right: an arbitrary expression that evaluates to a primitive C type.
992 : *
993 : * Sets an assertion that the values that @left and @right evaluate to are not
994 : * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
995 : * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
996 : */
997 : #define KUNIT_ASSERT_NE(test, left, right) \
998 : KUNIT_ASSERT_NE_MSG(test, left, right, NULL)
999 :
1000 : #define KUNIT_ASSERT_NE_MSG(test, left, right, fmt, ...) \
1001 : KUNIT_BINARY_INT_ASSERTION(test, \
1002 : KUNIT_ASSERTION, \
1003 : left, !=, right, \
1004 : fmt, \
1005 : ##__VA_ARGS__)
1006 :
1007 : /**
1008 : * KUNIT_ASSERT_PTR_NE() - Asserts that pointers @left and @right are not equal.
1009 : * KUNIT_ASSERT_PTR_EQ() - Asserts that pointers @left and @right are equal.
1010 : * @test: The test context object.
1011 : * @left: an arbitrary expression that evaluates to a pointer.
1012 : * @right: an arbitrary expression that evaluates to a pointer.
1013 : *
1014 : * Sets an assertion that the values that @left and @right evaluate to are not
1015 : * equal. This is the same as KUNIT_EXPECT_NE(), except it causes an assertion
1016 : * failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1017 : */
1018 : #define KUNIT_ASSERT_PTR_NE(test, left, right) \
1019 : KUNIT_ASSERT_PTR_NE_MSG(test, left, right, NULL)
1020 :
1021 : #define KUNIT_ASSERT_PTR_NE_MSG(test, left, right, fmt, ...) \
1022 : KUNIT_BINARY_PTR_ASSERTION(test, \
1023 : KUNIT_ASSERTION, \
1024 : left, !=, right, \
1025 : fmt, \
1026 : ##__VA_ARGS__)
1027 : /**
1028 : * KUNIT_ASSERT_LT() - An assertion that @left is less than @right.
1029 : * @test: The test context object.
1030 : * @left: an arbitrary expression that evaluates to a primitive C type.
1031 : * @right: an arbitrary expression that evaluates to a primitive C type.
1032 : *
1033 : * Sets an assertion that the value that @left evaluates to is less than the
1034 : * value that @right evaluates to. This is the same as KUNIT_EXPECT_LT(), except
1035 : * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1036 : * is not met.
1037 : */
1038 : #define KUNIT_ASSERT_LT(test, left, right) \
1039 : KUNIT_ASSERT_LT_MSG(test, left, right, NULL)
1040 :
1041 : #define KUNIT_ASSERT_LT_MSG(test, left, right, fmt, ...) \
1042 : KUNIT_BINARY_INT_ASSERTION(test, \
1043 : KUNIT_EXPECTATION, \
1044 : left, <, right, \
1045 : fmt, \
1046 : ##__VA_ARGS__)
1047 : /**
1048 : * KUNIT_ASSERT_LE() - An assertion that @left is less than or equal to @right.
1049 : * @test: The test context object.
1050 : * @left: an arbitrary expression that evaluates to a primitive C type.
1051 : * @right: an arbitrary expression that evaluates to a primitive C type.
1052 : *
1053 : * Sets an assertion that the value that @left evaluates to is less than or
1054 : * equal to the value that @right evaluates to. This is the same as
1055 : * KUNIT_EXPECT_LE(), except it causes an assertion failure (see
1056 : * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1057 : */
1058 : #define KUNIT_ASSERT_LE(test, left, right) \
1059 : KUNIT_ASSERT_LE_MSG(test, left, right, NULL)
1060 :
1061 : #define KUNIT_ASSERT_LE_MSG(test, left, right, fmt, ...) \
1062 : KUNIT_BINARY_INT_ASSERTION(test, \
1063 : KUNIT_ASSERTION, \
1064 : left, <=, right, \
1065 : fmt, \
1066 : ##__VA_ARGS__)
1067 :
1068 : /**
1069 : * KUNIT_ASSERT_GT() - An assertion that @left is greater than @right.
1070 : * @test: The test context object.
1071 : * @left: an arbitrary expression that evaluates to a primitive C type.
1072 : * @right: an arbitrary expression that evaluates to a primitive C type.
1073 : *
1074 : * Sets an assertion that the value that @left evaluates to is greater than the
1075 : * value that @right evaluates to. This is the same as KUNIT_EXPECT_GT(), except
1076 : * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1077 : * is not met.
1078 : */
1079 : #define KUNIT_ASSERT_GT(test, left, right) \
1080 : KUNIT_ASSERT_GT_MSG(test, left, right, NULL)
1081 :
1082 : #define KUNIT_ASSERT_GT_MSG(test, left, right, fmt, ...) \
1083 : KUNIT_BINARY_INT_ASSERTION(test, \
1084 : KUNIT_EXPECTATION, \
1085 : left, >, right, \
1086 : fmt, \
1087 : ##__VA_ARGS__)
1088 :
1089 : /**
1090 : * KUNIT_ASSERT_GE() - Assertion that @left is greater than or equal to @right.
1091 : * @test: The test context object.
1092 : * @left: an arbitrary expression that evaluates to a primitive C type.
1093 : * @right: an arbitrary expression that evaluates to a primitive C type.
1094 : *
1095 : * Sets an assertion that the value that @left evaluates to is greater than the
1096 : * value that @right evaluates to. This is the same as KUNIT_EXPECT_GE(), except
1097 : * it causes an assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion
1098 : * is not met.
1099 : */
1100 : #define KUNIT_ASSERT_GE(test, left, right) \
1101 : KUNIT_ASSERT_GE_MSG(test, left, right, NULL)
1102 :
1103 : #define KUNIT_ASSERT_GE_MSG(test, left, right, fmt, ...) \
1104 : KUNIT_BINARY_INT_ASSERTION(test, \
1105 : KUNIT_ASSERTION, \
1106 : left, >=, right, \
1107 : fmt, \
1108 : ##__VA_ARGS__)
1109 :
1110 : /**
1111 : * KUNIT_ASSERT_STREQ() - An assertion that strings @left and @right are equal.
1112 : * @test: The test context object.
1113 : * @left: an arbitrary expression that evaluates to a null terminated string.
1114 : * @right: an arbitrary expression that evaluates to a null terminated string.
1115 : *
1116 : * Sets an assertion that the values that @left and @right evaluate to are
1117 : * equal. This is the same as KUNIT_EXPECT_STREQ(), except it causes an
1118 : * assertion failure (see KUNIT_ASSERT_TRUE()) when the assertion is not met.
1119 : */
1120 : #define KUNIT_ASSERT_STREQ(test, left, right) \
1121 : KUNIT_ASSERT_STREQ_MSG(test, left, right, NULL)
1122 :
1123 : #define KUNIT_ASSERT_STREQ_MSG(test, left, right, fmt, ...) \
1124 : KUNIT_BINARY_STR_ASSERTION(test, \
1125 : KUNIT_ASSERTION, \
1126 : left, ==, right, \
1127 : fmt, \
1128 : ##__VA_ARGS__)
1129 :
1130 : /**
1131 : * KUNIT_ASSERT_STRNEQ() - Expects that strings @left and @right are not equal.
1132 : * @test: The test context object.
1133 : * @left: an arbitrary expression that evaluates to a null terminated string.
1134 : * @right: an arbitrary expression that evaluates to a null terminated string.
1135 : *
1136 : * Sets an expectation that the values that @left and @right evaluate to are
1137 : * not equal. This is semantically equivalent to
1138 : * KUNIT_ASSERT_TRUE(@test, strcmp((@left), (@right))). See KUNIT_ASSERT_TRUE()
1139 : * for more information.
1140 : */
1141 : #define KUNIT_ASSERT_STRNEQ(test, left, right) \
1142 : KUNIT_ASSERT_STRNEQ_MSG(test, left, right, NULL)
1143 :
1144 : #define KUNIT_ASSERT_STRNEQ_MSG(test, left, right, fmt, ...) \
1145 : KUNIT_BINARY_STR_ASSERTION(test, \
1146 : KUNIT_ASSERTION, \
1147 : left, !=, right, \
1148 : fmt, \
1149 : ##__VA_ARGS__)
1150 :
1151 : /**
1152 : * KUNIT_ASSERT_NOT_ERR_OR_NULL() - Assertion that @ptr is not null and not err.
1153 : * @test: The test context object.
1154 : * @ptr: an arbitrary pointer.
1155 : *
1156 : * Sets an assertion that the value that @ptr evaluates to is not null and not
1157 : * an errno stored in a pointer. This is the same as
1158 : * KUNIT_EXPECT_NOT_ERR_OR_NULL(), except it causes an assertion failure (see
1159 : * KUNIT_ASSERT_TRUE()) when the assertion is not met.
1160 : */
1161 : #define KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr) \
1162 : KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, NULL)
1163 :
1164 : #define KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ptr, fmt, ...) \
1165 : KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \
1166 : KUNIT_ASSERTION, \
1167 : ptr, \
1168 : fmt, \
1169 : ##__VA_ARGS__)
1170 :
1171 : /**
1172 : * KUNIT_ARRAY_PARAM() - Define test parameter generator from an array.
1173 : * @name: prefix for the test parameter generator function.
1174 : * @array: array of test parameters.
1175 : * @get_desc: function to convert param to description; NULL to use default
1176 : *
1177 : * Define function @name_gen_params which uses @array to generate parameters.
1178 : */
1179 : #define KUNIT_ARRAY_PARAM(name, array, get_desc) \
1180 : static const void *name##_gen_params(const void *prev, char *desc) \
1181 : { \
1182 : typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array); \
1183 : if (__next - (array) < ARRAY_SIZE((array))) { \
1184 : void (*__get_desc)(typeof(__next), char *) = get_desc; \
1185 : if (__get_desc) \
1186 : __get_desc(__next, desc); \
1187 : return __next; \
1188 : } \
1189 : return NULL; \
1190 : }
1191 :
1192 : // TODO(dlatypov@google.com): consider eventually migrating users to explicitly
1193 : // include resource.h themselves if they need it.
1194 : #include <kunit/resource.h>
1195 :
1196 : #endif /* _KUNIT_TEST_H */
|