X-Git-Url: http://git.madism.org/?p=apps%2Fpfixtools.git;a=blobdiff_plain;f=common%2Ftst-trie.c;h=739bd415e7a1e1575d1777e25be89c606648bbc0;hp=e5113b06cc2387c791bba431012427d279703ec3;hb=ab82546953101224d09dcf2e44ff170e454e7282;hpb=d220f961e78461d2360c8c737d58b50bb769a80e diff --git a/common/tst-trie.c b/common/tst-trie.c index e5113b0..739bd41 100644 --- a/common/tst-trie.c +++ b/common/tst-trie.c @@ -34,18 +34,70 @@ */ #include "common.h" +#include "str.h" #include "trie.h" +#include "file.h" -int main(void) +static trie_t *create_trie_from_file(const char *file) { + trie_t *db; + file_map_t map; + const char *p, *end; + char line[BUFSIZ]; + + if (!file_map_open(&map, file, false)) { + 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); + } + + 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; + } + file_map_close(&map); + trie_compile(db, false); + return db; +} + + +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_inspect(trie); + trie_insert(trie, "debout !"); + trie_compile(trie, false); + trie_inspect(trie, true); #define ASSERT_TRUE(str) \ if (!trie_lookup(trie, str)) { \ @@ -57,15 +109,34 @@ int main(void) printf("\"%s\" found in trie\n", str); \ return 1; \ } - ASSERT_FALSE(""); 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, false); + if (argc > 2) { + time_t now = time(NULL); + for (uint32_t i = 0 ; i < 1000000000 ; ++i) { + trie_lookup(trie, argv[2]); + } + printf("%lu lookups per second\n", 1000000000 / (time(NULL) - now)); + } + trie_delete(&trie); + } return 0; }