diff --git a/SLS_C/tests/lexer_tests.c b/SLS_C/tests/lexer_tests.c index 63c6a86..7f28d3f 100644 --- a/SLS_C/tests/lexer_tests.c +++ b/SLS_C/tests/lexer_tests.c @@ -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; } @@ -147,7 +172,7 @@ static char *token_name_should_be(size_t i, TokenType type, size_t length, const 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+1)); + logic_fail_test(test, result, unexpected_end_of_token_stream(i + 1)); return TRUE; } if (head->type == SLS_ERROR) { error_fail_test(test, result, result.error); @@ -168,7 +193,7 @@ static Boolean test_integer_value(LexerTest *test, LexerResult result, size_t i, static Boolean test_identifier_value(LexerTest *test, LexerResult result, size_t i, Boolean is_literal, const char *name) { LexerTokenResult *head = get_token(result.result, i); if (head == 0) { - logic_fail_test(test, result, unexpected_end_of_token_stream(i+1)); + logic_fail_test(test, result, unexpected_end_of_token_stream(i + 1)); return TRUE; } if (head->type == SLS_ERROR) { error_fail_test(test, result, result.error); @@ -192,7 +217,7 @@ static Boolean test_identifier_value(LexerTest *test, LexerResult result, size_t static Boolean test_eof_value(LexerTest *test, LexerResult result, size_t i) { LexerTokenResult *head = get_token(result.result, i); if (head == 0) { - logic_fail_test(test, result, unexpected_end_of_token_stream(i+1)); + logic_fail_test(test, result, unexpected_end_of_token_stream(i + 1)); return TRUE; } if (head->type == SLS_ERROR) { error_fail_test(test, result, result.error); @@ -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;