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

  1. /*
  2.  * This file is a (rather silly) demonstration of the use of the
  3.  * C Dynamic Object library.  It is a also reasonably thorough test
  4.  * of the library (except that it only tests it with one data size).
  5.  *
  6.  * There are no restrictions on this code; however, if you make any
  7.  * changes, I request that you document them so that I do not get
  8.  * credit or blame for your modifications.
  9.  *
  10.  * Written by Barr3y Jaspan, Student Information Processing Board (SIPB)
  11.  * and MIT-Project Athena, 1989.
  12.  */
  13.  
  14. #include <stdio.h>
  15.  
  16. #include "dyn.h"
  17.  
  18. static char random_string[] = "This is a random string.";
  19. static char insert1[] = "This will be put at the beginning.";
  20. static char insert2[] = "(parenthetical remark!) ";
  21. static char insert3[] = "  This follows the random string.";
  22.  
  23. main(argc, argv)
  24.    int    argc;
  25.    char    **argv;
  26. {
  27.      DynObject    obj;
  28.      int    i, s;
  29.      char    d, *data;
  30.  
  31.      obj = DynCreate(sizeof(char), 8);
  32.      if (! obj) {
  33.       fprintf(stderr, "test: create failed.\n");
  34.       exit(1);
  35.      }
  36.      
  37.      DynDebug(obj, 1);
  38.      DynParanoid(obj, 1);
  39.  
  40.      if (DynGet(obj, -5) || DynGet(obj, 0) || DynGet(obj, 1000)) {
  41.       fprintf(stderr, "test: Get did not fail when it should have.\n");
  42.       exit(1);
  43.      }
  44.  
  45.      if (DynDelete(obj, -1) != DYN_BADINDEX ||
  46.      DynDelete(obj, 0) != DYN_BADINDEX ||
  47.      DynDelete(obj, 100) != DYN_BADINDEX) {
  48.       fprintf(stderr, "test: Delete did not fail when it should have.\n");
  49.       exit(1);
  50.      }
  51.  
  52.      printf("Size of empty object: %d\n", DynSize(obj));
  53.  
  54.      for (i=0; i<14; i++) {
  55.       d = (char) i;
  56.       if (DynAdd(obj, &d) != DYN_OK) {
  57.            fprintf(stderr, "test: Adding %d failed.\n", i);
  58.            exit(1);
  59.       }
  60.      }
  61.  
  62.      if (DynAppend(obj, random_string, strlen(random_string)+1) != DYN_OK) {
  63.       fprintf(stderr, "test: appending array failed.\n");
  64.       exit(1);
  65.      }
  66.      
  67.      if (DynDelete(obj, DynHigh(obj) / 2) != DYN_OK) {
  68.       fprintf(stderr, "test: deleting element failed.\n");
  69.       exit(1);
  70.      }
  71.  
  72.      if (DynDelete(obj, DynHigh(obj) * 2) == DYN_OK) {
  73.       fprintf(stderr, "test: delete should have failed here.\n");
  74.       exit(1);
  75.      }
  76.  
  77.      d = 200;
  78.      if (DynAdd(obj, &d) != DYN_OK) {
  79.       fprintf(stderr, "test: Adding %d failed.\n", i);
  80.       exit(1);
  81.      }
  82.  
  83.      data = (char *) DynGet(obj, 0);
  84.      s = DynSize(obj);
  85.      for (i=0; i < s; i++)
  86.       printf("Element %d is %d.\n", i, (unsigned char) data[i]);
  87.  
  88.      data = (char *) DynGet(obj, 13);
  89.      printf("Element 13 is %d.\n", (unsigned char) *data);
  90.  
  91.      data = (char *) DynGet(obj, DynSize(obj));
  92.      if (data) {
  93.       fprintf(stderr, "DynGet did not return NULL when it should have.\n");
  94.       exit(1);
  95.      }
  96.  
  97.      printf("This should be the random string: \"%s\"\n", DynGet(obj, 14));
  98.  
  99.      if (DynInsert(obj, -1, "foo", 4) != DYN_BADINDEX ||
  100.      DynInsert(obj, DynSize(obj) + 1, "foo", 4) != DYN_BADINDEX ||
  101.      DynInsert(obj, 0, "foo", -1) != DYN_BADVALUE) {
  102.       fprintf(stderr, "DynInsert did not fail when it should have.\n");
  103.       exit(1);
  104.      }
  105.  
  106.      if (DynInsert(obj, DynSize(obj) - 2, insert3, strlen(insert3) +
  107.            1) != DYN_OK) {
  108.       fprintf(stderr, "DynInsert to end failed.\n");
  109.       exit(1);
  110.      }  
  111.  
  112.      if (DynInsert(obj, 19, insert2, strlen(insert2)) != DYN_OK) {
  113.       fprintf(stderr, "DynInsert to middle failed.\n");
  114.       exit(1);
  115.      }
  116.      
  117.      if (DynInsert(obj, 0, insert1, strlen(insert1)+1) != DYN_OK) {
  118.       fprintf(stderr, "DynInsert to start failed.\n");
  119.       exit(1);
  120.      }    
  121.  
  122.      printf("A new random string: \"%s\"\n", DynGet(obj, 14 +
  123.                             strlen(insert1) + 1));
  124.      printf("This was put at the beginning: \"%s\"\n", DynGet(obj, 0));
  125.  
  126.      DynDestroy(obj);
  127.  
  128.      return 0;
  129. }
  130.