summaryrefslogtreecommitdiff
path: root/dirname.c
blob: 761512a112b6ac2df086c239b618623e2fbfe876 (plain) (blame)
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
#include <stdio.h>
#include <string.h>
#include <libgen.h>
#include <stdlib.h>

#define TEST(p, b) ( \
tmp = strdup((p)), s = dirname(tmp), \
!strcmp((b),s) || \
(printf(__FILE__ ":%d: dirname(\"%s\") returned \"%s\"; expected \"%s\"\n", \
__LINE__, (p), s, (b)), err++, 0), free(tmp), 0 )

int test_dirname(void)
{
	char *tmp, *s;
	int err=0;

	if (strcmp(dirname(NULL), ".")) {
		printf(__FILE__ ":%d: dirname(NULL) returned \"%s\"; "
			"expected \".\"\n", __LINE__, dirname(NULL));
		err++;
	}
	TEST("", ".");
	TEST("/usr/lib", "/usr");
	TEST("/usr/", "/");
	TEST("usr", ".");
	TEST("/", "/");
	TEST("///", "/");
	TEST(".", ".");
	TEST("..", ".");

	return err;
}