Implement subtraction in evaluator

This commit is contained in:
Camden Dixie O'Brien 2024-10-24 17:40:50 +01:00
parent d4855813a2
commit bb622a0766
2 changed files with 28 additions and 1 deletions

View File

@ -6,5 +6,12 @@ int evaluate(const expression_t *expression)
return expression->number; return expression->number;
const int x = evaluate(expression->application.operands[0]); const int x = evaluate(expression->application.operands[0]);
const int y = evaluate(expression->application.operands[1]); const int y = evaluate(expression->application.operands[1]);
return x + y; switch (expression->application.operator) {
case OPERATOR_ADD:
return x + y;
case OPERATOR_SUBTRACT:
return x - y;
default:
return -1;
}
} }

View File

@ -116,6 +116,25 @@ static void sum_of_the_sum_of_4_and_66_and_83_evaluates_to_153(void)
ASSERT_EQUAL(153, result); ASSERT_EQUAL(153, result);
} }
static void subtraction_of_56_from_88_evaluates_to_32(void)
{
init_memory_pool(&pool);
expression_t *operand0 = allocate_expression(&pool);
operand0->is_number = true;
operand0->number = 88;
expression_t *operand1 = allocate_expression(&pool);
operand1->is_number = true;
operand1->number = 56;
expression_t *expression = allocate_expression(&pool);
expression->is_number = false;
expression->application.operator= OPERATOR_SUBTRACT;
expression->application.operands[0] = operand0;
expression->application.operands[1] = operand1;
const int result = evaluate(expression);
ASSERT_EQUAL(32, result);
}
int main(void) int main(void)
{ {
TESTING_BEGIN(); TESTING_BEGIN();
@ -125,5 +144,6 @@ int main(void)
RUN_TEST(sum_of_72_and_6_evaluates_to_78); RUN_TEST(sum_of_72_and_6_evaluates_to_78);
RUN_TEST(sum_of_21_and_the_sum_of_9_and_31_evaluates_to_61); RUN_TEST(sum_of_21_and_the_sum_of_9_and_31_evaluates_to_61);
RUN_TEST(sum_of_the_sum_of_4_and_66_and_83_evaluates_to_153); RUN_TEST(sum_of_the_sum_of_4_and_66_and_83_evaluates_to_153);
RUN_TEST(subtraction_of_56_from_88_evaluates_to_32);
TESTING_END(); TESTING_END();
} }