Started updating test helpers to standard function signatures
This commit is contained in:
parent
66a50da66c
commit
ef0008df77
|
|
@ -21,16 +21,6 @@ typedef struct {
|
|||
LexerInfo lexer_info;
|
||||
} LexerTest;
|
||||
|
||||
typedef struct {
|
||||
const char *value;
|
||||
size_t length;
|
||||
} ArrayStringValues;
|
||||
|
||||
typedef struct {
|
||||
Boolean (*token_handler)(LexerTest *, LexerResult, size_t, size_t, Token *, void *);
|
||||
void *value;
|
||||
} TokenStringValue;
|
||||
|
||||
// Test start and end helpers
|
||||
|
||||
static LexerTest start_up_test(const char *test_name, const char *test_code) {
|
||||
|
|
@ -271,102 +261,6 @@ static Boolean test_token_type(LexerTest *test, LexerResult result, size_t i, To
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_eof_value(LexerTest *test, LexerResult result, size_t i) {
|
||||
static const TokenType token_type = TOKEN_EOF;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, token_type)) {
|
||||
return TRUE;
|
||||
} if (head->next != 0) {
|
||||
logic_fail_test(test, result, expected_end_of_token_stream(i + 1));
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_identifier_value(LexerTest *test, LexerResult result, size_t i, Boolean is_literal, size_t length, const char *name) {
|
||||
static const TokenType token_type = TOKEN_IDENTIFIER;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, token_type)) {
|
||||
return TRUE;
|
||||
} if (head->result.identifier.is_literal != is_literal) {
|
||||
logic_fail_test(test, result, is_literal ? identifier_should_be_literal(i + 1) : identifier_should_not_be_literal(i + 1));
|
||||
return TRUE;
|
||||
} if (head->result.identifier.length == strnlen(name, length)) {
|
||||
logic_fail_test(test, result, token_length_should_be(i + 1, token_type, strnlen(name, length), head->result.identifier.length));
|
||||
return TRUE;
|
||||
} if (strncmp(head->result.identifier.name, name, length) != 0) {
|
||||
logic_fail_test(test, result, token_value_string_should_be(i + 1, token_type, strnlen(name, length), head->result.identifier.name, name));
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_integer_value(LexerTest *test, LexerResult result, size_t i, IntegerBuiltInType type, uint64_t value) {
|
||||
static const TokenType token_type = TOKEN_INTEGER;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, token_type)) {
|
||||
return TRUE;
|
||||
} if (head->result.integer_literal.type != type) {
|
||||
logic_fail_test(test, result, integer_type_should_be(i + 1, type, head->result.integer_literal.type));
|
||||
return TRUE;
|
||||
} if (head->result.integer_literal.value != value) {
|
||||
logic_fail_test(test, result, integer_value_should_be(i + 1, value, head->result.integer_literal.value));
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_float_value(LexerTest *test, LexerResult result, size_t i, float value) {
|
||||
static const TokenType token_type = TOKEN_FLOAT;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, token_type)) {
|
||||
return TRUE;
|
||||
} if (fabsf(head->result.float_literal - value) >= FLOAT_TEST_PRECISION) {
|
||||
logic_fail_test(test, result, float_value_should_be(i + 1, value, head->result.float_literal));
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_double_value(LexerTest *test, LexerResult result, size_t i, double value) {
|
||||
static const TokenType token_type = TOKEN_DOUBLE;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, token_type)) {
|
||||
return TRUE;
|
||||
} if (fabs(head->result.float_literal - value) >= FLOAT_TEST_PRECISION) {
|
||||
logic_fail_test(test, result, float_value_should_be(i + 1, value, head->result.float_literal));
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_string_value(LexerTest *test, LexerResult result, size_t i, size_t length, const char *value) {
|
||||
static const TokenType token_type = TOKEN_STRING;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, token_type)) {
|
||||
return TRUE;
|
||||
} if (head->result.string_literal.length == strnlen(value, length)) {
|
||||
logic_fail_test(test, result, token_length_should_be(i + 1, token_type, strnlen(value, length), head->result.string_literal.length));
|
||||
return TRUE;
|
||||
} if (strncmp(head->result.string_literal.value, value, length) != 0) {
|
||||
logic_fail_test(test, result, token_value_string_should_be(i + 1, token_type, fmax(strnlen(value, length), strnlen(head->result.string_literal.value, length)), value, head->result.string_literal.value));
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_boolean_value(LexerTest *test, LexerResult result, size_t i, Boolean value) {
|
||||
static const TokenType token_type = TOKEN_BOOLEAN;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, token_type)) {
|
||||
return TRUE;
|
||||
} if (head->result.boolean_literal != value) {
|
||||
logic_fail_test(test, result, boolean_should_be(i + 1, value));
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_array_type(LexerTest *test, LexerResult result, size_t i, ArrayType array_type, size_t *shape, size_t dimensions) {
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, TOKEN_ARRAY)) {
|
||||
|
|
@ -387,7 +281,127 @@ static Boolean test_array_type(LexerTest *test, LexerResult result, size_t i, Ar
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_array_identifier_value(LexerTest *test, LexerResult result, size_t i, ArrayStringValues *values, size_t *shape, size_t dimensions) {
|
||||
// TODO: Finish updating to standard function signatures
|
||||
|
||||
static Boolean test_eof_value(LexerTest *test, LexerResult result, size_t i, void *_) {
|
||||
static const TokenType token_type = TOKEN_EOF;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, token_type)) {
|
||||
return TRUE;
|
||||
} if (head->next != 0) {
|
||||
logic_fail_test(test, result, expected_end_of_token_stream(i + 1));
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
Boolean is_literal;
|
||||
size_t length;
|
||||
const char *name;
|
||||
} TestIdentifierValue;
|
||||
|
||||
static Boolean test_identifier_value(LexerTest *test, LexerResult result, size_t i, TestIdentifierValue *value) {
|
||||
static const TokenType token_type = TOKEN_IDENTIFIER;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, token_type)) {
|
||||
return TRUE;
|
||||
} if (head->result.identifier.is_literal != is_literal) {
|
||||
logic_fail_test(test, result, is_literal ? identifier_should_be_literal(i + 1) : identifier_should_not_be_literal(i + 1));
|
||||
return TRUE;
|
||||
} if (head->result.identifier.length == strnlen(name, length)) {
|
||||
logic_fail_test(test, result, token_length_should_be(i + 1, token_type, strnlen(name, length), head->result.identifier.length));
|
||||
return TRUE;
|
||||
} if (strncmp(head->result.identifier.name, name, length) != 0) {
|
||||
logic_fail_test(test, result, token_value_string_should_be(i + 1, token_type, strnlen(name, length), head->result.identifier.name, name));
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
IntegerBuiltInType type;
|
||||
uint64_t value;
|
||||
} TestIntegerValue;
|
||||
|
||||
static Boolean test_integer_value(LexerTest *test, LexerResult result, size_t i, TestIntegerValue *value) {
|
||||
static const TokenType token_type = TOKEN_INTEGER;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, token_type)) {
|
||||
return TRUE;
|
||||
} if (head->result.integer_literal.type != type) {
|
||||
logic_fail_test(test, result, integer_type_should_be(i + 1, type, head->result.integer_literal.type));
|
||||
return TRUE;
|
||||
} if (head->result.integer_literal.value != value) {
|
||||
logic_fail_test(test, result, integer_value_should_be(i + 1, value, head->result.integer_literal.value));
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_float_value(LexerTest *test, LexerResult result, size_t i, float *value) {
|
||||
static const TokenType token_type = TOKEN_FLOAT;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, token_type)) {
|
||||
return TRUE;
|
||||
} if (fabsf(head->result.float_literal - value) >= FLOAT_TEST_PRECISION) {
|
||||
logic_fail_test(test, result, float_value_should_be(i + 1, value, head->result.float_literal));
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_double_value(LexerTest *test, LexerResult result, size_t i, double *value) {
|
||||
static const TokenType token_type = TOKEN_DOUBLE;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, token_type)) {
|
||||
return TRUE;
|
||||
} if (fabs(head->result.float_literal - value) >= FLOAT_TEST_PRECISION) {
|
||||
logic_fail_test(test, result, float_value_should_be(i + 1, value, head->result.float_literal));
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
size_t length;
|
||||
const char *name;
|
||||
} TestStringValue;
|
||||
|
||||
static Boolean test_string_value(LexerTest *test, LexerResult result, size_t i, TestStringValue *value) {
|
||||
static const TokenType token_type = TOKEN_STRING;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, token_type)) {
|
||||
return TRUE;
|
||||
} if (head->result.string_literal.length == strnlen(value, length)) {
|
||||
logic_fail_test(test, result, token_length_should_be(i + 1, token_type, strnlen(value, length), head->result.string_literal.length));
|
||||
return TRUE;
|
||||
} if (strncmp(head->result.string_literal.value, value, length) != 0) {
|
||||
logic_fail_test(test, result, token_value_string_should_be(i + 1, token_type, fmax(strnlen(value, length), strnlen(head->result.string_literal.value, length)), value, head->result.string_literal.value));
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_boolean_value(LexerTest *test, LexerResult result, size_t i, Boolean *value) {
|
||||
static const TokenType token_type = TOKEN_BOOLEAN;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, token_type)) {
|
||||
return TRUE;
|
||||
} if (head->result.boolean_literal != value) {
|
||||
logic_fail_test(test, result, boolean_should_be(i + 1, value));
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
size_t dimensions;
|
||||
size_t *shape;
|
||||
TestIdentifierValue *values;
|
||||
} TestArrayIdentifierValue;
|
||||
|
||||
static Boolean test_array_identifier_value(LexerTest *test, LexerResult result, size_t i, TestArrayIdentifierValue *values) {
|
||||
static const ArrayType array_type = ARRAY_IDENTIFIER;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_array_type(test, result, i, array_type, shape, dimensions)) {
|
||||
|
|
@ -410,7 +424,13 @@ static Boolean test_array_identifier_value(LexerTest *test, LexerResult result,
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_array_integer_value(LexerTest *test, LexerResult result, size_t i, IntegerBuiltInType array_type, uint64_t *values, size_t *shape, size_t dimensions) {
|
||||
typedef struct {
|
||||
size_t dimensions;
|
||||
size_t *shape;
|
||||
TestIdentifierValue *values;
|
||||
} TestArrayIntegerValue;
|
||||
|
||||
static Boolean test_array_integer_value(LexerTest *test, LexerResult result, size_t i, TestArrayIntegerValue *values) {
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_array_type(test, result, i, array_type, shape, dimensions)) {
|
||||
return TRUE;
|
||||
|
|
@ -426,7 +446,13 @@ static Boolean test_array_integer_value(LexerTest *test, LexerResult result, siz
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_array_float_value(LexerTest *test, LexerResult result, size_t i, float *values, size_t *shape, size_t dimensions) {
|
||||
typedef struct {
|
||||
size_t dimensions;
|
||||
size_t *shape;
|
||||
float *values;
|
||||
} TestArrayFloatValue;
|
||||
|
||||
static Boolean test_array_float_value(LexerTest *test, LexerResult result, size_t i, TestArrayFloatValue *values) {
|
||||
static const ArrayType array_type = ARRAY_FLOAT;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_array_type(test, result, i, array_type, shape, dimensions)) {
|
||||
|
|
@ -443,7 +469,13 @@ static Boolean test_array_float_value(LexerTest *test, LexerResult result, size_
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_array_double_value(LexerTest *test, LexerResult result, size_t i, double *values, size_t *shape, size_t dimensions) {
|
||||
typedef struct {
|
||||
size_t dimensions;
|
||||
size_t *shape;
|
||||
double *values;
|
||||
} TestArrayDoubleValue;
|
||||
|
||||
static Boolean test_array_double_value(LexerTest *test, LexerResult result, size_t i, TestArrayDoubleValue *values) {
|
||||
static const ArrayType array_type = ARRAY_DOUBLE;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_array_type(test, result, i, array_type, shape, dimensions)) {
|
||||
|
|
@ -460,7 +492,13 @@ static Boolean test_array_double_value(LexerTest *test, LexerResult result, size
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_array_string_value(LexerTest *test, LexerResult result, size_t i, ArrayStringValues *values, size_t *shape, size_t dimensions) {
|
||||
typedef struct {
|
||||
size_t dimensions;
|
||||
size_t *shape;
|
||||
TestStringValue *values;
|
||||
} TestArrayStringValue;
|
||||
|
||||
static Boolean test_array_string_value(LexerTest *test, LexerResult result, size_t i, TestArrayStringValue *values) {
|
||||
static const ArrayType array_type = ARRAY_STRING;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_array_type(test, result, i, array_type, shape, dimensions)) {
|
||||
|
|
@ -480,7 +518,13 @@ static Boolean test_array_string_value(LexerTest *test, LexerResult result, size
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_array_boolean_value(LexerTest *test, LexerResult result, size_t i, Boolean *values, size_t *shape, size_t dimensions) {
|
||||
typedef struct {
|
||||
size_t dimensions;
|
||||
size_t *shape;
|
||||
Boolean *values;
|
||||
} TestArrayBooleanValue;
|
||||
|
||||
static Boolean test_array_boolean_value(LexerTest *test, LexerResult result, size_t i, TestArrayBooleanValue *values) {
|
||||
static const ArrayType array_type = ARRAY_BOOLEAN;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_array_type(test, result, i, array_type, shape, dimensions)) {
|
||||
|
|
@ -516,7 +560,17 @@ static Boolean test_array_struct_inline_value(LexerTest *test, LexerResult resul
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_token_string_value(LexerTest *test, LexerResult result, size_t i, size_t number_of_tokens, TokenStringValue *values) {
|
||||
typedef struct {
|
||||
Boolean (*token_handler)(LexerTest *, LexerResult, size_t, Token *, void *);
|
||||
void *value;
|
||||
} TestTokenStringToken;
|
||||
|
||||
typedef struct {
|
||||
size_t length;
|
||||
TestTokenStringToken *values;
|
||||
} TestTokenStringValue;
|
||||
|
||||
static Boolean test_token_string_value(LexerTest *test, LexerResult result, size_t i, TestTokenStringValue *values) {
|
||||
static const TokenType token_type = TOKEN_TOKEN_STRING;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, token_type)) {
|
||||
|
|
@ -533,7 +587,14 @@ static Boolean test_token_string_value(LexerTest *test, LexerResult result, size
|
|||
return FALSE;
|
||||
}
|
||||
|
||||
static Boolean test_type_tuple_value(LexerTest *test, LexerResult result, size_t i, ArrayStringValues *input_values, size_t input_length, ArrayStringValues *output_values, size_t output_length) {
|
||||
typedef struct {
|
||||
size_t input_length;
|
||||
TestIdentifierValue *input_values;
|
||||
size_t output_length;
|
||||
TestIdentifierValue *output_values;
|
||||
} TestTypeTupleValue;
|
||||
|
||||
static Boolean test_type_tuple_value(LexerTest *test, LexerResult result, size_t i, TestTypeTupleValue *values) {
|
||||
static const TokenType token_type = TOKEN_TYPE_TUPLE;
|
||||
LexerTokenResult *head = get_token(result.result, i);
|
||||
if (test_token_type(test, result, i, token_type)) {
|
||||
|
|
@ -657,6 +718,17 @@ static TestResult test_dup_and_mult_statement() {
|
|||
return pass_test(&test, result);
|
||||
}
|
||||
|
||||
static TestResult test_square_fn() {
|
||||
LexerTest test = start_up_test("test_dup_statement", "5 dup");
|
||||
LexerResult result = lexical_analysis(&test.lexer_info);
|
||||
if (result.type == SLS_ERROR) return error_fail_test(&test, result, result.error);
|
||||
size_t i = 0;
|
||||
if (test_integer_value(&test, result, i++, INTEGER_I64, 5)) return test.result;
|
||||
if (test_identifier_value(&test, result, i++, FALSE, 3, "dup")) return test.result;
|
||||
if (test_eof_value(&test, result, i++)) return test.result;
|
||||
return pass_test(&test, result);
|
||||
}
|
||||
|
||||
static TestResult test_dup_statement() {
|
||||
LexerTest test = start_up_test("test_dup_statement", "5 dup");
|
||||
LexerResult result = lexical_analysis(&test.lexer_info);
|
||||
|
|
|
|||
Loading…
Reference in New Issue