-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathofm-testing.c
264 lines (212 loc) · 5.67 KB
/
ofm-testing.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <time.h>
#include "OrderedFileMaintenance.c"
void testInsertNearRandom(int size){
printf("Testing Insert Sorted Random Near for OFM:\n");
clock_t start, end;
double cpu_time_used = 0;
list_t list;
setup(&list);
insert_first(&list, 0);
int* item = get_first(&list);
for(int i=0; i<size-1; i++){
int random = rand()%2;
if(random==1){
item = insert_after(&list, i+1, item);
}
else{
item = insert_before(&list, i+1, item);
}
}
printf("%d, %f \n",size, cpu_time_used);
free(list.items);
}
void testMax(int size){
clock_t start, end;
double cpu_time_used = 0;
int max_item = -1;
list_t list;
setup(&list);
for(int i=0; i<size; i++){
int item = rand()%100;
insert_sorted(&list, item);
}
start = clock();
int max_elem = ofm_max(&list);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf(" max:%d size:%d, time:%f\n", max_elem, size, cpu_time_used);
free(list.items);
}
// inserts N elements sequentially into ofm.
// prints out time it takes for N operations
void testSequentialInsertOFM(list_t* list, int size){
printf("Initial List: \n");
print_array(list);
clock_t start, end;
double cpu_time_used = 0;
insert_first(list, 0);
int* item = get_first(list);
for(int i=0; i<size-1; i++){
start = clock();
item = insert_after(list, i+1, item);
end = clock();
cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
}
print_array(list);
printf("%d sequential insertions, time: %f \n", size, cpu_time_used);
}
void testInsertSortedRandom(int size){
//printf("Testing Insert Sorted Random for OFM:\n");
clock_t start, end;
double cpu_time_used = 0;
list_t list;
setup(&list);
start = clock();
for(int i=0; i<size; i++){
int item = rand()%100;
insert_sorted(&list, item);
}
end = clock();
cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
// verify correctness
int index = 0;
int prev_val = -1;
while(index<size){
int val = list.items[index];
if(val!=-1){
if(val<prev_val){
print_array(&list);
printf("Insert Sorted is not working!\n");
break;
}
prev_val = val;
}
index++;
}
//printf("%d random insertions into an ordered list of size:%d, time: %f \n", size, size, cpu_time_used);
printf("%d, %f \n",size, cpu_time_used);
free(list.items);
}
//
void testInsertSortedVarious(){
printf("Insert Sorted Random Sizes: 10, 100, 1,000 , 10,000, 100,000\n");
testInsertSortedRandom(10);
testInsertSortedRandom(100);
testInsertSortedRandom(1000);
testInsertSortedRandom(10000);
testInsertSortedRandom(100000);
testInsertSortedRandom(1000000);
}
void testInsertSortedNearVarious(){
printf("Insert Sorted Near Sizes: 10, 100, 1,000 , 10,000, 100,000\n");
// testInsertNearRandom(10);
// testInsertNearRandom(100);
testInsertNearRandom(1000);
//testInsertNearRandom(10000);
//testInsertNearRandom(100000);
//testInsertNearRandom(1000000);
}
void testInsertSortedReverse(int size){
printf("Testing Insert Sorted Reverse Sequential for OFM:\n");
clock_t start, end;
double cpu_time_used = 0;
list_t list;
setup(&list);
start = clock();
for(int i=size-1; i>=0; i--){
insert_sorted(&list, i);
}
end = clock();
cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
printf("finished inserting\n");
// verify correctness
int index = 0;
int prev_val = -1;
while(index<size){
int val = list.items[index];
if(val!=-1){
// assume we only insert positive values into the ofm
if(val<prev_val){
print_array(&list);
printf("Insert Sorted is not working!\n");
break;
}
prev_val = val;
}
index++;
}
printf("%d random insertions into an ordered list of size:%d, time: %f \n", size, size, cpu_time_used);
free(list.items);
}
// to be called after sequential insert, delete some of the elements.
void testSequentialDelete(list_t *list, int size){
printf("Before deletion: \n");
print_array(list);
clock_t start, end;
double cpu_time_used = 0;
int* item = get_first(list);
for(int i=0; i<size; i++){
int* next_item = get_first(list);
start = clock();
delete_here(list, item);
end = clock();
cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
item = next_item;
}
print_array(list);
printf("%d sequential deletions, time: %f \n", size, cpu_time_used);
}
// inserts N elements at random locations into OFM.
// prints out itme it takes for N operations
void testRandomInsertOFM(list_t* list, int size){
/*
int randomInsert[N_0] = {0};
int index;
for(int i=0; i< size; i++){
index = rand()%(size-1);
randomInsert[i] = index;
}
setup(list);
print_array(list);
clock_t start, end;
double cpu_time_used = 0;
for(int i=0; i<size; i++){
start = clock();
insert(list, randomInsert[i],i);
end = clock();
cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
}
print_array(list);
printf("%d random insertions, time: %f \n", size, cpu_time_used);
*/
}
//sorting -> insert a bunch of elts, and maintain the fact that they're always sorted.
void testInsertGtime(int size){
list_t list;
setup(&list);
for(int i=0; i< size; i++){
insert_first(&list, i); }
free(list.items);
}
int main(){
int n = 1000000;
list_t list;
setup(&list);
//printf("Beginning Testing Suite. N is: %d\n", list.N);
//testSequentialInsertOFM(&list, 82);
//testSequentialDelete(&list, 40);
//testRandomInsertOFM(N/4);
//testInsertSortedReverse(100000);
//testInsertSortedRandom(1000000);
//testInsertSortedVarious();
testInsertNearRandom(1000000);
//testInsertSortedRandom(10000);
// testMax(1000);
// testMax(10000);
// testMax(100000);
// testMax(1000000);
// testInsertSortedReverse(n);
// testInsertSortedRandom(n);
free(list.items);
return 0;
}