From 9f42e34eb27b0aefdac897b6c37ce11e20db52e7 Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Wed, 10 Sep 2008 23:27:52 +0200 Subject: [PATCH] More tests and fixes. Signed-off-by: Florent Bruneau --- common/trie.c | 5 ++- common/tst-trie.c | 86 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 84 insertions(+), 7 deletions(-) diff --git a/common/trie.c b/common/trie.c index 5ed957f..8504128 100644 --- a/common/trie.c +++ b/common/trie.c @@ -58,7 +58,6 @@ struct trie_t { bool locked; }; - trie_t *trie_new() { return p_new(trie_t, 1); @@ -143,7 +142,6 @@ static inline void trie_grow(trie_t *trie, int delta) next_size = p_alloc_nr(next_size); } while (trie->entries_len + delta > next_size); p_allocgrow(&trie->entries, next_size, &trie->entries_size); - printf("After grow: %d\n", trie->entries_size); } static inline int trie_entry_new(trie_t *trie) @@ -156,7 +154,8 @@ static inline int trie_add_leaf(trie_t *trie, const char *key) { trie_entry_t *entry; entry = &trie->entries[trie_entry_new(trie)]; - entry->c = m_strdup(key); + entry->c = strdup(key); /* don't use m_strdup + since m_strdup("") == NULL */ entry->c_len = m_strlen(key) + 1; entry->c_own = true; return trie->entries_len - 1; diff --git a/common/tst-trie.c b/common/tst-trie.c index e5113b0..b4b064e 100644 --- a/common/tst-trie.c +++ b/common/tst-trie.c @@ -33,11 +33,84 @@ * Copyright © 2008 Florent Bruneau */ +#include +#include +#include +#include +#include + #include "common.h" +#include "str.h" #include "trie.h" -int main(void) +static trie_t *create_trie_from_file(const char *file) +{ + trie_t *db; + const char *map, *p, *end; + struct stat st; + int fd; + char line[BUFSIZ]; + + fd = open(file, O_RDONLY, 0000); + if (fd < 0) { + UNIXERR("open"); + return NULL; + } + + if (fstat(fd, &st) < 0) { + UNIXERR("fstat"); + close(fd); + return NULL; + } + + p = map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + if (map == MAP_FAILED) { + UNIXERR("mmap"); + close(fd); + return NULL; + } + close(fd); + + end = map + st.st_size; + while (end > map && end[-1] != '\n') { + --end; + } + if (end != map + st.st_size) { + syslog(LOG_WARNING, "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 0 + 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; + } + munmap((void*)map, st.st_size); + return db; +} + + +int main(int argc, char *argv[]) { + /* Trivial tests + */ trie_t *trie = trie_new(); trie_insert(trie, "abcdefghi"); trie_insert(trie, "abcde123654789"); @@ -45,7 +118,6 @@ int main(void) trie_insert(trie, "abcde123654789"); trie_insert(trie, "coucou"); trie_insert(trie, "coucou chez vous"); - trie_inspect(trie); #define ASSERT_TRUE(str) \ if (!trie_lookup(trie, str)) { \ @@ -57,7 +129,6 @@ int main(void) printf("\"%s\" found in trie\n", str); \ return 1; \ } - ASSERT_FALSE(""); ASSERT_FALSE("coucou "); ASSERT_FALSE("abcde123"); @@ -65,7 +136,14 @@ int main(void) ASSERT_TRUE("abcdefghi"); ASSERT_TRUE("coucou"); ASSERT_FALSE("coucou chez vous tous"); - trie_delete(&trie); + + /* Perf test + */ + if (argc > 1) { + trie = create_trie_from_file(argv[1]); +// trie_inspect(trie); + trie_delete(&trie); + } return 0; } -- 2.20.1