From 7505b22d1836e3ff17dc7d7f10751ab4182fb44a Mon Sep 17 00:00:00 2001 From: Elias Kohout Date: Fri, 15 Sep 2023 17:07:31 +0200 Subject: [PATCH] Updated the tests and declared new tests for the new functions. --- tests/main.c | 52 +++--- tests/main.h | 8 +- tests/tensor_test.c | 193 ---------------------- tests/tensor_test.h | 9 - tests/tensorfunc_test.c | 353 ++++++++++++++++++++++++++++++++++++++++ tests/tensorfunc_test.h | 28 ++++ tests/tensoriter_test.c | 209 ------------------------ tests/tensoriter_test.h | 17 -- 8 files changed, 412 insertions(+), 457 deletions(-) create mode 100644 tests/tensorfunc_test.c create mode 100644 tests/tensorfunc_test.h delete mode 100644 tests/tensoriter_test.c delete mode 100644 tests/tensoriter_test.h diff --git a/tests/main.c b/tests/main.c index e76edc5..75dfb7f 100644 --- a/tests/main.c +++ b/tests/main.c @@ -3,7 +3,7 @@ int main(void) { tensor_test_run_all(); - tensoriter_test_run_all(); + tensorfunc_test_run_all(); return 0; } @@ -21,6 +21,29 @@ void tensor_test_run_all(void) &tensor_test_init_zero, &tensor_test_init_rand, &tensor_test_cpy, + }; + + 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_TENSOR_TEST_FUNC); +} + +void tensorfunc_test_run_all(void) +{ + int i; + void (*test_func[NUM_TENSORFUNC_TEST_FUNC])(void) = { + &tensor_test_fill, + &tensor_test_inspect, + &tensor_test_map, + &tensor_test_map_inplace, + &tensor_test_combine, + &tensor_test_combine_inplace, + &tensor_test_add_scalar, + &tensor_test_sub_scalar, + &tensor_test_mul_scalar, + &tensor_test_div_scalar, &tensor_test_add_inplace, &tensor_test_sub_inplace, &tensor_test_mul_inplace, @@ -31,30 +54,9 @@ void tensor_test_run_all(void) &tensor_test_div, }; - printf("\n### Running tests for tensor.c ... ###\n\n"); - for(i = 0; i < NUM_TENSOR_TEST_FUNC; i++) { + printf("\n### Running tests for tensorfunc.c ... ###\n\n"); + for(i = 0; i < NUM_TENSORFUNC_TEST_FUNC; i++) { test_func[i](); } - 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_set, - &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); + printf("\n### %i functions tested. ###\n\n", NUM_TENSORFUNC_TEST_FUNC); } diff --git a/tests/main.h b/tests/main.h index ecd5bdb..6eaefb0 100644 --- a/tests/main.h +++ b/tests/main.h @@ -3,13 +3,13 @@ #include "tensor_test.h" -#include "tensoriter_test.h" +#include "tensorfunc_test.h" -#define NUM_TENSOR_TEST_FUNC 18 -#define NUM_TENSORITER_TEST_FUNC 8 +#define NUM_TENSOR_TEST_FUNC 10 +#define NUM_TENSORFUNC_TEST_FUNC 18 void tensor_test_run_all(void); -void tensoriter_test_run_all(void); +void tensorfunc_test_run_all(void); #endif // _MAIN_H_INCLUDED_ diff --git a/tests/tensor_test.c b/tests/tensor_test.c index a357d12..2cc9f54 100644 --- a/tests/tensor_test.c +++ b/tests/tensor_test.c @@ -193,196 +193,3 @@ void tensor_test_cpy(void) tensor_destroy(t1); tensor_destroy(t2); } - -void tensor_test_add_inplace(void) -{ - /* 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) -{ - /* 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_mul_inplace(void) -{ - /* 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_init_rand(t1, s, 3, (dtype) 30); - tensor_init_zero(t2, s, 3); - - tensor_mul_inplace(t1, t2); - tensor_assert_eq(t1, t2); - - tensor_destroy(t1); - tensor_destroy(t2); -} - -void tensor_test_div_inplace(void) -{ - /* Depends on tensor_is_equal, tensor_init_one, tensor_init_zero, tensor_add_inplace */ - 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_one(t3, s, 3); - - tensor_add_inplace(t1, t3); - tensor_add_inplace(t1, t3); - tensor_add_inplace(t2, t3); - tensor_add_inplace(t2, t3); - - tensor_div_inplace(t1, t2); - tensor_assert_eq(t1, t3); - - tensor_destroy(t1); - tensor_destroy(t2); - tensor_destroy(t3); -} - -void tensor_test_add(void) -{ - /* 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) -{ - /* 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); - tensor_destroy(t4); -} - -void tensor_test_mul(void) -{ - /* Depends on tensor_is_equal, tensor_init_rand, tensor_init_zero */ - uint32_t s[3] = {3, 2, 4}; - tensor t1 = tensor_new(); - tensor t2 = tensor_new(); - tensor t3 = tensor_new(); - - tensor_init_rand(t1, s, 3, (dtype) 100); - tensor_init_zero(t2, s, 3); - tensor_init_zero(t3, s, 3); - - tensor t4 = tensor_mul(t1, t2); - tensor_assert_eq(t4, t3); - - tensor_destroy(t1); - tensor_destroy(t2); - tensor_destroy(t3); - tensor_destroy(t4); -} - -void tensor_test_div(void) -{ - /* Depends on tensor_is_equal, tensor_init_one, tensor_init_zero, tensor_add_inplace */ - 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_one(t3, s, 3); - - tensor_add_inplace(t1, t3); - tensor_add_inplace(t1, t3); - tensor_add_inplace(t2, t3); - tensor_add_inplace(t2, t3); - - tensor t4 = tensor_div(t1, t2); - tensor_assert_eq(t4, t3); - - tensor_destroy(t1); - tensor_destroy(t2); - tensor_destroy(t3); - tensor_destroy(t4); -} diff --git a/tests/tensor_test.h b/tests/tensor_test.h index d1aac70..05b3c10 100644 --- a/tests/tensor_test.h +++ b/tests/tensor_test.h @@ -1,7 +1,6 @@ #ifndef _TEST_TENSOR_H_INCLUDED_ #define _TEST_TENSOR_H_INCLUDED_ - #include "../tensor.h" #include "tensor_assert.h" @@ -16,13 +15,5 @@ void tensor_test_init_one(void); void tensor_test_init_zero(void); void tensor_test_init_rand(void); void tensor_test_cpy(void); -void tensor_test_add_inplace(void); -void tensor_test_sub_inplace(void); -void tensor_test_mul_inplace(void); -void tensor_test_div_inplace(void); -void tensor_test_add(void); -void tensor_test_sub(void); -void tensor_test_mul(void); -void tensor_test_div(void); #endif // _TEST_TENSOR_H_INCLUDED_ diff --git a/tests/tensorfunc_test.c b/tests/tensorfunc_test.c new file mode 100644 index 0000000..f259a27 --- /dev/null +++ b/tests/tensorfunc_test.c @@ -0,0 +1,353 @@ +#include "tensorfunc_test.h" + +void tensor_test_fill(void) +{ + // TODO +} + +void tensor_test_inspect(void) +{ + // TODO +} + +void tensor_test_map(void) +{ + // TODO +} + +dtype tensor_test_map_helper(dtype d) { + static uint32_t contained = 0; + if(((1 << ((uint32_t) d - 1)) & contained) == 0) { + contained |= 1 << ((uint32_t) d - 1); + return DTYPE_ZERO; + } else { + return d; + } +} + +void tensor_test_map_inplace(void) +{ + /* Depends on tensor_init_one, tensor_init_zero, tensor_set*/ + uint32_t s[3] = {2, 4, 4}; + uint32_t index[3] = {0, 0, 0}; + tensor t = tensor_new(); + tensor t0 = tensor_new(); + + tensor_init_zero(t0, s, 3); + + tensor_init_one(t, s, 3); + float i = 1; + 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_set(t, index, i++); + } + } + } + + tensor_map_inplace(t, &tensor_test_map_helper); + + tensor_assert_eq(t0, t); + + tensor_destroy(t); +} + +void tensor_test_combine(void) +{ + // TODO +} + +void tensor_test_combine_inplace(void) +{ + // TODO +} + +void tensor_test_add_scalar(void) +{ + /* Depends on tensor_init_one, tensor_add_inplace */ + uint32_t s[4] = {2, 5, 3, 7}; + + tensor t1 = tensor_new(); + tensor t2 = tensor_new(); + tensor t3 = tensor_new(); + + tensor_init_one(t1, s, 4); + tensor_init_one(t2, s, 4); + tensor_init_one(t3, s, 4); + + tensor_add_inplace(t1, t2); + tensor_add_inplace(t1, t2); + tensor_add_inplace(t1, t2); + + tensor_add_scalar(t3, 3.0); + + tensor_assert_eq(t1, t3); + + tensor_destroy(t1); + tensor_destroy(t2); + tensor_destroy(t3); +} + +void tensor_test_sub_scalar(void) +{ + /* Depends on tensor_init_one, tensor_add_inplace */ + uint32_t s[4] = {2, 5, 3, 7}; + + tensor t1 = tensor_new(); + tensor t2 = tensor_new(); + tensor t3 = tensor_new(); + + tensor_init_one(t1, s, 4); + tensor_init_one(t2, s, 4); + tensor_init_one(t3, s, 4); + + tensor_add_inplace(t1, t2); + tensor_add_inplace(t1, t2); + tensor_add_inplace(t1, t2); + + tensor_sub_scalar(t3, 3.0); + + tensor_assert_eq(t1, t3); + + tensor_destroy(t1); + tensor_destroy(t2); + tensor_destroy(t3); +} + +void tensor_test_mul_scalar(void) +{ + /* Depends on tensor_init_one, tensor_init_zero */ + uint32_t s[4] = {2, 5, 3, 7}; + + tensor t1 = tensor_new(); + tensor t2 = tensor_new(); + + tensor_init_one(t1, s, 4); + tensor_init_zero(t2, s, 4); + + tensor_mul_scalar(t1, 0.0); + + tensor_assert_eq(t1, t2); + + tensor_destroy(t1); + tensor_destroy(t2); +} + +void tensor_test_div_scalar(void) +{ + /* Depends on tensor_init_one, tensor_add_inplace */ + uint32_t s[4] = {2, 5, 3, 7}; + + tensor t1 = tensor_new(); + tensor t2 = tensor_new(); + tensor t3 = tensor_new(); + + tensor_init_one(t1, s, 4); + tensor_init_one(t2, s, 4); + tensor_init_one(t3, s, 4); + + tensor_add_inplace(t1, t2); + tensor_add_inplace(t1, t2); + tensor_add_inplace(t1, t2); + + tensor_div_scalar(t3, 4.0); + + tensor_assert_eq(t1, t3); + + tensor_destroy(t1); + tensor_destroy(t2); + tensor_destroy(t3); +} + +void tensor_test_add_inplace(void) +{ + /* 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) +{ + /* 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_mul_inplace(void) +{ + /* 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_init_rand(t1, s, 3, (dtype) 30); + tensor_init_zero(t2, s, 3); + + tensor_mul_inplace(t1, t2); + tensor_assert_eq(t1, t2); + + tensor_destroy(t1); + tensor_destroy(t2); +} + +void tensor_test_div_inplace(void) +{ + /* Depends on tensor_is_equal, tensor_init_one, tensor_init_zero, tensor_add_inplace */ + 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_one(t3, s, 3); + + tensor_add_inplace(t1, t3); + tensor_add_inplace(t1, t3); + tensor_add_inplace(t2, t3); + tensor_add_inplace(t2, t3); + + tensor_div_inplace(t1, t2); + tensor_assert_eq(t1, t3); + + tensor_destroy(t1); + tensor_destroy(t2); + tensor_destroy(t3); +} + +void tensor_test_add(void) +{ + /* 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) +{ + /* 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); + tensor_destroy(t4); +} + +void tensor_test_mul(void) +{ + /* Depends on tensor_is_equal, tensor_init_rand, tensor_init_zero */ + uint32_t s[3] = {3, 2, 4}; + tensor t1 = tensor_new(); + tensor t2 = tensor_new(); + tensor t3 = tensor_new(); + + tensor_init_rand(t1, s, 3, (dtype) 100); + tensor_init_zero(t2, s, 3); + tensor_init_zero(t3, s, 3); + + tensor t4 = tensor_mul(t1, t2); + tensor_assert_eq(t4, t3); + + tensor_destroy(t1); + tensor_destroy(t2); + tensor_destroy(t3); + tensor_destroy(t4); +} + +void tensor_test_div(void) +{ + /* Depends on tensor_is_equal, tensor_init_one, tensor_init_zero, tensor_add_inplace */ + 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_one(t3, s, 3); + + tensor_add_inplace(t1, t3); + tensor_add_inplace(t1, t3); + tensor_add_inplace(t2, t3); + tensor_add_inplace(t2, t3); + + tensor t4 = tensor_div(t1, t2); + tensor_assert_eq(t4, t3); + + tensor_destroy(t1); + tensor_destroy(t2); + tensor_destroy(t3); + tensor_destroy(t4); +} diff --git a/tests/tensorfunc_test.h b/tests/tensorfunc_test.h new file mode 100644 index 0000000..20c0451 --- /dev/null +++ b/tests/tensorfunc_test.h @@ -0,0 +1,28 @@ +#ifndef _TESNORITER_TEST_H_INCLUDED_ +#define _TESNORITER_TEST_H_INCLUDED_ + +#include "../tensor.h" +#include "../tensorfunc.h" +#include "tensor_assert.h" + + +void tensor_test_fill(void); +void tensor_test_inspect(void); +void tensor_test_map(void); +void tensor_test_map_inplace(void); +void tensor_test_combine(void); +void tensor_test_combine_inplace(void); +void tensor_test_add_scalar(void); +void tensor_test_sub_scalar(void); +void tensor_test_mul_scalar(void); +void tensor_test_div_scalar(void); +void tensor_test_add_inplace(void); +void tensor_test_sub_inplace(void); +void tensor_test_mul_inplace(void); +void tensor_test_div_inplace(void); +void tensor_test_add(void); +void tensor_test_sub(void); +void tensor_test_mul(void); +void tensor_test_div(void); + +#endif // _TESNORITER_TEST_H_INCLUDED_ diff --git a/tests/tensoriter_test.c b/tests/tensoriter_test.c deleted file mode 100644 index 1d58d5c..0000000 --- a/tests/tensoriter_test.c +++ /dev/null @@ -1,209 +0,0 @@ -#include "tensoriter_test.h" - -void tensoriter_test_scalar_next(void) -{ - /* Depends on tensor_init_one */ - uint32_t s[4] = {2, 5, 3, 7}; - tensor t = tensor_new(); - - tensor_init_one(t, s, 4); - tensoriter_scalar iter = tensoriter_scalar_create(t); - - uint32_t i = 0; - do { - i++; - } while (tensoriter_scalar_next(iter)); - - tensor_assert(i == t->num_elem, "(not the rigth number of values)"); - - tensor_destroy(t); -} - -void tensoriter_test_scalar_get(void) -{ - /* Depends on tensor_init_one, tensor_set, tensoriter_scalar_next */ - uint32_t s[3] = {2, 4, 4}; - uint32_t index[3] = {0, 0, 0}; - tensor t = tensor_new(); - - tensor_init_one(t, s, 3); - float i = 1; - 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_set(t, index, i++); - } - } - } - - uint32_t contained = 0; - tensoriter_scalar iter = tensoriter_scalar_create(t); - do { - bool success; - uint32_t value = (uint32_t) tensoriter_scalar_get(iter, &success); - tensor_assert(success, "mute"); - tensor_assert(((1 << (value - 1)) & contained) == 0, "mute"); - contained |= 1 << (value - 1); - } while (tensoriter_scalar_next(iter)); - tensor_assert((contained & 0) == 0, "(missed a value)"); - - tensor_destroy(t); -} - -void tensoriter_test_scalar_set(void) -{ - /* Depends on tensor_init_one, tensor_init_rand, tensoriter_scalar_next */ - uint32_t s[3] = {2, 4, 4}; - - tensor t1 = tensor_new(); - tensor t2 = tensor_new(); - - tensor_init_one(t1, s, 3); - tensor_init_rand(t2, s, 3, (dtype) 30); - - tensoriter_scalar iter = tensoriter_scalar_create(t2); - do { - tensoriter_scalar_set(iter, DTYPE_ONE); - } while (tensoriter_scalar_next(iter)); - - tensor_assert_eq(t1, t2); -} - -dtype tensoriter_test_scalar_map_helper(dtype d) { - static uint32_t contained = 0; - if(((1 << ((uint32_t) d - 1)) & contained) == 0) { - contained |= 1 << ((uint32_t) d - 1); - return DTYPE_ZERO; - } else { - return d; - } -} - -void tensoriter_test_scalar_map(void) -{ - /* Depends on tensor_init_one, tensor_init_zero, tensor_set*/ - uint32_t s[3] = {2, 4, 4}; - uint32_t index[3] = {0, 0, 0}; - tensor t = tensor_new(); - tensor t0 = tensor_new(); - - tensor_init_zero(t0, s, 3); - - tensor_init_one(t, s, 3); - float i = 1; - 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_set(t, index, i++); - } - } - } - - tensoriter_scalar iter = tensoriter_scalar_create(t); - tensoriter_scalar_map(iter, &tensoriter_test_scalar_map_helper); - - tensor_assert_eq(t0, t); - - tensor_destroy(t); -} - -void tensoriter_test_scalar_map_add(void) -{ - /* Depends on tensor_init_one, tensor_add_inplace */ - uint32_t s[4] = {2, 5, 3, 7}; - - tensor t1 = tensor_new(); - tensor t2 = tensor_new(); - tensor t3 = tensor_new(); - - tensor_init_one(t1, s, 4); - tensor_init_one(t2, s, 4); - tensor_init_one(t3, s, 4); - - tensor_add_inplace(t1, t2); - tensor_add_inplace(t1, t2); - tensor_add_inplace(t1, t2); - - tensoriter_scalar iter = tensoriter_scalar_create(t3); - tensoriter_scalar_map_add(iter, 3.0); - - tensor_assert_eq(t1, t3); - - tensor_destroy(t1); - tensor_destroy(t2); - tensor_destroy(t3); -} - -void tensoriter_test_scalar_map_sub(void) -{ - /* Depends on tensor_init_one, tensor_add_inplace */ - uint32_t s[4] = {2, 5, 3, 7}; - - tensor t1 = tensor_new(); - tensor t2 = tensor_new(); - tensor t3 = tensor_new(); - - tensor_init_one(t1, s, 4); - tensor_init_one(t2, s, 4); - tensor_init_one(t3, s, 4); - - tensor_add_inplace(t1, t2); - tensor_add_inplace(t1, t2); - tensor_add_inplace(t1, t2); - - tensoriter_scalar iter = tensoriter_scalar_create(t1); - tensoriter_scalar_map_sub(iter, 3.0); - - tensor_assert_eq(t1, t3); - - tensor_destroy(t1); - tensor_destroy(t2); - tensor_destroy(t3); -} - -void tensoriter_test_scalar_map_mul(void) -{ - /* Depends on tensor_init_one, tensor_init_zero */ - uint32_t s[4] = {2, 5, 3, 7}; - - tensor t1 = tensor_new(); - tensor t2 = tensor_new(); - - tensor_init_one(t1, s, 4); - tensor_init_zero(t2, s, 4); - - tensoriter_scalar iter = tensoriter_scalar_create(t1); - tensoriter_scalar_map_mul(iter, 0.0); - - tensor_assert_eq(t1, t2); - - tensor_destroy(t1); - tensor_destroy(t2); -} - -void tensoriter_test_scalar_map_div(void) -{ - /* Depends on tensor_init_one, tensor_add_inplace */ - uint32_t s[4] = {2, 5, 3, 7}; - - tensor t1 = tensor_new(); - tensor t2 = tensor_new(); - tensor t3 = tensor_new(); - - tensor_init_one(t1, s, 4); - tensor_init_one(t2, s, 4); - tensor_init_one(t3, s, 4); - - tensor_add_inplace(t1, t2); - tensor_add_inplace(t1, t2); - tensor_add_inplace(t1, t2); - - tensoriter_scalar iter = tensoriter_scalar_create(t1); - tensoriter_scalar_map_div(iter, 4.0); - - tensor_assert_eq(t1, t3); - - tensor_destroy(t1); - tensor_destroy(t2); - tensor_destroy(t3); -} diff --git a/tests/tensoriter_test.h b/tests/tensoriter_test.h deleted file mode 100644 index de0065c..0000000 --- a/tests/tensoriter_test.h +++ /dev/null @@ -1,17 +0,0 @@ -#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_set(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_