X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=common%2Ftst-trie.c;h=2b7bdee64e7bb7e8cac5f9bbb80573249341337b;hb=76226c6f7b43aaa6480f776f70fb62f4a3e937df;hp=d2ccc14d21bb56d0705416a27fc4dcfc421f3c1e;hpb=fb519b0c3a78183d3fad3778d7d57bc2693598fc;p=apps%2Fpfixtools.git diff --git a/common/tst-trie.c b/common/tst-trie.c index d2ccc14..2b7bdee 100644 --- a/common/tst-trie.c +++ b/common/tst-trie.c @@ -33,54 +33,78 @@ * Copyright © 2008 Florent Bruneau */ -#include -#include -#include -#include -#include - +#include +#include #include "common.h" #include "str.h" #include "trie.h" +#include "file.h" static trie_t *create_trie_from_file(const char *file) { trie_t *db; - const char *map, *p, *end; - struct stat st; - int fd; + file_map_t map; + const char *p, *end; char line[BUFSIZ]; - fd = open(file, O_RDONLY, 0000); - if (fd < 0) { - UNIXERR("open"); + if (!file_map_open(&map, file, false)) { return NULL; } - - if (fstat(fd, &st) < 0) { - UNIXERR("fstat"); - close(fd); - return NULL; + p = map.map; + end = map.end; + while (end > p && end[-1] != '\n') { + --end; + } + if (end != map.end) { + warn("file %s miss a final \\n, ignoring last line", file); } - p = map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - if (map == MAP_FAILED) { - UNIXERR("mmap"); - close(fd); - return NULL; + db = trie_new(); + while (p < end && p != NULL) { + const char *eol = (char *)memchr(p, '\n', end - p); + if (eol == NULL) { + eol = end; + } + if (eol - p > BUFSIZ) { + p = eol - BUFSIZ; + } + int i = 0; +#if 1 + for (const char *s = eol - 1 ; s >= p ; --s) { + line[i++] = ascii_tolower(*s); + } +#else + memcpy(line, p, eol - p); + i = eol - p; +#endif + line[i] = '\0'; + trie_insert(db, line); + p = eol + 1; } - close(fd); + file_map_close(&map); + trie_compile(db, false); + return db; +} - end = map + st.st_size; - while (end > map && end[-1] != '\n') { +__attribute__((used)) +static void check_trie_with_file(const trie_t *db, const char *file) +{ + file_map_t map; + const char *p, *end; + char line[BUFSIZ]; + + if (!file_map_open(&map, file, false)) { + return; + } + p = map.map; + end = map.end; + while (end > p && end[-1] != '\n') { --end; } - if (end != map + st.st_size) { - syslog(LOG_WARNING, "file %s miss a final \\n, ignoring last line", - file); + if (end != map.end) { + warn("file %s miss a final \\n, ignoring last line", file); } - db = trie_new(); while (p < end && p != NULL) { const char *eol = (char *)memchr(p, '\n', end - p); if (eol == NULL) { @@ -99,11 +123,12 @@ static trie_t *create_trie_from_file(const char *file) i = eol - p; #endif line[i] = '\0'; - trie_insert(db, line); + if (!trie_lookup(db, line)) { + warn("'%s' not found in the trie", line); + } p = eol + 1; } - munmap((void*)map, st.st_size); - return db; + file_map_close(&map); } @@ -112,12 +137,14 @@ int main(int argc, char *argv[]) /* Trivial tests */ trie_t *trie = trie_new(); - trie_insert(trie, "abcdefghi"); - trie_insert(trie, "abcde123654789"); trie_insert(trie, "abcde123456789"); trie_insert(trie, "abcde123654789"); + trie_insert(trie, "abcdefghi"); trie_insert(trie, "coucou"); trie_insert(trie, "coucou chez vous"); + trie_insert(trie, "debout !"); + trie_compile(trie, false); + trie_inspect(trie, true); #define ASSERT_TRUE(str) \ if (!trie_lookup(trie, str)) { \ @@ -133,16 +160,36 @@ int main(int argc, char *argv[]) ASSERT_FALSE("coucou "); ASSERT_FALSE("abcde123"); ASSERT_FALSE("abcde"); + ASSERT_FALSE("coucou chez vous tous"); + ASSERT_TRUE("abcde123456789"); + ASSERT_TRUE("abcde123456789"); + ASSERT_TRUE("abcde123654789"); ASSERT_TRUE("abcdefghi"); ASSERT_TRUE("coucou"); - ASSERT_FALSE("coucou chez vous tous"); + ASSERT_TRUE("coucou chez vous"); + ASSERT_TRUE("debout !"); + trie_delete(&trie); /* Perf test */ if (argc > 1) { trie = create_trie_from_file(argv[1]); -// trie_inspect(trie); + trie_inspect(trie, false); + check_trie_with_file(trie, argv[1]); + if (argc > 2) { + const uint32_t how_many = 8 * 1000 * 1000; + struct timeval start, end; + double diff; + + gettimeofday(&start, NULL); + for (uint32_t i = 0 ; i < how_many ; ++i) { + trie_lookup(trie, argv[2]); + } + gettimeofday(&end, NULL); + diff = (end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) / 10e6; + printf("%u lookups per second\n", (int)(how_many / diff)); + } trie_delete(&trie); } return 0;