From 6ad5d34d2b92256654f568d9a60c21d0d06c8def Mon Sep 17 00:00:00 2001 From: thib <tempo2riz@gmail.com> Date: Wed, 4 Nov 2020 15:34:51 +0100 Subject: [PATCH] assert --- TP6-matrix/Makefile | 12 ++++++-- TP6-matrix/main.c | 1 - TP6-matrix/matrix.c | 23 +++++++++++++-- TP6-matrix/matrix.h | 3 ++ TP6-matrix/matrix_compute.c | 2 ++ TP6-matrix/matrix_test.c | 56 +++++++++++++++++++++++++++++++++++++ 6 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 TP6-matrix/matrix_test.c diff --git a/TP6-matrix/Makefile b/TP6-matrix/Makefile index 7bba2e1..44e953b 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 0cdf2b9..63c707d 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 c7bd626..c981e56 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 ed953a0..944f17f 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 54e1aeb..123f8ef 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 0000000..b27beb6 --- /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 -- GitLab