updated dirname/basepath to follow a modification of IEEE standard.
This commit is contained in:
parent
304049064e
commit
654f4bff80
6 changed files with 217 additions and 194 deletions
|
|
@ -14,9 +14,6 @@ raw_inotify :
|
|||
path :
|
||||
$(CC) -D__DEBUG__ $(CFLAGS) ../src/common/path.c t_path.c -o test_path
|
||||
|
||||
path2 :
|
||||
$(CC) -D__DEBUG__ $(CFLAGS) ../src/common/path.c t_path2.c -o test_path2
|
||||
|
||||
rbtree :
|
||||
$(CC) $(DEFS) $(CFLAGS) unit.c ../src/common/rbtree.c t_rbtree.c -o test_rbtree
|
||||
|
||||
|
|
|
|||
153
test/t_path.c
153
test/t_path.c
|
|
@ -1,82 +1,95 @@
|
|||
|
||||
#include <malloc.h>
|
||||
#include <stdio.h>
|
||||
#include "unit.h"
|
||||
#include "../src/common/path.h"
|
||||
|
||||
/* test data */
|
||||
void test_fmt_path() {
|
||||
|
||||
char string[8][128] = {
|
||||
"usr/include/",
|
||||
"/usr/src/linux",
|
||||
"/segment1/segment2/segment3/",
|
||||
"//double///tripple",
|
||||
"/stuff/with/ahell/lot/of/slashes/at/the/end/////////",
|
||||
"~/myhome/",
|
||||
"/",
|
||||
"///////////////////////////////////////////////////////////"
|
||||
};
|
||||
char *ptr;
|
||||
|
||||
char split[8][2][64] = {
|
||||
{"usr/", "include/" },
|
||||
{"/usr/src/", "linux" },
|
||||
{"/segment1/segment2/", "segment3/" },
|
||||
{"//double//", "/tripple" },
|
||||
{"/stuff/with/ahell/lot/of/slashes/at/the/", "end/////////" },
|
||||
{"~/", "myhome/" },
|
||||
{"/mnt/cdrom", "keff" },
|
||||
{"//////////////////////////////", "/////////////////////////////" }
|
||||
};
|
||||
ptr = fmt_path("usr/", "include/", 0);
|
||||
assert(ptr == NULL);
|
||||
|
||||
ptr = fmt_path("/usr/src/", "linux", 0);
|
||||
assert_string(ptr, "/usr/src/linux");
|
||||
free(ptr);
|
||||
/*
|
||||
ptr = fmt_path("/segment1/segment2/", "segment3/", 1);
|
||||
assert_string(ptr, "/segment1/segment2/segment3/");
|
||||
free(ptr);
|
||||
*/
|
||||
|
||||
/*
|
||||
ptr = fmt_path("/stuff/with/ahell/lot/of/slashes/at/the/", "end/////////", 1);
|
||||
assert_string(ptr, "/stuff/with/ahell/lot/of/slashes/at/the/end/////////");
|
||||
free(ptr);
|
||||
*/
|
||||
|
||||
ptr = fmt_path("/mnt/cdrom", "keff", 0);
|
||||
assert_string(ptr, "/mnt/cdrom/keff");
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
void test_pathlen() {
|
||||
|
||||
assert(pathlen("/usr/src/linux-2.6.30-r5/drivers") == 32);
|
||||
assert(pathlen("/usr///src/") == 9);
|
||||
assert(pathlen("/usr//include/sys//") == 17);
|
||||
assert(pathlen("///var/lib/misc") == 13);
|
||||
assert(pathlen("dir") == 3);
|
||||
}
|
||||
|
||||
void test_basename() {
|
||||
|
||||
int i;
|
||||
|
||||
char data[11][2][64] = {
|
||||
{ "", "." },
|
||||
{ "/", "/" },
|
||||
{ "///", "/" },
|
||||
{ ".", "."},
|
||||
{ "..", ".." },
|
||||
{ "../../rel1", "rel1" },
|
||||
{ "./rel2", "rel2" },
|
||||
{ "justsomestring", "justsomestring" },
|
||||
{ "/usr/src/", "src" },
|
||||
{ "/usr/src///", "src" },
|
||||
{ "/usr/src/linux-2.6.30-r5/drivers", "drivers" }
|
||||
};
|
||||
|
||||
for(i=0; i < 11; i++)
|
||||
assert_string(basename(data[i][0]), data[i][1]);
|
||||
}
|
||||
|
||||
void test_dirname() {
|
||||
|
||||
int i;
|
||||
|
||||
char data[11][2][64] = {
|
||||
{ "", "." },
|
||||
{ "/", "/" },
|
||||
{ "///", "/" },
|
||||
{ ".", "."},
|
||||
{ "..", "." },
|
||||
{ "../../rel", "../.." },
|
||||
{ "./rel", "." },
|
||||
{ "justsomestring", "." },
|
||||
{ "/usr/src/", "/usr" },
|
||||
{ "/usr/src///", "/usr" },
|
||||
{ "/usr/src/linux-2.6.30-r5/drivers", "/usr/src/linux-2.6.30-r5" }
|
||||
};
|
||||
|
||||
for(i=0; i < 11; i++)
|
||||
assert_string(dirname(data[i][0]), data[i][1]);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
int i;
|
||||
char *ptr = NULL;
|
||||
test_fmt_path();
|
||||
test_pathlen();
|
||||
test_basename();
|
||||
test_dirname();
|
||||
|
||||
for(i=0; i < 8; i++) {
|
||||
//printf("adding: %s\n", string[i]);
|
||||
ptr = fmt_path(string[i], NULL, 0);
|
||||
printf("str %i is: %s\n", i, ptr);
|
||||
|
||||
if (ptr != NULL) {
|
||||
free(ptr);
|
||||
ptr = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
for(i=0; i < 8; i++) {
|
||||
//printf("adding: %s%s\n", split[i][0], split[i][1]);
|
||||
ptr = fmt_path(split[i][0], split[i][1], 0);
|
||||
printf("str %i is: %s\n", i, ptr);
|
||||
|
||||
if (ptr != NULL) {
|
||||
free(ptr);
|
||||
ptr = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
for(i=0; i < 8; i++) {
|
||||
//printf("adding: %s%s\n", split[i][0], split[i][1]);
|
||||
ptr = fmt_path(split[i][0], split[i][1], 1);
|
||||
printf("str %i is: %s\n", i, ptr);
|
||||
|
||||
if (ptr != NULL) {
|
||||
free(ptr);
|
||||
ptr = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if(argc < 2)
|
||||
return 0;
|
||||
else if(argc == 2)
|
||||
ptr = fmt_path(argv[1], NULL, 0);
|
||||
else if(argc > 2) {
|
||||
ptr = fmt_path(argv[1], argv[2], 0);
|
||||
}
|
||||
|
||||
printf("%s\n", ptr);
|
||||
|
||||
if (ptr != NULL)
|
||||
free(ptr);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +0,0 @@
|
|||
|
||||
#include <malloc.h>
|
||||
#include <stdio.h>
|
||||
#include "../src/common/path.h"
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
char fp[] = "/this/is/my/path/to/file.pdf/";
|
||||
char *d;
|
||||
printf("%s\n=>\n", fp);
|
||||
|
||||
d = dirname(fp);
|
||||
|
||||
printf("%s\n", d);
|
||||
|
||||
return 0;
|
||||
}
|
||||
20
test/unit.h
20
test/unit.h
|
|
@ -8,8 +8,24 @@
|
|||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define assert_string(a, b) \
|
||||
assert(strcmp((char*)a, (char*)b) == 0)
|
||||
#define __uexit(file, line, func, fmt, ...) \
|
||||
do { \
|
||||
fprintf(stderr, "ASSERT %s in %s(%i): " fmt, func, file, line, __VA_ARGS__); \
|
||||
exit(1); \
|
||||
} while(0)
|
||||
|
||||
/* internal function. assert_* macros below expands to this */
|
||||
inline void __assert_str(char *file, int line, char *func, char *a, char *b) {
|
||||
|
||||
if (a == NULL || b == NULL)
|
||||
__uexit(file, line, func, "a or b is null\n", NULL);
|
||||
|
||||
if (strcmp(a, b) != 0)
|
||||
__uexit(file, line, func, "\"%s\" != \"%s\"\n", a, b);
|
||||
|
||||
}
|
||||
|
||||
#define assert_string(a, b) __assert_str(__FILE__, __LINE__, __FUNCTION__, a, b)
|
||||
|
||||
void utest_init_RNG();
|
||||
|
||||
|
|
|
|||
Reference in a new issue