#include #include #include "colors.h" #include "tests.hpp" #include // std::setw() #include #ifdef STL namespace ft = std; #else #include "vector.hpp" #endif int main() { TEST(vector::vector (constructor)) { ft::vector myvector; int myint[] = {12434, -2432, 12, 5345, 23, 0, -4, 387, 8432, -934723, 1}; int size = sizeof(myint) / sizeof(myint[0]); for (int i = 0; i < size; i++) myvector.push_back(myint[i]); for (int i = 0; i < size; i++) std::cout << "[" << std::setw(2) << i << "] " << myvector[i] << "\n"; std::cout << " -> myvector stores " << int(myvector.size()) << " numbers.\n"; } TESTEND /* TEST(vector::vector (constructor)) { // constructors used in the same order as described above: std::vector first; // empty vector of ints std::vector second (4,100); // four ints with value 100 std::vector third (second.begin(),second.end()); // iterating through second std::vector fourth (third); // a copy of third // the iterator constructor can also be used to construct from arrays: int myints[] = {16,2,77,29}; std::vector fifth (myints, myints + sizeof(myints) / sizeof(int) ); std::cout << "The contents of fifth are:"; for (std::vector::iterator it = fifth.begin(); it != fifth.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; } TESTEND TEST(vector::=operator) { std::vector foo (3,0); std::vector bar (5,0); bar = foo; foo = std::vector(); std::cout << "Size of foo: " << int(foo.size()) << '\n'; std::cout << "Size of bar: " << int(bar.size()) << '\n'; } TESTEND TEST(vector::begin) { std::vector myvector; for (int i=1; i<=5; i++) myvector.push_back(i); std::cout << "myvector contains:"; for (std::vector::iterator it = myvector.begin() ; it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; } TESTEND TEST(vector::end) { std::vector myvector; for (int i=1; i<=5; i++) myvector.push_back(i); std::cout << "myvector contains:"; for (std::vector::iterator it = myvector.begin() ; it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; } TESTEND TEST(vector::rbegin) { std::vector myvector (5); // 5 default-constructed ints int i=0; std::vector::reverse_iterator rit = myvector.rbegin(); for (; rit!= myvector.rend(); ++rit) *rit = ++i; std::cout << "myvector contains:"; for (std::vector::iterator it = myvector.begin(); it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; } TESTEND TEST(vector::rend) { std::vector myvector (5); // 5 default-constructed ints std::vector::reverse_iterator rit = myvector.rbegin(); int i=0; for (rit = myvector.rbegin(); rit!= myvector.rend(); ++rit) *rit = ++i; std::cout << "myvector contains:"; for (std::vector::iterator it = myvector.begin(); it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; } TESTEND TEST(vector::size) { std::vector 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'; } TESTEND TEST(vector::max_size) { std::vector 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"; std::cout << "max_size: " << myvector.max_size() << "\n"; } TESTEND TEST(vector::resize) { std::vector 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 (unsigned int i = 0; i < myvector.size(); i++) std::cout << ' ' << myvector[i]; std::cout << '\n'; } TESTEND TEST(vector::capacity) { std::vector 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'; std::cout << "max_size: " << (int) myvector.max_size() << '\n'; } TESTEND TEST(vector::empty) { std::vector 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'; } TESTEND TEST(vector::reserve) { std::vector::size_type sz; std::vector 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'; } } std::vector 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'; } } } TESTEND TEST(vector::operator[]) { std::vector myvector (10); // 10 zero-initialized elements std::vector::size_type sz = myvector.size(); // assign some values: for (unsigned i=0; i myvector (10); // 10 zero-initialized ints // assign some values: for (unsigned i=0; i 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'; } TESTEND TEST(vector::back) { std::vector 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'; } TESTEND TEST(vector::assign) { std::vector first; std::vector second; std::vector third; first.assign (7,100); // 7 ints with a value of 100 std::vector::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'; } TESTEND TEST(vector::push_back) { std::vector myvector; // original : // // int myint; // std::cout << "Please enter some integers (enter 0 to end):\n"; // do { // std::cin >> myint; // myvector.push_back (myint); // } while (myint); // // replaced by : int myint[] = {12434, -2432, 12, 5345, 23, 0, -4, 387, 8432, -934723, 1}; int size = sizeof(myint) / sizeof(myint[0]); for (int i = 0; i < size; i++) myvector.push_back (myint[i]); std::cout << "myvector stores " << int(myvector.size()) << " numbers.\n"; } TESTEND TEST(vector::pop_back) { std::vector 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'; } TESTEND TEST(vector::insert) { std::vector myvector (3,100); std::vector::iterator it; it = myvector.begin(); it = myvector.insert ( it , 200 ); myvector.insert (it,2,300); // "it" no longer valid, get a new one: it = myvector.begin(); std::vector anothervector (2,400); myvector.insert (it+2,anothervector.begin(),anothervector.end()); int myarray [] = { 501,502,503 }; myvector.insert (myvector.begin(), myarray, myarray+3); std::cout << "myvector contains:"; for (it=myvector.begin(); it myvector; // set some values (from 1 to 10) for (int i=1; i<=10; i++) myvector.push_back(i); // erase the 6th element myvector.erase (myvector.begin()+5); // erase the first 3 elements: myvector.erase (myvector.begin(),myvector.begin()+3); std::cout << "myvector contains:"; for (unsigned i=0; i foo (3,100); // three ints with a value of 100 std::vector bar (5,200); // five ints with a value of 200 foo.swap(bar); std::cout << "foo contains:"; for (unsigned i=0; i myvector; myvector.push_back (100); myvector.push_back (200); myvector.push_back (300); std::cout << "myvector contains:"; for (unsigned i=0; i 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); } TESTEND */ // execute tests and print them : int size = test_list.size(); for(int i = 0; i < size; i++) { std::cout << "\n" B_YELLOW "[" << i + 1 << "/" << size << "] " << test_list[i]->title << " :" RESET "\n"; test_list[i]->func(); } return 0; }