X-Git-Url: https://git.distorted.org.uk/~mdw/mLib/blobdiff_plain/323ffb6cddd1a6a65d06e254817a5c394195b758..cd3001477e8cada39b9db728f65ff610d89a8895:/sym.c diff --git a/sym.c b/sym.c index 03d2e82..3bf52f3 100644 --- a/sym.c +++ b/sym.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: sym.c,v 1.8 1999/08/02 14:45:48 mdw Exp $ + * $Id: sym.c,v 1.9 1999/10/22 22:36:37 mdw Exp $ * * Symbol table management * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: sym.c,v $ + * Revision 1.9 1999/10/22 22:36:37 mdw + * New test structure for symbol tables. + * * Revision 1.8 1999/08/02 14:45:48 mdw * Break low-level hashtable code out from sym. * @@ -316,216 +319,4 @@ void *sym_next(sym_iter *i) return (p); } -/*----- Symbol table test code --------------------------------------------*/ - -#ifdef TEST_RIG - -#include -#include - -typedef struct sym_word { - sym_base base; - size_t i; -} sym_word; - - -/* --- What it does --- * - * - * Reads the file /usr/dict/words (change to some other file full of - * interesting and appropriate bits of text to taste) into a big buffer and - * picks apart into lines. Then picks lines at random and enters them into - * the symbol table. - */ - -int main(void) -{ - char *buff, *p, *lim; - size_t sz, done; - FILE *fp; - int i; - char **line; - sym_word **flag; - sym_table tbl; - int entries; - - /* --- Initialize for reading the file --- */ - - sz = BUFSIZ; - buff = xmalloc(sz + 1); - done = 0; - sub_init(); - - if ((fp = fopen("/usr/dict/words", "r")) == 0) - fprintf(stderr, "buggered ;-( (%s)\n", strerror(errno)); - - /* --- Read buffers of text --- * - * - * Read a buffer. If more to come, double the buffer size and try again. - * This is the method I recommended to comp.lang.c, so I may as well try - * it. - */ - - for (;;) { - i = fread(buff + done, 1, sz - done, fp); - done += i; - if (done != sz) - break; - sz <<= 1; - buff = xrealloc(buff, sz + 1); - } - - /* --- Count the lines --- */ - - lim = buff + done; - - sz = 1; - for (p = buff; p < lim; p++) - if (*p == '\n') sz++; - - /* --- Build a table of line starts --- */ - - line = xmalloc(sz * sizeof(char *)); - i = 0; - line[i++] = buff; - for (p = buff; p < lim; p++) - if (*p == '\n') *p = 0, line[i++] = p + 1; - *lim = 0; - - /* --- Build a table of lines --- * - * - * This reverses the mapping which the symbol table performs, so that its - * accuracy can be tested. - */ - - flag = xmalloc(sz * sizeof(sym_word *)); - for (i = 0; i < sz; i++) - flag[i] = 0; - entries = 0; - - sym_create(&tbl); - - for (;;) { - i = (unsigned)rand() % sz; - - switch (rand() % 5) - { - case 0: { - sym_word *w; - - printf("? %s\n", line[i]); - - w = sym_find(&tbl, line[i], -1, 0, 0); - if (w != flag[i]) - printf("*** error: find `%s' gave %p not %p\n", - line[i], (void *)w, (void *)flag[i]); - else if (w && w->i != i) - printf("*** error: find sym for `%s' gives index %i not %i\n", - line[i], w->i, i); - } break; - - case 1: { - unsigned f; - sym_word *w; - - printf("+ %s\n", line[i]); - - w = sym_find(&tbl, line[i], -1, sizeof(sym_word), &f); - if (f) - { - if (w != flag[i]) - printf("*** error: create `%s' gave %p not %p\n", - line[i], (void *)w, (void *)flag[i]); - else if (w && w->i != i) - printf("*** error: create sym for `%s' gives index %i not %i\n", - line[i], w->i, i); - } - else - { - if (flag[i]) - printf("*** error: create `%s' gave new block, should be %p\n", - line[i], (void *)flag[i]); - else { - flag[i] = w; - w->i = i; - entries++; - } - } - } break; - - case 2: { - sym_iter it; - sym_word *w, **ntbl; - int v; - - if (!entries) - break; - v = (rand() % entries) == 0; - if (!v) - break; - - printf(".\n"); - - ntbl = xmalloc(sz * sizeof(sym_word *)); - memcpy(ntbl, flag, sz * sizeof(sym_word *)); - sym_mkiter(&it, &tbl); - - while ((w = sym_next(&it)) != 0) { - if (ntbl[w->i] == 0) - printf("*** error: iterate returned duff item %s\n", SYM_NAME(w)); - else { - printf(": %s\n", SYM_NAME(w)); - ntbl[w->i] = 0; - } - } - - for (i = 0; i < sz; i++) - if (ntbl[i]) printf("*** error: iterate didn't return item %s\n", - SYM_NAME(ntbl[i])); - free(ntbl); - } break; - - case 3: { - sym_base *b; - int v = rand() & 255 ? 0 : 1; - break; - - printf("dump\n"); - - for (i = 0; i <= tbl.b.mask; i++) { - if (!tbl.b.v[i]) continue; - if (v) printf(" %i: ", i); - b = (sym_base *)tbl.b.v[i]; - while (b) { - if ((b->b.hash & tbl.b.mask) != i) - printf("*** error: bad hash value found"); - if (v) printf("`%s'(%08lx:%lu) ", - line[((sym_word *)b)->i], - b->b.hash, - b->b.hash & tbl.b.mask); - b = (sym_base *)b->b.next; - } - if (v) putchar('\n'); - } - } break; - - case 4: { - if (flag[i]) { - printf("- %s\n", SYM_NAME(&flag[i]->base)); - if ((rand() & 1023) == 0) { - putchar('-'); fflush(stdout); - } - sym_remove(&tbl, flag[i]); - flag[i] = 0; - entries--; - } - } break; - } - - } - - return (0); -} - -#endif - /*----- That's all, folks -------------------------------------------------*/