Commit a6fd9a1f authored by Brice Videau's avatar Brice Videau
Browse files

Added more expression tests.

parent d660fd4e
......@@ -52,18 +52,18 @@ extern const int ccs_expression_arity[];
// Expressions
extern ccs_error_t
ccs_create_binary_expression(ccs_expression_type_t expresion_type,
ccs_create_binary_expression(ccs_expression_type_t type,
ccs_datum_t node_left,
ccs_datum_t node_right,
ccs_expression_t *expression_ret);
extern ccs_error_t
ccs_create_unary_expression(ccs_expression_type_t expression_type,
ccs_create_unary_expression(ccs_expression_type_t type,
ccs_datum_t node,
ccs_expression_t *expression_ret);
extern ccs_error_t
ccs_create_expression(ccs_expression_type_t expression_type,
ccs_create_expression(ccs_expression_type_t type,
size_t num_nodes,
ccs_datum_t *nodes,
ccs_expression_t *expression_ret);
......
......@@ -556,23 +556,21 @@ _ccs_expr_in_eval(_ccs_expression_data_t *data,
for (size_t i = 0; i < num_nodes; i++) {
ccs_datum_t right;
err = ccs_expression_list_eval_node((ccs_expression_t)(data->nodes[1].value.o), context, values, i, &right);
if (err)
return err;
check_hypers(data->nodes[0], right, htl);
ccs_bool_t equal;
ccs_error_t err = _ccs_datum_test_equal_generic(&left, &right, &equal);
if(htl != CCS_HYPERPARAMETER_TYPE_MAX) {
if (equal) {
result->type = CCS_BOOLEAN;
result->value.i = CCS_TRUE;
return CCS_SUCCESS;
}
} else {
if (err)
if (err) {
if (err != -CCS_INVALID_VALUE)
return err;
if (equal) {
result->type = CCS_BOOLEAN;
result->value.i = CCS_TRUE;
return CCS_SUCCESS;
}
else
continue;
}
if (equal) {
result->type = CCS_BOOLEAN;
result->value.i = CCS_TRUE;
return CCS_SUCCESS;
}
}
result->type = CCS_BOOLEAN;
......@@ -944,7 +942,7 @@ _ccs_expression_ops_broker(ccs_expression_type_t expression_type) {
}
ccs_error_t
ccs_create_expression(ccs_expression_type_t expression_type,
ccs_create_expression(ccs_expression_type_t type,
size_t num_nodes,
ccs_datum_t *nodes,
ccs_expression_t *expression_ret) {
......@@ -952,7 +950,7 @@ ccs_create_expression(ccs_expression_type_t expression_type,
return -CCS_INVALID_VALUE;
if (!expression_ret)
return -CCS_INVALID_VALUE;
int arity = ccs_expression_arity[expression_type];
int arity = ccs_expression_arity[type];
if (arity >= 0 && num_nodes != (size_t)arity)
return -CCS_INVALID_VALUE;
......@@ -984,10 +982,10 @@ ccs_create_expression(ccs_expression_type_t expression_type,
ccs_expression_t expression = (ccs_expression_t)mem;
_ccs_object_init(&(expression->obj), CCS_EXPRESSION,
(_ccs_object_ops_t*)_ccs_expression_ops_broker(expression_type));
(_ccs_object_ops_t*)_ccs_expression_ops_broker(type));
_ccs_expression_data_t *expression_data =
(_ccs_expression_data_t *)(mem + sizeof(struct _ccs_expression_s));
expression_data->type = expression_type;
expression_data->type = type;
expression_data->num_nodes = num_nodes;
expression_data->nodes = (ccs_datum_t *)(mem +
sizeof(struct _ccs_expression_s) +
......@@ -1011,6 +1009,24 @@ ccs_create_expression(ccs_expression_type_t expression_type,
return CCS_SUCCESS;
}
ccs_error_t
ccs_create_binary_expression(ccs_expression_type_t type,
ccs_datum_t node_left,
ccs_datum_t node_right,
ccs_expression_t *expression_ret) {
ccs_datum_t nodes[2];
nodes[0] = node_left;
nodes[1] = node_right;
return ccs_create_expression(type, 2, nodes, expression_ret);
}
ccs_error_t
ccs_create_unary_expression(ccs_expression_type_t type,
ccs_datum_t node,
ccs_expression_t *expression_ret) {
return ccs_create_expression(type, 1, &node, expression_ret);
}
ccs_error_t
ccs_expression_eval(ccs_expression_t expression,
ccs_configuration_space_t context,
......
......@@ -2,6 +2,7 @@
#include <assert.h>
#include <cconfigspace.h>
#include <string.h>
#include <math.h>
static inline ccs_datum_t
ccs_bool(ccs_bool_t v) {
......@@ -281,10 +282,482 @@ void test_equal_ordinal() {
assert( err == CCS_SUCCESS );
}
void test_binary_arithmetic(ccs_expression_type_t t,
ccs_datum_t a, ccs_datum_t b,
ccs_datum_t eres, ccs_error_t eerr) {
ccs_datum_t nodes[2];
nodes[0] = a;
nodes[1] = b;
test_expression_wrapper(t, 2, nodes, NULL, NULL, eres, eerr);
}
void test_unary_arithmetic(ccs_expression_type_t t,
ccs_datum_t a,
ccs_datum_t eres, ccs_error_t eerr) {
test_expression_wrapper(t, 1, &a, NULL, NULL, eres, eerr);
}
void test_arithmetic_add() {
test_binary_arithmetic(CCS_ADD,
ccs_float(1.0), ccs_float(2.0), ccs_float(1.0+2.0),
CCS_SUCCESS);
test_binary_arithmetic(CCS_ADD,
ccs_int(1), ccs_float(2.0), ccs_float(1+2.0),
CCS_SUCCESS);
test_binary_arithmetic(CCS_ADD,
ccs_float(1.0), ccs_int(2), ccs_float(1.0+2),
CCS_SUCCESS);
test_binary_arithmetic(CCS_ADD,
ccs_int(1), ccs_int(2), ccs_int(1+2),
CCS_SUCCESS);
test_binary_arithmetic(CCS_ADD,
ccs_int(1), ccs_bool(CCS_TRUE), ccs_none,
-CCS_INVALID_VALUE);
}
void test_arithmetic_substract() {
test_binary_arithmetic(CCS_SUBSTRACT,
ccs_float(1.0), ccs_float(2.0), ccs_float(1.0-2.0),
CCS_SUCCESS);
test_binary_arithmetic(CCS_SUBSTRACT,
ccs_int(1), ccs_float(2.0), ccs_float(1-2.0),
CCS_SUCCESS);
test_binary_arithmetic(CCS_SUBSTRACT,
ccs_float(1.0), ccs_int(2), ccs_float(1.0-2),
CCS_SUCCESS);
test_binary_arithmetic(CCS_SUBSTRACT,
ccs_int(1), ccs_int(2), ccs_int(1-2),
CCS_SUCCESS);
test_binary_arithmetic(CCS_SUBSTRACT,
ccs_int(1), ccs_bool(CCS_TRUE), ccs_none,
-CCS_INVALID_VALUE);
}
void test_arithmetic_multiply() {
test_binary_arithmetic(CCS_MULTIPLY,
ccs_float(3.0), ccs_float(2.0), ccs_float(3.0*2.0),
CCS_SUCCESS);
test_binary_arithmetic(CCS_MULTIPLY,
ccs_int(3), ccs_float(2.0), ccs_float(3*2.0),
CCS_SUCCESS);
test_binary_arithmetic(CCS_MULTIPLY,
ccs_float(3.0), ccs_int(2), ccs_float(3.0*2),
CCS_SUCCESS);
test_binary_arithmetic(CCS_MULTIPLY,
ccs_int(3), ccs_int(2), ccs_int(3*2),
CCS_SUCCESS);
test_binary_arithmetic(CCS_MULTIPLY,
ccs_int(3), ccs_bool(CCS_TRUE), ccs_none,
-CCS_INVALID_VALUE);
}
void test_arithmetic_divide() {
test_binary_arithmetic(CCS_DIVIDE,
ccs_float(3.0), ccs_float(2.0), ccs_float(3.0/2.0),
CCS_SUCCESS);
test_binary_arithmetic(CCS_DIVIDE,
ccs_int(3), ccs_float(2.0), ccs_float(3/2.0),
CCS_SUCCESS);
test_binary_arithmetic(CCS_DIVIDE,
ccs_float(3.0), ccs_int(2), ccs_float(3.0/2),
CCS_SUCCESS);
test_binary_arithmetic(CCS_DIVIDE,
ccs_int(3), ccs_int(2), ccs_int(3/2),
CCS_SUCCESS);
test_binary_arithmetic(CCS_DIVIDE,
ccs_int(3), ccs_bool(CCS_TRUE), ccs_none,
-CCS_INVALID_VALUE);
}
void test_arithmetic_modulo() {
test_binary_arithmetic(CCS_MODULO,
ccs_float(3.0), ccs_float(2.0), ccs_float(fmod(3.0, 2.0)),
CCS_SUCCESS);
test_binary_arithmetic(CCS_MODULO,
ccs_int(3), ccs_float(2.0), ccs_float(fmod(3, 2.0)),
CCS_SUCCESS);
test_binary_arithmetic(CCS_MODULO,
ccs_float(3.0), ccs_int(2), ccs_float(fmod(3.0, 2)),
CCS_SUCCESS);
test_binary_arithmetic(CCS_MODULO,
ccs_int(3), ccs_int(2), ccs_int(3%2),
CCS_SUCCESS);
test_binary_arithmetic(CCS_MODULO,
ccs_int(3), ccs_bool(CCS_TRUE), ccs_none,
-CCS_INVALID_VALUE);
}
void test_arithmetic_positive() {
test_unary_arithmetic(CCS_POSITIVE, ccs_float(3.0),
ccs_float(3.0), CCS_SUCCESS);
test_unary_arithmetic(CCS_POSITIVE, ccs_int(3),
ccs_int(3), CCS_SUCCESS);
test_unary_arithmetic(CCS_POSITIVE, ccs_bool(CCS_FALSE),
ccs_none, -CCS_INVALID_VALUE);
}
void test_arithmetic_negative() {
test_unary_arithmetic(CCS_NEGATIVE, ccs_float(3.0),
ccs_float(-3.0), CCS_SUCCESS);
test_unary_arithmetic(CCS_NEGATIVE, ccs_int(3),
ccs_int(-3), CCS_SUCCESS);
test_unary_arithmetic(CCS_NEGATIVE, ccs_bool(CCS_FALSE),
ccs_none, -CCS_INVALID_VALUE);
}
void test_arithmetic_not() {
test_unary_arithmetic(CCS_NOT, ccs_float(3.0),
ccs_none, -CCS_INVALID_VALUE);
test_unary_arithmetic(CCS_NOT, ccs_int(3),
ccs_none, -CCS_INVALID_VALUE);
test_unary_arithmetic(CCS_NOT, ccs_bool(CCS_FALSE),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_unary_arithmetic(CCS_NOT, ccs_bool(CCS_TRUE),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
}
void test_arithmetic_and() {
test_binary_arithmetic(CCS_AND,
ccs_bool(CCS_FALSE), ccs_bool(CCS_FALSE),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_AND,
ccs_bool(CCS_TRUE), ccs_bool(CCS_FALSE),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_AND,
ccs_bool(CCS_FALSE), ccs_bool(CCS_TRUE),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_AND,
ccs_bool(CCS_TRUE), ccs_bool(CCS_TRUE),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_AND,
ccs_int(1), ccs_bool(CCS_TRUE),
ccs_none, -CCS_INVALID_VALUE);
}
void test_arithmetic_or() {
test_binary_arithmetic(CCS_OR,
ccs_bool(CCS_FALSE), ccs_bool(CCS_FALSE),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_OR,
ccs_bool(CCS_TRUE), ccs_bool(CCS_FALSE),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_OR,
ccs_bool(CCS_FALSE), ccs_bool(CCS_TRUE),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_OR,
ccs_bool(CCS_TRUE), ccs_bool(CCS_TRUE),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_OR,
ccs_int(1), ccs_bool(CCS_TRUE),
ccs_none, -CCS_INVALID_VALUE);
}
void test_arithmetic_less() {
test_binary_arithmetic(CCS_LESS,
ccs_float(1.0), ccs_float(2.0),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_LESS,
ccs_float(1.0), ccs_float(1.0),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_LESS,
ccs_float(2.0), ccs_float(1.0),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_LESS,
ccs_int(1), ccs_float(2.0),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_LESS,
ccs_float(2.0), ccs_int(1),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_LESS,
ccs_int(1), ccs_int(1),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_LESS,
ccs_bool(CCS_TRUE), ccs_int(1),
ccs_none, -CCS_INVALID_VALUE);
test_binary_arithmetic(CCS_LESS,
ccs_string("bar"), ccs_string("foo"),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_LESS,
ccs_string("baz"), ccs_string("bar"),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_LESS,
ccs_string("bar"), ccs_string("bar"),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
}
void test_arithmetic_greater() {
test_binary_arithmetic(CCS_GREATER,
ccs_float(1.0), ccs_float(2.0),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_GREATER,
ccs_float(1.0), ccs_float(1.0),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_GREATER,
ccs_float(2.0), ccs_float(1.0),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_GREATER,
ccs_int(1), ccs_float(2.0),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_GREATER,
ccs_float(2.0), ccs_int(1),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_GREATER,
ccs_int(1), ccs_int(1),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_GREATER,
ccs_bool(CCS_TRUE), ccs_int(1),
ccs_none, -CCS_INVALID_VALUE);
test_binary_arithmetic(CCS_GREATER,
ccs_string("bar"), ccs_string("foo"),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_GREATER,
ccs_string("baz"), ccs_string("bar"),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_GREATER,
ccs_string("bar"), ccs_string("bar"),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
}
void test_arithmetic_less_or_equal() {
test_binary_arithmetic(CCS_LESS_OR_EQUAL,
ccs_float(1.0), ccs_float(2.0),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_LESS_OR_EQUAL,
ccs_float(1.0), ccs_float(1.0),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_LESS_OR_EQUAL,
ccs_float(2.0), ccs_float(1.0),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_LESS_OR_EQUAL,
ccs_int(1), ccs_float(2.0),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_LESS_OR_EQUAL,
ccs_float(2.0), ccs_int(1),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_LESS_OR_EQUAL,
ccs_int(1), ccs_int(1),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_LESS_OR_EQUAL,
ccs_bool(CCS_TRUE), ccs_int(1),
ccs_none, -CCS_INVALID_VALUE);
test_binary_arithmetic(CCS_LESS_OR_EQUAL,
ccs_string("bar"), ccs_string("foo"),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_LESS_OR_EQUAL,
ccs_string("baz"), ccs_string("bar"),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_LESS_OR_EQUAL,
ccs_string("bar"), ccs_string("bar"),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
}
void test_arithmetic_greater_or_equal() {
test_binary_arithmetic(CCS_GREATER_OR_EQUAL,
ccs_float(1.0), ccs_float(2.0),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_GREATER_OR_EQUAL,
ccs_float(1.0), ccs_float(1.0),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_GREATER_OR_EQUAL,
ccs_float(2.0), ccs_float(1.0),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_GREATER_OR_EQUAL,
ccs_int(1), ccs_float(2.0),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_GREATER_OR_EQUAL,
ccs_float(2.0), ccs_int(1),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_GREATER_OR_EQUAL,
ccs_int(1), ccs_int(1),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_GREATER_OR_EQUAL,
ccs_bool(CCS_TRUE), ccs_int(1),
ccs_none, -CCS_INVALID_VALUE);
test_binary_arithmetic(CCS_GREATER_OR_EQUAL,
ccs_string("bar"), ccs_string("foo"),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_GREATER_OR_EQUAL,
ccs_string("baz"), ccs_string("bar"),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_GREATER_OR_EQUAL,
ccs_string("bar"), ccs_string("bar"),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
}
void test_in() {
ccs_expression_t list;
ccs_datum_t values[4];
ccs_error_t err;
values[0] = ccs_float(3.0);
values[1] = ccs_int(1);
values[2] = ccs_string("foo");
values[3] = ccs_bool(CCS_TRUE);
err = ccs_create_expression(CCS_LIST, 4, values, &list);
assert( err == CCS_SUCCESS );
test_binary_arithmetic(CCS_IN,
ccs_float(3.0), ccs_object(list),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_IN,
ccs_int(1), ccs_object(list),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_IN,
ccs_string("foo"), ccs_object(list),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_IN,
ccs_bool(CCS_TRUE), ccs_object(list),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_IN,
ccs_int(3), ccs_object(list),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_IN,
ccs_float(1.0), ccs_object(list),
ccs_bool(CCS_TRUE), CCS_SUCCESS);
test_binary_arithmetic(CCS_IN,
ccs_float(2.0), ccs_object(list),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_IN,
ccs_int(2), ccs_object(list),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_IN,
ccs_string("bar"), ccs_object(list),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
test_binary_arithmetic(CCS_IN,
ccs_bool(CCS_FALSE), ccs_object(list),
ccs_bool(CCS_FALSE), CCS_SUCCESS);
err = ccs_release_object(list);
assert( err == CCS_SUCCESS );
}
void
test_compound() {
ccs_expression_t expression1, expression2;
ccs_datum_t result;
ccs_error_t err;
err = ccs_create_binary_expression(CCS_ADD, ccs_float(3.0), ccs_int(1),
&expression1);
assert( err == CCS_SUCCESS );
err = ccs_create_binary_expression(CCS_MULTIPLY, ccs_float(2.0),
ccs_object(expression1), &expression2);
assert( err == CCS_SUCCESS );
err = ccs_release_object(expression1);
assert( err == CCS_SUCCESS );
err = ccs_expression_eval(expression2, NULL, NULL, &result);
assert( err == CCS_SUCCESS );
assert( result.type == CCS_FLOAT );
assert( result.value.f == 8.0 );
err = ccs_release_object(expression2);
assert( err == CCS_SUCCESS );
}
int main(int argc, char *argv[]) {
ccs_init();
test_equal_literal();
test_equal_numerical();
test_equal_categorical();
test_equal_ordinal();
test_arithmetic_add();
test_arithmetic_substract();
test_arithmetic_multiply();
test_arithmetic_divide();
test_arithmetic_modulo();
test_arithmetic_positive();
test_arithmetic_negative();
test_arithmetic_not();
test_arithmetic_and();
test_arithmetic_or();
test_arithmetic_less();
test_arithmetic_greater();
test_arithmetic_less_or_equal();
test_arithmetic_greater_or_equal();
test_compound();
test_in();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment