Files
scc/libs/pprocessor/tests/test_pp.c
zzy 73d74f5e13 refactor(pprocessor): rename macro table type and update function names
- Change `scc_macro_table_t` to `scc_pp_macro_table_t` for consistency
- Rename `scc_pp_macro_create` to `scc_pp_macro_new` for naming convention
- Remove unused `scc_pp_compress_whitespace` function
- Update macro table function names: `scc_pp_find_macro` → `scc_pp_macro_table_get`, `scc_pp_remove_macro` → `scc_pp_macro_table_remove`
- Add new `scc_pp_macro_table_set` function for setting macros
- Update all function signatures to use new type name
- Remove commented-out whitespace compression code from implementation
2025-12-14 12:59:03 +08:00

92 lines
2.5 KiB
C

#include <assert.h>
#include <pprocessor.h>
#include <stdio.h>
#include <stdlib.h>
#include <utest/acutest.h>
static scc_probe_stream_t *from_file_stream(FILE *fp) {
if (fseek(fp, 0, SEEK_END) != 0) {
perror("fseek failed");
return NULL;
}
usize fsize = ftell(fp);
if (fseek(fp, 0, SEEK_SET)) {
perror("fseek failed");
return NULL;
}
char *buffer = (char *)malloc(fsize);
usize read_ret = fread(buffer, 1, fsize, fp);
fclose(fp);
scc_mem_probe_stream_t *mem_stream = malloc(sizeof(scc_mem_probe_stream_t));
scc_probe_stream_t *stream =
scc_mem_probe_stream_init(mem_stream, buffer, read_ret, true);
return stream;
}
static void test_file(const char *name) {
char src_fname[1024];
char expected_fname[1024];
snprintf(src_fname, sizeof(src_fname), __FILE__ "/../pp/%s.c", name);
snprintf(expected_fname, sizeof(expected_fname),
__FILE__ "/../pp/%s.expect", name);
FILE *fsrc = fopen(src_fname, "r");
assert(fsrc != NULL);
FILE *fexpect = fopen(expected_fname, "r");
assert(fexpect != NULL);
scc_pproc_t pp;
scc_probe_stream_t *output_stream =
scc_pproc_init(&pp, from_file_stream(fsrc));
scc_probe_stream_t *expect_stream = from_file_stream(fexpect);
fclose(fsrc);
fclose(fexpect);
TEST_CASE(src_fname);
#define BUFFER_LEN 4096
int ch;
char expect_buffer[BUFFER_LEN];
char output_buffer[BUFFER_LEN];
usize size_produced = 0, size_expected = 0;
for (usize i = 0; i < BUFFER_LEN; ++i) {
ch = scc_probe_stream_consume(expect_stream);
if (ch != scc_stream_eof) {
expect_buffer[i] = (char)ch;
} else {
size_expected = i;
break;
}
}
for (usize i = 0; i < BUFFER_LEN; ++i) {
ch = scc_probe_stream_consume(output_stream);
if (ch != scc_stream_eof) {
output_buffer[i] = (char)ch;
} else {
size_produced = i;
break;
}
}
TEST_CHECK(size_produced == size_expected &&
memcmp(output_buffer, expect_buffer, size_produced) == 0);
TEST_DUMP("Expected:", expect_buffer, size_expected);
TEST_DUMP("Produced:", output_buffer, size_produced);
scc_pproc_drop(&pp);
}
static void test_basic(void) {
char name[32];
for (int i = 1; i <= 22; ++i) {
snprintf(name, sizeof(name), "%02d", i);
test_file(name);
}
}
TEST_LIST = {
{"basic", test_basic},
{NULL, NULL},
};