Files
cKit/Foundation/c_utf8_file.t.c
T
2026-08-29 01:50:50 +08:00

117 lines
5.1 KiB
C

#include "c_utf8_file.h"
#include <stdlib.h>
#include <stdio.h>
#define RUN_TEST(test_case, name) \
do { \
printf("[RUN] %s... ", name); \
if (test_case) { \
printf("\033[32mPASSED\033[0m\n"); \
} else { \
printf("\033[31mFAILED\033[0m (%s:%d)\n", __FILE__, __LINE__); \
return C_ERR_FAIL; \
} \
} while(0)
static c_err_t c_utf8_file_test(void) {
printf("==================================================\n");
printf(" STARTING C_UTF8_FILE UNIT TESTING \n");
printf("==================================================\n");
/* 26. UTF-8 File I/O Operations Verification */
c_StringBuffer_t write_sb;
c_StringBuffer_t read_sb;
const char* test_filename = "nlp_utf8_test.txt";
const char* payload = "NLP大模型_2026_🚀";
c_StringBuffer_Init(&write_sb, 32);
c_StringBuffer_Init(&read_sb, 32);
c_StringBuffer_AppendStr(&write_sb, payload);
// Test Point 1: Parameter checks protection
RUN_TEST(c_utf8_file_read(NULL, &read_sb) == C_ERR_PARAM, "File read handles NULL filepath strings");
RUN_TEST(c_utf8_file_write(test_filename, NULL, C_FALSE) == C_ERR_PARAM, "File write handles NULL buffer contexts");
// Test Point 2: Write text payload with explicit BOM injection enabled
c_err_t err = c_utf8_file_write(test_filename, &write_sb, C_TRUE);
RUN_TEST(err == C_ERR_OK, "UTF-8 data written to disk with BOM successfully");
// Test Point 3: Read text payload back from disk space
err = c_utf8_file_read(test_filename, &read_sb);
RUN_TEST(err == C_ERR_OK, "UTF-8 data read from disk successfully");
// Verify that the BOM was skipped and data size matches exactly
RUN_TEST(read_sb.size == write_sb.size, "File reader successfully filtered out the 3 BOM byte footprints from data tracking metrics");
RUN_TEST(strcmp(read_sb.buffer, payload) == 0, "Reconstructed disk text payload holds proper string characters perfectly");
// Cleanup resources and temporary test files
c_StringBuffer_Destroy(&write_sb);
c_StringBuffer_Destroy(&read_sb);
remove(test_filename); // Remove transient testing file assets from system layout
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
/* 27. UTF-8 File Logging Append & Streaming Line Read Verification */
c_StringBuffer_t io_sb;
c_StringBuffer_t line_sb;
const char* log_filename = "nlp_stream_test.txt";
c_StringBuffer_Init(&io_sb, 32);
c_StringBuffer_Init(&line_sb, 32);
// Test Point 1: Consecutive appends to evaluate new file vs exist rules
c_StringBuffer_AppendStr(&io_sb, "First Line: 自然语言\n");
err = c_utf8_file_append(log_filename, &io_sb, C_TRUE); // Creates file with BOM
RUN_TEST(err == C_ERR_OK, "Append created a new file with a BOM header successfully");
c_StringBuffer_Clear(&io_sb);
c_StringBuffer_AppendStr(&io_sb, "Second Line: 大模型🚀\n");
err = c_utf8_file_append(log_filename, &io_sb, C_TRUE); // Appends to existing file (BOM skipped)
RUN_TEST(err == C_ERR_OK, "Append safely added data to the existing file without duplicate BOM injections");
// Test Point 2: Streaming Line Reads via c_utf8_file_readline
FILE* stream_in = fopen(log_filename, "rb");
RUN_TEST(stream_in != NULL, "Opened log test file for stream reading");
// Handle initial optional BOM detection before streaming lines
unsigned char check_bom[3];
if (fread(check_bom, 1, 3, stream_in) == 3 && check_bom[0] == 0xEF && check_bom[1] == 0xBB && check_bom[2] == 0xBF) {
// BOM detected and skipped successfully
} else {
fseek(stream_in, 0, SEEK_SET);
}
// Read Line 1
err = c_utf8_file_readline(stream_in, &line_sb);
RUN_TEST(err == C_ERR_OK, "Read the first text line via streaming readline API");
RUN_TEST(strcmp(line_sb.buffer, "First Line: 自然语言") == 0, "Line 1 string matches, trailing newline stripped cleanly");
// Read Line 2
err = c_utf8_file_readline(stream_in, &line_sb);
RUN_TEST(err == C_ERR_OK, "Read the second text line via streaming readline API");
RUN_TEST(strcmp(line_sb.buffer, "Second Line: 大模型🚀") == 0, "Line 2 string matches multi-byte characters and Emojis perfectly");
// Read Line 3 (Expect EOF termination failure status)
err = c_utf8_file_readline(stream_in, &line_sb);
RUN_TEST(err == C_ERR_FAIL, "Readline returns C_ERR_FAIL cleanly when hitting EOF boundaries");
// Cleanup resources
fclose(stream_in);
c_StringBuffer_Destroy(&io_sb);
c_StringBuffer_Destroy(&line_sb);
remove(log_filename); // Purge volatile testing file asset
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
printf("==================================================\n");
printf("\033[32mSUCCESS: ALL C_UTF8_FILE COMPATIBLE INTERFACES PASSED!\033[0m\n");
printf("==================================================\n");
return C_ERR_OK;
}
int main(int argc, char** argv){
return c_utf8_file_test();
}