Restuctured the project.

This commit is contained in:
2023-03-15 16:13:37 +01:00
parent 744da6316c
commit 1749dc41ac
7 changed files with 141 additions and 0 deletions

BIN
target/build/tensor.o Normal file

Binary file not shown.

BIN
target/build/tests/main.o Normal file

Binary file not shown.

Binary file not shown.

BIN
target/test Executable file

Binary file not shown.

9
tests/main.c Normal file
View File

@@ -0,0 +1,9 @@
#include "../tensor.h"
#include "test_tensor.h"
int main(void)
{
test_run_all();
return 0;
}

89
tests/test_tensor.c Normal file
View File

@@ -0,0 +1,89 @@
#include "test_tensor.h"
void test_run_all(void)
{
int i;
void (*test_func[NUM_TEST_FUNC])() = {
&test_tensor_is_equal,
&test_tensor_set,
&test_tensor_get,
};
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);
}
void test_tensor_is_equal(void)
{
/* Depends on tensor_cpy, tensor_is_equal, tensor_init_zero, tensor_init_rand */
int s[4] = {2, 5, 3, 7};
tensor t1 = tensor_new();
tensor t2 = tensor_new();
tensor_init_zero(t1, 4, s);
tensor_init_one(t2, 4, s);
tensor_assert_ne(t1, t2);
tensor_init_rand(t1, 4, s, 30);
tensor_cpy(t2, t1);
tensor_assert_eq(t1, t2);
tensor_destroy(t1);
tensor_destroy(t2);
}
void test_tensor_set(void)
{
/* Depends on tensor_is_equal, tensor_init_zero, tensor_init_rand */
int s[4] = {2, 5, 3, 7};
int index[4] = {0, 0, 0, 0};
tensor t1 = tensor_new();
tensor t2 = tensor_new();
tensor_init_rand(t1, 4, s, 30);
tensor_init_zero(t2, 4, s);
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);
tensor_destroy(t1);
tensor_destroy(t2);
}
void test_tensor_get(void)
{
/* Depends on tensor_is_equal, tensor_set, tensor_init_zero, tensor_init_rand */
int status;
int s[4] = {2, 5, 3, 7};
int index[4] = {0, 0, 0, 0};
tensor t1 = tensor_new();
tensor t2 = tensor_new();
tensor_init_rand(t1, 4, s, 30);
tensor_init_zero(t2, 4, s);
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);
tensor_destroy(t1);
tensor_destroy(t2);
}

43
tests/test_tensor.h Normal file
View File

@@ -0,0 +1,43 @@
#ifndef _TEST_TENSOR_H_INCLUDED_
#define _TEST_TENSOR_H_INCLUDED_
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "../tensor.h"
#define NUM_TEST_FUNC 3
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#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) { \
fputs(ANSI_COLOR_GREEN "Assertion succeeded: " ANSI_COLOR_RESET, 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_is_equal(void);
void test_tensor_set(void);
void test_tensor_get(void);
#endif