- 在argparse库中将所有null指针常量替换为nullptr - 更新头文件和源文件中的指针初始化和比较操作 - 修改测试文件中的相关断言检查 - 更新AST定义文件中的注释说明
139 lines
4.4 KiB
C
139 lines
4.4 KiB
C
#include <assert.h>
|
|
#include <scc_pproc.h>
|
|
#include <string.h>
|
|
#include <utest/acutest.h>
|
|
|
|
static void test_define_pos(void) {
|
|
int ret = 0;
|
|
scc_sstream_t mem_stream;
|
|
const char *input = "#define OBJ 1\nOBJ";
|
|
ret = scc_sstream_init_by_buffer(&mem_stream, input, strlen(input), false,
|
|
16);
|
|
Assert(ret == 0);
|
|
|
|
scc_lexer_t lexer;
|
|
scc_lexer_init(&lexer, scc_sstream_to_ring(&mem_stream));
|
|
|
|
scc_pproc_t pp;
|
|
scc_pproc_init(&pp, scc_lexer_to_ring(&lexer, 8, true));
|
|
|
|
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pp, 8, true, true);
|
|
scc_lexer_tok_t tok = {0};
|
|
|
|
scc_ring_next_consume(*tok_ring, tok, ret);
|
|
Assert(ret == true);
|
|
TEST_CHECK(tok.loc.line == 2 && tok.loc.col == 1 &&
|
|
scc_strcmp(tok.lexeme.data, "1") == 0);
|
|
TEST_MSG("Expected: %d:%d:%s", 2, 1, "1");
|
|
TEST_MSG("Produced: %zu:%zu:%s", tok.loc.line, tok.loc.col,
|
|
tok.lexeme.data);
|
|
|
|
scc_ring_free(*tok_ring);
|
|
scc_pproc_drop(&pp);
|
|
scc_lexer_drop(&lexer);
|
|
scc_sstream_drop(&mem_stream);
|
|
}
|
|
|
|
static void test_define_double_pos(void) {
|
|
int ret = 0;
|
|
scc_sstream_t mem_stream;
|
|
const char *input = "#define _OBJ 1\n#define OBJ _OBJ\nOBJ";
|
|
ret = scc_sstream_init_by_buffer(&mem_stream, input, strlen(input), false,
|
|
16);
|
|
Assert(ret == 0);
|
|
|
|
scc_lexer_t lexer;
|
|
scc_lexer_init(&lexer, scc_sstream_to_ring(&mem_stream));
|
|
|
|
scc_pproc_t pp;
|
|
scc_pproc_init(&pp, scc_lexer_to_ring(&lexer, 8, true));
|
|
|
|
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pp, 8, true, true);
|
|
scc_lexer_tok_t tok = {0};
|
|
|
|
scc_ring_next_consume(*tok_ring, tok, ret);
|
|
Assert(ret == true);
|
|
TEST_CHECK(tok.loc.line == 3 && tok.loc.col == 1 &&
|
|
scc_strcmp(tok.lexeme.data, "1") == 0);
|
|
TEST_MSG("Expected: %d:%d:%s", 3, 1, "1");
|
|
TEST_MSG("Produced: %zu:%zu:%s", tok.loc.line, tok.loc.col,
|
|
tok.lexeme.data);
|
|
|
|
scc_ring_free(*tok_ring);
|
|
scc_pproc_drop(&pp);
|
|
scc_lexer_drop(&lexer);
|
|
scc_sstream_drop(&mem_stream);
|
|
}
|
|
|
|
static void test_define_param_pos(void) {
|
|
int ret = 0;
|
|
scc_sstream_t mem_stream;
|
|
const char *input = "#define OBJ 1\n#define func(x) x\nfunc(OBJ)";
|
|
ret = scc_sstream_init_by_buffer(&mem_stream, input, strlen(input), false,
|
|
16);
|
|
Assert(ret == 0);
|
|
|
|
scc_lexer_t lexer;
|
|
scc_lexer_init(&lexer, scc_sstream_to_ring(&mem_stream));
|
|
|
|
scc_pproc_t pp;
|
|
scc_pproc_init(&pp, scc_lexer_to_ring(&lexer, 8, true));
|
|
|
|
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pp, 8, true, true);
|
|
scc_lexer_tok_t tok = {0};
|
|
|
|
scc_ring_next_consume(*tok_ring, tok, ret);
|
|
Assert(ret == true);
|
|
TEST_CHECK(tok.loc.line == 3 && tok.loc.col == 1 &&
|
|
scc_strcmp(tok.lexeme.data, "1") == 0);
|
|
TEST_MSG("Expected: %d:%d:%s", 3, 1, "1");
|
|
TEST_MSG("Produced: %zu:%zu:%s", tok.loc.line, tok.loc.col,
|
|
tok.lexeme.data);
|
|
|
|
scc_ring_free(*tok_ring);
|
|
scc_pproc_drop(&pp);
|
|
scc_lexer_drop(&lexer);
|
|
scc_sstream_drop(&mem_stream);
|
|
}
|
|
|
|
static void test_define_stringify_pos(void) {
|
|
int ret = 0;
|
|
scc_sstream_t mem_stream;
|
|
const char *input =
|
|
"#define _STR(x) #x\n#define STR(x) _STR(x)\n#define OBJ 1\nSTR(OBJ)";
|
|
ret = scc_sstream_init_by_buffer(&mem_stream, input, strlen(input), false,
|
|
16);
|
|
Assert(ret == 0);
|
|
|
|
scc_lexer_t lexer;
|
|
scc_lexer_init(&lexer, scc_sstream_to_ring(&mem_stream));
|
|
|
|
scc_pproc_t pp;
|
|
scc_pproc_init(&pp, scc_lexer_to_ring(&lexer, 8, true));
|
|
|
|
scc_lexer_tok_ring_t *tok_ring = scc_pproc_to_ring(&pp, 8, true, true);
|
|
scc_lexer_tok_t tok = {0};
|
|
|
|
scc_ring_next_consume(*tok_ring, tok, ret);
|
|
Assert(ret == true);
|
|
TEST_CHECK(tok.loc.line == 4 && tok.loc.col == 1 &&
|
|
scc_strcmp(tok.lexeme.data, "\"1\"") == 0);
|
|
TEST_MSG("Expected: %d:%d:%s", 4, 1, "\"1\"");
|
|
TEST_MSG("Produced: %zu:%zu:%s", tok.loc.line, tok.loc.col,
|
|
tok.lexeme.data);
|
|
|
|
scc_ring_free(*tok_ring);
|
|
scc_pproc_drop(&pp);
|
|
scc_lexer_drop(&lexer);
|
|
scc_sstream_drop(&mem_stream);
|
|
}
|
|
|
|
#define TEST_LIST_CASE(func_name) {#func_name, func_name}
|
|
TEST_LIST = {
|
|
TEST_LIST_CASE(test_define_pos),
|
|
TEST_LIST_CASE(test_define_double_pos),
|
|
TEST_LIST_CASE(test_define_param_pos),
|
|
TEST_LIST_CASE(test_define_stringify_pos),
|
|
{nullptr, nullptr},
|
|
};
|