diff --git a/main.c b/main.c index 2ddb2daccc7f078853ad037c429fb8410294a162..bf04085420f06083131721016f75981992ed1eb6 100644 --- a/main.c +++ b/main.c @@ -5,9 +5,6 @@ #include <stdio.h> #include <stdlib.h> -// #include <math.h> -// #include <time.h> -// #include <stdbool.h> #include "matrix.h" int main() { @@ -37,6 +34,24 @@ int main() { matrix_extract_submatrix(&sub, transposed, 1, 4, 1, 3); printf("Sub matrix :\n"); matrix_print(sub); + // Equal + if (matrix_is_equal(mat, cloned)) { + printf("The following matrix are equals :\n"); + } else { + printf("The following matrix are not equals :\n"); + } + printf("Matrix 1\n"); + matrix_print(mat); + printf("Matrix 2\n"); + matrix_print(cloned); + // Get & set + int32_t element; + matrix_get(&element, mat, 0, 0); + printf("First element of the first matrix : %d\n", element); + matrix_set(mat, 0, 0, 42); + printf("The first element of the first matrix has been changed :\n"); + matrix_print(mat); + // Free the memory matrix_destroy(&mat); matrix_destroy(&cloned); diff --git a/matrix.c b/matrix.c index c477170f24fc2ef9544f1996f655d06d31e45405..5d5c313a312166f5d1976bcb962470ae6a19ea1e 100644 --- a/matrix.c +++ b/matrix.c @@ -140,3 +140,39 @@ error_code matrix_extract_submatrix(matrix *sub, const matrix mat, int32_t m0, i return ok; } + + +bool matrix_is_equal(matrix mat1, matrix mat2) { + if (mat1.m != mat2.m || mat1.n != mat2.n) { + return false; + } + + // Parse each values of the matrix + for (int i = 0; i < mat1.m; i++) { + for (int k = 0; k < mat1.n; k++) { + if (mat1.data[i][k] != mat2.data[i][k]) { + return false; + } + } + } + return true; +} + +error_code matrix_get(int32_t *elem, const matrix mat, int32_t ix, int32_t iy) { + if (ix < 0 || iy < 0 || iy >= mat.m || ix >= mat.n) { + return err; + } + *elem = mat.data[iy][ix]; + + return ok; +} + + +error_code matrix_set(matrix mat, int32_t ix, int32_t iy, int32_t elem) { + if (ix < 0 || iy < 0 || ix > mat.m || iy > mat.n) { + return err; + } + mat.data[iy][ix] = elem; + + return ok; +} diff --git a/test.c b/test.c index f65c4e9a63925a804cbc36fb53dd91fc0627817a..9e9918430249b88413d188dbd0a1534a8fee4a7c 100644 --- a/test.c +++ b/test.c @@ -4,7 +4,6 @@ */ #include "matrix.h" #include <assert.h> -#include <math.h> #include <stdbool.h> #include <stdio.h> @@ -24,8 +23,11 @@ int main() { // Starting the tests... printf("Starting the tests...\n"); assert(matrix_alloc(&mat, m, n) == ok); + assert(matrix_destroy(&mat) == ok); assert(matrix_init(&mat, m, n, val) == ok); + assert(matrix_destroy(&mat) == ok); assert(matrix_init(&mat, -1, n, val) == err); + assert(matrix_destroy(&mat) == ok); assert(matrix_init_from_array(&mat, m, n, data, m*n) == ok); assert(matrix_clone(&cloned, mat) == ok); assert(matrix_transpose(&transposed, mat) == ok); @@ -37,8 +39,11 @@ int main() { assert(matrix_set(mat, 0, 0, set_elem) == ok); assert(mat.data[0][0] == set_elem); assert(matrix_destroy(&mat) == ok); + matrix_destroy(&cloned); + matrix_destroy(&transposed); + matrix_destroy(&sub); - printf("The tests are completed and were successful !"); + printf("The tests are completed and were successful !\n"); return EXIT_SUCCESS; } diff --git a/tests b/tests new file mode 100755 index 0000000000000000000000000000000000000000..cdf7679023159a823f7a880a0cb3df0001d843d5 Binary files /dev/null and b/tests differ