aboutsummaryrefslogtreecommitdiff
path: root/src/tests/fs.c
blob: db8ab96b4314981603eac4da38170bef8d0c26f9 (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
#include "tests/tests.h"
#include "fs.h"

#include <time.h>
#include <string.h>

#define DATETIME_TEST_FILE "tests/empty"

static void
test_rbasename(void)
{
	char *a = "/path/to/hello.jpg";
	const char *ob;
	ob = rbasename(a);
	asserteq(strcmp(ob, "hello.jpg"), 0);
}

static void
test_joinpath(void)
{
	char *a = "hello", *b = "world.jpeg";
	char *joined = joinpath(a, b);
	asserteq(strcmp(joined, "hello/world.jpeg"), 0);
	free(joined);
}

static void
test_isimage(void)
{
	char *a = "hello.jpg", *b = "goodbye.jpeg", *c = "iamge.png", *d = "b.tiff";
	char *notimg = "image.exe";
	asserteq(isimage(a), true);
	asserteq(isimage(b), true);
	asserteq(isimage(c), true);
	asserteq(isimage(d), true);
	asserteq(isimage(notimg), false);
}

static void
test_delext(void)
{
	char *na = "hello.jpg", *nb = "goodbye.tar.gz";
	size_t bla = 16, blb = 16, blc = 4;
	char bufa[bla], bufb[blb], bufc[blc];
	delext(na, bufa, bla);
	asserteq(strcmp(bufa, "hello"), 0);
	delext(nb, bufb, blb);
	asserteq(strcmp(bufb, "goodbye.tar"), 0);
	delext(nb, bufc, blc);
}

static void
test_setdatetime_uptodate(void)
{
	time_t now = time(NULL);
	struct timespec mtim = {
		.tv_sec = now,
		.tv_nsec = 690,
	};
	asserteq(file_is_uptodate(DATETIME_TEST_FILE, &mtim), 0);
	setdatetime(DATETIME_TEST_FILE, &mtim);
	asserteq(file_is_uptodate(DATETIME_TEST_FILE, &mtim), 1);
}

int
main(void)
{
	INIT_TESTS();
	RUN_TEST(test_rbasename);
	RUN_TEST(test_joinpath);
	RUN_TEST(test_isimage);
	RUN_TEST(test_delext);
	RUN_TEST(test_setdatetime_uptodate);
}