-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathprogram1.cpp
More file actions
339 lines (322 loc) · 10.4 KB
/
program1.cpp
File metadata and controls
339 lines (322 loc) · 10.4 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#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(unsigned long int ids[], unsigned int size){
for (unsigned int i = 0; i < size - 1; i++) {
for (unsigned int j = i + 1; j < size; j++) {
if (ids[i] == ids[j]) {
return false;
}
}
}
return true;
}
int main(){
srand(time(NULL));
cerr << "\t-----------------------------------\n";
cerr << "\t- Part A: Vectors -\n";
cerr << "\t-----------------------------------\n";
#ifndef TEST1
{
cerr << "\n\t=========Test #1: Creating your Vector===========\n\n";
Vector *vect = new Vector();
assert(vect->size() == 0);
delete vect;
cerr << "\n\tTest #1 Passed...\n\n";
}
#endif
#ifndef TEST2
{
cerr << "\n\t=========Test #2: Inserting 20 Planets into your Vector===========\n\n";
Vector *vect = new Vector();
for(int i = 0; i < 20 ; i++){
vect->insert(i, new Planet(0));
}
assert(vect->size() == 20);
delete vect;
cerr << "\n\tTest #2 Passed...\n\n";
}
#endif
#ifndef TEST3
{
cerr << "\n\t=========Test #3: Inserting values into out of bounds index===========\n\n";
Vector * vect = new Vector();
for(int i = 0; i < 20 ; i++){
vect->insert(i, new Planet(0));
}
vect->insert(49, new Planet(0));
assert(vect->size() == 50);
vect->insert(40, new Planet(0));
assert(vect->size() == 51);
delete vect;
cerr << "\n\tTest #3 Passed...\n\n";
}
#endif
#ifndef TEST4
{
cerr << "\n\t=========Test #4: Reading from your Vector===========\n\n";
Vector *vect = new Vector();
for(int i = 0; i < 20 ; i++){
vect->insert(i, new Planet(0));
}
Planet * p;
unsigned int id_list_size = 22;
unsigned long id_list[id_list_size] = {0};
for(unsigned int i = 0 ; i < id_list_size - 2 ; i++){
p = vect->read(i);
id_list[i] = p->getID();
}
assert(unique(id_list, id_list_size - 2));
vect->insert(49, new Planet(0));
vect->insert(40, new Planet(0));
//the new index of planet 20 will be 50 because the previous insert would increment the index
p = vect->read(50);
id_list[20] = p->getID();
p = vect->read(40);
id_list[21] = p->getID();
assert(unique(id_list, id_list_size));
delete vect;
cerr << "\n\tTest #4 Passed...\n\n";
}
#endif
#ifndef TEST5
{
cerr << "\n\t=========Test #5: Reading from uninitialized vector index (should return NULL)===========\n\n";
Vector *vect = new Vector();
vect->insert(5, new Planet(0));
for(int i = 0; i < 5; i++){
assert(vect->read(0) == NULL);
}
delete vect;
cerr << "\n\tTest #5 Passed...\n\n";
}
#endif
#ifndef TEST6
{
cerr << "\n\t=========Test #6: Attempting to read from out of bounds===========\n\n";
Vector *vect = new Vector();
vect->insert(0, new Planet(0));
assert(vect->read(100) == NULL);
delete vect;
cerr << "\n\tTest #6 Passed...\n\n";
}
#endif
#ifndef TEST7
{
cerr << "\n\t=========Test #7: Remove elements from the vector ===========\n\n";
Vector *vect = new Vector();
for(int i = 0 ; i < 50; i++){vect->insert(i, new Planet(0));}
assert(!vect->remove(51));
assert(vect->remove(40));
assert(vect->size() == 49);
delete vect;
cerr << "\n\tTest #7 Passed...\n\n";
}
#endif
#ifndef TEST8
{
cerr << "\n\t=========Test #8: Remove elements from the vector randomly ===========\n\n";
Vector *vect = new Vector();
for(int i = 0 ; i < 50; i++){vect->insert(i, new Planet(0));}
while(vect->size()){
vect->remove((rand()%5));
}
delete vect;
cerr << "\n\tTest #8 Passed...\n\n";
}
#endif
cerr << "\t-----------------------------------\n";
cerr << "\t- Part B: Lists -\n";
cerr << "\t-----------------------------------\n";
#ifndef TEST9
{
cerr << "\n\t=========Test #9: Creating your Linked List===========\n\n";
List * list = new List();
delete list;
cerr << "\n\tTest #9: Passed ...\n\n";
}
#endif
#ifndef TEST10
{
cerr << "\n\t=========Test #10: Linked List Insertion===========\n\n";
List * list = new List();
//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);
delete list;
cerr << "\n\tTest #10: Passed ...\n\n";
}
#endif
#ifndef TEST11
{
cerr << "\n\t=========Test #11: Linked List Read===========\n\n";
List * list = new List();
unsigned int id_list_size = 10;
unsigned long id_list[id_list_size];
for(unsigned int i = 0 ; i < id_list_size ; i++ ){list->insert(i, new Planet(0));}
for(unsigned int i = 0 ; i < id_list_size ; i++){
Planet * p = list->read(i);
id_list[i] = p->getID();
}
unique(id_list, id_list_size);
Planet * p = list->read(20);
assert(p == NULL);
delete list;
cerr << "\n\tTest #11: Passed ...\n\n";
}
#endif
#ifndef TEST12
{
cerr << "\n\t=========Test #12: Linked List Remove===========\n\n";
List * list = new List();
unsigned int list_size = 40;
for(unsigned int i = 0 ; i < list_size ; i++ ){list->insert(i, new Planet(0));}
for(unsigned int 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;
cerr << "\n\tTest #12 Passed...\n\n";
}
#endif
#ifndef TEST13
{
cerr << "\n\t=========Test #13: Star Classes Create and Read ===========\n\n";
Starlist * sl = new Starlist();
unsigned int id_list_size = 2;
unsigned long id_list[id_list_size];
assert(sl->getCurrentNumPlanets() == 0);
id_list[0] = sl->addPlanet();
assert(sl->getCurrentNumPlanets() == 1);
Planet * p = sl->getPlanet(id_list[0]);
unsigned 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);
delete sl;
delete sv;
cerr << "\n\tTest #13 Passed...\n\n";
}
#endif
#ifndef TEST14
{
cerr << "\n\t=========Test #14: Star Classes Remove ===========\n\n";
Starlist * sl = new Starlist();
Starvector * sv = new Starvector();
unsigned int id_list_size = 200;
unsigned long id_list[id_list_size];
//add 100 planets to each
for(unsigned int i = 0; i < id_list_size; i+=2){
id_list[i] = sv->addPlanet();
id_list[i+1] = sl->addPlanet();
}
//delete random planets in the star classes
for(unsigned int i = 0; i < id_list_size; i++){
unsigned int rand_id = (rand() % (id_list_size));
if(id_list[rand_id] == (unsigned) -1){
continue;
}else if(sv->removePlanet(id_list[rand_id]) || sl->removePlanet(id_list[rand_id])){
id_list[rand_id] = (unsigned) -1;
}else{
assert(false);
}
}
sv->printStarInfo();
sl->printStarInfo();
delete sl;
delete sv;
cerr << "\n\tTest #14 Passed (but check valgrind to be sure)...\n\n";
}
#endif
#ifndef TEST15
{
cerr << "\n\t========='Test' #15: Relax ===========\n\n";
cerr << "Comparing performance of Vectors and Lists\n";
Starlist * sl = new Starlist();
Starvector * sv = new Starvector();
unsigned int id_list_size = 200;
unsigned long id_list[id_list_size];
struct timeval start, stop;
float profile_time;
/************************ Starvector Performance Test *********************/
/** Testing performance of insert **/
gettimeofday(&start, NULL);
for(unsigned int i = 0; i < id_list_size; 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(unsigned int i = 0; i < id_list_size; 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(unsigned int i = 0; i < id_list_size; i+=rand()%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(unsigned int i = 0; i < id_list_size; 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(unsigned int i = 0; i < id_list_size; 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(unsigned int i = 0; i < id_list_size; 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========= All tests complete. Don't forget to submit your hash and check your code with Valgrind! ===========\n\n";
}
#endif
return 0;
}