2023-03-15 16:13:37 +01:00
|
|
|
#include "test_tensor.h"
|
2023-05-07 14:38:41 +02:00
|
|
|
#include <stdint.h>
|
2023-03-15 16:13:37 +01:00
|
|
|
|
2023-09-03 13:02:55 +02:00
|
|
|
void test_tensor_run_all(void)
|
2023-03-15 16:13:37 +01:00
|
|
|
{
|
|
|
|
|
int i;
|
2023-05-07 13:32:20 +02:00
|
|
|
void (*test_func[NUM_TEST_FUNC])(void) = {
|
2023-09-03 13:02:55 +02:00
|
|
|
&test_tensor_is_empty,
|
2023-03-15 16:13:37 +01:00
|
|
|
&test_tensor_is_equal,
|
2023-09-03 13:02:55 +02:00
|
|
|
&test_tensor_check_size,
|
|
|
|
|
&test_tensor_set_size,
|
2023-03-15 16:13:37 +01:00
|
|
|
&test_tensor_set,
|
|
|
|
|
&test_tensor_get,
|
2023-09-03 13:02:55 +02:00
|
|
|
&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,
|
2023-03-15 16:13:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-03 13:02:55 +02:00
|
|
|
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)");
|
2023-09-03 13:40:19 +02:00
|
|
|
|
|
|
|
|
tensor_destroy(t1);
|
|
|
|
|
tensor_destroy(t2);
|
2023-09-03 13:02:55 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-15 16:13:37 +01:00
|
|
|
void test_tensor_is_equal(void)
|
|
|
|
|
{
|
|
|
|
|
/* Depends on tensor_cpy, tensor_is_equal, tensor_init_zero, tensor_init_rand */
|
2023-05-07 14:38:41 +02:00
|
|
|
uint32_t s[4] = {2, 5, 3, 7};
|
2023-03-15 16:13:37 +01:00
|
|
|
tensor t1 = tensor_new();
|
|
|
|
|
tensor t2 = tensor_new();
|
2023-09-03 09:45:20 +02:00
|
|
|
tensor_init_zero(t1, s, 4);
|
|
|
|
|
tensor_init_one(t2, s, 4);
|
2023-03-15 16:13:37 +01:00
|
|
|
tensor_assert_ne(t1, t2);
|
|
|
|
|
|
2023-09-03 09:45:20 +02:00
|
|
|
tensor_init_rand(t1, s, 4, 30);
|
2023-03-15 16:13:37 +01:00
|
|
|
tensor_cpy(t2, t1);
|
|
|
|
|
|
|
|
|
|
tensor_assert_eq(t1, t2);
|
2023-09-03 13:40:19 +02:00
|
|
|
|
2023-03-15 16:13:37 +01:00
|
|
|
tensor_destroy(t1);
|
|
|
|
|
tensor_destroy(t2);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-03 13:02:55 +02:00
|
|
|
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)");
|
2023-09-03 13:40:19 +02:00
|
|
|
|
|
|
|
|
tensor_destroy(t);
|
2023-09-03 13:02:55 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-15 16:13:37 +01:00
|
|
|
void test_tensor_set(void)
|
|
|
|
|
{
|
|
|
|
|
/* Depends on tensor_is_equal, tensor_init_zero, tensor_init_rand */
|
2023-05-07 14:38:41 +02:00
|
|
|
uint32_t s[4] = {2, 5, 3, 7};
|
|
|
|
|
uint32_t index[4] = {0, 0, 0, 0};
|
2023-03-15 16:13:37 +01:00
|
|
|
tensor t1 = tensor_new();
|
|
|
|
|
tensor t2 = tensor_new();
|
|
|
|
|
|
2023-09-03 09:45:20 +02:00
|
|
|
tensor_init_rand(t1, s, 4, 30);
|
|
|
|
|
tensor_init_zero(t2, s, 4);
|
2023-03-15 16:13:37 +01:00
|
|
|
|
|
|
|
|
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);
|
2023-09-03 13:40:19 +02:00
|
|
|
|
2023-03-15 16:13:37 +01:00
|
|
|
tensor_destroy(t1);
|
|
|
|
|
tensor_destroy(t2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test_tensor_get(void)
|
|
|
|
|
{
|
|
|
|
|
/* Depends on tensor_is_equal, tensor_set, tensor_init_zero, tensor_init_rand */
|
2023-09-03 13:02:55 +02:00
|
|
|
bool status;
|
2023-05-07 14:38:41 +02:00
|
|
|
uint32_t s[4] = {2, 5, 3, 7};
|
|
|
|
|
uint32_t index[4] = {0, 0, 0, 0};
|
2023-03-15 16:13:37 +01:00
|
|
|
tensor t1 = tensor_new();
|
|
|
|
|
tensor t2 = tensor_new();
|
|
|
|
|
|
2023-09-03 09:45:20 +02:00
|
|
|
tensor_init_rand(t1, s, 4, 30);
|
|
|
|
|
tensor_init_zero(t2, s, 4);
|
2023-03-15 16:13:37 +01:00
|
|
|
|
|
|
|
|
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);
|
2023-09-03 13:40:19 +02:00
|
|
|
|
2023-03-15 16:13:37 +01:00
|
|
|
tensor_destroy(t1);
|
|
|
|
|
tensor_destroy(t2);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-03 13:02:55 +02:00
|
|
|
void test_tensor_init_one(void)
|
|
|
|
|
{
|
2023-09-03 13:40:19 +02:00
|
|
|
/* Depends on tensor_is_equal, tensor_set, tensor_init_rand */
|
|
|
|
|
uint32_t s[3] = {2, 5, 7};
|
|
|
|
|
uint32_t index[3] = {0, 0, 0};
|
|
|
|
|
tensor t1 = tensor_new();
|
|
|
|
|
tensor t2 = tensor_new();
|
|
|
|
|
|
|
|
|
|
tensor_init_rand(t1, s, 3, 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]) {
|
|
|
|
|
tensor_set(t1, index, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tensor_assert(tensor_init_one(t2, s, 3), "(there should be no error when initing)");
|
|
|
|
|
tensor_assert_eq(t1, t2);
|
|
|
|
|
|
|
|
|
|
tensor_destroy(t1);
|
|
|
|
|
tensor_destroy(t2);
|
2023-09-03 13:02:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test_tensor_init_zero(void)
|
|
|
|
|
{
|
2023-09-03 13:40:19 +02:00
|
|
|
/* Depends on tensor_is_equal, tensor_set, tensor_init_rand */
|
|
|
|
|
uint32_t s[3] = {2, 5, 7};
|
|
|
|
|
uint32_t index[3] = {0, 0, 0};
|
|
|
|
|
tensor t1 = tensor_new();
|
|
|
|
|
tensor t2 = tensor_new();
|
|
|
|
|
|
|
|
|
|
tensor_init_rand(t1, s, 3, 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]) {
|
|
|
|
|
tensor_set(t1, index, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tensor_assert(tensor_init_zero(t2, s, 3), "(there should be no error when initing)");
|
|
|
|
|
tensor_assert_eq(t1, t2);
|
|
|
|
|
|
|
|
|
|
tensor_destroy(t1);
|
|
|
|
|
tensor_destroy(t2);
|
2023-09-03 13:02:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test_tensor_init_rand(void)
|
|
|
|
|
{
|
2023-09-03 13:40:19 +02:00
|
|
|
/* Depends on tensor_is_equal, tensor_set, tensor_init_rand */
|
|
|
|
|
uint32_t s[3] = {2, 5, 7};
|
|
|
|
|
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)");
|
|
|
|
|
|
|
|
|
|
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, 0), "mute");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tensor_destroy(t1);
|
2023-09-03 13:02:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test_tensor_cpy(void)
|
|
|
|
|
{
|
2023-09-03 13:40:19 +02:00
|
|
|
/* Depends on tensor_is_equal, tensor_init_one, tensor_init_zero */
|
|
|
|
|
uint32_t s[3] = {2, 5, 7};
|
|
|
|
|
tensor t1 = tensor_new();
|
|
|
|
|
tensor t2 = tensor_new();
|
|
|
|
|
|
|
|
|
|
tensor_init_one(t1, s, 3);
|
|
|
|
|
tensor_init_zero(t2, s, 3);
|
|
|
|
|
|
|
|
|
|
tensor_assert(tensor_cpy(t1, t2), "(there should be no error when copying)");
|
|
|
|
|
tensor_assert_eq(t1, t2);
|
|
|
|
|
|
|
|
|
|
tensor_destroy(t1);
|
|
|
|
|
tensor_destroy(t2);
|
2023-09-03 13:02:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|