Merge branch 'master' of git://git.kernel.org/pub/scm/git/git
[git/jnareb-git.git] / test-wildmatch.c
bloba3e2643fbc5022cabb671054db7950bc991652b9
1 #ifdef USE_WILDMATCH
2 #undef USE_WILDMATCH /* We need real fnmatch implementation here */
3 #endif
4 #include "cache.h"
5 #include "wildmatch.h"
7 static int perf(int ac, char **av)
9 struct timeval tv1, tv2;
10 struct stat st;
11 int fd, i, n, flags1 = 0, flags2 = 0;
12 char *buffer, *p;
13 uint32_t usec1, usec2;
14 const char *lang;
15 const char *file = av[0];
16 const char *pattern = av[1];
18 lang = getenv("LANG");
19 if (lang && strcmp(lang, "C"))
20 die("Please test it on C locale.");
22 if ((fd = open(file, O_RDONLY)) == -1 || fstat(fd, &st))
23 die_errno("file open");
25 buffer = xmalloc(st.st_size + 2);
26 if (read(fd, buffer, st.st_size) != st.st_size)
27 die_errno("read");
29 buffer[st.st_size] = '\0';
30 buffer[st.st_size + 1] = '\0';
31 for (i = 0; i < st.st_size; i++)
32 if (buffer[i] == '\n')
33 buffer[i] = '\0';
35 n = atoi(av[2]);
36 if (av[3] && !strcmp(av[3], "pathname")) {
37 flags1 = WM_PATHNAME;
38 flags2 = FNM_PATHNAME;
41 gettimeofday(&tv1, NULL);
42 for (i = 0; i < n; i++) {
43 for (p = buffer; *p; p += strlen(p) + 1)
44 wildmatch(pattern, p, flags1, NULL);
46 gettimeofday(&tv2, NULL);
48 usec1 = (uint32_t)tv2.tv_sec * 1000000 + tv2.tv_usec;
49 usec1 -= (uint32_t)tv1.tv_sec * 1000000 + tv1.tv_usec;
50 printf("wildmatch %ds %dus\n",
51 (int)(usec1 / 1000000),
52 (int)(usec1 % 1000000));
54 gettimeofday(&tv1, NULL);
55 for (i = 0; i < n; i++) {
56 for (p = buffer; *p; p += strlen(p) + 1)
57 fnmatch(pattern, p, flags2);
59 gettimeofday(&tv2, NULL);
61 usec2 = (uint32_t)tv2.tv_sec * 1000000 + tv2.tv_usec;
62 usec2 -= (uint32_t)tv1.tv_sec * 1000000 + tv1.tv_usec;
63 if (usec2 > usec1)
64 printf("fnmatch %ds %dus or %.2f%% slower\n",
65 (int)((usec2 - usec1) / 1000000),
66 (int)((usec2 - usec1) % 1000000),
67 (float)(usec2 - usec1) / usec1 * 100);
68 else
69 printf("fnmatch %ds %dus or %.2f%% faster\n",
70 (int)((usec1 - usec2) / 1000000),
71 (int)((usec1 - usec2) % 1000000),
72 (float)(usec1 - usec2) / usec1 * 100);
73 return 0;
76 int main(int argc, char **argv)
78 int i;
80 if (!strcmp(argv[1], "perf"))
81 return perf(argc - 2, argv + 2);
83 for (i = 2; i < argc; i++) {
84 if (argv[i][0] == '/')
85 die("Forward slash is not allowed at the beginning of the\n"
86 "pattern because Windows does not like it. Use `XXX/' instead.");
87 else if (!strncmp(argv[i], "XXX/", 4))
88 argv[i] += 3;
90 if (!strcmp(argv[1], "wildmatch"))
91 return !!wildmatch(argv[3], argv[2], WM_PATHNAME, NULL);
92 else if (!strcmp(argv[1], "iwildmatch"))
93 return !!wildmatch(argv[3], argv[2], WM_PATHNAME | WM_CASEFOLD, NULL);
94 else if (!strcmp(argv[1], "pathmatch"))
95 return !!wildmatch(argv[3], argv[2], 0, NULL);
96 else if (!strcmp(argv[1], "fnmatch"))
97 return !!fnmatch(argv[3], argv[2], FNM_PATHNAME);
98 else
99 return 1;