Yield NULL from read_expression() on invalid input
This commit is contained in:
parent
e0070bfb1e
commit
10803109db
@ -123,6 +123,9 @@ const expression_t *
|
|||||||
read_expression(memory_pool_t *pool, const char *input, int len)
|
read_expression(memory_pool_t *pool, const char *input, int len)
|
||||||
{
|
{
|
||||||
expression_t *expression;
|
expression_t *expression;
|
||||||
parse_expression(pool, input, len, &expression);
|
const int used = parse_expression(pool, input, len, &expression);
|
||||||
return expression;
|
if (used == len)
|
||||||
|
return expression;
|
||||||
|
else
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -166,6 +166,20 @@ static void paren_1_plus_2_times_3_close_paren_parses_as_left_nested(void)
|
|||||||
ASSERT_EQUAL(3, operand1->number);
|
ASSERT_EQUAL(3, operand1->number);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void garbage_input_yeilds_null(void)
|
||||||
|
{
|
||||||
|
init_memory_pool(&pool);
|
||||||
|
const expression_t *result = read_expression(&pool, "arwizxhu", 8);
|
||||||
|
ASSERT_NULL(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void valid_expression_followed_by_garbage_yeilds_null(void)
|
||||||
|
{
|
||||||
|
init_memory_pool(&pool);
|
||||||
|
const expression_t *result = read_expression(&pool, "5+2jvkre", 8);
|
||||||
|
ASSERT_NULL(result);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
TESTING_BEGIN();
|
TESTING_BEGIN();
|
||||||
@ -180,5 +194,7 @@ int main(void)
|
|||||||
RUN_TEST(input_1_times_2_times_3_is_parsed_as_a_left_nested_application);
|
RUN_TEST(input_1_times_2_times_3_is_parsed_as_a_left_nested_application);
|
||||||
RUN_TEST(input_1_plus_2_times_3_is_parsed_as_a_right_nested_application);
|
RUN_TEST(input_1_plus_2_times_3_is_parsed_as_a_right_nested_application);
|
||||||
RUN_TEST(paren_1_plus_2_times_3_close_paren_parses_as_left_nested);
|
RUN_TEST(paren_1_plus_2_times_3_close_paren_parses_as_left_nested);
|
||||||
|
RUN_TEST(garbage_input_yeilds_null);
|
||||||
|
RUN_TEST(valid_expression_followed_by_garbage_yeilds_null);
|
||||||
TESTING_END();
|
TESTING_END();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user