Added tests for element wise mul and div functions. Fixed memory leak in tests.

This commit is contained in:
2023-09-07 14:48:12 +02:00
parent 7fa38be6a3
commit f21b8562cc
5 changed files with 106 additions and 8 deletions

View File

@@ -23,8 +23,12 @@ void tensor_test_run_all(void)
&tensor_test_cpy,
&tensor_test_add_inplace,
&tensor_test_sub_inplace,
&tensor_test_mul_inplace,
&tensor_test_div_inplace,
&tensor_test_add,
&tensor_test_sub,
&tensor_test_mul,
&tensor_test_div,
};
printf("\n### Running tests for tensor.c ... ###\n\n");

View File

@@ -5,7 +5,7 @@
#include "tensor_test.h"
#include "tensoriter_test.h"
#define NUM_TENSOR_TEST_FUNC 14
#define NUM_TENSOR_TEST_FUNC 18
#define NUM_TENSORITER_TEST_FUNC 7

View File

@@ -26,7 +26,7 @@ void tensor_test_is_equal(void)
tensor_init_one(t2, s, 4);
tensor_assert_ne(t1, t2);
tensor_init_rand(t1, s, 4, 30);
tensor_init_rand(t1, s, 4, (dtype) 30);
tensor_cpy(t2, t1);
tensor_assert_eq(t1, t2);
@@ -63,7 +63,7 @@ void tensor_test_set(void)
tensor t1 = tensor_new();
tensor t2 = tensor_new();
tensor_init_rand(t1, s, 4, 30);
tensor_init_rand(t1, s, 4, (dtype) 30);
tensor_init_zero(t2, s, 4);
for (index[0] = 0; index[0] < s[0]; ++index[0]) {
@@ -90,7 +90,7 @@ void tensor_test_get(void)
tensor t1 = tensor_new();
tensor t2 = tensor_new();
tensor_init_rand(t1, s, 4, 30);
tensor_init_rand(t1, s, 4, (dtype) 30);
tensor_init_zero(t2, s, 4);
for (index[0] = 0; index[0] < s[0]; ++index[0]) {
@@ -117,7 +117,7 @@ void tensor_test_init_one(void)
tensor t1 = tensor_new();
tensor t2 = tensor_new();
tensor_init_rand(t1, s, 3, 30);
tensor_init_rand(t1, s, 3, (dtype) 30);
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]) {
@@ -141,7 +141,7 @@ void tensor_test_init_zero(void)
tensor t1 = tensor_new();
tensor t2 = tensor_new();
tensor_init_rand(t1, s, 3, 30);
tensor_init_rand(t1, s, 3, (dtype) 30);
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]) {
@@ -164,7 +164,7 @@ void tensor_test_init_rand(void)
uint32_t index[3] = {0, 0, 0};
tensor t1 = tensor_new();
tensor_assert(tensor_init_rand(t1, s, 3, 30), "(there should be no error when initing)");
tensor_assert(tensor_init_rand(t1, s, 3, (dtype) 30), "(there should be no error when initing)");
for (index[0] = 0; index[0] < s[0]; ++index[0]) {
for (index[1] = 0; index[1] < s[1]; ++index[1]) {
@@ -245,6 +245,48 @@ void tensor_test_sub_inplace(void)
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 */
@@ -295,4 +337,52 @@ void tensor_test_sub(void)
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);
}

View File

@@ -18,7 +18,11 @@ 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_