lib/tests/min_heap_kunit.c
Source file repositories/reference/linux-study-clean/lib/tests/min_heap_kunit.c
File Facts
- System
- Linux kernel
- Corpus path
lib/tests/min_heap_kunit.c- Extension
.c- Size
- 5743 bytes
- Lines
- 210
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
kunit/test.hlinux/min_heap.hlinux/module.hlinux/random.h
Detected Declarations
struct min_heap_test_casefunction less_thanfunction greater_thanfunction pop_verify_heapfunction test_heapify_allfunction test_heap_pushfunction test_heap_pop_pushfunction test_heap_del
Annotated Snippet
struct min_heap_test_case {
const char *str;
bool min_heap;
};
static struct min_heap_test_case min_heap_cases[] = {
{
.str = "min",
.min_heap = true,
},
{
.str = "max",
.min_heap = false,
},
};
KUNIT_ARRAY_PARAM_DESC(min_heap, min_heap_cases, str);
DEFINE_MIN_HEAP(int, min_heap_test);
static bool less_than(const void *lhs, const void *rhs, void __always_unused *args)
{
return *(int *)lhs < *(int *)rhs;
}
static bool greater_than(const void *lhs, const void *rhs, void __always_unused *args)
{
return *(int *)lhs > *(int *)rhs;
}
static void pop_verify_heap(struct kunit *test,
bool min_heap,
struct min_heap_test *heap,
const struct min_heap_callbacks *funcs)
{
int *values = heap->data;
int last;
last = values[0];
min_heap_pop_inline(heap, funcs, NULL);
while (heap->nr > 0) {
if (min_heap)
KUNIT_EXPECT_LE(test, last, values[0]);
else
KUNIT_EXPECT_GE(test, last, values[0]);
last = values[0];
min_heap_pop_inline(heap, funcs, NULL);
}
}
static void test_heapify_all(struct kunit *test)
{
const struct min_heap_test_case *params = test->param_value;
int values[] = { 3, 1, 2, 4, 0x8000000, 0x7FFFFFF, 0,
-3, -1, -2, -4, 0x8000000, 0x7FFFFFF };
struct min_heap_test heap = {
.data = values,
.nr = ARRAY_SIZE(values),
.size = ARRAY_SIZE(values),
};
struct min_heap_callbacks funcs = {
.less = params->min_heap ? less_than : greater_than,
.swp = NULL,
};
int i;
/* Test with known set of values. */
min_heapify_all_inline(&heap, &funcs, NULL);
pop_verify_heap(test, params->min_heap, &heap, &funcs);
/* Test with randomly generated values. */
heap.nr = ARRAY_SIZE(values);
for (i = 0; i < heap.nr; i++)
values[i] = get_random_u32();
min_heapify_all_inline(&heap, &funcs, NULL);
pop_verify_heap(test, params->min_heap, &heap, &funcs);
}
static void test_heap_push(struct kunit *test)
{
const struct min_heap_test_case *params = test->param_value;
const int data[] = { 3, 1, 2, 4, 0x80000000, 0x7FFFFFFF, 0,
-3, -1, -2, -4, 0x80000000, 0x7FFFFFFF };
int values[ARRAY_SIZE(data)];
struct min_heap_test heap = {
.data = values,
.nr = 0,
.size = ARRAY_SIZE(values),
};
Annotation
- Immediate include surface: `kunit/test.h`, `linux/min_heap.h`, `linux/module.h`, `linux/random.h`.
- Detected declarations: `struct min_heap_test_case`, `function less_than`, `function greater_than`, `function pop_verify_heap`, `function test_heapify_all`, `function test_heap_push`, `function test_heap_pop_push`, `function test_heap_del`.
- 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.