diff --git a/main.c b/main.c new file mode 100644 index 0000000..c163b1b --- /dev/null +++ b/main.c @@ -0,0 +1,9 @@ +#include "tensor.h" +#include "test_tensor.h" + +int main(void) +{ + test_run_all(); + return 0; +} + diff --git a/test_tensor.c b/test_tensor.c new file mode 100644 index 0000000..065aafd --- /dev/null +++ b/test_tensor.c @@ -0,0 +1,91 @@ +#include "test_tensor.h" + +void test_run_all(void) +{ + int i; + void (*test_func[NUM_TEST_FUNC])() = { + &test_tensor_is_equal, + &test_tensor_set, + &test_tensor_get, + }; + + printf("\n### Running tests... ###\n\n"); + for(i = 0; i < NUM_TEST_FUNC; i++) { + test_func[i](); + } + printf("\n### %i functions tested. ###\n\n", NUM_TEST_FUNC); +} + +void test_tensor_is_equal(void) +{ + /* Depends on tensor_cpy, tensor_is_equal, tensor_init_zero, tensor_init_rand */ + int status; + int s[4] = {2, 5, 3, 7}; + tensor t1 = tensor_new(); + tensor t2 = tensor_new(); + + tensor_init_zero(t1, 4, s); + tensor_init_one(t2, 4, s); + tensor_assert_ne(t1, t2); + + tensor_init_rand(t1, 4, s, 30); + tensor_cpy(t2, t1); + + tensor_assert_eq(t1, t2); + tensor_destroy(t1); + tensor_destroy(t2); +} + +void test_tensor_set(void) +{ + /* Depends on tensor_is_equal, tensor_init_zero, tensor_init_rand */ + int status; + int s[4] = {2, 5, 3, 7}; + int index[4] = {0, 0, 0, 0}; + tensor t1 = tensor_new(); + tensor t2 = tensor_new(); + + tensor_init_rand(t1, 4, s, 30); + tensor_init_zero(t2, 4, s); + + for (index[0] = 0; index[0] < s[0]; ++index[0]) { + for (index[1] = 0; index[1] < s[1]; ++index[1]) { + for (index[2] = 0; index[2] < s[2]; ++index[2]) { + for (index[3] = 0; index[3] < s[3]; ++index[3]) { + tensor_assert(tensor_set(t1, index, 0), "mute"); + } + } + } + } + tensor_assert_eq(t1, t2); + tensor_destroy(t1); + tensor_destroy(t2); +} + +void test_tensor_get(void) +{ + /* Depends on tensor_is_equal, tensor_set, tensor_init_zero, tensor_init_rand */ + int status; + int s[4] = {2, 5, 3, 7}; + int index[4] = {0, 0, 0, 0}; + tensor t1 = tensor_new(); + tensor t2 = tensor_new(); + + tensor_init_rand(t1, 4, s, 30); + tensor_init_zero(t2, 4, s); + + for (index[0] = 0; index[0] < s[0]; ++index[0]) { + for (index[1] = 0; index[1] < s[1]; ++index[1]) { + for (index[2] = 0; index[2] < s[2]; ++index[2]) { + for (index[3] = 0; index[3] < s[3]; ++index[3]) { + tensor_set(t2, index, tensor_get(t1, index, &status)); + tensor_assert(status, "mute"); + } + } + } + } + tensor_assert_eq(t1, t2); + tensor_destroy(t1); + tensor_destroy(t2); +} + diff --git a/test_tensor.h b/test_tensor.h new file mode 100644 index 0000000..55ed7a5 --- /dev/null +++ b/test_tensor.h @@ -0,0 +1,45 @@ +#ifndef _TEST_TENSOR_H_INCLUDED_ +#define _TEST_TENSOR_H_INCLUDED_ + +#include +#include +#include +#include "tensor.h" + +#define NUM_TEST_FUNC 3 + +#define ANSI_COLOR_GREEN "\x1b[32m" +#define ANSI_COLOR_RESET "\x1b[0m" +#define ANSI_COLOR_RED "\x1b[31m" +#define ANSI_COLOR_YELLOW "\x1b[33m" +#define ANSI_COLOR_BLUE "\x1b[34m" + +#define tensor_assert(X, msg) do{ \ + FILE* stream = stderr; \ + if (!X) { \ + fputs(ANSI_COLOR_RED "Assertion failed: " ANSI_COLOR_RESET, stream); \ + fprintf(stream, \ + "function %s, file %s, line %i. ", __func__, __FILE__, __LINE__); \ + fputs(msg, stream); \ + fputc('\n', stream); \ + } else if (strcmp(msg, "mute") != 0) { \ + fputs(ANSI_COLOR_GREEN "Assertion succeeded: " ANSI_COLOR_RESET, stream); \ + fprintf(stream, \ + "function %s, file %s, line %i. ", __func__, __FILE__, __LINE__); \ + fputc('\n', stream); \ + } \ +} while (0) + +#define tensor_assert_eq(X, Y) tensor_assert(tensor_is_equal((X), (Y)), "(tensor_assert_eq)") +#define tensor_assert_ne(X, Y) tensor_assert(!tensor_is_equal((X), (Y)), "(tensor_assert_ne)") + +#define SUCCESS_PRINT printf(ANSI_COLOR_GREEN "success" ANSI_COLOR_RESET " of %s\n", __func__); + +void test_run_all(void); + +void test_tensor_is_equal(void); +void test_tensor_set(void); +void test_tensor_get(void); + +#endif +