| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "testutils.h" |
| 24 |
|
| 25 |
# define NPL_TESTING |
| 26 |
# include "../lib/files.c" |
| 27 |
# undef NPL_TESTING |
| 28 |
|
| 29 |
typedef struct test_data |
| 30 |
{ |
| 31 |
int64_t age; |
| 32 |
time_t filemtime; |
| 33 |
time_t now; |
| 34 |
bool expect_value; |
| 35 |
} test_data; |
| 36 |
|
| 37 |
static int |
| 38 |
test_files_check_age (const void *tdata) |
| 39 |
{ |
| 40 |
const struct test_data *data = tdata; |
| 41 |
int ret = 0; |
| 42 |
|
| 43 |
bool check = files_check_age (data->age, data->now, data->filemtime); |
| 44 |
TEST_ASSERT_EQUAL_NUMERIC (check, data->expect_value); |
| 45 |
return ret; |
| 46 |
} |
| 47 |
|
| 48 |
static int |
| 49 |
mymain (void) |
| 50 |
{ |
| 51 |
int ret = 0; |
| 52 |
|
| 53 |
# define DO_TEST(AGE, NOW, FILEMTIME, EXPECT_VALUE) \ |
| 54 |
do \ |
| 55 |
{ \ |
| 56 |
test_data data = { \ |
| 57 |
.age = AGE, \ |
| 58 |
.filemtime = FILEMTIME, \ |
| 59 |
.now = NOW, \ |
| 60 |
.expect_value = EXPECT_VALUE, \ |
| 61 |
}; \ |
| 62 |
if (test_run("check function files_check_age", \ |
| 63 |
test_files_check_age, (&data)) < 0) \ |
| 64 |
ret = -1; \ |
| 65 |
} \ |
| 66 |
while (0) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
int64_t age = 3600 * 8; |
| 71 |
time_t now; |
| 72 |
|
| 73 |
now = time (NULL); |
| 74 |
|
| 75 |
DO_TEST (0, now, 0, true); |
| 76 |
|
| 77 |
DO_TEST (+age, now, now - (age + 1), true); |
| 78 |
DO_TEST (+age, now, now - (age - 1), false); |
| 79 |
DO_TEST (-age, now, now - (age - 1), true); |
| 80 |
DO_TEST (-age, now, now - (age + 1), false); |
| 81 |
|
| 82 |
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
| 83 |
} |
| 84 |
|
| 85 |
TEST_MAIN (mymain) |