diff --git a/src/main.c b/src/main.c index bc54a62..524d18b 100755 --- a/src/main.c +++ b/src/main.c @@ -3,38 +3,78 @@ * @author guishenking (guishenking@outlook.com) * @brief * @version 0.1 - * @date 2025-07-24 + * @date 2025-08-15 * * @copyright Copyright (c) 2025 * */ - - +#include #include "lib/kfifo/kfifo.h" -#include -#include -#include - -kfifo_def(fifo,1024); - - int main(){ - // 示例数据 - uint8_t src_data[] = {0x01, 0x02, 0x03, 0x04, 0x05}; - size_t src_length = sizeof(src_data); - uint8_t dest_data[1024]; - size_t dest_length = sizeof(dest_data); - uint32_t offset; - - memset(dest_data, 0, dest_length); // 清空目标数据缓冲区 - - kfifo_in(&fifo, dest_data, dest_length); // 将数据写入FIFO - - int ret = kfifo_peek_block(&fifo, dest_data, dest_length); // 从FIFO读取数据 - - memset(src_data, 0, src_length); // 清空源数据缓冲区 - - // 解码数据 - kfifo_seek(&fifo, offset); // 根据偏移量调整FIFO读取位置 +int main() { + printf("KFIFO Test Program\n"); + + // 测试静态FIFO + printf("\n=== Testing Static FIFO ===\n"); + kfifo_def(static_fifo, 8); // 创建大小为8的静态FIFO + + // 测试空状态 + printf("Is empty (should be 1): %d\n", kfifo_is_empty(&static_fifo)); + + // 测试写入单个字节 + kfifo_put_byte(&static_fifo, 0xAA); + printf("After put 0xAA, used: %u\n", kfifo_used(&static_fifo)); + + // 测试读取单个字节 + uint8_t byte; + kfifo_get_byte(&static_fifo, &byte); + printf("Got byte: 0x%02X\n", byte); + + // 测试块写入 + const uint8_t test_data[] = {0x11, 0x22, 0x33, 0x44, 0x55}; + int written = kfifo_in(&static_fifo, test_data, sizeof(test_data)); + printf("Wrote %d bytes, used: %u\n", written, kfifo_used(&static_fifo)); + + // 测试块读取 + uint8_t read_buf[8]; + int read = kfifo_out(&static_fifo, read_buf, sizeof(read_buf)); + printf("Read %d bytes: ", read); + for (int i = 0; i < read; i++) { + printf("0x%02X ", read_buf[i]); + } + printf("\n"); + + // 测试动态FIFO + printf("\n=== Testing Dynamic FIFO ===\n"); + kfifo_t *dynamic_fifo = kfifo_init_dynamic(16); + if (!dynamic_fifo) { + printf("Failed to create dynamic FIFO\n"); + return -1; + } + + // 测试写入超过容量 + const uint8_t big_data[20] = {0}; + written = kfifo_in(dynamic_fifo, big_data, sizeof(big_data)); + printf("Tried to write 20 bytes to 16-byte FIFO, actually wrote %d\n", written); + + // 测试peek功能 + const uint8_t *peek_ptr; + int peek_len = kfifo_peek(dynamic_fifo, &peek_ptr); + printf("Peeked %d bytes at %p\n", peek_len, peek_ptr); + + // 测试seek功能 + int seeked = kfifo_seek(dynamic_fifo, 8); + printf("Seeked %d bytes\n", seeked); + + // 测试peek_seek功能 + peek_len = kfifo_peek_seek(dynamic_fifo, &peek_ptr, 4); + printf("Peeked %d bytes after seeking 4\n", peek_len); + + // 清理动态FIFO + if (!kfifo_destroy(dynamic_fifo)) { + printf("Failed to destroy dynamic FIFO\n"); + } + + printf("\nAll tests completed\n"); return 0; - } \ No newline at end of file +} \ No newline at end of file