lib/tests/kfifo_kunit.c
Source file repositories/reference/linux-study-clean/lib/tests/kfifo_kunit.c
File Facts
- System
- Linux kernel
- Corpus path
lib/tests/kfifo_kunit.c- Extension
.c- Size
- 6400 bytes
- Lines
- 225
- 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/kfifo.h
Detected Declarations
function Copyrightfunction kfifo_test_define_should_define_an_empty_fifofunction kfifo_test_len_should_ret_n_of_stored_elementsfunction kfifo_test_put_should_insert_and_get_should_popfunction kfifo_test_in_should_insert_multiple_elementsfunction kfifo_test_out_should_pop_multiple_elementsfunction kfifo_test_dec_init_should_define_an_empty_fifofunction kfifo_test_define_should_equal_declare_initfunction kfifo_test_alloc_should_initiliaze_a_ptr_fifofunction kfifo_test_peek_should_not_remove_elements
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* KUnit test for the generic kernel FIFO implementation.
*
* Copyright (C) 2024 Diego Vieira <diego.daniel.professional@gmail.com>
*/
#include <kunit/test.h>
#include <linux/kfifo.h>
#define KFIFO_SIZE 32
#define N_ELEMENTS 5
static void kfifo_test_reset_should_clear_the_fifo(struct kunit *test)
{
DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
kfifo_put(&my_fifo, 1);
kfifo_put(&my_fifo, 2);
kfifo_put(&my_fifo, 3);
KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 3);
kfifo_reset(&my_fifo);
KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
KUNIT_EXPECT_TRUE(test, kfifo_is_empty(&my_fifo));
}
static void kfifo_test_define_should_define_an_empty_fifo(struct kunit *test)
{
DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
KUNIT_EXPECT_TRUE(test, kfifo_initialized(&my_fifo));
KUNIT_EXPECT_TRUE(test, kfifo_is_empty(&my_fifo));
KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
}
static void kfifo_test_len_should_ret_n_of_stored_elements(struct kunit *test)
{
u8 buffer1[N_ELEMENTS];
for (int i = 0; i < N_ELEMENTS; i++)
buffer1[i] = i + 1;
DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
kfifo_in(&my_fifo, buffer1, N_ELEMENTS);
KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), N_ELEMENTS);
kfifo_in(&my_fifo, buffer1, N_ELEMENTS);
KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), N_ELEMENTS * 2);
kfifo_reset(&my_fifo);
KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
}
static void kfifo_test_put_should_insert_and_get_should_pop(struct kunit *test)
{
u8 out_data = 0;
int processed_elements;
u8 elements[] = { 3, 5, 11 };
DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
// If the fifo is empty, get returns 0
processed_elements = kfifo_get(&my_fifo, &out_data);
KUNIT_EXPECT_EQ(test, processed_elements, 0);
KUNIT_EXPECT_EQ(test, out_data, 0);
for (int i = 0; i < 3; i++)
kfifo_put(&my_fifo, elements[i]);
for (int i = 0; i < 3; i++) {
processed_elements = kfifo_get(&my_fifo, &out_data);
KUNIT_EXPECT_EQ(test, processed_elements, 1);
KUNIT_EXPECT_EQ(test, out_data, elements[i]);
}
}
static void kfifo_test_in_should_insert_multiple_elements(struct kunit *test)
{
u8 in_buffer[] = { 11, 25, 65 };
u8 out_data;
int processed_elements;
DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
kfifo_in(&my_fifo, in_buffer, 3);
Annotation
- Immediate include surface: `kunit/test.h`, `linux/kfifo.h`.
- Detected declarations: `function Copyright`, `function kfifo_test_define_should_define_an_empty_fifo`, `function kfifo_test_len_should_ret_n_of_stored_elements`, `function kfifo_test_put_should_insert_and_get_should_pop`, `function kfifo_test_in_should_insert_multiple_elements`, `function kfifo_test_out_should_pop_multiple_elements`, `function kfifo_test_dec_init_should_define_an_empty_fifo`, `function kfifo_test_define_should_equal_declare_init`, `function kfifo_test_alloc_should_initiliaze_a_ptr_fifo`, `function kfifo_test_peek_should_not_remove_elements`.
- 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.