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