Add * and / support to evaluator
This commit is contained in:
parent
9932aa7ef1
commit
7b0f94ce98
@ -11,6 +11,10 @@ int evaluate(const expression_t *expression)
|
|||||||
return x + y;
|
return x + y;
|
||||||
case OPERATOR_SUBTRACT:
|
case OPERATOR_SUBTRACT:
|
||||||
return x - y;
|
return x - y;
|
||||||
|
case OPERATOR_MULTIPLY:
|
||||||
|
return x * y;
|
||||||
|
case OPERATOR_DIVIDE:
|
||||||
|
return x / y;
|
||||||
default:
|
default:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -135,6 +135,44 @@ static void subtraction_of_56_from_88_evaluates_to_32(void)
|
|||||||
ASSERT_EQUAL(32, result);
|
ASSERT_EQUAL(32, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void product_of_55_and_3_evaluates_to_165(void)
|
||||||
|
{
|
||||||
|
init_memory_pool(&pool);
|
||||||
|
expression_t *operand0 = allocate_expression(&pool);
|
||||||
|
operand0->is_number = true;
|
||||||
|
operand0->number = 55;
|
||||||
|
expression_t *operand1 = allocate_expression(&pool);
|
||||||
|
operand1->is_number = true;
|
||||||
|
operand1->number = 3;
|
||||||
|
expression_t *expression = allocate_expression(&pool);
|
||||||
|
expression->is_number = false;
|
||||||
|
expression->application.operator= OPERATOR_MULTIPLY;
|
||||||
|
expression->application.operands[0] = operand0;
|
||||||
|
expression->application.operands[1] = operand1;
|
||||||
|
|
||||||
|
const int result = evaluate(expression);
|
||||||
|
ASSERT_EQUAL(165, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void quotient_of_18_by_3_evaluates_to_6(void)
|
||||||
|
{
|
||||||
|
init_memory_pool(&pool);
|
||||||
|
expression_t *operand0 = allocate_expression(&pool);
|
||||||
|
operand0->is_number = true;
|
||||||
|
operand0->number = 18;
|
||||||
|
expression_t *operand1 = allocate_expression(&pool);
|
||||||
|
operand1->is_number = true;
|
||||||
|
operand1->number = 3;
|
||||||
|
expression_t *expression = allocate_expression(&pool);
|
||||||
|
expression->is_number = false;
|
||||||
|
expression->application.operator= OPERATOR_DIVIDE;
|
||||||
|
expression->application.operands[0] = operand0;
|
||||||
|
expression->application.operands[1] = operand1;
|
||||||
|
|
||||||
|
const int result = evaluate(expression);
|
||||||
|
ASSERT_EQUAL(6, result);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
TESTING_BEGIN();
|
TESTING_BEGIN();
|
||||||
@ -145,5 +183,7 @@ int main(void)
|
|||||||
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);
|
RUN_TEST(subtraction_of_56_from_88_evaluates_to_32);
|
||||||
|
RUN_TEST(product_of_55_and_3_evaluates_to_165);
|
||||||
|
RUN_TEST(quotient_of_18_by_3_evaluates_to_6);
|
||||||
TESTING_END();
|
TESTING_END();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user