-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
498 lines (417 loc) · 12.8 KB
/
test.c
File metadata and controls
498 lines (417 loc) · 12.8 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include <stdio.h>
#include <string.h>
#include "curve.h"
#include "fan.h"
#include "sensor.h"
#include "zone.h"
#include "config.h"
static int tests_passed = 0;
static int tests_failed = 0;
#define ASSERT(cond) do { \
if (cond) { \
tests_passed++; \
} else { \
fprintf(stderr, "FAIL: %s:%d: assertion failed: %s\n", __FILE__, __LINE__, #cond); \
tests_failed++; \
} \
} while(0)
#define ASSERT_EQ(a, b) do { \
int _a = (a); int _b = (b); \
if (_a == _b) { \
tests_passed++; \
} else { \
fprintf(stderr, "FAIL: %s:%d: expected %d, got %d\n", __FILE__, __LINE__, _b, _a); \
tests_failed++; \
} \
} while(0)
/* curve tests */
static void test_curve_create_destroy(void)
{
int inputs[] = {30, 40, 50};
int outputs[] = {100, 200, 255};
struct curve *c = curve_create(inputs, outputs, 3);
ASSERT(c != NULL);
ASSERT_EQ(c->length, 3);
ASSERT_EQ(c->inputs[0], 30);
ASSERT_EQ(c->outputs[0], 100);
curve_destroy(c);
tests_passed++; /* destroy didn't crash */
}
static void test_curve_below_min(void)
{
int inputs[] = {30, 40, 50};
int outputs[] = {100, 200, 255};
struct curve *c = curve_create(inputs, outputs, 3);
/* below minimum temperature: should return minimum speed */
ASSERT_EQ(curve_get_value(c, 10.0f), 100);
ASSERT_EQ(curve_get_value(c, 29.9f), 100);
ASSERT_EQ(curve_get_value(c, 30.0f), 100);
curve_destroy(c);
}
static void test_curve_above_max(void)
{
int inputs[] = {30, 40, 50};
int outputs[] = {100, 200, 255};
struct curve *c = curve_create(inputs, outputs, 3);
/* above maximum temperature: should return maximum speed */
ASSERT_EQ(curve_get_value(c, 50.0f), 255);
ASSERT_EQ(curve_get_value(c, 60.0f), 255);
ASSERT_EQ(curve_get_value(c, 100.0f), 255);
curve_destroy(c);
}
static void test_curve_exact_point(void)
{
int inputs[] = {30, 40, 50};
int outputs[] = {100, 200, 255};
struct curve *c = curve_create(inputs, outputs, 3);
/* exact curve points should return the corresponding output */
ASSERT_EQ(curve_get_value(c, 40.0f), 200);
curve_destroy(c);
}
static void test_curve_interpolation(void)
{
int inputs[] = {30, 40, 50};
int outputs[] = {100, 200, 255};
struct curve *c = curve_create(inputs, outputs, 3);
/*
* Between 30 and 40: slope = (200-100)/(40-30) = 10
* at 35: 10*(35-30)+100 = 150
*/
ASSERT_EQ(curve_get_value(c, 35.0f), 150);
/*
* Between 40 and 50: slope = (255-200)/(50-40) = 5.5
* at 45: 5.5*(45-40)+200 = 227 (truncated to int)
*/
ASSERT_EQ(curve_get_value(c, 45.0f), 227);
curve_destroy(c);
}
static void test_curve_single_point(void)
{
int inputs[] = {50};
int outputs[] = {128};
struct curve *c = curve_create(inputs, outputs, 1);
/* any value should return the single output */
ASSERT_EQ(curve_get_value(c, 0.0f), 128);
ASSERT_EQ(curve_get_value(c, 50.0f), 128);
ASSERT_EQ(curve_get_value(c, 100.0f), 128);
curve_destroy(c);
}
/* sensor tests */
static void test_sensor_create_destroy(void)
{
struct sensor *s = sensor_create("/sys/class/hwmon/hwmon0", 1, 0);
ASSERT(s != NULL);
ASSERT(s->hwmon_path != NULL);
ASSERT_EQ(s->index, 1);
ASSERT_EQ(s->offset, 0);
ASSERT(strstr(s->temp_path, "temp1_input") != NULL);
sensor_destroy(s);
tests_passed++; /* destroy didn't crash */
}
static void test_sensor_create_with_offset(void)
{
struct sensor *s = sensor_create("/sys/class/hwmon/hwmon2", 3, 5);
ASSERT(s != NULL);
ASSERT_EQ(s->index, 3);
ASSERT_EQ(s->offset, 5);
ASSERT(strstr(s->temp_path, "temp3_input") != NULL);
sensor_destroy(s);
}
/* fan tests */
static void test_fan_create_destroy(void)
{
int inputs[] = {30, 40};
int outputs[] = {100, 255};
struct curve *c = curve_create(inputs, outputs, 2);
struct fan *f = fan_create("/sys/class/hwmon/hwmon0", 1, c);
ASSERT(f != NULL);
ASSERT(f->hwmon_path != NULL);
ASSERT_EQ(f->index, 1);
ASSERT(f->curve != NULL);
ASSERT(strstr(f->pwm_path, "pwm1") != NULL);
ASSERT(strstr(f->pwm_enable_path, "pwm1_enable") != NULL);
ASSERT(strstr(f->rpm_path, "fan1_input") != NULL);
fan_destroy(f);
tests_passed++; /* destroy didn't crash, including curve */
}
/* zone tests */
static void test_zone_create_destroy(void)
{
struct zone *z = zone_create();
ASSERT(z != NULL);
ASSERT_EQ(z->fans_len, 0);
ASSERT_EQ(z->sensors_len, 0);
zone_destroy(z);
tests_passed++; /* destroy didn't crash */
}
static void test_zone_attach_sensor(void)
{
struct zone *z = zone_create();
struct sensor *s1 = sensor_create("/sys/class/hwmon/hwmon0", 1, 0);
struct sensor *s2 = sensor_create("/sys/class/hwmon/hwmon0", 2, 0);
int ret;
ret = zone_attach_sensor(z, s1);
ASSERT_EQ(ret, 1);
ASSERT_EQ(z->sensors_len, 1);
ret = zone_attach_sensor(z, s2);
ASSERT_EQ(ret, 2);
ASSERT_EQ(z->sensors_len, 2);
zone_destroy(z); /* also destroys sensors */
}
static void test_zone_attach_fan(void)
{
struct zone *z = zone_create();
int inputs[] = {30, 50};
int outputs[] = {100, 255};
struct fan *f1 = fan_create("/sys/class/hwmon/hwmon0", 1, curve_create(inputs, outputs, 2));
struct fan *f2 = fan_create("/sys/class/hwmon/hwmon0", 2, curve_create(inputs, outputs, 2));
int ret;
ret = zone_attach_fan(z, f1);
ASSERT_EQ(ret, 1);
ASSERT_EQ(z->fans_len, 1);
ret = zone_attach_fan(z, f2);
ASSERT_EQ(ret, 2);
ASSERT_EQ(z->fans_len, 2);
zone_destroy(z); /* also destroys fans */
}
static void test_zone_attach_limit(void)
{
struct zone *z = zone_create();
struct sensor *extra;
int i, ret;
/* fill up to MAX_ZONE_SIZE */
for (i = 0; i < MAX_ZONE_SIZE; i++) {
struct sensor *s = sensor_create("/sys/class/hwmon/hwmon0", i + 1, 0);
ret = zone_attach_sensor(z, s);
ASSERT(ret > 0);
}
ASSERT_EQ(z->sensors_len, MAX_ZONE_SIZE);
/* next attach should fail */
extra = sensor_create("/sys/class/hwmon/hwmon0", MAX_ZONE_SIZE + 1, 0);
ret = zone_attach_sensor(z, extra);
ASSERT_EQ(ret, -1);
ASSERT_EQ(z->sensors_len, MAX_ZONE_SIZE);
sensor_destroy(extra);
zone_destroy(z);
}
/* config tests */
static void test_config_load_valid(void)
{
const char *cfg_path = "/tmp/fand_test.conf";
FILE *f = fopen(cfg_path, "w");
ASSERT(f != NULL);
fprintf(f,
"zones:\n"
" - sensors:\n"
" - path: /sys/class/hwmon/hwmon0\n"
" index: 1\n"
" offset: 0\n"
" fans:\n"
" - path: /sys/class/hwmon/hwmon0\n"
" index: 1\n"
" curve:\n"
" temperatures: [30, 40, 50]\n"
" speeds: [100, 200, 255]\n"
);
fclose(f);
struct fand_config *cfg = fand_config_load(cfg_path);
ASSERT(cfg != NULL);
ASSERT_EQ(cfg->zones_len, 1);
ASSERT_EQ(cfg->zones[0]->sensors_len, 1);
ASSERT_EQ(cfg->zones[0]->fans_len, 1);
fand_config_destroy(cfg);
remove(cfg_path);
}
static void test_config_load_missing_file(void)
{
struct fand_config *cfg = fand_config_load("/tmp/fand_nonexistent_12345.conf");
ASSERT(cfg == NULL);
}
static void test_config_load_no_zones(void)
{
const char *cfg_path = "/tmp/fand_test_empty.conf";
FILE *f = fopen(cfg_path, "w");
ASSERT(f != NULL);
fprintf(f, "zones: []\n");
fclose(f);
struct fand_config *cfg = fand_config_load(cfg_path);
ASSERT(cfg == NULL);
remove(cfg_path);
}
static void test_config_load_multiple_zones(void)
{
const char *cfg_path = "/tmp/fand_test_multi.conf";
FILE *f = fopen(cfg_path, "w");
ASSERT(f != NULL);
fprintf(f,
"zones:\n"
" - sensors:\n"
" - path: /sys/class/hwmon/hwmon0\n"
" index: 1\n"
" offset: 0\n"
" fans:\n"
" - path: /sys/class/hwmon/hwmon0\n"
" index: 1\n"
" curve:\n"
" temperatures: [30, 50]\n"
" speeds: [100, 255]\n"
" - sensors:\n"
" - path: /sys/class/hwmon/hwmon1\n"
" index: 1\n"
" offset: 0\n"
" fans:\n"
" - path: /sys/class/hwmon/hwmon1\n"
" index: 2\n"
" curve:\n"
" temperatures: [25, 45]\n"
" speeds: [80, 255]\n"
);
fclose(f);
struct fand_config *cfg = fand_config_load(cfg_path);
ASSERT(cfg != NULL);
ASSERT_EQ(cfg->zones_len, 2);
fand_config_destroy(cfg);
remove(cfg_path);
}
static void test_curve_unsorted_inputs(void)
{
int inputs[] = {50, 30, 40};
int outputs[] = {255, 100, 200};
struct curve *c = curve_create(inputs, outputs, 3);
ASSERT(c == NULL);
}
static void test_curve_clamping(void)
{
int inputs[] = {30, 40};
int outputs[] = {0, 300};
struct curve *c = curve_create(inputs, outputs, 2);
ASSERT(c != NULL);
ASSERT_EQ(curve_get_value(c, 30.0f), 0);
ASSERT_EQ(curve_get_value(c, 100.0f), 255);
curve_destroy(c);
}
static void test_config_default_poll_interval(void)
{
const char *cfg_path = "/tmp/fand_test_poll_default.conf";
FILE *f = fopen(cfg_path, "w");
ASSERT(f != NULL);
fprintf(f,
"zones:\n"
" - sensors:\n"
" - path: /sys/class/hwmon/hwmon0\n"
" index: 1\n"
" offset: 0\n"
" fans:\n"
" - path: /sys/class/hwmon/hwmon0\n"
" index: 1\n"
" curve:\n"
" temperatures: [30, 50]\n"
" speeds: [100, 255]\n"
);
fclose(f);
struct fand_config *cfg = fand_config_load(cfg_path);
ASSERT(cfg != NULL);
ASSERT_EQ(cfg->poll_interval, 1);
fand_config_destroy(cfg);
remove(cfg_path);
}
static void test_config_custom_poll_interval(void)
{
const char *cfg_path = "/tmp/fand_test_poll_custom.conf";
FILE *f = fopen(cfg_path, "w");
ASSERT(f != NULL);
fprintf(f,
"poll_interval: 5\n"
"zones:\n"
" - sensors:\n"
" - path: /sys/class/hwmon/hwmon0\n"
" index: 1\n"
" offset: 0\n"
" fans:\n"
" - path: /sys/class/hwmon/hwmon0\n"
" index: 1\n"
" curve:\n"
" temperatures: [30, 50]\n"
" speeds: [100, 255]\n"
);
fclose(f);
struct fand_config *cfg = fand_config_load(cfg_path);
ASSERT(cfg != NULL);
ASSERT_EQ(cfg->poll_interval, 5);
fand_config_destroy(cfg);
remove(cfg_path);
}
static void test_config_fan_hysteresis(void)
{
const char *cfg_path = "/tmp/fand_test_hyst.conf";
FILE *f = fopen(cfg_path, "w");
ASSERT(f != NULL);
fprintf(f,
"zones:\n"
" - sensors:\n"
" - path: /sys/class/hwmon/hwmon0\n"
" index: 1\n"
" offset: 0\n"
" fans:\n"
" - path: /sys/class/hwmon/hwmon0\n"
" index: 1\n"
" hysteresis: 15\n"
" curve:\n"
" temperatures: [30, 50]\n"
" speeds: [100, 255]\n"
);
fclose(f);
struct fand_config *cfg = fand_config_load(cfg_path);
ASSERT(cfg != NULL);
ASSERT_EQ(cfg->zones[0]->fans[0]->hysteresis, 15);
fand_config_destroy(cfg);
remove(cfg_path);
}
static void test_config_zone_no_sensors(void)
{
const char *cfg_path = "/tmp/fand_test_nosensors.conf";
FILE *f = fopen(cfg_path, "w");
ASSERT(f != NULL);
fprintf(f,
"zones:\n"
" - fans:\n"
" - path: /sys/class/hwmon/hwmon0\n"
" index: 1\n"
" curve:\n"
" temperatures: [30, 50]\n"
" speeds: [100, 255]\n"
);
fclose(f);
struct fand_config *cfg = fand_config_load(cfg_path);
ASSERT(cfg == NULL);
remove(cfg_path);
}
int main(void)
{
printf("Running fand tests...\n");
test_curve_create_destroy();
test_curve_below_min();
test_curve_above_max();
test_curve_exact_point();
test_curve_interpolation();
test_curve_single_point();
test_sensor_create_destroy();
test_sensor_create_with_offset();
test_fan_create_destroy();
test_zone_create_destroy();
test_zone_attach_sensor();
test_zone_attach_fan();
test_zone_attach_limit();
test_config_load_valid();
test_config_load_missing_file();
test_config_load_no_zones();
test_config_load_multiple_zones();
test_curve_unsorted_inputs();
test_curve_clamping();
test_config_default_poll_interval();
test_config_custom_poll_interval();
test_config_fan_hysteresis();
test_config_zone_no_sensors();
printf("\n%d passed, %d failed\n", tests_passed, tests_failed);
return tests_failed > 0 ? 1 : 0;
}