-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_test_main.cpp
More file actions
510 lines (430 loc) · 16.8 KB
/
vector_test_main.cpp
File metadata and controls
510 lines (430 loc) · 16.8 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include "vector.hpp"
#include <iostream>
#include <vector>
#include <iterator>
// #if 0 //CREATE A REAL STL EXAMPLE
// #include <map>
// #include <stack>
// #include <vector>
// namespace ft = std;
// #else
// #include "map.hpp"
// #include "stack.hpp"
// #include "vector.hpp"
// #endif
#ifndef NS
#define NS ft
#endif
int main()
{
// {
// NS::vector<int> vec;
// NS::vector<int>::iterator vec_it = vec.begin();
// NS::vector<int>::const_iterator vec_it2 = vec.begin();
// if (vec_it == vec_it2)
// std::cout << "ok" << std::endl;
// }
// {
// NS::pair <int,int> foo;
// NS::pair <int,int> bar;
// foo = NS::make_pair (10,20);
// bar = NS::make_pair (10.5,'A'); // ok: implicit conversion from pair<double,char>
// std::cout << "foo: " << foo.first << ", " << foo.second << '\n';
// std::cout << "bar: " << bar.first << ", " << bar.second << '\n';
// }
/////////////////////////
// testing constructors//
/////////////////////////
{
NS::vector<int> first; // empty vector of ints
NS::vector<int> second (4,100); // four ints with value 100
NS::vector<int> third (second.begin(),second.end()); // iterating through second
NS::vector<int> fourth (third); // a copy of third
// the iterator constructor can also be used to construct from arrays:
int myints[] = {16,2,77,29};
NS::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
std::cout << "The contents of fifth are:";
for (NS::vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "The contents of third are:";
for (NS::vector<int>::iterator it = third.begin(); it != third.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
// //////////////////////
// TESTING OPERATOR=/////
// //////////////////////
{
NS::vector<int> foo (3,0);
NS::vector<int> bar (5,0);
bar = foo;
foo = NS::vector<int>();
std::cout << "Size of foo: " << int(foo.size()) << '\n';
std::cout << "Size of bar: " << int(bar.size()) << '\n';
}
// //////////////////
// TESTING BEGIN()///
// //////////////////
{
NS::vector<int> myvector;
for (int i=1; i<=5; i++) myvector.push_back(i);
std::cout << "myvector contains:";
for (NS::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
// //////////////////
// TESTING END()/////
// //////////////////
{
NS::vector<int> myvector;
for (int i=1; i<=5; i++) myvector.push_back(i);
std::cout << "myvector contains:";
for (NS::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
// //////////////////
// TESTING RBEGIN()//
// //////////////////
{
NS::vector<int> myvector (5); // 5 default-constructed ints
int i = 0;
NS::vector<int>::reverse_iterator rit = myvector.rbegin();
for (; rit!= myvector.rend(); ++rit)
*rit = ++i;
std::cout << "myvector contains:";
for (NS::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
// //////////////////
// TESTING REND()////
// //////////////////
{
NS::vector<int> myvector (5); // 5 default-constructed ints
NS::vector<int>::reverse_iterator rit = myvector.rbegin();
int i=0;
for (rit = myvector.rbegin(); rit != myvector.rend(); ++rit)
*rit = ++i;
std::cout << "myvector contains:";
for (NS::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
// //////////////////
// TESTING SIZE()////
// //////////////////
{
NS::vector<int> myints;
std::cout << "0. size: " << myints.size() << '\n';
for (int i=0; i<10; i++) myints.push_back(i);
std::cout << "1. size: " << myints.size() << '\n';
myints.insert (myints.end(),10,100);
std::cout << "2. size: " << myints.size() << '\n';
myints.pop_back();
std::cout << "3. size: " << myints.size() << '\n';
}
// //////////////////////
// TESTING MAX_SIZE()////
// /////////////////////
{
NS::vector<int> myvector;
// set some content in the vector:
for (int i=0; i<100; i++) myvector.push_back(i);
std::cout << "size: " << myvector.size() << "\n";
std::cout << "capacity: " << myvector.capacity() << "\n"; ///////////FIXED//////////
std::cout << "max_size: " << myvector.max_size() << "\n";
}
// /////////////////////
// TESTING RESIZE()/////
// /////////////////////
{
NS::vector<int> myvector;
// set some initial content:
for (int i=1;i<10;i++) myvector.push_back(i);
myvector.resize(5);
myvector.resize(8,100);
myvector.resize(12);
std::cout << "myvector contains:";
for (size_t i=0; i < myvector.size();i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
}
// /////////////////////
// TESTING CAPACITY()///
// /////////////////////
{
NS::vector<int> myvector;
// set some content in the vector:
for (int i=0; i<100; i++) myvector.push_back(i);
std::cout << "size: " << (int) myvector.size() << '\n';
std::cout << "capacity: " << (int) myvector.capacity() << '\n';//FIXED
std::cout << "max_size: " << myvector.max_size() << '\n';
}
// /////////////////////
// TESTING EMPTY()//////
// /////////////////////
{
NS::vector<int> myvector;
int sum (0);
for (int i=1;i<=10;i++) myvector.push_back(i);
while (!myvector.empty())
{
sum += myvector.back();
myvector.pop_back();
}
std::cout << "total: " << sum << '\n';
}
// /////////////////////
// TESTING RESERVE()////
// /////////////////////
{
NS::vector<int>::size_type sz;
NS::vector<int> foo;
sz = foo.capacity();
std::cout << "making foo grow:\n";
for (int i=0; i<100; ++i) {
foo.push_back(i);
if (sz!=foo.capacity()) {
sz = foo.capacity();
std::cout << "capacity changed: " << sz << '\n';
}
}
NS::vector<int> bar;
sz = bar.capacity();
bar.reserve(100); // this is the only difference with foo above
std::cout << "making bar grow:\n";
for (int i=0; i<100; ++i) {
bar.push_back(i);
if (sz!=bar.capacity()) {
sz = bar.capacity();
std::cout << "capacity changed: " << sz << '\n';
}
}
}
// /////////////////////
// TESTING OERATOR[]////
// /////////////////////
{
NS::vector<int> myvector (10); // 10 zero-initialized elements
NS::vector<int>::size_type sz = myvector.size();
// assign some values:
for (size_t i = 0; i < sz; i++) myvector[i]=i;
std::cout << "myvector contains:";
for (size_t i = 0; i<sz; i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
}
// /////////////////////
// TESTING AT///////////
// /////////////////////
{
NS::vector<int> myvector (10); // 10 zero-initialized ints
// assign some values:
for (size_t i=0; i<myvector.size(); i++)
myvector.at(i)=i;
std::cout << "myvector contains:";
for (size_t i=0; i<myvector.size(); i++)
std::cout << ' ' << myvector.at(i);
std::cout << '\n';
}
// /////////////////////
// TESTING FRONT////////
// /////////////////////
{
NS::vector<int> myvector;
myvector.push_back(78);
myvector.push_back(16);
// now front equals 78, and back 16
myvector.front() -= myvector.back();
std::cout << "myvector.front() is now " << myvector.front() << '\n';
}
// /////////////////////
// TESTING BACK////////
// /////////////////////
{
NS::vector<int> myvector;
myvector.push_back(10);
while (myvector.back() != 0)
{
myvector.push_back ( myvector.back() -1 );
}
std::cout << "myvector contains:";
for (size_t i=0; i<myvector.size() ; i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
}
// /////////////////////
// TESTING ASSIGN//////
// ///////////////////////
{
NS::vector<int> first;
NS::vector<int> second;
NS::vector<int> third;
first.assign (7,100); // 7 ints with a value of 100
NS::vector<int>::iterator it;
it = first.begin() + 1;
second.assign (it, first.end() - 1); // the 5 central values of first
int myints[] = {1776,7,4};
third.assign (myints,myints+3); // assigning from array.
std::cout << "Size of first: " << int (first.size()) << '\n';
std::cout << "Size of second: " << int (second.size()) << '\n';
std::cout << "Size of third: " << int (third.size()) << '\n';/////////////////////FIXEDDDDD/////////////////
}
// /////////////////////
// TESTING PUSH_BACK////
// /////////////////////
// {
// NS::vector<int> myvector;
// int myint;
// std::cout << "Please enter some integers (enter 0 to end):\n";
// do {
// std::cin >> myint;
// myvector.push_back (myint);
// } while (myint);
// std::cout << "myvector stores " << int(myvector.size()) << " numbers.\n";
// }
// /////////////////////
// TESTING POP_BACK/////
// /////////////////////
{
NS::vector<int> myvector;
int sum (0);
myvector.push_back (100);
myvector.push_back (200);
myvector.push_back (300);
while (!myvector.empty())
{
sum+=myvector.back();
myvector.pop_back();
}
std::cout << "The elements of myvector add up to " << sum << '\n';
}
// /////////////////////
// TESTING INSERT///////
// /////////////////////
{
NS::vector<int> myvector (3,100);
NS::vector<int>::iterator it;
it = myvector.begin();
it = myvector.insert ( it , 200 );
std::cout << "my capacity = " << myvector.capacity() << std::endl;
myvector.insert (it,10,300);
// "it" no longer valid, get a new one:
it = myvector.begin();
NS::vector<int> anothervector (2,400);
std::cout << "my capacity = " << myvector.capacity() << std::endl;
myvector.insert (it+1,anothervector.begin(),anothervector.end());
std::cout << "my capacity = " << myvector.capacity() << std::endl;
int myarray [] = { 501,502,503 };
myvector.insert (myvector.begin(), myarray, myarray+3);
std::cout << "my capacity = " << myvector.capacity() << std::endl;
std::cout << "myvector contains:";
for (it=myvector.begin(); it < myvector.end(); it++)
std::cout << ' ' << *it;
std::cout << '\n';
}
/////////////////////
// TESTING ERASE////////
/////////////////////
{
NS::vector<int> myvector;
// set some values (from 1 to 10)
for (int i=1; i<=10; i++) myvector.push_back(i);
// erase the 6th element
NS::vector<int>::iterator ite = myvector.begin()+5;
myvector.erase (ite);
// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3);
std::cout << "myvector contains:";
for (size_t i=0; i<myvector.size(); ++i)
std::cout << ' ' << myvector[i];
std::cout << '\n';
}
/////////////////////
// TESTING SWAP/////////
/////////////////////
{
NS::vector<int> foo (3,100); // three ints with a value of 100//////////////////fixed///////////////////
NS::vector<int> bar (5,200); // five ints with a value of 200
foo.swap(bar);
std::cout << "foo contains:";
for (size_t i=0; i<foo.size(); i++)
std::cout << ' ' << foo[i];
std::cout << '\n';
std::cout << "bar contains:";
for (size_t i=0; i<bar.size(); i++)
std::cout << ' ' << bar[i];
std::cout << '\n';
}
////////////////////////
// TESTING PCLEAR//////////
///////////////////////
{
NS::vector<int> myvector;
myvector.push_back (100);
myvector.push_back (200);
myvector.push_back (300);
std::cout << "myvector contains:";
for (size_t i=0; i<myvector.size(); i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
myvector.clear();
myvector.push_back (1101);
myvector.push_back (2202);
std::cout << "myvector contains:";
for (size_t i=0; i<myvector.size(); i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
}
////////////////////////
// TESTING GET_ALLOCATOR//////////
///////////////////////
{
std::vector<int> myvector;
int * p;
unsigned int i;
// allocate an array with space for 5 elements using vector's allocator:
p = myvector.get_allocator().allocate(5);
// construct values in-place on the array:
for (i=0; i<5; i++) myvector.get_allocator().construct(&p[i],i);
std::cout << "The allocated array contains:";
for (i=0; i<5; i++) std::cout << ' ' << p[i];
std::cout << '\n';
// destroy and deallocate:
for (i=0; i<5; i++) myvector.get_allocator().destroy(&p[i]);
myvector.get_allocator().deallocate(p,5);
}
//////////////////////////
//RATIONAL_OPERATORS///////
//////////////////////////
{
NS::vector<int> foo (3,100); // three ints with a value of 100
NS::vector<int> bar (2,200); // two ints with a value of 200
if (foo==bar) std::cout << "foo and bar are equal\n";
if (foo!=bar) std::cout << "foo and bar are not equal\n";
if (foo< bar) std::cout << "foo is less than bar\n";
if (foo> bar) std::cout << "foo is greater than bar\n";
if (foo<=bar) std::cout << "foo is less than or equal to bar\n";
if (foo>=bar) std::cout << "foo is greater than or equal to bar\n";
}
//////////////
///SWAP///////
//////////////
{
// unsigned int i;
NS::vector<int> foo (3,100); // three ints with a value of 100
NS::vector<int> bar (5,200); // five ints with a value of 200
foo.swap(bar);
std::cout << "foo contains:";
for (NS::vector<int>::iterator it = foo.begin(); it!=foo.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "bar contains:";
for (NS::vector<int>::iterator it = bar.begin(); it!=bar.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
}