]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/libdyn/dyntest.c
new upstream version
[1ts-debian.git] / zephyr / libdyn / dyntest.c
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 #include <string.h>
16
17 #include "dyn.h"
18
19 static char random_string[] = "This is a random string.";
20 static char insert1[] = "This will be put at the beginning.";
21 static char insert2[] = "(parenthetical remark!) ";
22 static char insert3[] = "  This follows the random string.";
23
24 int
25 main(int argc,
26      char **argv)
27 {
28      DynObject  obj;
29      int        i, s;
30      char       d, *data;
31
32      obj = DynCreate(sizeof(char), 8);
33      if (! obj) {
34           fprintf(stderr, "test: create failed.\n");
35           exit(1);
36      }
37      
38      DynDebug(obj, 1);
39      DynParanoid(obj, 1);
40
41      if (DynGet(obj, -5) || DynGet(obj, 0) || DynGet(obj, 1000)) {
42           fprintf(stderr, "test: Get did not fail when it should have.\n");
43           exit(1);
44      }
45
46      if (DynDelete(obj, -1) != DYN_BADINDEX ||
47          DynDelete(obj, 0) != DYN_BADINDEX ||
48          DynDelete(obj, 100) != DYN_BADINDEX) {
49           fprintf(stderr, "test: Delete did not fail when it should have.\n");
50           exit(1);
51      }
52
53      printf("Size of empty object: %d\n", DynSize(obj));
54
55      for (i=0; i<14; i++) {
56           d = (char) i;
57           if (DynAdd(obj, &d) != DYN_OK) {
58                fprintf(stderr, "test: Adding %d failed.\n", i);
59                exit(1);
60           }
61      }
62
63      if (DynAppend(obj, random_string, strlen(random_string)+1) != DYN_OK) {
64           fprintf(stderr, "test: appending array failed.\n");
65           exit(1);
66      }
67      
68      if (DynDelete(obj, DynHigh(obj) / 2) != DYN_OK) {
69           fprintf(stderr, "test: deleting element failed.\n");
70           exit(1);
71      }
72
73      if (DynDelete(obj, DynHigh(obj) * 2) == DYN_OK) {
74           fprintf(stderr, "test: delete should have failed here.\n");
75           exit(1);
76      }
77
78      d = 200;
79      if (DynAdd(obj, &d) != DYN_OK) {
80           fprintf(stderr, "test: Adding %d failed.\n", i);
81           exit(1);
82      }
83
84      data = (char *) DynGet(obj, 0);
85      s = DynSize(obj);
86      for (i=0; i < s; i++)
87           printf("Element %d is %d.\n", i, (unsigned char) data[i]);
88
89      data = (char *) DynGet(obj, 13);
90      printf("Element 13 is %d.\n", (unsigned char) *data);
91
92      data = (char *) DynGet(obj, DynSize(obj));
93      if (data) {
94           fprintf(stderr, "DynGet did not return NULL when it should have.\n");
95           exit(1);
96      }
97
98      printf("This should be the random string: \"%s\"\n",
99             (char *) DynGet(obj, 14));
100
101      if (DynInsert(obj, -1, "foo", 4) != DYN_BADINDEX ||
102          DynInsert(obj, DynSize(obj) + 1, "foo", 4) != DYN_BADINDEX ||
103          DynInsert(obj, 0, "foo", -1) != DYN_BADVALUE) {
104           fprintf(stderr, "DynInsert did not fail when it should have.\n");
105           exit(1);
106      }
107
108      if (DynInsert(obj, DynSize(obj) - 2, insert3, strlen(insert3) +
109                    1) != DYN_OK) {
110           fprintf(stderr, "DynInsert to end failed.\n");
111           exit(1);
112      }  
113
114      if (DynInsert(obj, 19, insert2, strlen(insert2)) != DYN_OK) {
115           fprintf(stderr, "DynInsert to middle failed.\n");
116           exit(1);
117      }
118      
119      if (DynInsert(obj, 0, insert1, strlen(insert1)+1) != DYN_OK) {
120           fprintf(stderr, "DynInsert to start failed.\n");
121           exit(1);
122      }  
123
124      printf("A new random string: \"%s\"\n",
125             (char *) DynGet(obj, 14 + strlen(insert1) + 1));
126      printf("This was put at the beginning: \"%s\"\n",
127             (char *) DynGet(obj, 0));
128
129      DynDestroy(obj);
130
131      return 0;
132 }