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 * 2002-07-17 Moved here from util/dyn/test.c. Older changes described 14 * at end of file; for newer changes see ChangeLog. 15 */ 16 17 #include <stdio.h> 18 #include <string.h> 19 #ifdef USE_DBMALLOC 20 #include <sys/stdtypes.h> 21 #include <malloc.h> 22 #endif 23 #include <stdlib.h> 24 25 #include "dyn.h" 26 27 static char random_string[] = "This is a random string."; 28 static char insert1[] = "This will be put at the beginning."; 29 static char insert2[] = "(parenthetical remark!) "; 30 static char insert3[] = " This follows the random string."; 31 32 int 33 main(argc, argv) 34 /*@unused@*/int argc; 35 /*@unused@*/char **argv; 36 { 37 /*@-exitarg@*/ 38 DynObject obj; 39 int i, s; 40 char d, *data; 41 42 #ifdef _DEBUG_MALLOC_INC 43 union dbmalloptarg arg; 44 unsigned long hist1, hist2, o_size, c_size; 45 #endif 46 47 #ifdef _DEBUG_MALLOC_INC 48 arg.i = 0; 49 dbmallopt(MALLOC_ZERO, &arg); 50 dbmallopt(MALLOC_REUSE, &arg); 51 52 o_size = malloc_inuse(&hist1); 53 #endif 54 55 /*@+matchanyintegral@*/ 56 obj = DynCreate(sizeof(char), -8); 57 if (! obj) { 58 fprintf(stderr, "test: create failed.\n"); 59 exit(1); 60 } 61 62 if(DynDebug(obj, 1) != DYN_OK) { 63 fprintf(stderr, "test: setting paranoid failed.\n"); 64 exit(1); 65 } 66 if(DynParanoid(obj, 1) != DYN_OK) { 67 fprintf(stderr, "test: setting paranoid failed.\n"); 68 exit(1); 69 } 70 71 72 if ((DynGet(obj, -5) != NULL) || 73 (DynGet(obj, 0) != NULL) || (DynGet(obj, 1000) != NULL)) { 74 fprintf(stderr, "test: Get did not fail when it should have.\n"); 75 exit(1); 76 } 77 78 if (DynDelete(obj, -1) != DYN_BADINDEX || 79 DynDelete(obj, 0) != DYN_BADINDEX || 80 DynDelete(obj, 100) != DYN_BADINDEX) { 81 fprintf(stderr, "test: Delete did not fail when it should have.\n"); 82 exit(1); 83 } 84 85 printf("Size of empty object: %d\n", DynSize(obj)); 86 87 for (i=0; i<14; i++) { 88 d = (char) i; 89 if (DynAdd(obj, &d) != DYN_OK) { 90 fprintf(stderr, "test: Adding %d failed.\n", i); 91 exit(1); 92 } 93 } 94 95 if (DynAppend(obj, random_string, strlen(random_string)+1) != DYN_OK) { 96 fprintf(stderr, "test: appending array failed.\n"); 97 exit(1); 98 } 99 100 if (DynDelete(obj, DynHigh(obj) / 2) != DYN_OK) { 101 fprintf(stderr, "test: deleting element failed.\n"); 102 exit(1); 103 } 104 105 if (DynDelete(obj, DynHigh(obj) * 2) == DYN_OK) { 106 fprintf(stderr, "test: delete should have failed here.\n"); 107 exit(1); 108 } 109 110 d = '\200'; 111 if (DynAdd(obj, &d) != DYN_OK) { 112 fprintf(stderr, "test: Adding %d failed.\n", i); 113 exit(1); 114 } 115 116 data = (char *) DynGet(obj, 0); 117 if(data == NULL) { 118 fprintf(stderr, "test: getting object 0 failed.\n"); 119 exit(1); 120 } 121 s = DynSize(obj); 122 for (i=0; i < s; i++) 123 printf("Element %d is %d.\n", i, (int) data[i]); 124 125 data = (char *) DynGet(obj, 13); 126 if(data == NULL) { 127 fprintf(stderr, "test: getting element 13 failed.\n"); 128 exit(1); 129 } 130 printf("Element 13 is %d.\n", (int) *data); 131 132 data = (char *) DynGet(obj, DynSize(obj)); 133 if (data) { 134 fprintf(stderr, "DynGet did not return NULL when it should have.\n"); 135 exit(1); 136 } 137 138 data = DynGet(obj, 14); 139 if(data == NULL) { 140 fprintf(stderr, "test: getting element 13 failed.\n"); 141 exit(1); 142 } 143 printf("This should be the random string: \"%s\"\n", data); 144 145 if (DynInsert(obj, -1, "foo", 4) != DYN_BADINDEX || 146 DynInsert(obj, DynSize(obj) + 1, "foo", 4) != DYN_BADINDEX || 147 DynInsert(obj, 0, "foo", -1) != DYN_BADVALUE) { 148 fprintf(stderr, "DynInsert did not fail when it should have.\n"); 149 exit(1); 150 } 151 152 if (DynInsert(obj, DynSize(obj) - 2, insert3, strlen(insert3) + 153 1) != DYN_OK) { 154 fprintf(stderr, "DynInsert to end failed.\n"); 155 exit(1); 156 } 157 158 if (DynInsert(obj, 19, insert2, strlen(insert2)) != DYN_OK) { 159 fprintf(stderr, "DynInsert to middle failed.\n"); 160 exit(1); 161 } 162 163 if (DynInsert(obj, 0, insert1, strlen(insert1)+1) != DYN_OK) { 164 fprintf(stderr, "DynInsert to start failed.\n"); 165 exit(1); 166 } 167 168 data = DynGet(obj, 14 + strlen(insert1) + 1); 169 if (data == NULL) { 170 fprintf(stderr, "DynGet of 14+strelen(insert1) failed.\n"); 171 exit(1); 172 173 } 174 printf("A new random string: \"%s\"\n", data); 175 176 data = DynGet(obj, 0); 177 if (data == NULL) { 178 fprintf(stderr, "DynGet of 0 failed.\n"); 179 exit(1); 180 181 } 182 printf("This was put at the beginning: \"%s\"\n", data); 183 184 if(DynDestroy(obj) != DYN_OK) { 185 fprintf(stderr, "test: destroy failed.\n"); 186 exit(1); 187 } 188 189 #ifdef _DEBUG_MALLOC_INC 190 c_size = malloc_inuse(&hist2); 191 if (o_size != c_size) { 192 printf("\n\nIgnore a single unfreed malloc segment " 193 "(stdout buffer).\n\n"); 194 malloc_list(2, hist1, hist2); 195 } 196 #endif 197 198 printf("All tests pass\n"); 199 200 return 0; 201 } 202 203 /* Old change log, as it relates to source code; build system stuff 204 discarded. 205 206 2001-04-25 Ezra Peisach <epeisach@mit.edu> 207 208 * test.c: Always include stdlib.h 209 210 * test.c: Check the return values of all library calls. 211 212 2000-11-09 Ezra Peisach <epeisach@mit.edu> 213 214 * test.c: Include string,h, stdlib.h. 215 216 */ 217