#include "reader.h" #include "testing.h" #include static memory_pool_t pool; static char *test_input; static int test_input_len; static int test_get_byte(void) { if (test_input_len < 0) return EOF; --test_input_len; return *test_input++; } static void input_1234_is_read_as_number_with_expected_value(void) { init_memory_pool(&pool); expression_t *result; parse_expression(&pool, "1234", 4, &result); ASSERT_NOT_NULL(result); ASSERT_TRUE(result->is_number); ASSERT_EQUAL(1234, result->number); } static void input_4321_is_read_as_number_with_expected_value(void) { init_memory_pool(&pool); expression_t *result; parse_expression(&pool, "4321", 4, &result); ASSERT_NOT_NULL(result); ASSERT_TRUE(result->is_number); ASSERT_EQUAL(4321, result->number); } static void input_1234_with_len_3_is_read_as_123(void) { init_memory_pool(&pool); expression_t *result; parse_expression(&pool, "1234", 3, &result); ASSERT_NOT_NULL(result); ASSERT_TRUE(result->is_number); ASSERT_EQUAL(123, result->number); } static void input_1_plus_2_with_no_spaces_is_read_as_an_add_application(void) { init_memory_pool(&pool); expression_t *result; parse_expression(&pool, "1+2", 3, &result); ASSERT_NOT_NULL(result); ASSERT_FALSE(result->is_number); ASSERT_EQUAL(OPERATOR_ADD, result->application.operator); } static void operands_of_15_plus_54_are_read_as_numbers(void) { init_memory_pool(&pool); expression_t *result; parse_expression(&pool, "15+54", 5, &result); ASSERT_NOT_NULL(result); ASSERT_FALSE(result->is_number); ASSERT_NOT_NULL(result->application.operands[0]); ASSERT_TRUE(result->application.operands[0]->is_number); ASSERT_EQUAL(15, result->application.operands[0]->number); ASSERT_NOT_NULL(result->application.operands[1]); ASSERT_TRUE(result->application.operands[1]->is_number); ASSERT_EQUAL(54, result->application.operands[1]->number); } static void input_6_minus_2_with_no_spaces_is_read_as_a_subtract_application(void) { init_memory_pool(&pool); expression_t *result; parse_expression(&pool, "6-2", 3, &result); ASSERT_NOT_NULL(result); ASSERT_FALSE(result->is_number); ASSERT_EQUAL(OPERATOR_SUBTRACT, result->application.operator); } static void input_1_plus_2_plus_3_is_parsed_as_a_left_nested_application(void) { init_memory_pool(&pool); expression_t *result; parse_expression(&pool, "1+2+3", 5, &result); ASSERT_NOT_NULL(result); ASSERT_FALSE(result->is_number); ASSERT_EQUAL(OPERATOR_ADD, result->application.operator); const expression_t *operand0 = result->application.operands[0]; ASSERT_NOT_NULL(operand0); ASSERT_FALSE(operand0->is_number); ASSERT_EQUAL(OPERATOR_ADD, operand0->application.operator); ASSERT_NOT_NULL(operand0->application.operands[0]); ASSERT_TRUE(operand0->application.operands[0]->is_number); ASSERT_EQUAL(1, operand0->application.operands[0]->number); ASSERT_NOT_NULL(operand0->application.operands[1]); ASSERT_TRUE(operand0->application.operands[1]->is_number); ASSERT_EQUAL(2, operand0->application.operands[1]->number); const expression_t *operand1 = result->application.operands[1]; ASSERT_NOT_NULL(operand1); ASSERT_TRUE(operand1->is_number); ASSERT_EQUAL(3, operand1->number); } static void input_1_times_2_times_3_is_parsed_as_a_left_nested_application(void) { init_memory_pool(&pool); expression_t *result; parse_expression(&pool, "1*2*3", 5, &result); ASSERT_NOT_NULL(result); ASSERT_FALSE(result->is_number); ASSERT_EQUAL(OPERATOR_MULTIPLY, result->application.operator); const expression_t *operand0 = result->application.operands[0]; ASSERT_NOT_NULL(operand0); ASSERT_FALSE(operand0->is_number); ASSERT_EQUAL(OPERATOR_MULTIPLY, operand0->application.operator); ASSERT_NOT_NULL(operand0->application.operands[0]); ASSERT_TRUE(operand0->application.operands[0]->is_number); ASSERT_EQUAL(1, operand0->application.operands[0]->number); ASSERT_NOT_NULL(operand0->application.operands[1]); ASSERT_TRUE(operand0->application.operands[1]->is_number); ASSERT_EQUAL(2, operand0->application.operands[1]->number); const expression_t *operand1 = result->application.operands[1]; ASSERT_NOT_NULL(operand1); ASSERT_TRUE(operand1->is_number); ASSERT_EQUAL(3, operand1->number); } static void input_1_plus_2_times_3_is_parsed_as_a_right_nested_application(void) { init_memory_pool(&pool); expression_t *result; parse_expression(&pool, "1+2*3", 5, &result); ASSERT_NOT_NULL(result); ASSERT_FALSE(result->is_number); ASSERT_EQUAL(OPERATOR_ADD, result->application.operator); const expression_t *operand0 = result->application.operands[0]; ASSERT_NOT_NULL(operand0); ASSERT_TRUE(operand0->is_number); ASSERT_EQUAL(1, operand0->number); const expression_t *operand1 = result->application.operands[1]; ASSERT_NOT_NULL(operand1); ASSERT_FALSE(operand1->is_number); ASSERT_EQUAL(OPERATOR_MULTIPLY, operand1->application.operator); ASSERT_NOT_NULL(operand1->application.operands[0]); ASSERT_TRUE(operand1->application.operands[0]->is_number); ASSERT_EQUAL(2, operand1->application.operands[0]->number); ASSERT_NOT_NULL(operand1->application.operands[1]); ASSERT_TRUE(operand1->application.operands[1]->is_number); ASSERT_EQUAL(3, operand1->application.operands[1]->number); } static void paren_1_plus_2_times_3_close_paren_parses_as_left_nested(void) { init_memory_pool(&pool); expression_t *result; parse_expression(&pool, "(1+2)*3", 7, &result); ASSERT_NOT_NULL(result); ASSERT_FALSE(result->is_number); ASSERT_EQUAL(OPERATOR_MULTIPLY, result->application.operator); const expression_t *operand0 = result->application.operands[0]; ASSERT_NOT_NULL(operand0); ASSERT_FALSE(operand0->is_number); ASSERT_EQUAL(OPERATOR_ADD, operand0->application.operator); ASSERT_NOT_NULL(operand0->application.operands[0]); ASSERT_TRUE(operand0->application.operands[0]->is_number); ASSERT_EQUAL(1, operand0->application.operands[0]->number); ASSERT_NOT_NULL(operand0->application.operands[1]); ASSERT_TRUE(operand0->application.operands[1]->is_number); ASSERT_EQUAL(2, operand0->application.operands[1]->number); const expression_t *operand1 = result->application.operands[1]; ASSERT_NOT_NULL(operand1); ASSERT_TRUE(operand1->is_number); ASSERT_EQUAL(3, operand1->number); } static void garbage_input_yeilds_error(void) { init_memory_pool(&pool); expression_t *result; const int used = parse_expression(&pool, "arwizxhu", 8, &result); ASSERT_EQUAL(0, used); } static void sum_with_single_spaces_around_plus_is_parsed_successfully(void) { init_memory_pool(&pool); expression_t *result; parse_expression(&pool, "1 + 2", 5, &result); ASSERT_NOT_NULL(result); ASSERT_FALSE(result->is_number); ASSERT_EQUAL(OPERATOR_ADD, result->application.operator); } static void sum_with_multiple_spaces_around_plus_is_parsed_successfully(void) { init_memory_pool(&pool); expression_t *result; parse_expression(&pool, "1 + 2", 7, &result); ASSERT_NOT_NULL(result); ASSERT_FALSE(result->is_number); ASSERT_EQUAL(OPERATOR_ADD, result->application.operator); } static void product_with_spaces_around_times_is_parsed_successfully(void) { init_memory_pool(&pool); expression_t *result; parse_expression(&pool, "1 * 2", 6, &result); ASSERT_NOT_NULL(result); ASSERT_FALSE(result->is_number); ASSERT_EQUAL(OPERATOR_MULTIPLY, result->application.operator); } static void spaces_inside_parens_are_ignored(void) { init_memory_pool(&pool); expression_t *result; parse_expression(&pool, "( 14 )", 6, &result); ASSERT_NOT_NULL(result); ASSERT_TRUE(result->is_number); ASSERT_EQUAL(14, result->number); } static void leading_spaces_are_ignored(void) { init_memory_pool(&pool); expression_t *result; parse_expression(&pool, " 14", 5, &result); ASSERT_NOT_NULL(result); ASSERT_TRUE(result->is_number); ASSERT_EQUAL(14, result->number); } static void trailing_spaces_are_ignored(void) { init_memory_pool(&pool); expression_t *result; parse_expression(&pool, "14 ", 5, &result); ASSERT_NOT_NULL(result); ASSERT_TRUE(result->is_number); ASSERT_EQUAL(14, result->number); } static void read_line_copies_line_into_buffer(void) { test_input = "foobar\nbazquux"; test_input_len = strlen(test_input); char buffer[10]; read_line(test_get_byte, buffer, sizeof(buffer)); ASSERT_MEM_EQUAL("foobar", buffer, 6); } static void read_line_returns_length_of_line(void) { test_input = "foobar\nbazquux"; test_input_len = strlen(test_input); char buffer[10]; const int len = read_line(test_get_byte, buffer, sizeof(buffer)); ASSERT_EQUAL(6, len); } static void read_line_returns_minus_1_on_eof(void) { test_input_len = 0; char buffer[10]; const int len = read_line(test_get_byte, buffer, sizeof(buffer)); ASSERT_EQUAL(-1, len); } static void read_line_returns_buffer_size_if_no_newline(void) { test_input = "foobarbazquux"; test_input_len = strlen(test_input); char buffer[10]; const int len = read_line(test_get_byte, buffer, sizeof(buffer)); ASSERT_EQUAL(10, len); } int main(void) { TESTING_BEGIN(); RUN_TEST(input_1234_is_read_as_number_with_expected_value); RUN_TEST(input_4321_is_read_as_number_with_expected_value); RUN_TEST(input_1234_with_len_3_is_read_as_123); RUN_TEST(operands_of_15_plus_54_are_read_as_numbers); RUN_TEST(input_1_plus_2_with_no_spaces_is_read_as_an_add_application); RUN_TEST( input_6_minus_2_with_no_spaces_is_read_as_a_subtract_application); RUN_TEST(input_1_plus_2_plus_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(paren_1_plus_2_times_3_close_paren_parses_as_left_nested); RUN_TEST(garbage_input_yeilds_error); RUN_TEST(sum_with_single_spaces_around_plus_is_parsed_successfully); RUN_TEST(sum_with_multiple_spaces_around_plus_is_parsed_successfully); RUN_TEST(product_with_spaces_around_times_is_parsed_successfully); RUN_TEST(spaces_inside_parens_are_ignored); RUN_TEST(leading_spaces_are_ignored); RUN_TEST(trailing_spaces_are_ignored); RUN_TEST(read_line_copies_line_into_buffer); RUN_TEST(read_line_returns_length_of_line); RUN_TEST(read_line_returns_minus_1_on_eof); RUN_TEST(read_line_returns_buffer_size_if_no_newline); TESTING_END(); }