home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / ndx303.zip / TEST.CPP < prev    next >
C/C++ Source or Header  |  1993-02-08  |  3KB  |  119 lines

  1. //        Test NDX v3.0
  2.  
  3. #include    "ndx.h"
  4. #include    <stdlib.h>
  5.  
  6. void Index::print(char *filename)
  7. {
  8.     Node *node = new Node;
  9.     unsigned long *blocks;
  10.     int cblock = 0, last_block = 1;
  11.     FILE *file = fopen(filename? filename: "CON", "w");
  12.  
  13.     blocks =(unsigned long *)calloc(20, sizeof(long));
  14.     *blocks = root;
  15.     if (! *blocks) {
  16.         fputs("Empty index\n", file);
  17.         return;
  18.     }
  19.     for (; cblock < last_block; cblock++) {
  20.         Key *kp, *end;
  21.  
  22.         read(blocks[cblock], node, nodesize);
  23.         kp = (Key *)node->keys;
  24.         end = (Key *)(node->keys + node->endkeys);
  25.  
  26.         fprintf(file, "\nBlock at %lu\n", blocks[cblock]);
  27.         for (; kp < end; (char *)kp += 2*sizeof(long) + strlen(kp->data)
  28.             + 1) {
  29.             if (kp->lson)
  30.                 blocks[last_block++] = kp->lson;
  31.             fprintf(file, "         %-8lu %-8s %-8lu\n",
  32.                 kp->lson, kp->data, kp->offset);
  33.         }
  34.         if (kp->lson) {
  35.             blocks[last_block++] = kp->lson;
  36.             fprintf(file, "         %-8lu\n", kp->lson);
  37.         }
  38.     }
  39.     free(node);
  40.     free(blocks);
  41.     fclose(file);
  42. }
  43.  
  44. Index *index;
  45. Key *entry = new Key;
  46. int seed = 16383;
  47. unsigned rval[10000];
  48.  
  49. void output()
  50. {
  51.     index->print("t.tmp");
  52. }
  53.  
  54. void check(int i, int j, int last)
  55. {
  56.     while (j--) {
  57.         printf("%5u\r", j);
  58.         if (! i++) index->first(last);
  59.         else if (! (last? index->prev(): index->next())) {
  60.             printf("Key <%u, %u> not found\n", last? j + 1: i,
  61.                     rval[last? j: i - 1]);
  62.             output();
  63.             exit(-1);
  64.             }
  65.         }
  66.     printf("All keys accessible on %s scan\n", last? "backward":
  67.             "forward");
  68. }
  69.  
  70. main(int argc, char **argv)
  71. {
  72.     int n = atoi(argv[1]), i, j, m = (argc >= 2? atoi(argv[2]): 0);
  73.     int fault = 0;
  74.  
  75.     if (argc < 2) {
  76.         puts("\n\tUsage: NDX <number of keys> [<modulus>] [print]\n\n"
  77.             "\tAny third argument causes the index to be printed\n"
  78.             "\tUse of the second argument exercises the ISAM feature\n"
  79.             "\n\tTry NDX 200 10 0, for instance\n");
  80.         exit(-1);
  81.         }
  82.     srand(seed);
  83.     for (i = 0; i < n; i++) rval[i] = m? rand() % m: rand() + 1;
  84.     index = new Index("T.NDX", FIFO);
  85.     entry->lson = 0L;
  86.     for (i = 1; i <= n; i++) {
  87.         sprintf(entry->data, "%u", rval[i - 1]);
  88.         entry->offset = i;
  89.         index->insert(entry->data, entry->offset);
  90.         }
  91.     puts("Keys written");
  92.     if (!index->isvalid()) return puts("Index is not valid");
  93.     delete index;
  94.     index = new Index("t.ndx");
  95.     check(0, n, 0);
  96.     check(0, n, 1);
  97.     if (argc > 3) {index->print(NULL); return 0; }
  98.     for (i = 1; i <= n; i++) {
  99.         sprintf(entry->data, "%u", rval[i - 1]);
  100.         entry->offset = i;
  101.         if (! index->remove(entry->data, entry->offset)) {
  102.             printf("Delete failure: record %-u, data %-s\a\a\n", i, entry->data);
  103.             output();
  104.             fault++;
  105.             break;
  106.             }
  107.           else if (index->findkeyrec(entry->data, entry->offset)) {
  108.             printf("Key <%u, %u> not deleted!\n", i, rval[i - 1]);
  109.             output();
  110.             fault++;
  111.             break;
  112.             }
  113.     }
  114.     output();
  115.     delete index;
  116.     if (! fault) puts("All keys successfully deleted");
  117.     return fault;
  118. }
  119.