diff --git a/TP6-matrix/Makefile b/TP6-matrix/Makefile index 7bba2e11707c4fc7155f58b5a56f63117e457be9..44e953bfdb287371b93bea2ad72aa6ca570f9284 100644 --- a/TP6-matrix/Makefile +++ b/TP6-matrix/Makefile @@ -1,10 +1,13 @@ -matrix: matrix.o main.o matrix_compute.o - gcc matrix.o main.o -o matrix.x -lm -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak +matrix: matrix.o main.o matrix_compute.o + gcc matrix.o main.o -o matrix.x -lm -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak matrix_compute.o: matrix_compute.c gcc -Wall -Wextra -c matrix_compute.c -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak +matrix_test.o: matrix_test.c + gcc -Wall -Wextra -c matrix_test.c -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak + matrix.o: matrix.c matrix.h gcc -Wall -Wextra -c matrix.c -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak @@ -14,4 +17,7 @@ main.o: main.c clean: rm -f *.o matrix -rebuild: clean matrix \ No newline at end of file +rebuild: clean matrix + +test: matrix.o matrix_test.o + gcc matrix.o matrix_test.o -o matrix_test.x -lm -g -Wall -Wextra -pedantic -fsanitize=address -fsanitize=leak \ No newline at end of file diff --git a/TP6-matrix/main.c b/TP6-matrix/main.c index 0cdf2b9712666d38e7099d0efa6a9205f84d7929..63c707dffc905560d1c62f8831389bad61cb4282 100644 --- a/TP6-matrix/main.c +++ b/TP6-matrix/main.c @@ -7,6 +7,5 @@ int main() { compute(); - return EXIT_SUCCESS; } diff --git a/TP6-matrix/matrix.c b/TP6-matrix/matrix.c index c7bd626c096c182a4e0d897c288f5c7892abf13b..c981e5643cf6299846559dbecf660d7965f5bd1a 100644 --- a/TP6-matrix/matrix.c +++ b/TP6-matrix/matrix.c @@ -17,6 +17,7 @@ error_code matrix_alloc(matrix *mat, int m, int n) { } mat->data = malloc(m * sizeof(int *)); + if(mat->data == NULL){ return memory_error; } @@ -36,7 +37,7 @@ error_code matrix_alloc(matrix *mat, int m, int n) { error_code matrix_init(matrix *mat, int m, int n, int val) { -if (m < 0 || n < 0) { +if (m < 1 || n < 1) { return out_of_bounds; } @@ -53,7 +54,7 @@ if (m < 0 || n < 0) { error_code matrix_init_from_array(matrix *mat, int m, int n, int data[]) { -if (m < 0 || n < 0) { +if (m < 1 || n < 1) { return out_of_bounds; } @@ -152,6 +153,8 @@ if (m1-m0 < 0 || n1-n0 < 0) { return ok; } + + bool matrix_is_equal(matrix mat1, matrix mat2) { if (mat1.m != mat2.m || mat1.n != mat2.n) { @@ -189,6 +192,22 @@ void mult2(int *elem){ *elem*=2; } +void multX(int *elem,int x){ + *elem*=x; +} + +error_code matrix_map_ip2(matrix mat,int x, void (*foo)(int *,int)) { + + for (int i = 0; i < mat.m; i++) { // ligne + for (int j = 0; j < mat.n; j++) { // col + + foo(&mat.data[i][j],x); + } + } + return ok; +} + + error_code matrix_map_ip(matrix mat, void (*foo)(int *)) { for (int i = 0; i < mat.m; i++) { // ligne diff --git a/TP6-matrix/matrix.h b/TP6-matrix/matrix.h index ed953a02a552cb2a996322e2864e465e2e49ca57..944f17f8dc9d2006d6e7eae269cb89fd9fc3482a 100644 --- a/TP6-matrix/matrix.h +++ b/TP6-matrix/matrix.h @@ -40,9 +40,12 @@ error_code matrix_get(int *elem, const matrix mat, int ix, int iy); error_code matrix_set(matrix mat, int ix, int iy, int elem); void mult2(int *elem); +void multX(int *elem,int x); error_code matrix_map_ip(matrix mat, void (*foo)(int *)); +error_code matrix_map_ip2(matrix mat,int x, void (*foo)(int *,int)); + error_code matrix_map(matrix *mapped, const matrix mat, void (*foo)(int *)); #endif diff --git a/TP6-matrix/matrix_compute.c b/TP6-matrix/matrix_compute.c index 54e1aebe452260b1cf3c1239580634ab9f3e65a0..123f8efa7e15386cd95d97aec2fba50b4afd570f 100644 --- a/TP6-matrix/matrix_compute.c +++ b/TP6-matrix/matrix_compute.c @@ -45,6 +45,8 @@ void compute() { matrix_map_ip(mat1, mult2); matrix_print(mat1); + //matrix_map_ip2(mat1,6, multX); + matrix_destroy(&mat1); matrix_destroy(&mat2); matrix_destroy(&mat3); diff --git a/TP6-matrix/matrix_test.c b/TP6-matrix/matrix_test.c new file mode 100644 index 0000000000000000000000000000000000000000..b27beb6bb25b2b471e232153e78289857596d018 --- /dev/null +++ b/TP6-matrix/matrix_test.c @@ -0,0 +1,56 @@ +#include "matrix.h" +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> + +int main(){ + + matrix mat1; + matrix mat2; + matrix mat3; + matrix mat4; + + int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + + //printf("init\n"); + assert(matrix_init(&mat1, 8, 6, 1)==ok); + assert(matrix_print(mat1)==ok); + + //printf("init from array\n"); + assert(matrix_init_from_array(&mat2, 3, 3, arr)==ok); + assert(matrix_print(mat2)==ok); + + //printf("clone\n"); + assert(matrix_clone(&mat3, mat1)==ok); + assert(matrix_print(mat3)==ok); + + //printf("compare cloned matrix :"); + assert(matrix_is_equal(mat1, mat3)); + + //printf("transpose\n"); + assert(matrix_transpose(&mat4, mat3)==ok); + assert(matrix_print(mat4)==ok); + + assert(matrix_destroy(&mat1)==ok); + //printf("submatrix\n"); + assert(matrix_extract_submatrix(&mat1, mat4, 2, mat4.m, 2, mat4.n)==ok); + assert(matrix_print(mat1)==ok); + + //printf("set element [1][2] to 0\n"); + assert(matrix_set(mat1, 1, 2, 0)==ok); + assert(matrix_print(mat1)==ok); + + //printf("double each item\n"); + assert(matrix_map_ip(mat1, mult2)==ok); + assert(matrix_print(mat1)==ok); + + //assert(matrix_map_ip2(mat1,6, multX); + + assert(matrix_destroy(&mat1)==ok); + assert(matrix_destroy(&mat2)==ok); + assert(matrix_destroy(&mat3)==ok); + assert(matrix_destroy(&mat4)==ok); + +return EXIT_SUCCESS; +} \ No newline at end of file