diff options
| author | Gertjan van den Burg <burg@ese.eur.nl> | 2016-10-17 12:07:41 +0200 |
|---|---|---|
| committer | Gertjan van den Burg <burg@ese.eur.nl> | 2016-10-17 12:08:26 +0200 |
| commit | 7004385c7204586ec3bbfdd4f4f46ee858a13892 (patch) | |
| tree | 615fe43716f2a5183528bf2771521ac53a0d3b24 | |
| parent | remove temporary test seed setting (diff) | |
| download | gensvm-7004385c7204586ec3bbfdd4f4f46ee858a13892.tar.gz gensvm-7004385c7204586ec3bbfdd4f4f46ee858a13892.zip | |
Create debug function for printing GenSparse structs
| -rw-r--r-- | include/gensvm_debug.h | 2 | ||||
| -rw-r--r-- | src/gensvm_debug.c | 45 | ||||
| -rw-r--r-- | tests/data/test_debug_dense.txt (renamed from tests/data/test_debug_print.txt) | 0 | ||||
| -rw-r--r-- | tests/data/test_debug_sparse.txt | 5 | ||||
| -rw-r--r-- | tests/src/test_gensvm_debug.c | 60 |
5 files changed, 106 insertions, 6 deletions
diff --git a/include/gensvm_debug.h b/include/gensvm_debug.h index 361e885..e0a7482 100644 --- a/include/gensvm_debug.h +++ b/include/gensvm_debug.h @@ -14,7 +14,9 @@ #define GENSVM_DEBUG_H #include "gensvm_print.h" +#include "gensvm_sparse.h" void gensvm_print_matrix(double *M, long rows, long cols); +void gensvm_print_sparse(struct GenSparse *A); #endif diff --git a/src/gensvm_debug.c b/src/gensvm_debug.c index f4b4151..632bf73 100644 --- a/src/gensvm_debug.c +++ b/src/gensvm_debug.c @@ -12,7 +12,7 @@ #include "gensvm_debug.h" /** - * @brief print a matrix + * @brief Print a dense matrix * * @details * Debug function to print a matrix @@ -35,3 +35,46 @@ void gensvm_print_matrix(double *M, long rows, long cols) } note("\n"); } + +/** + * @brief Print a sparse matrix + * + * @details + * Debug function to print a GenSparse sparse matrix + * + * @param[in] A a GenSparse matrix to print + * + */ +void gensvm_print_sparse(struct GenSparse *A) +{ + long i; + + // print matrix dimensions + note("Sparse Matrix:\n"); + note("\tnnz = %li, rows = %li, cols = %li\n", A->nnz, A->n_row, + A->n_col); + + // print nonzero values + note("\tvalues = [ "); + for (i=0; i<A->nnz; i++) { + if (i != 0) note(", "); + note("%f", A->values[i]); + } + note(" ]\n"); + + // print row indices + note("\tIA = [ "); + for (i=0; i<A->n_row+1; i++) { + if (i != 0) note(", "); + note("%i", A->ia[i]); + } + note(" ]\n"); + + // print column indices + note("\tJA = [ "); + for (i=0; i<A->nnz; i++) { + if (i != 0) note(", "); + note("%i", A->ja[i]); + } + note(" ]\n"); +} diff --git a/tests/data/test_debug_print.txt b/tests/data/test_debug_dense.txt index 9ef2b13..9ef2b13 100644 --- a/tests/data/test_debug_print.txt +++ b/tests/data/test_debug_dense.txt diff --git a/tests/data/test_debug_sparse.txt b/tests/data/test_debug_sparse.txt new file mode 100644 index 0000000..3bdbb08 --- /dev/null +++ b/tests/data/test_debug_sparse.txt @@ -0,0 +1,5 @@ +Sparse Matrix: + nnz = 4, rows = 4, cols = 4 + values = [ 5.000000, 8.000000, 3.000000, 6.000000 ] + IA = [ 0, 0, 2, 3, 4 ] + JA = [ 0, 1, 2, 1 ] diff --git a/tests/src/test_gensvm_debug.c b/tests/src/test_gensvm_debug.c index c4f29d3..5a20dcd 100644 --- a/tests/src/test_gensvm_debug.c +++ b/tests/src/test_gensvm_debug.c @@ -13,7 +13,8 @@ extern FILE *GENSVM_OUTPUT_FILE; char *test_print_matrix() { FILE *fid = NULL; - GENSVM_OUTPUT_FILE = fopen("./data/test_debug_print.txt", "w"); + const char *filename = "./data/test_debug_dense.txt"; + GENSVM_OUTPUT_FILE = fopen(filename, "w"); double *mat = Calloc(double, 3*2); matrix_set(mat, 2, 0, 0, -0.241053050258449); @@ -28,19 +29,19 @@ char *test_print_matrix() fclose(GENSVM_OUTPUT_FILE); char buffer[MAX_LINE_LENGTH]; - fid = fopen("./data/test_debug_print.txt", "r"); + fid = fopen(filename, "r"); fgets(buffer, MAX_LINE_LENGTH, fid); mu_assert(strcmp(buffer, "-0.241053 -0.599809\n") == 0, - "Line doesn't contain expected content (0).\n"); + "Line doesn't contain expected content (0)."); fgets(buffer, MAX_LINE_LENGTH, fid); mu_assert(strcmp(buffer, "+0.893318 -0.344058\n") == 0, - "Line doesn't contain expected content (1).\n"); + "Line doesn't contain expected content (1)."); fgets(buffer, MAX_LINE_LENGTH, fid); mu_assert(strcmp(buffer, "+0.933948 -0.474352\n") == 0, - "Line doesn't contain expected content (2).\n"); + "Line doesn't contain expected content (2)."); fclose(fid); // end test code // @@ -49,10 +50,59 @@ char *test_print_matrix() return NULL; } +char *test_print_sparse() +{ + FILE *fid = NULL; + double *A = Calloc(double, 4*4); + A[4] = 5; + A[5] = 8; + A[10] = 3; + A[13] = 6; + struct GenSparse *sp = gensvm_dense_to_sparse(A, 4, 4); + const char *filename = "./data/test_debug_sparse.txt"; + GENSVM_OUTPUT_FILE = fopen(filename, "w"); + + // start test code // + gensvm_print_sparse(sp); + fclose(GENSVM_OUTPUT_FILE); + + char buffer[MAX_LINE_LENGTH]; + fid = fopen(filename, "r"); + + fgets(buffer, MAX_LINE_LENGTH, fid); + mu_assert(strcmp(buffer, "Sparse Matrix:\n") == 0, + "Line doesn't contain expected content (0)."); + + fgets(buffer, MAX_LINE_LENGTH, fid); + mu_assert(strcmp(buffer, "\tnnz = 4, rows = 4, cols = 4\n") == 0, + "Line doesn't contain expected content (1)."); + + fgets(buffer, MAX_LINE_LENGTH, fid); + mu_assert(strcmp(buffer, "\tvalues = [ 5.000000, 8.000000, " + "3.000000, 6.000000 ]\n") == 0, + "Line doesn't contain expected content (2)."); + + fgets(buffer, MAX_LINE_LENGTH, fid); + mu_assert(strcmp(buffer, "\tIA = [ 0, 0, 2, 3, 4 ]\n") == 0, + "Line doesn't contain expected content (3)."); + + fgets(buffer, MAX_LINE_LENGTH, fid); + mu_assert(strcmp(buffer, "\tJA = [ 0, 1, 2, 1 ]\n") == 0, + "Line doesn't contain expected content (4)."); + + fclose(fid); + // end test code // + gensvm_free_sparse(sp); + free(A); + + return NULL; +} + char *all_tests() { mu_suite_start(); mu_run_test(test_print_matrix); + mu_run_test(test_print_sparse); return NULL; } |
