• Home
  • Raw
  • Download

Lines Matching +full:no +full:- +full:reset +full:- +full:on +full:- +full:init

1 .. SPDX-License-Identifier: GPL-2.0
7 ----------
13 .. code-block:: c
25 nothing; no expectations are set, and therefore all expectations pass. On the
38 .. code-block:: c
59 To learn about more KUnit expectations, see Documentation/dev-tools/kunit/api/test.rst.
62 A single test case should be short, easy to understand, and focused on a
69 .. code-block:: c
79 KUNIT_EXPECT_EQ(test, 0, add(-1, 1));
85 KUNIT_EXPECT_EQ(test, -1, add(INT_MAX, INT_MIN));
99 .. code-block:: c
111 for (i = 0; i < TEST_LEN-1; i++)
122 current kthread on failure, so you can call them from anywhere.
131 --------------------------
137 .. code-block:: c
151 .. code-block:: c
176 .. code-block:: c
187 .init = example_test_init,
204 The ``exit`` and ``suite_exit`` functions will run even if ``init`` or
206 state which may result from ``init`` or ``suite_init`` encountering errors
214 For more information, see Documentation/dev-tools/kunit/api/test.rst.
216 .. _kunit-on-non-uml:
219 -------------------------------------
221 It is better to write tests that run on UML to tests that only run under a
228 belongs in ``arch/some-arch/*``. Even so, try to write the test so that it does
229 not depend on physical hardware. Some of our test cases may not need hardware,
238 We may have to reset hardware state. If this is not possible, we may only
241 .. TODO(brendanhiggins@google.com): Add an actual example of an architecture-
248 ------------------
255 provided by the implementer, and architecture-specific functions, which have
265 programming; the Linux kernel is no exception.
284 .. code-block:: c
300 return self->length * self->width;
305 self->parent.area = rectangle_area;
306 self->length = length;
307 self->width = width;
326 .. code-block:: c
335 .. code-block:: c
348 .. code-block:: c
359 count = min(count, FAKE_EEPROM_CONTENTS_SIZE - offset);
360 memcpy(buffer, this->contents + offset, count);
369 count = min(count, FAKE_EEPROM_CONTENTS_SIZE - offset);
370 memcpy(this->contents + offset, buffer, count);
377 this->parent.read = fake_eeprom_read;
378 this->parent.write = fake_eeprom_write;
379 memset(this->contents, 0, FAKE_EEPROM_CONTENTS_SIZE);
384 .. code-block:: c
393 struct eeprom_buffer_test *ctx = test->priv;
394 struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer;
395 struct fake_eeprom *fake_eeprom = ctx->fake_eeprom;
398 eeprom_buffer->flush_count = SIZE_MAX;
400 eeprom_buffer->write(eeprom_buffer, buffer, 1);
401 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0);
403 eeprom_buffer->write(eeprom_buffer, buffer, 1);
404 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0);
406 eeprom_buffer->flush(eeprom_buffer);
407 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff);
408 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff);
413 struct eeprom_buffer_test *ctx = test->priv;
414 struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer;
415 struct fake_eeprom *fake_eeprom = ctx->fake_eeprom;
418 eeprom_buffer->flush_count = 2;
420 eeprom_buffer->write(eeprom_buffer, buffer, 1);
421 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0);
423 eeprom_buffer->write(eeprom_buffer, buffer, 1);
424 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff);
425 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff);
430 struct eeprom_buffer_test *ctx = test->priv;
431 struct eeprom_buffer *eeprom_buffer = ctx->eeprom_buffer;
432 struct fake_eeprom *fake_eeprom = ctx->fake_eeprom;
435 eeprom_buffer->flush_count = 2;
437 eeprom_buffer->write(eeprom_buffer, buffer, 1);
438 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0);
440 eeprom_buffer->write(eeprom_buffer, buffer, 2);
441 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[0], 0xff);
442 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[1], 0xff);
444 KUNIT_EXPECT_EQ(test, fake_eeprom->contents[2], 0);
454 ctx->fake_eeprom = kunit_kzalloc(test, sizeof(*ctx->fake_eeprom), GFP_KERNEL);
455 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->fake_eeprom);
456 fake_eeprom_init(ctx->fake_eeprom);
458 ctx->eeprom_buffer = new_eeprom_buffer(&ctx->fake_eeprom->parent);
459 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->eeprom_buffer);
461 test->priv = ctx;
468 struct eeprom_buffer_test *ctx = test->priv;
470 destroy_eeprom_buffer(ctx->eeprom_buffer);
474 -------------------------------
482 .. code-block:: c
499 In complicated cases, we recommend using a *table-driven test* compared to the
502 .. code-block:: c
533 * For example, see ``fs/ext4/inode-test.c``.
545 The table-driven testing pattern is common enough that KUnit has special
551 .. code-block:: c
553 // This is copy-pasted from above.
573 // Looks no different from a normal test.
576 // This function can just contain the body of the for-loop.
577 // The former `cases[i]` is accessible under test->param_value.
579 struct sha1_test_case *test_param = (struct sha1_test_case *)(test->param_value);
581 sha1sum(test_param->str, out);
582 KUNIT_EXPECT_STREQ_MSG(test, out, test_param->sha1,
583 "sha1sum(%s)", test_param->str);
594 -----------------
603 .. code-block:: c
615 ---------------------------
621 Actions are simple functions with no return value, and a single ``void*``
631 .. code-block:: C
650 pointer-sized argument, it's possible to automatically generate a wrapper
653 .. code-block:: C
671 ------------------------
676 .. code-block:: c
692 .. code-block:: c
702 Injecting Test-Only Code
703 ------------------------
705 Similar to as shown above, we can add test-specific logic. For example:
707 .. code-block:: c
718 This test-only code can be made more useful by accessing the current ``kunit_test``
722 --------------------------
724 In some cases, we need to call test-only code from outside the test file. This
728 access using the ``kunit_get_current_test()`` function in ``kunit/test-bug.h``.
731 KUnit is not enabled, or if no test is running in the current task, it will
732 return ``NULL``. This compiles down to either a no-op or a static key check,
733 so will have a negligible performance impact when no test is running.
737 .. code-block:: c
739 #include <kunit/test-bug.h> /* for kunit_get_current_test */
749 struct test_data *test_data = test->priv;
751 KUNIT_EXPECT_EQ(test, test_data->want_foo_called_with, arg);
752 return test_data->foo_result;
758 * the init function) is allocated in the suite's .init */
759 struct test_data *test_data = test->priv;
761 test_data->foo_result = 42;
762 test_data->want_foo_called_with = 1;
770 of passing data to the test from the init function. In general ``priv`` is
779 avoid resource leaks. For more information, see Documentation/dev-tools/kunit/api/resource.rst.
782 ------------------------
785 which is defined in ``<kunit/test-bug.h>`` and does not require pulling in ``<kunit/test.h>``.
786 For example, we have an option to enable some extra debug checks on some data
789 .. code-block:: c
791 #include <kunit/test-bug.h>
801 /* Normal, non-KUnit, error reporting code here. */
808 KUnit is not enabled, or if no test is running in the current task, it will do
809 nothing. This compiles down to either a no-op or a static key check, so will
810 have a negligible performance impact when no test is running.
813 ---------------------------------
823 described in Documentation/driver-api/driver-model/devres.rst
825 To create a KUnit-managed ``struct device_driver``, use ``kunit_driver_create()``,
826 which will create a driver with the given name, on the ``kunit_bus``. This driver
831 and register a device, using a new KUnit-managed driver created with ``kunit_driver_create()``.
832 To provide a specific, non-KUnit-managed driver, use ``kunit_device_register_with_driver()``
833 instead. Like with managed drivers, KUnit-managed fake devices are automatically
843 .. code-block:: c