home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1868 / testsp.c < prev   
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.3 KB  |  119 lines

  1. /*
  2. From moraes@csri.toronto.edu Mon Apr  3 15:16:23 1989
  3. From: Mark Moraes <moraes@csri.toronto.edu>
  4. To: daveb@rtech.uucp
  5. Subject: small fix for sptree
  6. Date:     Mon, 3 Apr 89 02:18:23 EDT
  7.  
  8. Hi.
  9.  
  10. I enclose a diff to fix a problem in spaux.c, without which it dumps
  11. code when deleting an item.
  12.  
  13. I also enclose a little example program I used to test some of the
  14. tree functions. (not the queue ones though) Do you have a better test?
  15.  
  16. [ nope.  I'll use/hack the one he gave me -dB ]
  17. */
  18.  
  19. #include <stdio.h>
  20. #define MAXSTR 512
  21.  
  22. #include "sptree.h"
  23.  
  24. char *
  25. strsave( s )
  26. char *s;
  27. {
  28.     char *emalloc();
  29.     char *strcpy();
  30.     return ( strcpy( emalloc( strlen(s) + 1 ), s ) );
  31. }
  32.  
  33. char *
  34. emalloc(n)
  35. int n;
  36. {
  37.     char *malloc();
  38.     char *p = malloc( n );
  39.     if( p )
  40.         return( p );
  41.  
  42.     fprintf(stderr, "emalloc: can't allocate %d bytes\n", n );
  43.     perror("possible cause");
  44.     exit( 1 );
  45.     /*NOTREACHED*/
  46. }
  47.  
  48.  
  49. #define prompt() if(prompt_p) printf("testsp>"); else ;
  50.  
  51. main()
  52. {
  53.     char buf[MAXSTR];
  54.      char s1[8], s2[MAXSTR], s3[MAXSTR];
  55.     SPBLK *result;
  56.     int prompt_p;
  57.     SPTREE *sp;
  58.     int prnode();
  59.  
  60.     sp = spinit();
  61.     prompt_p = isatty(fileno(stdin));
  62.     prompt();
  63.     while(fgets(buf, sizeof(buf), stdin) != NULL) {
  64.         sscanf(buf, " %s %s", s1, s2);
  65.         switch (*s1) {
  66.         case 'a':
  67.             sscanf(buf, " %*s %*s %s", s3);
  68.             spinstall(strsave(s2), strsave(s3), NULL, sp);
  69.             break;
  70.         case 's':
  71.             if ((result = splookup(s2, sp)) != NULL)
  72.                 printf("found %s\n", result->data);
  73.             else
  74.                 printf("no match\n");
  75.             break;
  76.         case 'd':
  77.             if ((result = splookup(s2, sp)) != NULL)
  78.                 spdelete(result, sp);
  79.             else
  80.                 printf("no match\n");
  81.             break;
  82.         case 'l':
  83.             spscan(prnode, NULL, sp); 
  84.             break;
  85.         case 'p':
  86.             spshow(sp);
  87.             break;
  88.         case 'q':
  89.             exit( 0 );
  90.             break;
  91.         default:
  92.             printf(
  93.     "'a string1 string2' inserts datum (string2) keyed by string1\n");
  94.             printf(
  95.     "'d string1' deletes the datum associated with string1 if any\n");
  96.             printf(
  97.     "'s string1' prints the datum associated with string1 if any\n");
  98.             printf(
  99.     "'l' lists the elements of the tree in spscan() order\n");
  100.             printf(
  101.     "'p' prints the shape and contents of the tree with spshow()\n");
  102.             break;
  103.         }
  104.         prompt();
  105.     }
  106.     printf("\n");
  107. #ifdef PRINT
  108.     printf("Stats = %s\n", spstats(sp));
  109. #endif
  110. }
  111.  
  112. int
  113. prnode(spblk)
  114. SPBLK *spblk;
  115. {
  116.     printf("Key = \"%s\", Data = \"%s\"\n", spblk->key, spblk->data);
  117. }
  118.  
  119.