diff --git a/matrix.c b/matrix.c index 64fe388d222e8fd2ed571dc5942725ed370900cf..a390d2df57e300208d409d1229e2ace43ff6fba6 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 e34b3b44f498110f10eeb65c12ef8b844e1b41ad..f65c4e9a63925a804cbc36fb53dd91fc0627817a 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;