aboutsummaryrefslogtreecommitdiff
path: root/tests/src/test_gensvm_debug.c
blob: e534e42bf355ad1bbb12370919b376f199730c12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
 * @file test_gensvm_debug.c
 * @author G.J.J. van den Burg
 * @date 2016-09-01
 * @brief Unit tests for gensvm_io.c functions
 *
 * @copyright
 Copyright 2016, G.J.J. van den Burg.

 This file is part of GenSVM.

 GenSVM is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 GenSVM is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GenSVM. If not, see <http://www.gnu.org/licenses/>.

 */

#include "minunit.h"
#include "gensvm_debug.h"

extern FILE *GENSVM_OUTPUT_FILE;

char *test_print_matrix()
{
	FILE *fid = NULL;
	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);
	matrix_set(mat, 2, 0, 1, -0.599809408260836);
	matrix_set(mat, 2, 1, 0,  0.893318163305108);
	matrix_set(mat, 2, 1, 1, -0.344057630469285);
	matrix_set(mat, 2, 2, 0,  0.933948479216127);
	matrix_set(mat, 2, 2, 1, -0.474352026604967);

	// start test code //
	gensvm_print_matrix(mat, 3, 2);
	fclose(GENSVM_OUTPUT_FILE);

	char buffer[MAX_LINE_LENGTH];
	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).");

	fgets(buffer, MAX_LINE_LENGTH, fid);
	mu_assert(strcmp(buffer, "+0.893318 -0.344058\n") == 0,
		       	"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).");

	fclose(fid);
	// end test code //
	free(mat);

	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;
}

RUN_TESTS(all_tests);