Skip to content
Snippets Groups Projects
Commit 22a2d56b authored by dario.genga's avatar dario.genga
Browse files

Add various tests

parent 3feecc2b
No related branches found
No related tags found
No related merge requests found
......@@ -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++) {
......
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment