-
Notifications
You must be signed in to change notification settings - Fork 1
/
linked-list-testing.c
249 lines (203 loc) · 5.89 KB
/
linked-list-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
#include "linked_list.c"
#include <time.h>
void testFindMax(list_t* list , int size){
clock_t start, end;
double cpu_time_used = 0;
start = clock();
int x = max(list);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Max:%d of %d items, time: %f \n",x, size, cpu_time_used);
}
// Inserted items in reverse order, should come out as the correct order in the end
void testInsertSortedReverse(int size){
printf("Testing Insert Sorted with Reverse Values: \n");
clock_t start, end;
double cpu_time_used = 0;
list_t list;
setup(&list);
// populate list with items
for(int i=size-1; i>=0; i--){
start = clock();
insert_sorted(&list, i);
end = clock();
cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
}
node_t cur = list.items[0];
for(int i=0; i<size-1; i++){
int val = cur.val;
int next_val = cur.next->val;
if(next_val<val){
printf("Insert Sorted does not work!");
break;
}
cur = *(cur.next);
}
printf("%d reverse sequential insertions into an ordered list of size:%d, time: %f \n", size/2, size, cpu_time_used);
}
// Inserts items close to eachother randomly
void testInsertSortedNear(int size){
printf("Testing Insert Sorted Near with Random Values: \n");
clock_t start, end;
double cpu_time_used = 0;
list_t list;
setupSize(&list, size);
insert_here(&list, NULL, 999999999);
int lastVal =0;
// populate list with items
for(int i=0; i<size-1; i++){
int dev = rand()%5;
int item = lastVal + dev; // keep it to in between 1-100
if(item%2==1){
item = abs(lastVal - dev);
}
//start = clock();
insert_sorted(&list, item);
//end = clock();
//cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
}
node_t cur = list.items[0];
for(int i=0; i<size-1; i++){
int val = cur.val;
if (cur.next){
int next_val = cur.next->val;
if(next_val<val){
printf("Insert Sorted does not work!");
}
cur = *(cur.next);
}
}
//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);
}
// Inserted items in reverse order, should come out as the correct order in the end
void testInsertSortedRandom(int size){
// printf("Testing Insert Sorted with Random Values: \n");
clock_t start, end;
double cpu_time_used = 0;
list_t list;
setupSize(&list, size);
insert_here(&list, NULL, 999999999);
// populate list with items
for(int i=0; i<size-1; i++){
int item = rand()%100; // keep it to in between 1-100
start = clock();
insert_sorted(&list, item);
end = clock();
cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
}
node_t cur = list.items[0];
for(int i=0; i<size-1; i++){
int val = cur.val;
if (cur.next){
int next_val = cur.next->val;
if(next_val<val){
printf("Insert Sorted does not work!");
}
cur = *(cur.next);
}
}
//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);
}
// inserts N elements sequentially into ofm.
// prints out time it takes for N operations
list_t testSequentialInsert(int size){
list_t list;
setup(&list);
//print_array(&list);
clock_t start, end;
double cpu_time_used = 0;
for(int i=0; i<size; i++){
node_t *node = get(&list, i);
//start = clock();
insert_here(&list, node, i);
//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);
return list;
}
// inserts N elements at random locations into OFM.
// prints out time it takes for N operations
list_t testRandomInsert(int size){
printf("Linked List random insert: %d\n", size);
list_t list;
setup(&list);
//print_array(&list);
clock_t start, end;
double cpu_time_used = 0;
//start = clock();
for(int i=0; i<size; i++){
int index = rand()%size;
node_t *node = get(&list, index);
start = clock();
insert_here(&list, node, 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);
return list;
}
void testInsertSortedNearVarious(){
printf("Tests Linked List Insert Sorted Near for sizes 10, 100, 1,000, 10,000, 100,000 \n\n");
testInsertSortedNear(10);
testInsertSortedNear(100);
testInsertSortedNear(1000);
testInsertSortedNear(10000);
testInsertSortedNear(100000);
testInsertSortedNear(1000000);
}
void testInsertSortedRandomVarious(){
printf("Tests Linked List Insert Sorted Random for sizes 10, 100, 1,000, 10,000, 100,000 \n\n");
testInsertSortedRandom(10);
testInsertSortedRandom(100);
testInsertSortedRandom(1000);
testInsertSortedRandom(10000);
testInsertSortedRandom(100000);
testInsertSortedRandom(1000000);
}
void testInsertGtime(int size){
list_t list;
setup(&list);
for(int i=0; i< size; i++){
quick_insert(&list, 5);
}
}
void testMax(int size){
clock_t start, end;
double cpu_time_used = 0;
list_t list;
setupSize(&list, size);
insert_here(&list, NULL, 999999999);
// populate list with items
for(int i=0; i<size-1; i++){
int item = rand()%100; // keep it to in between 1-100
insert_sorted(&list, item);
}
start = clock();
int max_elem = max(&list);
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("%d random insertions, time: %f, max:%d, \n", size, cpu_time_used, max_elem);
free(list.items);
}
int main(){
int size=100000;
// printf("Beginning Testing Suite for Linked List. N is: %d\n", size);
// testSequentialInsert(size);
// list_t list = testRandomInsert(size);
// testFindMax(&list, size);
testInsertSortedRandomVarious();
//testInsertSortedNear(100000);
//testInsertSortedRandom(100000);
//testInsertGtime(1000000);
// testMax(1000);
// testMax(10000);
// testMax(100000);
// testMax(1000000);
}