forked from bucs240/program1-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogram1.cpp
More file actions
275 lines (221 loc) · 8.56 KB
/
program1.cpp
File metadata and controls
275 lines (221 loc) · 8.56 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "Star.h"
#include "Star.h"
#include "Planet.h"
#include "Planet.h"
#include "Vector.h"
#include "Vector.h"
#include "List.h"
#include "List.h"
#include <cassert>
#include <iostream>
#include <ctime>
#include <sys/time.h>
using namespace std;
bool unique(long int ids[], int size){
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (ids[i] == ids[j]) {
return false;
}
}
}
return true;
}
int main(){
srand(time(NULL));
long id_list[200];
cerr << "\t-----------------------------------\n";
cerr << "\t- Part A: Vectors -\n";
cerr << "\t-----------------------------------\n";
cerr << "\n\t=========Test #1: Creating your Vector===========\n\n";
Vector *vect = new Vector();
assert(vect->size() == 0);
cerr << "\n\t=========Test #1 Passed... ===========\n\n";
cerr << "\n\t=========Test #2: Inserting 20 Planets into your Vector===========\n\n";
int i;
for(i = 0; i < 20 ; i++){
vect->insert(i, new Planet(0));
}
assert(vect->size() == 20);
cerr << "\n\t=========Test #2 Passed... ===========\n\n";
cerr << "\n\t=========Test #3: Inserting values into out of bounds index===========\n\n";
vect->insert(49, new Planet(0));
assert(vect->size() == 50);
vect->insert(40, new Planet(0));
assert(vect->size() == 51);
cerr << "\n\t=========Test #3 Passed... ===========\n\n";
cerr << "\n\t=========Test #4: Reading from your Vector===========\n\n";
Planet * p;
for(i = 0 ; i < 20 ; i++){
p = vect->read(i);
id_list[i] = p->getID();
}
assert(unique(id_list, i));
//the new index of planet 20 will be 50 because the previous insert would increment the index
p = vect->read(50);
id_list[i++] = p->getID();
p = vect->read(40);
id_list[i++] = p->getID();
assert(unique(id_list, i));
cerr << "\n\t=========Test #4 Passed... ===========\n\n";
cerr << "\n\t=========Test #5: Reading from unititialized vector index (should return NULL)===========\n\n";
assert(vect->read(45) == NULL);
cerr << "\n\t=========Test #5 Passed... ===========\n\n";
cerr << "\n\t=========Test #6: Attempting to read from out of bounds===========\n\n";
assert(vect->read(100) == NULL);
cerr << "\n\t=========Test #6 Passed... ===========\n\n";
cerr << "\n\t=========Test #7: Remove elements from the vector ===========\n\n";
//RemoveVector
vect->remove(50);
vect->remove(40);
cerr << vect->size() << endl;
assert(vect->size() == 49);
cerr << "\n\t=========Test #7 Passed... ===========\n\n";
cerr << "\n\t=========Test #8: Remove elements from the vector randomly ===========\n\n";
while(vect->size()){
vect->remove((rand()%5));
}
vect->remove(50);
cerr << "\n\t=========Test #8 Passed... ===========\n\n";
cerr << "\n\t=========Test #9: Delete the vector from memory ===========\n\n";
delete vect;
vect = NULL;
cerr << "\n\t=========Test #9 Passed... ===========\n\n";
cerr << "\t-----------------------------------\n";
cerr << "\t- Part B: Lists -\n";
cerr << "\t-----------------------------------\n";
cerr << "\n\t=========Test #10: Creating your Linked List===========\n\n";
List * list = new List();
cerr << "\n\t\tTest #10: Passed ...\n\n";
cerr << "\n\t=========Test #11: Linked List Insertion===========\n\n";
int index = 0;
//add elements to the list
for(int index = 0 ; index < 10 ; index++ ){
list->insert(index, new Planet(0));
}
assert(list->size() == 10);
//***** Test that out of bounds indexes are appended to the list ****/
list->insert(20, new Planet(0));
assert(list->size() == 11);
cerr << "\n\t\tTest #11: Passed ...\n\n";
cerr << "\n\t=========Test #12: Linked List Read===========\n\n";
for(i = 0 ; i < 11 ; i++){
p = list->read(i);
id_list[i] = p->getID();
}
unique(id_list, 11);
p = list->read(20);
assert(p == NULL);
cerr << "\n\t\tTest #12: Passed ...\n\n";
cerr << "\n\t=========Test #13: Linked List Remove===========\n\n";
for(index = 0; list->size() > 4; index++){
index = index % 4;
unsigned int s = list->size();
list->remove(index);
assert(list->size() == s-1);
}
assert(list->size() == 4);
delete list;
list = NULL;
cerr << "\n\t\tTest #13 Passed...\n\n";
cerr << "\n\t=========Test #14: Star Classes ===========\n\n";
Starlist * sl = new Starlist();
assert(sl->getCurrentNumPlanets() == 0);
id_list[0] = sl->addPlanet();
assert(sl->getCurrentNumPlanets() == 1);
p = sl->getPlanet(id_list[0]);
int pos = p->getPos();
sl->orbit();
assert(p->getPos() == (pos + 1) % 360);
Starvector * sv = new Starvector();
assert(sv->getCurrentNumPlanets() == 0);
id_list[1] = sv->addPlanet();
assert(sv->getCurrentNumPlanets() == 1);
p = sv->getPlanet(id_list[1]);
pos = p->getPos();
sv->orbit();
assert(p->getPos() == (pos + 1) % 360);
//add 200 more planets to each
for(int i = 0; i < 200; i+=2){
id_list[i] = sv->addPlanet();
id_list[i+1] = sl->addPlanet();
}
//delete 10 random planets in the sun
for(int i = 0; i < 50; i++){
int rand_id = (rand() % (200));
if(id_list[rand_id] == -1){
continue;
}else if(sv->removePlanet(id_list[rand_id]) ||
sl->removePlanet(id_list[rand_id])){
id_list[rand_id] = -1;
}else{
assert(false);
}
}
sv->printStarInfo();
sl->printStarInfo();
cerr << "\n\t\tTest #14 Passed...\n\n";
cerr << "\n\t=========Test #15: Star Delete ===========\n\n";
delete sl;
delete sv;
cerr << "\n\t\tTest #15 Passed (but check valgrind to be sure)...\n\n";
cerr << "Comparing performance of Vectors and Lists\n";
sl = new Starlist();
sv = new Starvector();
struct timeval start, stop;
float profile_time;
/************************ Starvector Performance Test *********************/
/** Testing performance of insert **/
gettimeofday(&start, NULL);
for(int i = 0; i < 200; i++){
id_list[i] = sv->addPlanet();
}
gettimeofday(&stop, NULL);
profile_time = ((stop.tv_sec* 1000000) + stop.tv_usec) - ((start.tv_sec* 1000000) + start.tv_usec);
printf("\tCompleted Starvector insert profile time = %lf\n", profile_time);
/** Testing performance of read **/
gettimeofday(&start, NULL);
for(int i = 0; i < 200; i++){
sv->getPlanet(id_list[i]);
}
gettimeofday(&stop, NULL);
profile_time = ((stop.tv_sec* 1000000) + stop.tv_usec) - ((start.tv_sec* 1000000) + start.tv_usec);
printf("\tCompleted Starvector read profile time = %lf\n", profile_time);
/** Testing performance of remove **/
gettimeofday(&start, NULL);
for(int i = 0; i < 200; i+=5){
sv->removePlanet(id_list[i]);
}
delete sv;
gettimeofday(&stop, NULL);
profile_time = ((stop.tv_sec* 1000000) + stop.tv_usec) - ((start.tv_sec* 1000000) + start.tv_usec);
printf("\tCompleted Starvector remove profile time = %lf\n", profile_time);
/************************ Starlist Performance Test *********************/
/** Testing performance of insert **/
gettimeofday(&start, NULL);
for(int i = 0; i < 200; i++){
id_list[i] = sl->addPlanet();
}
gettimeofday(&stop, NULL);
profile_time = ((stop.tv_sec* 1000000) + stop.tv_usec) - ((start.tv_sec* 1000000) + start.tv_usec);
printf("\tCompleted Starlist insert profile time = %lf\n", profile_time);
/** Testing performance of read **/
gettimeofday(&start, NULL);
for(int i = 0; i < 200; i++){
sl->getPlanet(id_list[i]);
}
gettimeofday(&stop, NULL);
profile_time = ((stop.tv_sec* 1000000) + stop.tv_usec) - ((start.tv_sec* 1000000) + start.tv_usec);
printf("\tCompleted Starlist read profile time = %lf\n", profile_time);
/** Testing performance of remove **/
gettimeofday(&start, NULL);
for(int i = 0; i < 200; i+=5){
sl->removePlanet(id_list[i]);
}
delete sl;
gettimeofday(&stop, NULL);
profile_time = ((stop.tv_sec* 1000000) + stop.tv_usec) - ((start.tv_sec* 1000000) + start.tv_usec);
printf("\tCompleted Starlist remove profile time = %lf\n", profile_time);
cerr << "\n\t========= Don't forget to submit your hash and check your code with Valgrind! ===========\n\n";
return 0;
}