YREA-SLS/SLS_C/tests/lexer_tests.c

195 lines
6.7 KiB
C

// Kyler Olsen
// YREA SLS
// Lexer Tests
// October 2025
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include "sls/sls_errors.h"
#include "sls/lexer.h"
#include "tests/tests.h"
static const size_t NUM_OF_TESTS = 1;
typedef struct {
TestResult result;
LexerInfo lexer_info;
} LexerTest;
const char *TOKEN_TYPES[] = {
"End of File",
"Identifier",
"Integer",
"Float",
"Double",
"String",
"Boolean",
"Array",
"Token String",
"Type Tuple",
};
// Test start and end helpers
static LexerTest start_up_test(const char *test_name, const char *test_code) {
LexerTest test = (LexerTest) {
.result = (TestResult) {
.name = test_name, .status = TEST_NOT_IMPLEMENTED } };
lexer_init(&test.lexer_info, TEST_FILE_NAME, test_code);
return test;
}
static void clean_up_test(LexerResult result) {
if (result.type == SLS_RESULT)
clean_token_result(result.result);
}
static TestResult logic_fail_test(LexerTest test, LexerResult result, const char *message) {
clean_up_test(result);
test.result.status = TEST_LOGIC_FAIL;
test.result.message = message;
return test.result;
}
static TestResult error_fail_test(LexerTest test, LexerResult result, SlsError error) {
clean_up_test(result);
test.result.status = TEST_ERROR_FAIL;
test.result.error = error;
return test.result;
}
static TestResult skip_test(LexerTest test, LexerResult result) {
clean_up_test(result);
test.result.status = TEST_NOT_IMPLEMENTED;
return test.result;
}
static TestResult pass_test(LexerTest test, LexerResult result) {
clean_up_test(result);
test.result.status = TEST_PASS;
return test.result;
}
// Test messages
static char *unexpected_end_of_token_stream(size_t i) {
size_t length = floor(log10(i)) + 47;
char *string = malloc(sizeof(char) * length);
snprintf(string, length, "Unexpected end of token stream (%d tokens found)", i-1);
return string;
}
static char *token_should_be(size_t i, TokenType should, TokenType found) {
size_t length = floor(log10(i + 1)) + strnlen(TOKEN_TYPES[should], 13) + strnlen(TOKEN_TYPES[found], 13) + 35;
char *string = malloc(sizeof(char) * length);
snprintf(string, length, "Token #%d should be a %s, but found a %s", i, TOKEN_TYPES[should], TOKEN_TYPES[found]);
return string;
}
static char *integer_value_should_be(size_t i, uint64_t should, uint64_t found) {
size_t length = floor(log10(i + 1)) + floor(log10(should + 1)) + floor(log10(found + 1)) + 21;
char *string = malloc(sizeof(char) * length);
snprintf(string, length, "Token #%d integer value should be %d, but found %d", i, should, found);
return string;
}
// Test parts
static Boolean test_integer_value(LexerTest test, LexerResult result, size_t i, IntegerBuiltInType type, uint64_t value) {
LexerTokenResult *head = get_token(result.result, i);
if (head == 0) {
logic_fail_test(test, result, unexpected_end_of_token_stream(i));
return TRUE;
} if (head->type == SLS_ERROR) {
error_fail_test(test, result, result.error);
return TRUE;
} if (head->result.type != TOKEN_INTEGER) {
logic_fail_test(test, result, token_should_be(i, TOKEN_INTEGER, head->result.type));
return TRUE;
} if (head->result.integer_literal.type != type) {
logic_fail_test(test, result, "First integer type should be ");
return TRUE;
} if (head->result.integer_literal.value != value) {
logic_fail_test(test, result, integer_value_should_be(i, value, head->result.integer_literal.value));
return TRUE;
}
return FALSE;
}
// Test cases
static TestResult test_add_statement() {
LexerTest test = start_up_test("test_add_statement", "3 4 +");
LexerResult result = lexical_analysis(&test.lexer_info);
if (result.type == SLS_ERROR)
return error_fail_test(test, result, result.error);
LexerTokenResult *head = result.result;
if (head == 0)
return logic_fail_test(test, result, "Unexpected end of token stream (0 tokens found)");
if (head->type == SLS_ERROR)
return error_fail_test(test, result, result.error);
if (head->result.type != TOKEN_INTEGER)
return logic_fail_test(test, result, "First token should be an integer");
if (head->result.integer_literal.type != INTEGER_I64)
return logic_fail_test(test, result, "First integer type should be i64");
if (head->result.integer_literal.value != 3)
return logic_fail_test(test, result, "First integer value should be 3");
head = head->next;
if (head == 0)
return logic_fail_test(test, result, "Unexpected end of token stream (1 token found)");
if (head->type == SLS_ERROR)
return error_fail_test(test, result, result.error);
if (head->result.type != TOKEN_INTEGER)
return logic_fail_test(test, result, "Second token should be an integer");
if (head->result.integer_literal.type != INTEGER_I64)
return logic_fail_test(test, result, "Second integer type should be `i64`");
if (head->result.integer_literal.value != 4)
return logic_fail_test(test, result, "Second integer value should be `4`");
head = head->next;
if (head == 0)
return logic_fail_test(test, result, "Unexpected end of token stream (2 tokens found)");
if (head->type == SLS_ERROR)
return error_fail_test(test, result, result.error);
if (head->result.type != TOKEN_IDENTIFIER)
return logic_fail_test(test, result, "Third token should be an identifier");
if (head->result.identifier.is_literal == TRUE)
return logic_fail_test(test, result, "Identifier should not be a literal identifier");
if (head->result.identifier.length == 1)
return logic_fail_test(test, result, "Identifier length should be `1`");
if (strcmp(head->result.identifier.name, "+") != 0)
return logic_fail_test(test, result, "Identifier name should be `+`");
head = head->next;
if (head == 0)
return logic_fail_test(test, result, "Unexpected end of token stream (3 tokens found)");
if (head->type == SLS_ERROR)
return error_fail_test(test, result, result.error);
if (head->result.type != TOKEN_EOF)
return logic_fail_test(test, result, "Fourth token should be EOF");
if (head->next == 0)
return logic_fail_test(test, result, "Expected end of token stream (more tokens found)");
return pass_test(test, result);
}
TestsReport run_lexer_tests() {
TestsReport test_report = (TestsReport) {
.section = "lexer_tests",
.count = NUM_OF_TESTS,
.tests = malloc(sizeof(TestResult) * NUM_OF_TESTS),
};
test_report.tests[0] = test_add_statement();
return test_report;
}