-
Notifications
You must be signed in to change notification settings - Fork 4
/
tests.c
394 lines (295 loc) · 9.9 KB
/
tests.c
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include "knn.c"
#include "greatest.h"
#include "terminal_user_input.h"
//Defining tolerances for tests
#define FLOAT_TOLERANCE 0.01
TEST mode_3_inputs(void) {
//Setup array of integers
int inputs[3] = {3, 3, 7};
//Pass array of integers into function
//Check mode is correct
ASSERT_EQ(3, mode(inputs, 3));
PASS();
}
TEST mode_with_zero (void) {
//Setup array of integers
int inputs[3] = {0, 1, 1};
//Pass array of integers into function
//Check mode is correct
ASSERT_EQ(1, mode(inputs, 3));
PASS();
}
TEST mode_7_inputs(void) {
//Setup array of integers
int inputs[7] = {1, 2, 3, 1, 7, 8, 1};
//Pass array of integers into function
//Check mode is correct
ASSERT_EQ(1, mode(inputs, 7));
PASS();
}
//Test bimodal
//Compare two integers that are equal
TEST compare_ints(void) {
int n1 = 1;
int n2 = 1;
ASSERT_EQ(compare_int(&n1, &n2), 0);
PASS();
}
//Compare two integers that are equal
TEST compare_greater_int(void) {
int n1 = 2;
int n2 = 1;
ASSERT_EQ(compare_int(&n1, &n2), 1);
PASS();
}
//Compare two integers that are equal
TEST compare_very_different_int_negative (void) {
int n1 = 1;
int n2 = 4;
ASSERT_EQ(compare_int(&n1, &n2), -1);
PASS();
}
TEST compare_very_different_int_positive (void) {
int n1 = 4;
int n2 = 1;
ASSERT_EQ(compare_int(&n1, &n2), 1);
PASS();
}
/* A test runs various assertions, then calls PASS(), FAIL(), or SKIP(). */
TEST distance_3_dimensions(void) {
float array1[3] = {2.0, 2.0, 2.0};
Comparison_Point point1 = {array1, NULL};
float array2[3] = {5.0, 5.0, 5.0};
Point point2 = {array2, 0, NULL};
ASSERT_IN_RANGE(5.1962, point_distance(point1, point2, 3), FLOAT_TOLERANCE);
PASS();
}
TEST distance_10_dimensions(void) {
float array1[10] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
Comparison_Point point1 = {array1, NULL};
float array2[10] = {10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0};
Point point2 = {array2, 0};
ASSERT_IN_RANGE(28.4605, point_distance(point1, point2, 10), FLOAT_TOLERANCE);
PASS();
}
TEST distance_1_dimension(void) {
float array1[1] = {3.0};
Comparison_Point point1 = {array1, NULL};
float array2[1] = {6.0};
Point point2 = {array2, 0};
ASSERT_IN_RANGE(3.0, point_distance(point1, point2, 1), FLOAT_TOLERANCE);
PASS();
}
TEST distance_1_dimension_fraction(void) {
float array1[1] = {3.0};
Comparison_Point point1 = {array1, NULL};
float array2[1] = {3.5};
Point point2 = {array2, 0};
ASSERT_IN_RANGE(0.5, point_distance(point1, point2, 1), FLOAT_TOLERANCE);
PASS();
}
//How do I initialise arrays with the {} curly braces syntax?
//Test the creation of an array (neighbours) with distances to every single point,
//taking one point and a dataset
//We need a distance associated with a point
//Test k NN search
//Ensure that the returned array contains the correct integers
//Test the nearest 1 neighbour can be found
TEST find_1_nearest_neighbour(void) {
int k = 1;
int category = 0;
//Pass it the stuff it needs, the dataset,
float dimensions[] = {5};
Point point1 = {dimensions, category};
//Since we've only got a length of 1, just use a pointer straight to the single point
Point* points = &point1;
Dataset single_point_dataset = {1, 1, points};
float comparison_dimensions[] = {3};
//TODO, fix the comparison point category
Comparison_Point compare = {comparison_dimensions, NULL};
//One point to compare to the rest
ASSERT_EQ(category, knn_search(k, compare, &single_point_dataset));
free(compare.neighbour);
PASS();
}
//One dimensional, 5 point dataset, find average of k=3 neighbours
//Test the code can handle updating 3 of the 5 without having to update a distance
TEST find_3_nearest_neighbour(void) {
//Setup
int k = 3;
//Pass it the stuff it needs, the dataset,
float dimensions0[] = {5.0};
Point point0 = {dimensions0, 0};
float dimensions1[] = {6.0};
Point point1 = {dimensions1, 1};
float dimensions2[] = {7.0};
Point point2 = {dimensions2, 1};
float dimensions3[] = {0.0};
Point point3 = {dimensions3, 0};
float dimensions4[] = {-1.0};
Point point4 = {dimensions4, 0};
//Since we've only got a length of 1, just use a pointer straight to the single point
Point points[5] = {point0, point1, point2, point3, point4};
Dataset point_dataset = {1, 5, points};
float comparison_dimensions[] = {6.5};
//TODO, fix the comparison point category
Comparison_Point compare = {comparison_dimensions, NULL};
int category = knn_search(k, compare, &point_dataset);
free(compare.neighbour);
//One point to compare to the rest
ASSERT_EQ(1, category);
PASS();
}
TEST classify_int(void) {
//The class integer to be selected
int class = 0;
//Using only the minimum 1 categories
Classifier_List flower_map;
flower_map.categories = (my_string*)malloc(sizeof(my_string));
strcpy(flower_map.categories[0].str, "Iris");
my_string category = classify(flower_map, class);
ASSERT_STR_EQ("Iris", category.str);
PASS();
free(flower_map.categories);
}
TEST extract_field_1(void) {
//From a string of "1.1, 1.2, 1.3, 1.4", extract field 1
my_string test_line;
strcpy(test_line.str, "1.1, 1.2, 1.3, 1.4");
ASSERT_STR_EQ("1.1", extract_field(test_line, 1).str);
PASS();
}
TEST extract_field_4(void) {
//From a string of "1.1, 1.2, 1.3, 1.4", extract field 1
my_string test_line;
strcpy(test_line.str, "1.1, 1.2, 1.3, 1.4");
ASSERT_STR_EQ("1.4", extract_field(test_line, 4).str);
PASS();
}
TEST extract_field_different_formatting(void) {
//From a string of "1.1, 1.2, 1.3, 1.4", extract field 1
my_string test_line;
strcpy(test_line.str, "1.1,,,''1.2,three, 6");
ASSERT_STR_EQ("three", extract_field(test_line, 3).str);
PASS();
}
TEST extract_flower_field(void) {
//From a string of "1.1, 1.2, 1.3, 1.4", extract field 1
my_string test_line;
strcpy(test_line.str, "5.1,3.5,1.4,0.2,Iris-setosa");
ASSERT_STR_EQ("Iris-setosa", extract_field(test_line, 5).str);
PASS();
}
TEST field_2(void) {
//From a string of "1.1, 1.2, 1.3, 1.4", extract field 1
my_string test_line;
strcpy(test_line.str, "5.1,3.5,1.4,0.2,Iris-setosa");
ASSERT_STR_EQ("3.5", extract_field(test_line, 2).str);
PASS();
}
TEST out_of_bounds(void) {
//From a string of "1.1, 1.2, 1.3, 1.4", extract field 1
my_string test_line;
strcpy(test_line.str, "5.1,3.5,1.4,0.2,Iris-setosa");
ASSERT_STR_EQ("\0", extract_field(test_line, 6).str);
PASS();
}
TEST gets_class_int(void) {
//Pass in a string, with a class_list which contains it, see if the correct value is returned
my_string strings[4] = {{"mycategory1"}, {"mycategory2"}, {"mycategory3"}, {"mycategory4"}};
Classifier_List class_list = {strings, 4};
ASSERT_EQ(0, get_class_num(class_list.categories[0], &class_list));
PASS();
}
TEST initialise_category(void) {
Classifier_List new_list = new_classifier_list();
strcpy(new_list.categories[0].str, "Testing Category");
ASSERT_EQ(0, new_list.num_categories);
PASS();
}
TEST create_first_category(void) {
//Pass in a string, with a class_list which contains it, see if the correct value is returned
my_string first_class = {"Test Category"};
Classifier_List class_list = new_classifier_list();
ASSERT_EQ(0, get_class_num(first_class, &class_list));
ASSERT_STR_EQ(first_class.str, class_list.categories[0].str);
PASS();
}
TEST create_new_category(void) {
//Pass in a string, with a class_list which contains it, see if the correct value is returned
my_string first_class = {"Test Category"};
my_string second_class = {"Class2"};
Classifier_List class_list = new_classifier_list();
ASSERT_EQ(0, get_class_num(first_class, &class_list));
ASSERT_STR_EQ(first_class.str, class_list.categories[0].str);
ASSERT_EQ(1, get_class_num(second_class, &class_list));
ASSERT_STR_EQ(second_class.str, class_list.categories[1].str);
#ifdef DEBUG
print_classes(class_list);
#endif
PASS();
}
TEST knn_accuracy(void) {
//Comments step through the expected classification of the knn
//for each point removed and then consider the percentage correct for that k
//In this case k=3
float dimensions0[] = {5.0};
Point point0 = {dimensions0, 0};
//Classed 1
//Incorrect
float dimensions1[] = {6.0};
Point point1 = {dimensions1, 1};
//Classed 0
//Incorrect
float dimensions2[] = {7.0};
Point point2 = {dimensions2, 1};
//Classed 0
//Incorrect
float dimensions3[] = {0.0};
Point point3 = {dimensions3, 0};
//Classed 0
//Correct
float dimensions4[] = {-1.0};
Point point4 = {dimensions4, 0};
//Classed 0
//correct
//Count is 2
// 2/5=0.4
Point points[5] = {point0, point1, point2, point3, point4};
Dataset test_dataset = {1, 5, points};
evaluate_knn(3, &test_dataset);
ASSERT_IN_RANGE(0.4, evaluate_knn(3, &test_dataset), FLOAT_TOLERANCE);
PASS();
}
//Test that the correct number is returned after a call to the string is passed to the classifier
/* Suites can group multiple tests with common setup. */
SUITE(external_suite) {
RUN_TEST(distance_1_dimension);
RUN_TEST(distance_1_dimension_fraction);
RUN_TEST(distance_3_dimensions);
RUN_TEST(distance_10_dimensions);
RUN_TEST(classify_int);
RUN_TEST(mode_3_inputs);
RUN_TEST(mode_with_zero);
RUN_TEST(mode_7_inputs);
RUN_TEST(compare_ints);
RUN_TEST(compare_greater_int);
RUN_TEST(compare_very_different_int_positive);
RUN_TEST(compare_very_different_int_negative);
RUN_TEST(find_1_nearest_neighbour);
RUN_TEST(find_3_nearest_neighbour);
RUN_TEST(extract_field_1);
RUN_TEST(extract_field_4);
RUN_TEST(extract_field_different_formatting);
RUN_TEST(extract_flower_field);
RUN_TEST(field_2);
RUN_TEST(out_of_bounds);
//Testing the configuration manager
RUN_TEST(gets_class_int);
RUN_TEST(initialise_category);
RUN_TEST(create_first_category);
RUN_TEST(create_new_category);
RUN_TEST(knn_accuracy);
}
/* Add definitions that need to be in the test runner's main file. */
GREATEST_SUITE(external_suite);