From 8c2906765f707a8a906a29db29ad72ca0b08d393 Mon Sep 17 00:00:00 2001 From: Elias Kohout Date: Mon, 4 Sep 2023 16:41:07 +0200 Subject: [PATCH] Added more tests. --- tensoriterator.c => tensoriter.c | 2 +- tensoriterator.h => tensoriter.h | 0 tests/main.c | 28 ++++++++-- tests/main.h | 5 +- tests/tensor_assert.h | 2 + tests/tensor_test.c | 92 ++++++++++++++++++++++++++++++-- tests/tensor_test.h | 3 -- tests/tensoriter_test.c | 36 +++++++++++++ tests/tensoriter_test.h | 16 ++++++ 9 files changed, 170 insertions(+), 14 deletions(-) rename tensoriterator.c => tensoriter.c (99%) rename tensoriterator.h => tensoriter.h (100%) create mode 100644 tests/tensoriter_test.c create mode 100644 tests/tensoriter_test.h diff --git a/tensoriterator.c b/tensoriter.c similarity index 99% rename from tensoriterator.c rename to tensoriter.c index 0a730e9..390831f 100644 --- a/tensoriterator.c +++ b/tensoriter.c @@ -1,4 +1,4 @@ -#include "tensoriterator.h" +#include "tensoriter.h" tensoriter_scalar tensoriter_scalar_create(tensor t) { diff --git a/tensoriterator.h b/tensoriter.h similarity index 100% rename from tensoriterator.h rename to tensoriter.h diff --git a/tests/main.c b/tests/main.c index 78d084a..56e2c17 100644 --- a/tests/main.c +++ b/tests/main.c @@ -3,13 +3,14 @@ int main(void) { tensor_test_run_all(); + tensoriter_test_run_all(); return 0; } void tensor_test_run_all(void) { int i; - void (*test_func[NUM_TEST_FUNC])(void) = { + void (*test_func[NUM_TENSOR_TEST_FUNC])(void) = { &tensor_test_is_empty, &tensor_test_is_equal, &tensor_test_check_size, @@ -26,10 +27,29 @@ void tensor_test_run_all(void) &tensor_test_sub, }; - printf("\n### Running tests... ###\n\n"); - for(i = 0; i < NUM_TEST_FUNC; i++) { + printf("\n### Running tests for tensor.c ... ###\n\n"); + for(i = 0; i < NUM_TENSOR_TEST_FUNC; i++) { test_func[i](); } - printf("\n### %i functions tested. ###\n\n", NUM_TEST_FUNC); + printf("\n### %i functions tested. ###\n\n", NUM_TENSOR_TEST_FUNC); } +void tensoriter_test_run_all(void) +{ + int i; + void (*test_func[NUM_TENSORITER_TEST_FUNC])(void) = { + &tensoriter_test_scalar_next, + &tensoriter_test_scalar_get, + &tensoriter_test_scalar_map, + &tensoriter_test_scalar_map_add, + &tensoriter_test_scalar_map_sub, + &tensoriter_test_scalar_map_mul, + &tensoriter_test_scalar_map_div, + }; + + printf("\n### Running tests for tensoriter.c ... ###\n\n"); + for(i = 0; i < NUM_TENSORITER_TEST_FUNC; i++) { + test_func[i](); + } + printf("\n### %i functions tested. ###\n\n", NUM_TENSORITER_TEST_FUNC); +} diff --git a/tests/main.h b/tests/main.h index a1fac36..345e3c4 100644 --- a/tests/main.h +++ b/tests/main.h @@ -3,10 +3,13 @@ #include "tensor_test.h" +#include "tensoriter_test.h" -#define NUM_TEST_FUNC 14 +#define NUM_TENSOR_TEST_FUNC 14 +#define NUM_TENSORITER_TEST_FUNC 7 void tensor_test_run_all(void); +void tensoriter_test_run_all(void); #endif // _MAIN_H_INCLUDED_ diff --git a/tests/tensor_assert.h b/tests/tensor_assert.h index fcf5ed2..16541df 100644 --- a/tests/tensor_assert.h +++ b/tests/tensor_assert.h @@ -27,5 +27,7 @@ #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)") +#include +#include #endif // _TENSOR_ASSERT_H_INCLUDED_ diff --git a/tests/tensor_test.c b/tests/tensor_test.c index 3effc36..8552362 100644 --- a/tests/tensor_test.c +++ b/tests/tensor_test.c @@ -196,21 +196,103 @@ void tensor_test_cpy(void) void tensor_test_add_inplace(void) { - //TODO + /* Depends on tensor_is_equal, tensor_init_one, tensor_set */ + uint32_t s[3] = {3, 2, 4}; + uint32_t index[3] = {0, 0, 0}; + tensor t1 = tensor_new(); + tensor t2 = tensor_new(); + tensor t3 = tensor_new(); + + tensor_init_one(t1, s, 3); + tensor_init_one(t2, s, 3); + tensor_init_one(t3, s, 3); + + 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]) { + tensor_assert(tensor_set(t1, index, index[0] + index[1] + index[2]), "mute"); + tensor_assert(tensor_set(t2, index, (s[0] + s[1] + s[2]) - (index[0] + index[1] + index[2])), "mute"); + tensor_assert(tensor_set(t3, index, (s[0] + s[1] + s[2])), "mute"); + } + } + } + + tensor_add_inplace(t1, t2); + tensor_assert_eq(t1, t3); + + tensor_destroy(t1); + tensor_destroy(t2); + tensor_destroy(t3); } void tensor_test_sub_inplace(void) { - //TODO + /* Depends on tensor_is_equal, tensor_init_one, tensor_init_zero */ + uint32_t s[3] = {3, 2, 4}; + tensor t1 = tensor_new(); + tensor t2 = tensor_new(); + tensor t3 = tensor_new(); + + tensor_init_one(t1, s, 3); + tensor_init_one(t2, s, 3); + tensor_init_zero(t3, s, 3); + + tensor_sub_inplace(t1, t2); + tensor_assert_eq(t1, t3); + + tensor_destroy(t1); + tensor_destroy(t2); + tensor_destroy(t3); } void tensor_test_add(void) { - //TODO + /* Depends on tensor_is_equal, tensor_init_one, tensor_set */ + uint32_t s[3] = {3, 2, 4}; + uint32_t index[3] = {0, 0, 0}; + tensor t1 = tensor_new(); + tensor t2 = tensor_new(); + tensor t3 = tensor_new(); + + tensor_init_one(t1, s, 3); + tensor_init_one(t2, s, 3); + tensor_init_one(t3, s, 3); + + 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]) { + tensor_assert(tensor_set(t1, index, index[0] + index[1] + index[2]), "mute"); + tensor_assert(tensor_set(t2, index, (s[0] + s[1] + s[2]) - (index[0] + index[1] + index[2])), "mute"); + tensor_assert(tensor_set(t3, index, (s[0] + s[1] + s[2])), "mute"); + } + } + } + + tensor t4 = tensor_add(t1, t2); + tensor_assert_eq(t4, t3); + + tensor_destroy(t1); + tensor_destroy(t2); + tensor_destroy(t3); + tensor_destroy(t4); } void tensor_test_sub(void) { - //TODO -} + /* Depends on tensor_is_equal, tensor_init_one, tensor_init_zero */ + uint32_t s[3] = {3, 2, 4}; + tensor t1 = tensor_new(); + tensor t2 = tensor_new(); + tensor t3 = tensor_new(); + tensor_init_one(t1, s, 3); + tensor_init_one(t2, s, 3); + tensor_init_zero(t3, s, 3); + + tensor t4 = tensor_sub(t1, t2); + tensor_assert_eq(t4, t3); + + tensor_destroy(t1); + tensor_destroy(t2); + tensor_destroy(t3); +} diff --git a/tests/tensor_test.h b/tests/tensor_test.h index 45ca716..ad85628 100644 --- a/tests/tensor_test.h +++ b/tests/tensor_test.h @@ -1,9 +1,6 @@ #ifndef _TEST_TENSOR_H_INCLUDED_ #define _TEST_TENSOR_H_INCLUDED_ -#include -#include -#include #include "../tensor.h" #include "tensor_assert.h" diff --git a/tests/tensoriter_test.c b/tests/tensoriter_test.c new file mode 100644 index 0000000..f76f98f --- /dev/null +++ b/tests/tensoriter_test.c @@ -0,0 +1,36 @@ +#include "tensoriter_test.h" + +void tensoriter_test_scalar_next(void) +{ + //TODO +} + +void tensoriter_test_scalar_get(void) +{ + //TODO +} + +void tensoriter_test_scalar_map(void) +{ + //TODO +} + +void tensoriter_test_scalar_map_add(void) +{ + //TODO +} + +void tensoriter_test_scalar_map_sub(void) +{ + //TODO +} + +void tensoriter_test_scalar_map_mul(void) +{ + //TODO +} + +void tensoriter_test_scalar_map_div(void) +{ + //TODO +} diff --git a/tests/tensoriter_test.h b/tests/tensoriter_test.h new file mode 100644 index 0000000..34c14d7 --- /dev/null +++ b/tests/tensoriter_test.h @@ -0,0 +1,16 @@ +#ifndef _TESNORITER_TEST_H_INCLUDED_ +#define _TESNORITER_TEST_H_INCLUDED_ + +#include "../tensor.h" +#include "../tensoriter.h" +#include "tensor_assert.h" + +void tensoriter_test_scalar_next(void); +void tensoriter_test_scalar_get(void); +void tensoriter_test_scalar_map(void); +void tensoriter_test_scalar_map_add(void); +void tensoriter_test_scalar_map_sub(void); +void tensoriter_test_scalar_map_mul(void); +void tensoriter_test_scalar_map_div(void); + +#endif // _TESNORITER_TEST_H_INCLUDED_