GenSVM
test_gensvm_debug.c
Go to the documentation of this file.
1 
27 #include "minunit.h"
28 #include "gensvm_debug.h"
29 
30 extern FILE *GENSVM_OUTPUT_FILE;
31 
33 {
34  FILE *fid = NULL;
35  const char *filename = "./data/test_debug_dense.txt";
36  GENSVM_OUTPUT_FILE = fopen(filename, "w");
37 
38  double *mat = Calloc(double, 3*2);
39  matrix_set(mat, 2, 0, 0, -0.241053050258449);
40  matrix_set(mat, 2, 0, 1, -0.599809408260836);
41  matrix_set(mat, 2, 1, 0, 0.893318163305108);
42  matrix_set(mat, 2, 1, 1, -0.344057630469285);
43  matrix_set(mat, 2, 2, 0, 0.933948479216127);
44  matrix_set(mat, 2, 2, 1, -0.474352026604967);
45 
46  // start test code //
47  gensvm_print_matrix(mat, 3, 2);
48  fclose(GENSVM_OUTPUT_FILE);
49 
50  char buffer[GENSVM_MAX_LINE_LENGTH];
51  fid = fopen(filename, "r");
52 
53  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
54  mu_assert(strcmp(buffer, "-0.241053 -0.599809\n") == 0,
55  "Line doesn't contain expected content (0).");
56 
57  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
58  mu_assert(strcmp(buffer, "+0.893318 -0.344058\n") == 0,
59  "Line doesn't contain expected content (1).");
60 
61  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
62  mu_assert(strcmp(buffer, "+0.933948 -0.474352\n") == 0,
63  "Line doesn't contain expected content (2).");
64 
65  fclose(fid);
66  // end test code //
67  free(mat);
68 
69  return NULL;
70 }
71 
73 {
74  FILE *fid = NULL;
75  double *A = Calloc(double, 4*4);
76  A[4] = 5;
77  A[5] = 8;
78  A[10] = 3;
79  A[13] = 6;
80  struct GenSparse *sp = gensvm_dense_to_sparse(A, 4, 4);
81  const char *filename = "./data/test_debug_sparse.txt";
82  GENSVM_OUTPUT_FILE = fopen(filename, "w");
83 
84  // start test code //
86  fclose(GENSVM_OUTPUT_FILE);
87 
88  char buffer[GENSVM_MAX_LINE_LENGTH];
89  fid = fopen(filename, "r");
90 
91  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
92  mu_assert(strcmp(buffer, "Sparse Matrix:\n") == 0,
93  "Line doesn't contain expected content (0).");
94 
95  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
96  mu_assert(strcmp(buffer, "\tnnz = 4, rows = 4, cols = 4\n") == 0,
97  "Line doesn't contain expected content (1).");
98 
99  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
100  mu_assert(strcmp(buffer, "\tvalues = [ 5.000000, 8.000000, "
101  "3.000000, 6.000000 ]\n") == 0,
102  "Line doesn't contain expected content (2).");
103 
104  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
105  mu_assert(strcmp(buffer, "\tIA = [ 0, 0, 2, 3, 4 ]\n") == 0,
106  "Line doesn't contain expected content (3).");
107 
108  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
109  mu_assert(strcmp(buffer, "\tJA = [ 0, 1, 2, 1 ]\n") == 0,
110  "Line doesn't contain expected content (4).");
111 
112  fclose(fid);
113  // end test code //
114  gensvm_free_sparse(sp);
115  free(A);
116 
117  return NULL;
118 }
119 
120 char *all_tests()
121 {
122  mu_suite_start();
125 
126  return NULL;
127 }
128 
Minimal unit testing framework for C.
#define Calloc(type, size)
Definition: gensvm_memory.h:40
Header file for gensvm_debug.c.
#define mu_assert(test, message)
Definition: minunit.h:29
#define GENSVM_MAX_LINE_LENGTH
char * all_tests()
void gensvm_free_sparse(struct GenSparse *sp)
Free an allocated GenSparse structure.
Definition: gensvm_sparse.c:62
void gensvm_print_sparse(struct GenSparse *A)
Print a sparse matrix.
Definition: gensvm_debug.c:66
char * test_print_sparse()
#define mu_run_test(test)
Definition: minunit.h:35
FILE * GENSVM_OUTPUT_FILE
Definition: gensvm_print.c:33
char * test_print_matrix()
RUN_TESTS(all_tests)
void gensvm_print_matrix(double *M, long rows, long cols)
Print a dense matrix.
Definition: gensvm_debug.c:42
struct GenSparse * gensvm_dense_to_sparse(double *A, long rows, long cols)
Convert a dense matrix to a GenSparse structure if advantageous.
#define matrix_set(M, cols, i, j, val)
#define mu_suite_start()
Definition: minunit.h:24
A structure to represent a sparse matrix in CSR format.
Definition: gensvm_sparse.h:55