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).
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.
10 * Written by Barr3y Jaspan, Student Information Processing Board (SIPB)
11 * and MIT-Project Athena, 1989.
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.";
32 obj = DynCreate(sizeof(char), 8);
34 fprintf(stderr, "test: create failed.\n");
41 if (DynGet(obj, -5) || DynGet(obj, 0) || DynGet(obj, 1000)) {
42 fprintf(stderr, "test: Get did not fail when it should have.\n");
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");
53 printf("Size of empty object: %d\n", DynSize(obj));
55 for (i=0; i<14; i++) {
57 if (DynAdd(obj, &d) != DYN_OK) {
58 fprintf(stderr, "test: Adding %d failed.\n", i);
63 if (DynAppend(obj, random_string, strlen(random_string)+1) != DYN_OK) {
64 fprintf(stderr, "test: appending array failed.\n");
68 if (DynDelete(obj, DynHigh(obj) / 2) != DYN_OK) {
69 fprintf(stderr, "test: deleting element failed.\n");
73 if (DynDelete(obj, DynHigh(obj) * 2) == DYN_OK) {
74 fprintf(stderr, "test: delete should have failed here.\n");
79 if (DynAdd(obj, &d) != DYN_OK) {
80 fprintf(stderr, "test: Adding %d failed.\n", i);
84 data = (char *) DynGet(obj, 0);
87 printf("Element %d is %d.\n", i, (unsigned char) data[i]);
89 data = (char *) DynGet(obj, 13);
90 printf("Element 13 is %d.\n", (unsigned char) *data);
92 data = (char *) DynGet(obj, DynSize(obj));
94 fprintf(stderr, "DynGet did not return NULL when it should have.\n");
98 printf("This should be the random string: \"%s\"\n",
99 (char *) DynGet(obj, 14));
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");
108 if (DynInsert(obj, DynSize(obj) - 2, insert3, strlen(insert3) +
110 fprintf(stderr, "DynInsert to end failed.\n");
114 if (DynInsert(obj, 19, insert2, strlen(insert2)) != DYN_OK) {
115 fprintf(stderr, "DynInsert to middle failed.\n");
119 if (DynInsert(obj, 0, insert1, strlen(insert1)+1) != DYN_OK) {
120 fprintf(stderr, "DynInsert to start failed.\n");
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));