73 lines
2.8 KiB
C
73 lines
2.8 KiB
C
#include "c_FastByteRingBuffer.h"
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
|
|
void test_log(const char* test_name) {
|
|
printf("[PASS] %s\n", test_name);
|
|
}
|
|
|
|
int main() {
|
|
printf("==================================================\n");
|
|
printf(" 開始執行 c_FastByteRingBuffer 最終最佳化版測試\n");
|
|
printf("==================================================\n\n");
|
|
|
|
c_FastByteRingBuffer_t q;
|
|
// 故意傳入 6,測試是否會自動向上對齊到 8 (2的3次方)
|
|
c_err_t err = c_FastByteRingBuffer_Init(&q, 6);
|
|
assert(err == C_ERR_SUCCESS);
|
|
|
|
// 核心斷言:容量必須被修正為 8,遮罩必須為 7 (二進位 0111)
|
|
assert(q.capacity == 8);
|
|
assert(q.mask == 7);
|
|
assert(c_FastByteRingBuffer_IsEmpty(&q) == C_TRUE);
|
|
test_log("1. 2的冪次方自動容量對齊與遮罩初始化成功");
|
|
|
|
// ==========================================
|
|
// 2. 測試單一 Byte 寫入與讀取
|
|
// ==========================================
|
|
err = c_FastByteRingBuffer_WriteByte(&q, 0x11); assert(err == C_ERR_SUCCESS);
|
|
err = c_FastByteRingBuffer_WriteByte(&q, 0x22); assert(err == C_ERR_SUCCESS);
|
|
assert(c_FastByteRingBuffer_GetSize(&q) == 2);
|
|
|
|
uint8_t out = 0;
|
|
err = c_FastByteRingBuffer_ReadByte(&q, &out);
|
|
assert(err == C_ERR_SUCCESS);
|
|
assert(out == 0x11);
|
|
assert(c_FastByteRingBuffer_GetSize(&q) == 1);
|
|
test_log("2. 單一 Byte 讀寫與位元環形遞增驗證成功");
|
|
|
|
// 清空
|
|
c_FastByteRingBuffer_ReadByte(&q, &out);
|
|
assert(c_FastByteRingBuffer_IsEmpty(&q) == C_TRUE);
|
|
|
|
// ==========================================
|
|
// 3. 測試大量區塊資料串流與滿載邊界
|
|
// ==========================================
|
|
uint8_t src_stream[10] = {0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA};
|
|
|
|
// 目前容量為 8。傳入長度 10 的陣列,預期只能成功寫入 8 個位元組
|
|
c_size_t written = c_FastByteRingBuffer_WriteBuffer(&q, src_stream, 10);
|
|
assert(written == 8);
|
|
assert(c_FastByteRingBuffer_IsFull(&q) == C_TRUE);
|
|
|
|
// 溢位防呆檢查
|
|
assert(c_FastByteRingBuffer_WriteByte(&q, 0xFF) == C_ERR_FULL);
|
|
|
|
// 大量讀出驗證
|
|
uint8_t dest_stream[8] = {0};
|
|
c_size_t read = c_FastByteRingBuffer_ReadBuffer(&q, dest_stream, 8);
|
|
assert(read == 8);
|
|
assert(dest_stream[0] == 0xA1);
|
|
assert(dest_stream[7] == 0xA8);
|
|
assert(c_FastByteRingBuffer_IsEmpty(&q) == C_TRUE);
|
|
test_log("3. 區塊串流 API 與滿載位元遮罩邊界防呆成功");
|
|
|
|
c_FastByteRingBuffer_Destroy(&q);
|
|
test_log("4. 資源安全銷毀成功");
|
|
|
|
printf("\n==================================================\n");
|
|
printf(" 恭喜!快取位元最佳化版 RingBuffer 所有單元測試順利通過!\n");
|
|
printf("==================================================\n");
|
|
return 0;
|
|
} |