Skip to content

Commit

Permalink
Added some tests to show example usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Griezn committed Oct 15, 2024
1 parent 4ffa80f commit 711f3c6
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 26 deletions.
6 changes: 3 additions & 3 deletions include/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
15 changes: 7 additions & 8 deletions src/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
#include "query.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>


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;
Expand All @@ -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;
}


Expand Down
147 changes: 132 additions & 15 deletions tests/queryTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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}
};
Expand All @@ -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};
Expand All @@ -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}
};

Expand All @@ -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);
}

0 comments on commit 711f3c6

Please sign in to comment.