-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.c
196 lines (151 loc) · 3.5 KB
/
test2.c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdio.h>
#include <stdlib.h>
#ifdef ARRAY
#include "array.h"
#elif defined LIST
#include "list.h"
#else
#error You must compile this code with either the -DLIST or the -DARRAY option.
#include <stop>
#endif
/* This program will conduct a bunch of tests on either an array or a list.
*/
#define CAPACITY 100 // total capacity of the array
#define NEL 20 // number of elements of the series to include
void report( char *message, struct Performance *perf )
{
printf( "%s", message );
printf( " reads: %5d\n", perf->reads );
printf( " writes: %5d\n", perf->writes );
printf( " mallocs: %5d\n", perf->mallocs );
printf( " frees: %5d\n", perf->frees );
perf->reads = 0;
perf->writes = 0;
perf->mallocs = 0;
perf->frees = 0;
}
int main( int argc, char **argv )
{
int i;
double number;
struct Performance *perf;
#ifdef ARRAY
struct Array *array;
#endif
#ifdef LIST
struct Node *list;
#endif
perf = newPerformance();
#ifdef ARRAY
array = newArray( perf, sizeof( double ), CAPACITY );
#endif
#ifdef LIST
list = NULL;
#endif
report( "after new\n", perf );
printf( "\n\n" );
// fill data structure with doubles, numbered from 0 to NEL-1
for (i=0;i<NEL;i++)
{
number = i;
#ifdef ARRAY
appendItem( perf, array, &number );
#endif
#ifdef LIST
appendItem( perf, &list, &number, sizeof(number) );
#endif
}
report( "after appendItem x 20\n", perf );
printf( "\n\n" );
printf( "Initial data:\n" );
for (i=0;i<NEL;i++)
{
#ifdef ARRAY
readItem( perf, array, i, &number );
#endif
#ifdef LIST
readItem( perf, &list, i, &number, sizeof(number) );
#endif
printf( "%d %f\n", i, number );
}
report( "after readItem x 20\n", perf );
printf( "\n\n" );
// now insert the value 7.5 at index 8
number = 7.5;
#ifdef ARRAY
insertItem( perf, array, 8, &number );
#endif
#ifdef LIST
insertItem( perf, &list, 8, &number, sizeof(number) );
#endif
report( "after insertItem =7.5 at index 8\n", perf );
printf( "\n\n" );
printf( "After insert:\n" );
for (i=0;i<NEL+1;i++)
{
#ifdef ARRAY
readItem( perf, array, i, &number );
#endif
#ifdef LIST
readItem( perf, &list, i, &number, sizeof(number) );
#endif
printf( "%d %f\n", i, number );
}
report( "after readItem x 21\n", perf );
printf( "\n\n" );
// now prepend the value -1.0
number = -1.0;
#ifdef ARRAY
prependItem( perf, array, &number );
#endif
#ifdef LIST
prependItem( perf, &list, &number, sizeof(number) );
#endif
report( "after prependItem\n", perf );
printf( "\n\n" );
printf( "After prepend:\n" );
for (i=0;i<NEL+2;i++)
{
#ifdef ARRAY
readItem( perf, array, i, &number );
#endif
#ifdef LIST
readItem( perf, &list, i, &number, sizeof(number) );
#endif
printf( "%d %f\n", i, number );
}
report( "after readItem x 22\n", perf );
printf( "\n\n" );
// delete element 16
#ifdef ARRAY
deleteItem( perf, array, 16 );
#endif
#ifdef LIST
deleteItem( perf, &list, 16 );
#endif
report( "after deleteItem\n", perf );
printf( "\n\n" );
printf( "After delete:\n" );
for (i=0;i<NEL+1;i++)
{
#ifdef ARRAY
readItem( perf, array, i, &number );
#endif
#ifdef LIST
readItem( perf, &list, i, &number, sizeof(number) );
#endif
printf( "%d %f\n", i, number );
}
report( "after readItem x 21\n", perf );
printf( "\n\n" );
// free the data structure
#ifdef ARRAY
freeArray( perf, array );
#endif
#ifdef LIST
freeList( perf, &list );
#endif
report( "after free\n", perf );
printf( "\n\n" );
free( perf );
}