// 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]);
}
}
staticvoid kfifo_test_define_should_equal_declare_init(struct kunit *test)
{ // declare a variable my_fifo of type struct kfifo of u8
DECLARE_KFIFO(my_fifo1, u8, KFIFO_SIZE); // initialize the my_fifo variable
INIT_KFIFO(my_fifo1);
// DEFINE_KFIFO declares the variable with the initial value // essentially the same as calling DECLARE_KFIFO and INIT_KFIFO
DEFINE_KFIFO(my_fifo2, u8, KFIFO_SIZE);
// my_fifo1 and my_fifo2 have the same size
KUNIT_EXPECT_EQ(test, sizeof(my_fifo1), sizeof(my_fifo2));
KUNIT_EXPECT_EQ(test, kfifo_initialized(&my_fifo1),
kfifo_initialized(&my_fifo2));
KUNIT_EXPECT_EQ(test, kfifo_is_empty(&my_fifo1),
kfifo_is_empty(&my_fifo2));
}
staticvoid kfifo_test_alloc_should_initiliaze_a_ptr_fifo(struct kunit *test)
{ int ret;
DECLARE_KFIFO_PTR(my_fifo, u8);
INIT_KFIFO(my_fifo);
// kfifo_initialized returns false signaling the buffer pointer is NULL
KUNIT_EXPECT_FALSE(test, kfifo_initialized(&my_fifo));
// kfifo_alloc allocates the buffer
ret = kfifo_alloc(&my_fifo, KFIFO_SIZE, GFP_KERNEL);
KUNIT_EXPECT_EQ_MSG(test, ret, 0, "Memory allocation should succeed");
KUNIT_EXPECT_TRUE(test, kfifo_initialized(&my_fifo));
// kfifo_free frees the buffer
kfifo_free(&my_fifo);
}
staticvoid kfifo_test_peek_should_not_remove_elements(struct kunit *test)
{
u8 out_data; int processed_elements;
DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
// If the fifo is empty, peek returns 0
processed_elements = kfifo_peek(&my_fifo, &out_data);
KUNIT_EXPECT_EQ(test, processed_elements, 0);
// Using peek doesn't remove the element // so the read element and the fifo length // remains the same
processed_elements = kfifo_peek(&my_fifo, &out_data);
KUNIT_EXPECT_EQ(test, processed_elements, 1);
KUNIT_EXPECT_EQ(test, out_data, 3);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.