tests: add tests on calc functions
Functions tested: - calc_operator_precedence - calc_pop_value - calc_list_free_cb - calc_operation - calc_operation_stacksmaster
parent
a693125c81
commit
007fd03bc8
|
@ -65,22 +65,22 @@ calc_list_free_cb (void *data, struct t_arraylist *arraylist, void *pointer)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int
|
int
|
||||||
calc_operator_precedence (char *operator)
|
calc_operator_precedence (const char *oper)
|
||||||
{
|
{
|
||||||
if (!operator)
|
if (!oper)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if ((strcmp (operator, "*") == 0)
|
if ((strcmp (oper, "*") == 0)
|
||||||
|| (strcmp (operator, "/") == 0)
|
|| (strcmp (oper, "/") == 0)
|
||||||
|| (strcmp (operator, "//") == 0)
|
|| (strcmp (oper, "//") == 0)
|
||||||
|| (strcmp (operator, "%") == 0)
|
|| (strcmp (oper, "%") == 0)
|
||||||
|| (strcmp (operator, "**") == 0))
|
|| (strcmp (oper, "**") == 0))
|
||||||
{
|
{
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((strcmp (operator, "+") == 0)
|
if ((strcmp (oper, "+") == 0)
|
||||||
|| (strcmp (operator, "-") == 0))
|
|| (strcmp (oper, "-") == 0))
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -98,6 +98,9 @@ calc_pop_value (struct t_arraylist *list_values)
|
||||||
int size_values;
|
int size_values;
|
||||||
double *ptr_value, value;
|
double *ptr_value, value;
|
||||||
|
|
||||||
|
if (!list_values)
|
||||||
|
return 0;
|
||||||
|
|
||||||
size_values = arraylist_size (list_values);
|
size_values = arraylist_size (list_values);
|
||||||
|
|
||||||
if (size_values < 1)
|
if (size_values < 1)
|
||||||
|
@ -116,27 +119,30 @@ calc_pop_value (struct t_arraylist *list_values)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
double
|
double
|
||||||
calc_operation (char *operator, double value1, double value2)
|
calc_operation (const char *oper, double value1, double value2)
|
||||||
{
|
{
|
||||||
if (strcmp (operator, "+") == 0)
|
if (!oper)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (strcmp (oper, "+") == 0)
|
||||||
return value1 + value2;
|
return value1 + value2;
|
||||||
|
|
||||||
if (strcmp (operator, "-") == 0)
|
if (strcmp (oper, "-") == 0)
|
||||||
return value1 - value2;
|
return value1 - value2;
|
||||||
|
|
||||||
if (strcmp (operator, "*") == 0)
|
if (strcmp (oper, "*") == 0)
|
||||||
return value1 * value2;
|
return value1 * value2;
|
||||||
|
|
||||||
if (strcmp (operator, "/") == 0)
|
if (strcmp (oper, "/") == 0)
|
||||||
return (value2 != 0) ? value1 / value2 : 0;
|
return (value2 != 0) ? value1 / value2 : 0;
|
||||||
|
|
||||||
if (strcmp (operator, "//") == 0)
|
if (strcmp (oper, "//") == 0)
|
||||||
return (value2 != 0) ? floor (value1 / value2) : 0;
|
return (value2 != 0) ? floor (value1 / value2) : 0;
|
||||||
|
|
||||||
if (strcmp (operator, "%") == 0)
|
if (strcmp (oper, "%") == 0)
|
||||||
return (value2 != 0) ? fmod (value1, value2) : 0;
|
return (value2 != 0) ? fmod (value1, value2) : 0;
|
||||||
|
|
||||||
if (strcmp (operator, "**") == 0)
|
if (strcmp (oper, "**") == 0)
|
||||||
return pow (value1, value2);
|
return pow (value1, value2);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -157,6 +163,9 @@ calc_operation_stacks (struct t_arraylist *list_values,
|
||||||
double value1, value2, result, *ptr_result;
|
double value1, value2, result, *ptr_result;
|
||||||
char *ptr_operator;
|
char *ptr_operator;
|
||||||
|
|
||||||
|
if (!list_values || !list_ops)
|
||||||
|
return;
|
||||||
|
|
||||||
size_ops = arraylist_size (list_ops);
|
size_ops = arraylist_size (list_ops);
|
||||||
if (size_ops < 1)
|
if (size_ops < 1)
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -24,8 +24,17 @@
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "src/core/wee-arraylist.h"
|
||||||
#include "src/core/wee-calc.h"
|
#include "src/core/wee-calc.h"
|
||||||
|
|
||||||
|
extern void calc_list_free_cb (void *data, struct t_arraylist *arraylist,
|
||||||
|
void *pointer);
|
||||||
|
extern int calc_operator_precedence (const char *oper);
|
||||||
|
extern double calc_pop_value (struct t_arraylist *list_values);
|
||||||
|
extern double calc_operation (const char *oper, double value1, double value2);
|
||||||
|
extern void calc_operation_stacks (struct t_arraylist *list_values,
|
||||||
|
struct t_arraylist *list_ops);
|
||||||
extern int calc_sanitize_decimal_number (char *string);
|
extern int calc_sanitize_decimal_number (char *string);
|
||||||
extern void calc_format_result (double value, char *result, int max_size);
|
extern void calc_format_result (double value, char *result, int max_size);
|
||||||
}
|
}
|
||||||
|
@ -49,6 +58,138 @@ TEST_GROUP(CoreCalc)
|
||||||
{
|
{
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Tests functions:
|
||||||
|
* calc_operator_precedence
|
||||||
|
*/
|
||||||
|
|
||||||
|
TEST(CoreCalc, OperatorPrecedence)
|
||||||
|
{
|
||||||
|
LONGS_EQUAL(0, calc_operator_precedence (NULL));
|
||||||
|
LONGS_EQUAL(0, calc_operator_precedence (""));
|
||||||
|
LONGS_EQUAL(0, calc_operator_precedence ("$"));
|
||||||
|
|
||||||
|
LONGS_EQUAL(1, calc_operator_precedence ("+"));
|
||||||
|
LONGS_EQUAL(1, calc_operator_precedence ("-"));
|
||||||
|
|
||||||
|
LONGS_EQUAL(2, calc_operator_precedence ("*"));
|
||||||
|
LONGS_EQUAL(2, calc_operator_precedence ("/"));
|
||||||
|
LONGS_EQUAL(2, calc_operator_precedence ("//"));
|
||||||
|
LONGS_EQUAL(2, calc_operator_precedence ("%"));
|
||||||
|
LONGS_EQUAL(2, calc_operator_precedence ("**"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Tests functions:
|
||||||
|
* calc_pop_value
|
||||||
|
* calc_list_free_cb
|
||||||
|
*/
|
||||||
|
|
||||||
|
TEST(CoreCalc, PopValue)
|
||||||
|
{
|
||||||
|
struct t_arraylist *list_values;
|
||||||
|
double *ptr_value;
|
||||||
|
|
||||||
|
list_values = arraylist_new (32, 0, 1,
|
||||||
|
NULL, NULL,
|
||||||
|
&calc_list_free_cb, NULL);
|
||||||
|
|
||||||
|
DOUBLES_EQUAL(0, calc_pop_value (NULL), 0.001);
|
||||||
|
DOUBLES_EQUAL(0, calc_pop_value (list_values), 0.001);
|
||||||
|
|
||||||
|
ptr_value = (double *)malloc (sizeof (*ptr_value));
|
||||||
|
*ptr_value = 123.5;
|
||||||
|
arraylist_add (list_values, ptr_value);
|
||||||
|
LONGS_EQUAL(1, list_values->size);
|
||||||
|
|
||||||
|
DOUBLES_EQUAL(123.5, calc_pop_value (list_values), 0.001);
|
||||||
|
LONGS_EQUAL(0, list_values->size);
|
||||||
|
|
||||||
|
ptr_value = (double *)malloc (sizeof (*ptr_value));
|
||||||
|
*ptr_value = 123.5;
|
||||||
|
arraylist_add (list_values, ptr_value);
|
||||||
|
LONGS_EQUAL(1, list_values->size);
|
||||||
|
|
||||||
|
ptr_value = (double *)malloc (sizeof (*ptr_value));
|
||||||
|
*ptr_value = 456.2;
|
||||||
|
arraylist_add (list_values, ptr_value);
|
||||||
|
LONGS_EQUAL(2, list_values->size);
|
||||||
|
|
||||||
|
DOUBLES_EQUAL(456.2, calc_pop_value (list_values), 0.001);
|
||||||
|
LONGS_EQUAL(1, list_values->size);
|
||||||
|
|
||||||
|
DOUBLES_EQUAL(123.5, calc_pop_value (list_values), 0.001);
|
||||||
|
LONGS_EQUAL(0, list_values->size);
|
||||||
|
|
||||||
|
arraylist_free (list_values);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Tests functions:
|
||||||
|
* calc_operation
|
||||||
|
*/
|
||||||
|
|
||||||
|
TEST(CoreCalc, CalcOperation)
|
||||||
|
{
|
||||||
|
DOUBLES_EQUAL(0, calc_operation (NULL, 2, 3), 0.001);
|
||||||
|
DOUBLES_EQUAL(0, calc_operation ("", 2, 3), 0.001);
|
||||||
|
DOUBLES_EQUAL(0, calc_operation ("", 2, 3), 0.001);
|
||||||
|
DOUBLES_EQUAL(0, calc_operation ("$", 2, 3), 0.001);
|
||||||
|
|
||||||
|
DOUBLES_EQUAL(5.2, calc_operation ("+", 2, 3.2), 0.001);
|
||||||
|
DOUBLES_EQUAL(-1.2, calc_operation ("-", 2, 3.2), 0.001);
|
||||||
|
DOUBLES_EQUAL(6.4, calc_operation ("*", 2, 3.2), 0.001);
|
||||||
|
DOUBLES_EQUAL(0.625, calc_operation ("/", 2, 3.2), 0.001);
|
||||||
|
DOUBLES_EQUAL(2, calc_operation ("//", 7, 3), 0.001);
|
||||||
|
DOUBLES_EQUAL(3.3, calc_operation ("%", 9, 5.7), 0.001);
|
||||||
|
DOUBLES_EQUAL(256, calc_operation ("**", 2, 8), 0.001);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Tests functions:
|
||||||
|
* calc_operation_stacks
|
||||||
|
*/
|
||||||
|
|
||||||
|
TEST(CoreCalc, CalcOperationStacks)
|
||||||
|
{
|
||||||
|
struct t_arraylist *list_values, *list_ops;
|
||||||
|
double *ptr_value;
|
||||||
|
char *ptr_op;
|
||||||
|
|
||||||
|
calc_operation_stacks (NULL, NULL);
|
||||||
|
|
||||||
|
list_values = arraylist_new (32, 0, 1,
|
||||||
|
NULL, NULL,
|
||||||
|
&calc_list_free_cb, NULL);
|
||||||
|
|
||||||
|
list_ops = arraylist_new (32, 0, 1,
|
||||||
|
NULL, NULL,
|
||||||
|
&calc_list_free_cb, NULL);
|
||||||
|
|
||||||
|
calc_operation_stacks (list_values, list_ops);
|
||||||
|
|
||||||
|
ptr_value = (double *)malloc (sizeof (*ptr_value));
|
||||||
|
*ptr_value = 123.5;
|
||||||
|
arraylist_add (list_values, ptr_value);
|
||||||
|
|
||||||
|
ptr_value = (double *)malloc (sizeof (*ptr_value));
|
||||||
|
*ptr_value = 456.2;
|
||||||
|
arraylist_add (list_values, ptr_value);
|
||||||
|
|
||||||
|
ptr_op = strdup ("+");
|
||||||
|
arraylist_add (list_ops, ptr_op);
|
||||||
|
|
||||||
|
calc_operation_stacks (list_values, list_ops);
|
||||||
|
|
||||||
|
ptr_value = (double *)arraylist_get (list_values, 0);
|
||||||
|
DOUBLES_EQUAL(579.7, *ptr_value, 0.001);
|
||||||
|
|
||||||
|
arraylist_free (list_values);
|
||||||
|
arraylist_free (list_ops);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Tests functions:
|
* Tests functions:
|
||||||
* calc_sanitize_decimal_number
|
* calc_sanitize_decimal_number
|
||||||
|
|
Loading…
Reference in New Issue