diff --git a/include/query.h b/include/query.h index f267fd7..9ce62f0 100644 --- a/include/query.h +++ b/include/query.h @@ -14,11 +14,11 @@ enum OPERATORS { struct join_params { - bool (*check)(int *in1, int *in2); + bool (*check)(const int *in1, const int *in2); }; struct filter_params { - bool (*check)(int *in); + bool (*check)(const int *in); }; struct window_params { @@ -27,7 +27,7 @@ struct window_params { typedef union Parameters { - struct join_params join; + struct join_params join; struct filter_params filter; struct window_params window; } parameter_t; diff --git a/src/query.c b/src/query.c index 8419583..34add34 100644 --- a/src/query.c +++ b/src/query.c @@ -4,22 +4,21 @@ #include "query.h" #include -#include #include -void join(int *in1, int *in2, int **out, parameter_t param) +void join(const int *in1, const int *in2, int **out, const parameter_t param) { int *local_out = malloc(sizeof(int)); *out = local_out; - printf("The join funftion\n"); + if (param.join.check(in1, in2)) { - printf("Join allowed\n"); + *local_out = (*in1) + (*in2); } }; -void filter(int *in, int **out, parameter_t param) +void filter(const int *in, int **out, const parameter_t param) { int *local_out = malloc(sizeof(int)); *out = local_out; @@ -30,12 +29,12 @@ void filter(int *in, int **out, parameter_t param) } -void window(int *in, int **out, parameter_t param) +void window(const int *in, int **out, const parameter_t param) { int *local_out = malloc(sizeof(int)); *out = local_out; - printf("The window function, %p\n", in); - printf("The window size is: %i\n", param.window.window_size); + + *local_out = (*in) * param.window.window_size; } diff --git a/tests/queryTests.cpp b/tests/queryTests.cpp index 38dd3c9..9d173d5 100644 --- a/tests/queryTests.cpp +++ b/tests/queryTests.cpp @@ -7,17 +7,18 @@ extern "C" { #include "query.h" } -bool check_join(int *in1, int *in2) +bool check_join(const int *in1, const int *in2) { return *in1 > *in2; } -TEST(QueryTests, test_query_join_no_children) { - int *ptr; +TEST(QueryTests, test_query_join_death_no_children) +{ + int *ptr = nullptr; operator_t join_op = { .type = JOIN, - .left = NULL, - .right = NULL, + .left = nullptr, + .right = nullptr, .params = {check_join} }; query_t query_join = {.root = &join_op}; @@ -26,11 +27,12 @@ TEST(QueryTests, test_query_join_no_children) { ASSERT_DEATH(execute_query(&query_join, ptr, &ptr), ""); } -TEST(QueryTests, test_query_join_death_no_left_child) { - int *ptr; +TEST(QueryTests, test_query_join_death_no_left_child) +{ + int *ptr = nullptr; operator_t join_op = { .type = JOIN, - .left = NULL, + .left = nullptr, .right = &join_op, .params = {check_join} }; @@ -40,12 +42,13 @@ TEST(QueryTests, test_query_join_death_no_left_child) { ASSERT_DEATH(execute_query(&query_join, ptr, &ptr), ""); } -TEST(QueryTests, test_query_join_death_no_right_child) { - int *ptr; +TEST(QueryTests, test_query_join_death_no_right_child) +{ + int *ptr = nullptr; operator_t join_op = { .type = JOIN, .left = &join_op, - .right = NULL, + .right = nullptr, .params = {check_join} }; query_t query_join = {.root = &join_op}; @@ -55,19 +58,20 @@ TEST(QueryTests, test_query_join_death_no_right_child) { } -bool check_filter(int *in) +bool check_filter(const int *in) { return *in < 2; } -TEST(QueryTests, test_query_filter) { +TEST(QueryTests, test_query_filter) +{ int input = 0; int *out; operator_t filter_op = { .type = FILTER, - .left = NULL, - .right = NULL, + .left = nullptr, + .right = nullptr, .params = {.filter = check_filter} }; @@ -76,5 +80,118 @@ TEST(QueryTests, test_query_filter) { execute_query(&query_filter, &input, &out); ASSERT_EQ(*out, 1); + free(out); +} + + +TEST(QueryTests, test_query_filter2) +{ + int input = 0; + int *out; + + operator_t filter_op = { + .type = FILTER, + .left = nullptr, + .right = nullptr, + .params = {.filter = check_filter} + }; + + operator_t filter_op2 = { + .type = FILTER, + .left = &filter_op, + .right = nullptr, + .params = {.filter = check_filter} + }; + + query_t query_filter = {.root = &filter_op2}; + + execute_query(&query_filter, &input, &out); + ASSERT_EQ(*out, 2); + + free(out); +} + + +TEST(QueryTests, test_query_window) +{ + int input = 1; + int *out; + + operator_t window_op = { + .type = WINDOW, + .left = nullptr, + .right = nullptr, + .params = {.window = 10} + }; + + query_t query_window = {.root = &window_op}; + + execute_query(&query_window, &input, &out); + ASSERT_EQ(*out, 10); + + free(out); +} + + +TEST(QueryTests, test_query_window2) +{ + int input = 1; + int *out; + + operator_t window_op = { + .type = WINDOW, + .left = nullptr, + .right = nullptr, + .params = {.window = 10} + }; + + operator_t window_op2 = { + .type = WINDOW, + .left = &window_op, + .right = nullptr, + .params = {.window = 5} + }; + + query_t query_window = {.root = &window_op2}; + + execute_query(&query_window, &input, &out); + ASSERT_EQ(*out, 50); + + free(out); +} + + +TEST(QueryTests, test_query_join) +{ + int input = 1; + int *out; + + operator_t window_op = { + .type = WINDOW, + .left = nullptr, + .right = nullptr, + .params = {.window = 10} + }; + + operator_t filter_op = { + .type = FILTER, + .left = nullptr, + .right = nullptr, + .params = {.filter = check_filter} + }; + + operator_t join_op = { + .type = JOIN, + .left = &window_op, + .right = &filter_op, + .params = {.join = check_join} + }; + + query_t query = {.root = &join_op}; + + execute_query(&query, &input, &out); + + ASSERT_EQ(*out, 12); + free(out); } \ No newline at end of file