lib/kunit/kunit-example-test.c
Source file repositories/reference/linux-study-clean/lib/kunit/kunit-example-test.c
File Facts
- System
- Linux kernel
- Corpus path
lib/kunit/kunit-example-test.c- Extension
.c- Size
- 17175 bytes
- Lines
- 596
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: implementation source
- Status
- source implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
kunit/test.hkunit/static_stub.h
Detected Declarations
function example_simple_testfunction example_test_initfunction example_test_exitfunction example_test_init_suitefunction example_test_exit_suitefunction example_skip_testfunction example_mark_skipped_testfunction example_all_expect_macros_testfunction add_onefunction subtract_onefunction example_static_stub_testfunction example_static_stub_using_fn_ptr_testfunction example_param_get_descfunction example_params_testfunction example_priv_testfunction example_slow_testfunction example_resource_initfunction example_resource_freefunction kunit_find_resourcefunction example_param_array_get_descfunction example_param_initfunction example_params_test_with_initfunction example_param_dynamic_arr_get_descfunction param_initfunction param_exitfunction example_params_test_with_init_dynamic_arrfunction init_addfunction example_init_test
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* Example KUnit test to show how to use KUnit.
*
* Copyright (C) 2019, Google LLC.
* Author: Brendan Higgins <brendanhiggins@google.com>
*/
#include <kunit/test.h>
#include <kunit/static_stub.h>
/*
* This is the most fundamental element of KUnit, the test case. A test case
* makes a set EXPECTATIONs and ASSERTIONs about the behavior of some code; if
* any expectations or assertions are not met, the test fails; otherwise, the
* test passes.
*
* In KUnit, a test case is just a function with the signature
* `void (*)(struct kunit *)`. `struct kunit` is a context object that stores
* information about the current test.
*/
static void example_simple_test(struct kunit *test)
{
/*
* This is an EXPECTATION; it is how KUnit tests things. When you want
* to test a piece of code, you set some expectations about what the
* code should do. KUnit then runs the test and verifies that the code's
* behavior matched what was expected.
*/
KUNIT_EXPECT_EQ(test, 1 + 1, 2);
}
/*
* This is run once before each test case, see the comment on
* example_test_suite for more information.
*/
static int example_test_init(struct kunit *test)
{
kunit_info(test, "initializing\n");
return 0;
}
/*
* This is run once after each test case, see the comment on
* example_test_suite for more information.
*/
static void example_test_exit(struct kunit *test)
{
kunit_info(test, "cleaning up\n");
}
/*
* This is run once before all test cases in the suite.
* See the comment on example_test_suite for more information.
*/
static int example_test_init_suite(struct kunit_suite *suite)
{
kunit_info(suite, "initializing suite\n");
return 0;
}
/*
* This is run once after all test cases in the suite.
* See the comment on example_test_suite for more information.
*/
static void example_test_exit_suite(struct kunit_suite *suite)
{
kunit_info(suite, "exiting suite\n");
}
/*
* This test should always be skipped.
*/
static void example_skip_test(struct kunit *test)
{
/* This line should run */
kunit_info(test, "You should not see a line below.");
/* Skip (and abort) the test */
kunit_skip(test, "this test should be skipped");
/* This line should not execute */
KUNIT_FAIL(test, "You should not see this line.");
}
/*
Annotation
- Immediate include surface: `kunit/test.h`, `kunit/static_stub.h`.
- Detected declarations: `function example_simple_test`, `function example_test_init`, `function example_test_exit`, `function example_test_init_suite`, `function example_test_exit_suite`, `function example_skip_test`, `function example_mark_skipped_test`, `function example_all_expect_macros_test`, `function add_one`, `function subtract_one`.
- Atlas domain: Kernel Services / lib.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.