aboutsummaryrefslogtreecommitdiff
path: root/tests/src/test_gensvm_kernel.c
blob: 47eb8b12d9fdfa1d04995b34c8c5a2cb3d4ce7d1 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
 *@file test_gensvm_kernel.c
 *@author G.J.J. van den Burg
 *@date 2016-11-09
 *@brief Unit tests for gensvm_kernel.c
 *
 *@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_kernel.h"

char *test_dot_rbf()
{
	double dot;
	double *a = Malloc(double, 5);
	double *b = Malloc(double, 5);
	double *kernelparam = Malloc(double, 1);

	a[0] = 0.5203363837176203;
	a[1] = 0.3860628599460129;
	a[2] = 0.3592536954640216;
	a[3] = 0.6824659760765744;
	a[4] = 0.5390520090020700;

	b[0] = 0.1782643262351465;
	b[1] = 0.0314270210724957;
	b[2] = 0.5887219369641497;
	b[3] = 0.7710042954911620;
	b[4] = 0.8805451245738238;

	// start test code //
	kernelparam[0] = 1.0;
	dot = gensvm_kernel_dot_rbf(a, b, kernelparam, 5);
	mu_assert(fabs(dot - 0.657117701533133) < 1e-14, "Incorrect dot (1)");

	kernelparam[0] = 5.0;
	dot = gensvm_kernel_dot_rbf(a, b, kernelparam, 5);
	mu_assert(fabs(dot - 0.122522495044048) < 1e-14, "Incorrect dot (2)");

	// end test code //
	free(a);
	free(b);
	free(kernelparam);

	return NULL;
}

char *test_dot_poly()
{
	double dot;
	double *a = Malloc(double, 5);
	double *b = Malloc(double, 5);
	double *kernelparam = Malloc(double, 3);

	a[0] = 0.5203363837176203;
	a[1] = 0.3860628599460129;
	a[2] = 0.3592536954640216;
	a[3] = 0.6824659760765744;
	a[4] = 0.5390520090020700;

	b[0] = 0.1782643262351465;
	b[1] = 0.0314270210724957;
	b[2] = 0.5887219369641497;
	b[3] = 0.7710042954911620;
	b[4] = 0.8805451245738238;

	// start test code //
	kernelparam[0] = 1.0;
	kernelparam[1] = 1.0;
	kernelparam[2] = 1.0;
	dot = gensvm_kernel_dot_poly(a, b, kernelparam, 5);
	mu_assert(fabs(dot - 2.31723456944910) < 1e-14, "Incorrect dot (1)");

	kernelparam[0] = 1.5;
	kernelparam[1] = 2.5;
	kernelparam[2] = 3.5;
	dot = gensvm_kernel_dot_poly(a, b, kernelparam, 5);
	mu_assert(fabs(dot - 189.6989652572890179) < 1e-14, "Incorrect dot (2)");

	// end test code //
	free(a);
	free(b);
	free(kernelparam);
	return NULL;
}

char *test_dot_sigmoid()
{
	double dot;
	double *a = Malloc(double, 5);
	double *b = Malloc(double, 5);
	double *kernelparam = Malloc(double, 2);

	a[0] = 0.5203363837176203;
	a[1] = 0.3860628599460129;
	a[2] = 0.3592536954640216;
	a[3] = 0.6824659760765744;
	a[4] = 0.5390520090020700;

	b[0] = 0.1782643262351465;
	b[1] = 0.0314270210724957;
	b[2] = 0.5887219369641497;
	b[3] = 0.7710042954911620;
	b[4] = 0.8805451245738238;

	// start test code //
	kernelparam[0] = 1.0;
	kernelparam[1] = 1.0;
	dot = gensvm_kernel_dot_sigmoid(a, b, kernelparam, 5);
	mu_assert(fabs(dot - 0.9807642810850747) < 1e-14, "Incorrect dot (1)");

	kernelparam[0] = 1.5;
	kernelparam[1] = 2.5;
	dot = gensvm_kernel_dot_sigmoid(a, b, kernelparam, 5);
	mu_assert(fabs(dot - 0.9997410009167159) < 1e-14, "Incorrect dot (2)");

	// end test code //
	free(a);
	free(b);
	free(kernelparam);

	return NULL;
}

char *all_tests()
{
	mu_suite_start();
	mu_run_test(test_dot_rbf);
	mu_run_test(test_dot_poly);
	mu_run_test(test_dot_sigmoid);

	return NULL;
}

RUN_TESTS(all_tests);