diff --git a/tests/main.c b/tests/main.c index 6156436..74b21a4 100644 --- a/tests/main.c +++ b/tests/main.c @@ -2,7 +2,7 @@ int main(void) { - test_run_all(); + test_tensor_run_all(); return 0; } diff --git a/tests/test_tensor.c b/tests/test_tensor.c index 80ddc56..e75ef8f 100644 --- a/tests/test_tensor.c +++ b/tests/test_tensor.c @@ -1,13 +1,24 @@ #include "test_tensor.h" #include -void test_run_all(void) +void test_tensor_run_all(void) { int i; void (*test_func[NUM_TEST_FUNC])(void) = { + &test_tensor_is_empty, &test_tensor_is_equal, + &test_tensor_check_size, + &test_tensor_set_size, &test_tensor_set, &test_tensor_get, + &test_tensor_init_one, + &test_tensor_init_zero, + &test_tensor_init_rand, + &test_tensor_cpy, + &test_tensor_add_inplace, + &test_tensor_sub_inplace, + &test_tensor_add, + &test_tensor_sub, }; printf("\n### Running tests... ###\n\n"); @@ -17,6 +28,19 @@ void test_run_all(void) printf("\n### %i functions tested. ###\n\n", NUM_TEST_FUNC); } +void test_tensor_is_empty(void) +{ + /* Depends on tensor_init_zero */ + uint32_t s[4] = {2, 5, 3, 7}; + tensor t1 = tensor_new(); + tensor t2 = tensor_new(); + + tensor_init_zero(t2, s, 4); + + tensor_assert(tensor_is_empty(t1), "(empty tensor not detected)"); + tensor_assert(!tensor_is_empty(t2), "(non empty tensor not detected)"); +} + void test_tensor_is_equal(void) { /* Depends on tensor_cpy, tensor_is_equal, tensor_init_zero, tensor_init_rand */ @@ -35,6 +59,24 @@ void test_tensor_is_equal(void) tensor_destroy(t2); } +void test_tensor_check_size(void) +{ + uint32_t size[] = {3, 2, 5, 0}; + tensor_assert(_tensor_check_size(size, 3), "(valid size declared as invalid)"); + tensor_assert(!_tensor_check_size(size, 5), "(no dimension should be 0)"); + tensor_assert(_tensor_check_size(size, 0), "(rank can be 0)"); + tensor_assert(!_tensor_check_size(size, -3), "(rank should be non negative)"); +} + +void test_tensor_set_size(void) +{ + uint32_t size[] = {3, 2, 7}; + tensor t = tensor_new(); + + tensor_assert(_tensor_set_size(t, size, 3), "(size should be set without error)"); + tensor_assert(t->num_elem == 3 * 2 * 7, "(wrong number of elements)"); +} + void test_tensor_set(void) { /* Depends on tensor_is_equal, tensor_init_zero, tensor_init_rand */ @@ -63,7 +105,7 @@ void test_tensor_set(void) void test_tensor_get(void) { /* Depends on tensor_is_equal, tensor_set, tensor_init_zero, tensor_init_rand */ - uint8_t status; + bool status; uint32_t s[4] = {2, 5, 3, 7}; uint32_t index[4] = {0, 0, 0, 0}; tensor t1 = tensor_new(); @@ -87,3 +129,43 @@ void test_tensor_get(void) tensor_destroy(t2); } +void test_tensor_init_one(void) +{ + //TODO +} + +void test_tensor_init_zero(void) +{ + //TODO +} + +void test_tensor_init_rand(void) +{ + //TODO +} + +void test_tensor_cpy(void) +{ + //TODO +} + +void test_tensor_add_inplace(void) +{ + //TODO +} + +void test_tensor_sub_inplace(void) +{ + //TODO +} + +void test_tensor_add(void) +{ + //TODO +} + +void test_tensor_sub(void) +{ + //TODO +} + diff --git a/tests/test_tensor.h b/tests/test_tensor.h index 4e64066..a6e1050 100644 --- a/tests/test_tensor.h +++ b/tests/test_tensor.h @@ -6,7 +6,7 @@ #include #include "../tensor.h" -#define NUM_TEST_FUNC 3 +#define NUM_TEST_FUNC 14 #define ANSI_COLOR_GREEN "\x1b[32m" #define ANSI_COLOR_RESET "\x1b[0m" @@ -14,30 +14,41 @@ #define ANSI_COLOR_YELLOW "\x1b[33m" #define ANSI_COLOR_BLUE "\x1b[34m" -#define tensor_assert(X, msg) do{ \ - FILE* stream = stderr; \ - if (!X) { \ +#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) { \ + 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); \ - } \ + 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)") -void test_run_all(void); +void test_tensor_run_all(void); + +void test_tensor_is_empty(void); void test_tensor_is_equal(void); +void test_tensor_check_size(void); +void test_tensor_set_size(void); void test_tensor_set(void); void test_tensor_get(void); +void test_tensor_init_one(void); +void test_tensor_init_zero(void); +void test_tensor_init_rand(void); +void test_tensor_cpy(void); +void test_tensor_add_inplace(void); +void test_tensor_sub_inplace(void); +void test_tensor_add(void); +void test_tensor_sub(void); -#endif - +#endif // _TEST_TENSOR_H_INCLUDED_