-/*----- Symbol table test code --------------------------------------------*/
-
-#ifdef TEST_RIG
-
-#include <errno.h>
-#include <time.h>
-
-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;
-
- /* --- Initialise 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_createTable(&tbl);
-
- for (;;) {
- i = (unsigned)rand() % sz;
-
- switch (rand() % 5)
- {
- case 0: {
- sym_word *w;
-
- printf("find `%s'\n", line[i]);
- if ((rand() & 1023) == 0) {
- putchar('.'); fflush(stdout);
- }
-
- 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("create `%s'\n", line[i]);
- if ((rand() & 1023) == 0) {
- putchar('+'); fflush(stdout);
- }
-
- 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("\niterated %i entries\n", entries);
- break;
-
- printf("iterate\n");
-
- ntbl = xmalloc(sz * sizeof(sym_word *));
- memcpy(ntbl, flag, sz * sizeof(sym_word *));
- sym_createIter(&it, &tbl);
-
- while ((w = sym_next(&it)) != 0) {
- if (ntbl[w->i] == 0)
- printf("*** error: iterate returned duff item %i\n", w->i);
- else
- ntbl[w->i] = 0;
- }
-
- for (i = 0; i < sz; i++)
- if (ntbl[i]) printf("*** error: iterate didn't return item %i\n",
- i);
- free(ntbl);
- } break;
-
- case 3: {
- sym_base *b;
- int v = rand() & 255 ? 0 : 1;
- break;
-
- printf("dump\n");
-
- for (i = 0; i <= tbl.mask; i++) {
- if (!tbl.a[i]) continue;
- if (v) printf(" %i: ", i);
- b = tbl.a[i];
- while (b) {
- if ((b->hash & tbl.mask) != i)
- printf("*** error: bad hash value found");
- if (v) printf("`%s'(%08lx:%lu) ",
- line[((sym_word *)b)->i],
- b->hash,
- b->hash & tbl.mask);
- b = b->next;
- }
- if (v) putchar('\n');
- }
- } break;
-
- case 4: {
- if (flag[i]) {
- printf("remove `%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
-