summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2020-04-08 21:31:53 +0200
committerSébastien Helleu <flashcode@flashtux.org>2020-04-08 21:38:51 +0200
commit007fd03bc8251a93c26e5df024ad4179a896f948 (patch)
tree06e4582044bba37d47bf23ae48bce79f08881cc7 /src/core
parenta693125c8184034ec8e00d952f243e221f69c9ae (diff)
downloadweechat-007fd03bc8251a93c26e5df024ad4179a896f948.zip
tests: add tests on calc functions
Functions tested: - calc_operator_precedence - calc_pop_value - calc_list_free_cb - calc_operation - calc_operation_stacks
Diffstat (limited to 'src/core')
-rw-r--r--src/core/wee-calc.c43
1 files changed, 26 insertions, 17 deletions
diff --git a/src/core/wee-calc.c b/src/core/wee-calc.c
index 3348b0eb4..29c6c9ece 100644
--- a/src/core/wee-calc.c
+++ b/src/core/wee-calc.c
@@ -65,22 +65,22 @@ calc_list_free_cb (void *data, struct t_arraylist *arraylist, void *pointer)
*/
int
-calc_operator_precedence (char *operator)
+calc_operator_precedence (const char *oper)
{
- if (!operator)
+ if (!oper)
return 0;
- if ((strcmp (operator, "*") == 0)
- || (strcmp (operator, "/") == 0)
- || (strcmp (operator, "//") == 0)
- || (strcmp (operator, "%") == 0)
- || (strcmp (operator, "**") == 0))
+ if ((strcmp (oper, "*") == 0)
+ || (strcmp (oper, "/") == 0)
+ || (strcmp (oper, "//") == 0)
+ || (strcmp (oper, "%") == 0)
+ || (strcmp (oper, "**") == 0))
{
return 2;
}
- if ((strcmp (operator, "+") == 0)
- || (strcmp (operator, "-") == 0))
+ if ((strcmp (oper, "+") == 0)
+ || (strcmp (oper, "-") == 0))
{
return 1;
}
@@ -98,6 +98,9 @@ calc_pop_value (struct t_arraylist *list_values)
int size_values;
double *ptr_value, value;
+ if (!list_values)
+ return 0;
+
size_values = arraylist_size (list_values);
if (size_values < 1)
@@ -116,27 +119,30 @@ calc_pop_value (struct t_arraylist *list_values)
*/
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;
- if (strcmp (operator, "-") == 0)
+ if (strcmp (oper, "-") == 0)
return value1 - value2;
- if (strcmp (operator, "*") == 0)
+ if (strcmp (oper, "*") == 0)
return value1 * value2;
- if (strcmp (operator, "/") == 0)
+ if (strcmp (oper, "/") == 0)
return (value2 != 0) ? value1 / value2 : 0;
- if (strcmp (operator, "//") == 0)
+ if (strcmp (oper, "//") == 0)
return (value2 != 0) ? floor (value1 / value2) : 0;
- if (strcmp (operator, "%") == 0)
+ if (strcmp (oper, "%") == 0)
return (value2 != 0) ? fmod (value1, value2) : 0;
- if (strcmp (operator, "**") == 0)
+ if (strcmp (oper, "**") == 0)
return pow (value1, value2);
return 0;
@@ -157,6 +163,9 @@ calc_operation_stacks (struct t_arraylist *list_values,
double value1, value2, result, *ptr_result;
char *ptr_operator;
+ if (!list_values || !list_ops)
+ return;
+
size_ops = arraylist_size (list_ops);
if (size_ops < 1)
return;