Skip to content
Snippets Groups Projects
Commit 6ad5d34d authored by thib's avatar thib
Browse files

assert

parent 56435699
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,9 @@ matrix: matrix.o main.o matrix_compute.o
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
......@@ -15,3 +18,6 @@ clean:
rm -f *.o matrix
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
......@@ -7,6 +7,5 @@
int main() {
compute();
return EXIT_SUCCESS;
}
......@@ -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
......
......@@ -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
......@@ -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);
......
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment