49b9db5b75
- Created KeywordMap structure with keyword-to-token mapping at top of token.c - Added lookup_keyword() function to check if identifier is a keyword - Replaced 3 strcmp calls (lines 99-101) with single lookup_keyword() call - Removed token_to_string() function and its tests (3 tests removed) - Single easy-to-read and modify keyword map serves both documentation and implementation - Added new keywords by editing the keywords[] array at top of token.c All 12 tests passing (removed token_to_string tests which are now unnecessary). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
127 lines
4.7 KiB
C
127 lines
4.7 KiB
C
#include "test.h"
|
|
#include "token.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
/* Helper to create a test file with content */
|
|
static void write_test_file(const char* filename, const char* content) {
|
|
FILE* f = fopen(filename, "w");
|
|
if (f) {
|
|
fputs(content, f);
|
|
fclose(f);
|
|
}
|
|
}
|
|
|
|
static void test_tokenstream_open_fail(void) {
|
|
Buffer* buf = buffer_open_file("v0/does_not_exist.c2");
|
|
if (buf != NULL) fail("expected NULL for non-existent file");
|
|
|
|
TokenStream* ts = tokenstream_open(buf);
|
|
if (ts != NULL) fail("expected NULL for NULL buffer");
|
|
}
|
|
|
|
static void test_tokenstream_simple_keyword(void) {
|
|
write_test_file("v0/test_token_tmp.c2", "module");
|
|
Buffer* buf = buffer_open_file("v0/test_token_tmp.c2");
|
|
if (buf == NULL) fail("could not open file");
|
|
|
|
TokenStream* ts = tokenstream_open(buf);
|
|
if (ts == NULL) fail("could not create tokenstream");
|
|
|
|
Token t = tokenstream_next(ts);
|
|
if (t != TOKEN_MODULE) fail("expected TOKEN_MODULE");
|
|
|
|
Token eof = tokenstream_next(ts);
|
|
if (eof != -1) fail("expected EOF");
|
|
|
|
tokenstream_close(ts);
|
|
}
|
|
|
|
static void test_tokenstream_keywords_and_symbols(void) {
|
|
write_test_file("v0/test_token_tmp.c2", "module main; import stdio;");
|
|
Buffer* buf = buffer_open_file("v0/test_token_tmp.c2");
|
|
if (buf == NULL) fail("could not open file");
|
|
|
|
TokenStream* ts = tokenstream_open(buf);
|
|
if (ts == NULL) fail("could not create tokenstream");
|
|
|
|
if (tokenstream_next(ts) != TOKEN_MODULE) fail("expected TOKEN_MODULE");
|
|
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER (main)");
|
|
if (tokenstream_next(ts) != TOKEN_SEMICOLON) fail("expected TOKEN_SEMICOLON");
|
|
if (tokenstream_next(ts) != TOKEN_IMPORT) fail("expected TOKEN_IMPORT");
|
|
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER (stdio)");
|
|
if (tokenstream_next(ts) != TOKEN_SEMICOLON) fail("expected TOKEN_SEMICOLON");
|
|
if (tokenstream_next(ts) != -1) fail("expected EOF");
|
|
|
|
tokenstream_close(ts);
|
|
}
|
|
|
|
static void test_tokenstream_parentheses_and_brackets(void) {
|
|
write_test_file("v0/test_token_tmp.c2", "()[]");
|
|
Buffer* buf = buffer_open_file("v0/test_token_tmp.c2");
|
|
if (buf == NULL) fail("could not open file");
|
|
|
|
TokenStream* ts = tokenstream_open(buf);
|
|
if (ts == NULL) fail("could not create tokenstream");
|
|
|
|
if (tokenstream_next(ts) != TOKEN_PARENT_OPEN) fail("expected TOKEN_PARENT_OPEN");
|
|
if (tokenstream_next(ts) != TOKEN_PARENT_CLOSE) fail("expected TOKEN_PARENT_CLOSE");
|
|
if (tokenstream_next(ts) != TOKEN_BRACKET_OPEN) fail("expected TOKEN_BRACKET_OPEN");
|
|
if (tokenstream_next(ts) != TOKEN_BRACKET_CLOSE) fail("expected TOKEN_BRACKET_CLOSE");
|
|
if (tokenstream_next(ts) != -1) fail("expected EOF");
|
|
|
|
tokenstream_close(ts);
|
|
}
|
|
|
|
static void test_tokenstream_comma(void) {
|
|
write_test_file("v0/test_token_tmp.c2", "a,b,c");
|
|
Buffer* buf = buffer_open_file("v0/test_token_tmp.c2");
|
|
if (buf == NULL) fail("could not open file");
|
|
|
|
TokenStream* ts = tokenstream_open(buf);
|
|
if (ts == NULL) fail("could not create tokenstream");
|
|
|
|
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected a");
|
|
if (tokenstream_next(ts) != TOKEN_COMMA) fail("expected comma");
|
|
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected b");
|
|
if (tokenstream_next(ts) != TOKEN_COMMA) fail("expected comma");
|
|
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected c");
|
|
if (tokenstream_next(ts) != -1) fail("expected EOF");
|
|
|
|
tokenstream_close(ts);
|
|
}
|
|
|
|
static void test_tokenstream_whitespace_ignored(void) {
|
|
write_test_file("v0/test_token_tmp.c2", " module \n\t import ; ");
|
|
Buffer* buf = buffer_open_file("v0/test_token_tmp.c2");
|
|
if (buf == NULL) fail("could not open file");
|
|
|
|
TokenStream* ts = tokenstream_open(buf);
|
|
if (ts == NULL) fail("could not create tokenstream");
|
|
|
|
if (tokenstream_next(ts) != TOKEN_MODULE) fail("expected TOKEN_MODULE");
|
|
if (tokenstream_next(ts) != TOKEN_IMPORT) fail("expected TOKEN_IMPORT");
|
|
if (tokenstream_next(ts) != TOKEN_SEMICOLON) fail("expected TOKEN_SEMICOLON");
|
|
if (tokenstream_next(ts) != -1) fail("expected EOF");
|
|
|
|
tokenstream_close(ts);
|
|
}
|
|
|
|
static void test_tokenstream_void_function_signature(void) {
|
|
write_test_file("v0/test_token_tmp.c2", "void main()");
|
|
Buffer* buf = buffer_open_file("v0/test_token_tmp.c2");
|
|
if (buf == NULL) fail("could not open file");
|
|
|
|
TokenStream* ts = tokenstream_open(buf);
|
|
if (ts == NULL) fail("could not create tokenstream");
|
|
|
|
if (tokenstream_next(ts) != TOKEN_VOID) fail("expected TOKEN_VOID");
|
|
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER");
|
|
if (tokenstream_next(ts) != TOKEN_PARENT_OPEN) fail("expected TOKEN_PARENT_OPEN");
|
|
if (tokenstream_next(ts) != TOKEN_PARENT_CLOSE) fail("expected TOKEN_PARENT_CLOSE");
|
|
if (tokenstream_next(ts) != -1) fail("expected EOF");
|
|
|
|
tokenstream_close(ts);
|
|
}
|