From 22a2d56bc4e76a0c31600910bb43f8a3d8697b6c Mon Sep 17 00:00:00 2001 From: "dario.genga" <dario.genga@etu.hesge.ch> Date: Mon, 22 Nov 2021 19:07:15 +0100 Subject: [PATCH] Add various tests --- matrix.c | 8 ++++++++ test.c | 23 +++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/matrix.c b/matrix.c index 64fe388..a390d2d 100644 --- a/matrix.c +++ b/matrix.c @@ -7,6 +7,10 @@ #include <stdio.h> error_code matrix_alloc(matrix *mat, int32_t m, int32_t n) { + if (m <= 0 || n <= 0) { + return err; + } + int32_t **data = malloc(m * sizeof(int32_t *)); mat->m = m; mat->n = n; @@ -30,6 +34,10 @@ error_code matrix_alloc(matrix *mat, int32_t m, int32_t n) { } error_code matrix_init(matrix *mat, int32_t m, int32_t n, int32_t val) { + if (m <= 0 || n <= 0) { + return err; + } + matrix_alloc(mat, m, n); for (int32_t i = 0; i < m; i++) { diff --git a/test.c b/test.c index e34b3b4..f65c4e9 100644 --- a/test.c +++ b/test.c @@ -11,14 +11,33 @@ int main() { // Initialisation matrix mat; + matrix cloned; + matrix transposed; + matrix sub; int32_t m = 3; int32_t n = 4; int32_t val = 1; + int32_t first_elem = 2; + int32_t set_elem = 100; + int32_t data[12] = { first_elem, 1, -1, -2, 3, 1, 1, 3, 1, 4, -1, -1 }; + int32_t elem; + // Starting the tests... printf("Starting the tests...\n"); - // Test allocation assert(matrix_alloc(&mat, m, n) == ok); - // Test initialisation assert(matrix_init(&mat, m, n, val) == ok); + assert(matrix_init(&mat, -1, n, val) == err); + assert(matrix_init_from_array(&mat, m, n, data, m*n) == ok); + assert(matrix_clone(&cloned, mat) == ok); + assert(matrix_transpose(&transposed, mat) == ok); + assert(matrix_print(mat) == ok); + assert(matrix_extract_submatrix(&sub, mat, 0, 2, 0, 2) == ok); + assert(matrix_is_equal(mat, cloned) == true); + assert(matrix_get(&elem, mat, 0, 0) == ok); + assert(mat.data[0][0] == first_elem); + assert(matrix_set(mat, 0, 0, set_elem) == ok); + assert(mat.data[0][0] == set_elem); + assert(matrix_destroy(&mat) == ok); + printf("The tests are completed and were successful !"); return EXIT_SUCCESS; -- GitLab