added test unit

This commit is contained in:
Raffaele
2022-01-13 23:09:36 +01:00
parent 2fd45dab7b
commit d0ad8e5fb1
4 changed files with 117 additions and 12 deletions
+2 -1
View File
@@ -1,2 +1,3 @@
.swp
test
test
.vscode
+25 -9
View File
@@ -1,9 +1,8 @@
#include <stddef.h> // NULL
#include <assert.h> // assert
#include <stdlib.h>
#include "lina.h"
// Evaluate the dot product C = A*B where C is of size m by l, A is m by n and B is n by l.
// NOTE: C can't be the same pointer of A or B.
void lina_dot(double *A, double *B, double *C, int m, int n, int l){
assert(m > 0 && n > 0 && l > 0);
@@ -32,8 +31,6 @@ void lina_dot(double *A, double *B, double *C, int m, int n, int l){
}
// Evaluate C = A + B where A,B,C are m by n.
// NOTE: C can be the same location of A or B.
void lina_add(double *A, double *B, double *C, int m, int n){
assert(m > 0 && n > 0);
@@ -46,8 +43,6 @@ void lina_add(double *A, double *B, double *C, int m, int n){
C[i * n + j] = A[i * n + j] + B[i * n + j];
}
// Evaluate B = k*A where A and B are m by n matrices.
// NOTE: B can be the same location of A.
void lina_scale(double *A, double *B, double k, int m, int n){
assert(m > 0 && n > 0);
@@ -60,13 +55,34 @@ void lina_scale(double *A, double *B, double k, int m, int n){
B[i * n + j] = k * A[i * n + j];
}
// Evaluate B = A^t (the transpose of A) where A is m by n.
// NOTE: B can be the same location of A.
void lina_transpose(double *A, double *B, int m, int n){
assert(m > 0 && n > 0);
assert(A != NULL && B != NULL);
double *support = malloc(sizeof(*support) * m * n);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
support[i*m + j] = A[j*n + i];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
B[i*m + j] = support[i*m + j];
}
}
free(support);
support = NULL;
/*
| A B C |
| D E F |
@@ -79,5 +95,5 @@ void lina_transpose(double *A, double *B, int m, int n){
| A B C D E F - - - |
| A D - B E - C F - |
*/
#warning "TODO"
#warning "Try to think better algorithm"
}
BIN
View File
Binary file not shown.
+90 -2
View File
@@ -1,8 +1,96 @@
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include "lina.h"
#define NUM_TESTS 100 //Number of tests case to create
#define MATRIX_MAX_ROWS 4 //Max rows that a matrix can have
#define MATRIX_MAX_COLLUMNS 4 //Max collumns a matrix can have
#define MAX_VALUE 15 //Max value a matrix can have
//Matrix type. A is m by n
typedef struct matrix_t
{
double *A;
int m;
int n;
}matrix_t;
//Dinamically create NUM_TESTS tests case randomly generating matrices with random values (whom max is MAX_VALUE)
//and random collumns and row numbers (whom max is MATRIX_MAX_ROWS and MATRIX_MAX_COLLUMNS)
void create_tests(matrix_t **tests);
//Free the heap memory from the test structure
void delete_tests(matrix_t **tests);
//Print the matrix A with size m by n
void pmatrix(double *A, int m, int n);
int main()
{
fprintf(stderr, "There's nothing here yet..\n");
matrix_t *tests[NUM_TESTS];
create_tests(tests);
pmatrix(tests[0]->A,tests[0]->m,tests[0]->n);
return 0;
}
}
void pmatrix(double *A,int m,int n){
for(int i = 0; i<m; i++){
for(int j = 0; j< n; j++)
printf("%f\t",A[i*n + j]);
printf("\n");
}
}
void create_tests(matrix_t **tests){
srand(1);
for(int i=0;i<NUM_TESTS;i++){
int rows = rand() % MATRIX_MAX_ROWS;
int cols = rand() % MATRIX_MAX_COLLUMNS;
double *ptr = malloc(sizeof(*ptr)*rows*cols);
for(int j=0;j<rows*cols;j++){
ptr[j] = rand() % MAX_VALUE;
}
matrix_t *m_point = malloc(sizeof(matrix_t));
m_point->A = ptr;
m_point->m = rows;
m_point->n = cols;
tests[i] = m_point;
}
}
void delete_tests(matrix_t **tests){
for(int i=0;i<NUM_TESTS;i++){
free(tests[i]->A);
free(tests[i]);
}
tests = NULL;
}