aboutsummaryrefslogtreecommitdiff
path: root/tests/src/test_gensvm_strutil.c
blob: 520560f2e6c972a071196cf8db3dc94a93e2b8ae (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
 * @file test_gensvm_strutil.c
 * @author Gertjan van den Burg
 * @date May, 2016
 * @brief Unit tests for gensvm_strutil.c functions
 */

#include "minunit.h"
#include "gensvm_strutil.h"

char *test_str_startswith()
{
	mu_assert(str_startswith("test this string", "test") == true,
			"startswith first test failed.");
	mu_assert(str_startswith("test this string", "word") == false,
			"startswith second test failed.");
	return NULL;
}

char *test_str_endswith()
{
	mu_assert(str_endswith("this is a string", "string") == true,
			"endswith first test failed.");
	mu_assert(str_endswith("this is a string", "word") == false,
			"endswith second test failed.");
	return NULL;
}

char *test_get_line()
{
	char *fname = "test_get_line.txt";
	// prepare the test file
	FILE *fid = fopen(fname, "w");
	if (fid == NULL) return "couldn't open test file for get_line";
	fprintf(fid, "first line\n");
	fprintf(fid, "second line\n");
	fclose(fid);
	// end preparation

	// start of test code
	char buffer[MAX_LINE_LENGTH];
	char *retval = NULL;
	fid = fopen(fname, "r");
	retval = get_line(fid, fname, buffer);
	mu_assert(retval == buffer, "return value not buffer (1)");
	retval = get_line(fid, fname, buffer);
	mu_assert(retval == buffer, "return value not buffer (2)");
	retval = get_line(fid, fname, buffer);
	mu_assert(retval == NULL, "return value not NULL");

	fclose(fid);
	// end of test code

	// cleanup
	remove(fname);

	return NULL;
}

char *test_next_line()
{
	char *fname = "test_next_line.txt";
	// prepare the test file
	FILE *fid = fopen(fname, "w");
	if (fid == NULL) return "couldn't open test file for next_line";
	fprintf(fid, "first line\n");
	fprintf(fid, "second line\n");
	fprintf(fid, "third line\n");
	fprintf(fid, "fourth line\n");
	fclose(fid);
	// end preparation

	// start of test code
	char line[60];
	fid = fopen(fname, "r");
	next_line(fid, fname);
	fgets(line, 60, fid);
	mu_assert(strcmp(line, "second line\n") == 0, "second line unequal");
	next_line(fid, fname);
	fgets(line, 60, fid);
	mu_assert(strcmp(line, "fourth line\n") == 0, "fourth line unequal");
	fclose(fid);
	// end of test code

	// cleanup
	remove(fname);

	return NULL;
}

char *test_get_fmt_double()
{
	char *fname = "test_get_fmt_double.txt";
	// prepare the test file
	FILE *fid = fopen(fname, "w");
	if (fid == NULL) return "couldn't open test file for get_fmt_double";
	fprintf(fid, "double = 3.1416\n");
	fprintf(fid, "another double = 42.42\n");
	fprintf(fid, "line without a double\n");
	fclose(fid);

	// start test code //
	double value = -1.0;
	fid = fopen(fname, "r");

	value = get_fmt_double(fid, fname, "double = %lf");
	mu_assert(value == 3.1416, "first value wrong");

	value = get_fmt_double(fid, fname, "another double = %lf");
	mu_assert(value == 42.42, "second value wrong");

	value = get_fmt_double(fid, fname, "line without a double");
	mu_assert(isnan(value), "third value wrong");

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

	// cleanup
	remove(fname);

	return NULL;
}

char *test_get_fmt_long()
{
	char *fname = "test_get_fmt_long.txt";
	// prepare the test file
	FILE *fid = fopen(fname, "w");
	if (fid == NULL) return "couldn't open test file for get_fmt_double";
	fprintf(fid, "long = 53161\n");
	fprintf(fid, "another long = 1212\n");
	fprintf(fid, "line without a long\n");
	fclose(fid);

	// start test code //
	long value = -1;
	fid = fopen(fname, "r");

	value = get_fmt_long(fid, fname, "long = %li");
	mu_assert(value == 53161, "first value wrong");

	value = get_fmt_long(fid, fname, "another long = %li");
	mu_assert(value == 1212, "second value wrong");

	value = get_fmt_long(fid, fname, "line without a long");
	// unfortunately we can't test this properly, a warning will be 
	// generated but the return value will be 0, which can be a valid 
	// value. In general therefore, the real test is to see if a warning 
	// is printed to stderr.
	mu_assert(value == 0, "third value wrong")

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

	// cleanup
	remove(fname);

	return NULL;
}

char *test_all_doubles_str()
{
	char *fname = "test_all_doubles_str.txt";
	// prepare the test file
	FILE *fid = fopen(fname, "w");
	if (fid == NULL) return "couldn't open test file for all_doubles_str";
	fprintf(fid, "1.0 2.0 3.0 4.0\n");
	fprintf(fid, "9.1 8.2 7.3\n");
	fprintf(fid, "offset 1.0 2.0\n");
	fclose(fid);

	// start test code //
	char buffer[60];
	double *values = Calloc(double, 10);
	fid = fopen(fname, "r");
	int nr = -1;

	fgets(buffer, 60, fid);
	nr = all_doubles_str(buffer, 0, values);
	mu_assert(nr == 4, "incorrect number of doubles (1)");
	mu_assert(values[0] == 1.0, "incorrect first value (1)");
	mu_assert(values[1] == 2.0, "incorrect second value (1)");
	mu_assert(values[2] == 3.0, "incorrect third value (1)");
	mu_assert(values[3] == 4.0, "incorrect fourth value (1)");

	fgets(buffer, 60, fid);
	nr = all_doubles_str(buffer, 0, values);
	mu_assert(nr == 3, "incorrect number of doubles (2)");
	mu_assert(values[0] == 9.1, "incorrect first value (2)");
	mu_assert(values[1] == 8.2, "incorrect second value (2)");
	mu_assert(values[2] == 7.3, "incorrect third value (2)");

	fgets(buffer, 60, fid);
	nr = all_doubles_str(buffer, 7, values);
	mu_assert(nr == 2, "incorrect number of doubles (3)");
	mu_assert(values[0] == 1.0, "incorrect first value (3)");
	mu_assert(values[1] == 2.0, "incorrect second value (3)");
	// end test code //

	// cleanup
	remove(fname);
	free(values);

	return NULL;
}

char *test_all_longs_str()
{
	char *fname = "test_all_longs_str.txt";
	// prepare the test file
	FILE *fid = fopen(fname, "w");
	if (fid == NULL) return "couldn't open test file for all_longs_str";
	fprintf(fid, "1 2 3 4\n");
	fprintf(fid, "91 82 73\n");
	fprintf(fid, "offset 10 20\n");
	fclose(fid);

	// start test code //
	char buffer[60];
	long *values = Calloc(long, 10);
	fid = fopen(fname, "r");
	int nr = -1;

	fgets(buffer, 60, fid);
	nr = all_longs_str(buffer, 0, values);
	mu_assert(nr == 4, "incorrect number of longs (1)");
	mu_assert(values[0] == 1, "incorrect first value (1)");
	mu_assert(values[1] == 2, "incorrect second value (1)");
	mu_assert(values[2] == 3, "incorrect third value (1)");
	mu_assert(values[3] == 4, "incorrect fourth value (1)");

	fgets(buffer, 60, fid);
	nr = all_longs_str(buffer, 0, values);
	mu_assert(nr == 3, "incorrect number of longs (2)");
	mu_assert(values[0] == 91, "incorrect first value (2)");
	mu_assert(values[1] == 82, "incorrect second value (2)");
	mu_assert(values[2] == 73, "incorrect third value (2)");

	fgets(buffer, 60, fid);
	nr = all_longs_str(buffer, 7, values);
	mu_assert(nr == 2, "incorrect number of longs (3)");
	mu_assert(values[0] == 10, "incorrect first value (3)");
	mu_assert(values[1] == 20, "incorrect second value (3)");
	// end test code //

	// cleanup
	remove(fname);
	free(values);

	return NULL;
}

char *all_tests()
{
	mu_suite_start();
	mu_run_test(test_str_startswith);
	mu_run_test(test_str_endswith);
	mu_run_test(test_get_line);
	mu_run_test(test_next_line);
	mu_run_test(test_get_fmt_double);
	mu_run_test(test_get_fmt_long);
	mu_run_test(test_all_doubles_str);
	mu_run_test(test_all_longs_str);

	return NULL;
}

RUN_TESTS(all_tests);