Improved string memory handling

This commit is contained in:
Kyler Olsen 2025-10-30 13:40:55 -06:00
parent 19783bbe6b
commit 271470ff39
1 changed files with 29 additions and 4 deletions

View File

@ -59,6 +59,8 @@ static void clean_up_test(LexerResult result) {
}
static TestResult logic_fail_test(LexerTest *test, LexerResult result, const char *message) {
if (message == 0) return error_test(test, result, (SlsError) {
.message = "Out of Memory Error!", .code = 3458, });
clean_up_test(result);
test->result.status = TEST_LOGIC_FAIL;
test->result.message = message;
@ -72,6 +74,13 @@ static TestResult error_fail_test(LexerTest *test, LexerResult result, SlsError
return test->result;
}
static TestResult error_test(LexerTest *test, LexerResult result, SlsError error) {
clean_up_test(result);
test->result.status = TEST_ERROR;
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;
@ -89,13 +98,23 @@ static TestResult pass_test(LexerTest *test, LexerResult result) {
static char *unexpected_end_of_token_stream(size_t i) {
size_t length = floor(log10(i)) + 47;
char *string = malloc(sizeof(char) * length);
if (string = 0) return string;
snprintf(string, length, "Unexpected end of token stream (%d tokens found)", i-1);
return string;
}
static char *expected_end_of_token_stream(size_t i) {
size_t length = floor(log10(i)) + 47;
char *string = malloc(sizeof(char) * length);
if (string = 0) return string;
snprintf(string, length, "Expected end of token stream (more than %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_NAMES[should], 13) + strnlen(TOKEN_TYPES_NAMES[found], 13) + 35;
char *string = malloc(sizeof(char) * length);
if (string = 0) return string;
snprintf(string, length, "Token #%d should be a %s, but found a %s", i, TOKEN_TYPES_NAMES[should], TOKEN_TYPES_NAMES[found]);
return string;
}
@ -103,6 +122,7 @@ static char *token_should_be(size_t i, TokenType should, TokenType found) {
static char *integer_type_should_be(size_t i, TokenType should, TokenType found) {
size_t length = floor(log10(i + 1)) + strnlen(INTEGER_TYPES_NAMES[should], 5) + strnlen(INTEGER_TYPES_NAMES[found], 5) + 48;
char *string = malloc(sizeof(char) * length);
if (string = 0) return string;
snprintf(string, length, "Token #%d integer type should be a %s, but found a %s", i, TOKEN_TYPES_NAMES[should], TOKEN_TYPES_NAMES[found]);
return string;
}
@ -110,6 +130,7 @@ static char *integer_type_should_be(size_t i, TokenType should, TokenType found)
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);
if (string = 0) return string;
snprintf(string, length, "Token #%d integer value should be %d, but found %d", i, should, found);
return string;
}
@ -117,6 +138,7 @@ static char *integer_value_should_be(size_t i, uint64_t should, uint64_t found)
static char *identifier_should_be_literal(size_t i) {
size_t length = floor(log10(i + 1)) + 51;
char *string = malloc(sizeof(char) * length);
if (string = 0) return string;
snprintf(string, length, "Token #%d identifier should be an identifier literal", i);
return string;
}
@ -124,6 +146,7 @@ static char *identifier_should_be_literal(size_t i) {
static char *identifier_should_not_be_literal(size_t i) {
size_t length = floor(log10(i + 1)) + 55;
char *string = malloc(sizeof(char) * length);
if (string = 0) return string;
snprintf(string, length, "Token #%d identifier should not be an identifier literal", i);
return string;
}
@ -131,6 +154,7 @@ static char *identifier_should_not_be_literal(size_t i) {
static char *token_length_should_be(size_t i, TokenType type, uint64_t should, uint64_t found) {
size_t length = floor(log10(i + 1)) + strnlen(TOKEN_TYPES_NAMES[type], 13) + floor(log10(should + 1)) + floor(log10(found + 1)) + 47;
char *string = malloc(sizeof(char) * length);
if (string = 0) return string;
snprintf(string, length, "Token #%d of type %s length should be %d, but found %d", i, TOKEN_TYPES_NAMES[type], should, found);
return string;
}
@ -138,6 +162,7 @@ static char *token_length_should_be(size_t i, TokenType type, uint64_t should, u
static char *token_name_should_be(size_t i, TokenType type, size_t length, const char *should, const char *found) {
size_t length = floor(log10(i + 1)) + strnlen(TOKEN_TYPES_NAMES[type], 13) + strnlen(should, length) + strnlen(found, length) + 45;
char *string = malloc(sizeof(char) * length);
if (string = 0) return string;
snprintf(string, length, "Token #%d of type %s name should be %s, but found %s", i, TOKEN_TYPES_NAMES[type], should, found);
return string;
}
@ -201,7 +226,7 @@ static Boolean test_eof_value(LexerTest *test, LexerResult result, size_t i) {
logic_fail_test(test, result, token_should_be(i + 1, TOKEN_EOF, head->result.type));
return TRUE;
} if (head->next != 0) {
logic_fail_test(test, result, "Expected end of token stream (more tokens found)");
logic_fail_test(test, result, expected_end_of_token_stream(i + 1));
return TRUE;
}
return FALSE;