Files
42_INT_11_ft_containers/tests/tests_vector.cpp
2022-06-21 14:41:24 +02:00

874 lines
18 KiB
C++

#ifndef TESTS_VECTOR_CPP
#define TESTS_VECTOR_CPP
#include "tests_utils.hpp"
TEST_V(tests_vector_constructor)
{
// title
TITLE(cplusplus.com reference)
// constructors used in the same order as described above:
ft::vector<T> first; // empty vector of ints
ft::vector<T> second (4,VAL(100)); // four ints with value 100
ft::vector<T> third (second.begin(),second.end()); // iterating through second
ft::vector<T> fourth (third); // a copy of third
// the iterator constructor can also be used to construct from arrays:
T myints[] = {VAL(16),VAL(2),VAL(77),VAL(29)};
ft::vector<T> fifth (myints, myints + sizeof(myints) / sizeof(T) );
PRINT(fifth);
DELETE
}
TEST_V(tests_vector_operator_assignation)
{
// title
TITLE(cplusplus.com reference)
ft::vector<T> foo (3,VAL(0));
ft::vector<T> bar (5,VAL(0));
bar = foo;
foo = ft::vector<T>();
std::cout << "Size of foo: " << int(foo.size()) << '\n';
std::cout << "Size of bar: " << int(bar.size()) << '\n';
// title
TITLE(more informations)
PRINT(foo);
PRINT(bar);
DELETE
}
TEST_V(tests_vector_begin)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector;
for (int i=1; i<=5; i++) myvector.push_back(VAL(i));
PRINT(myvector);
DELETE
}
TEST_V(tests_vector_end)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector;
for (int i=1; i<=5; i++) myvector.push_back(VAL(i));
PRINT(myvector);
DELETE
}
TEST_V(tests_vector_rbegin)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector (5);
int i=0;
typename ft::vector<T>::reverse_iterator rit = myvector.rbegin();
for (; rit!= myvector.rend(); ++rit)
*rit = VAL(++i);
PRINT(myvector);
DELETE
}
TEST_V(tests_vector_rend)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector (5); // 5 default-constructed Ts
typename ft::vector<T>::reverse_iterator rit = myvector.rbegin();
int i=0;
for (rit = myvector.rbegin(); rit!= myvector.rend(); ++rit)
*rit = VAL(++i);
PRINT(myvector);
DELETE
}
TEST_V(tests_vector_size)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myarr;
std::cout << "0. size: " << myarr.size() << '\n';
for (int i=0; i<10; i++) myarr.push_back(VAL(i));
std::cout << "1. size: " << myarr.size() << '\n';
myarr.insert (myarr.end(),10,VAL(100));
std::cout << "2. size: " << myarr.size() << '\n';
myarr.pop_back();
std::cout << "3. size: " << myarr.size() << '\n';
PRINT(myarr);
DELETE
}
TEST_V(tests_vector_max_size)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector;
// set some content in the vector:
for (int i=0; i<100; i++) myvector.push_back(VAL(i));
std::cout << "size: " << myvector.size() << "\n";
std::cout << "capacity: " << myvector.capacity() << "\n";
std::cout << "max_size: " << myvector.max_size() << "\n";
PRINT(myvector);
DELETE
}
TEST_V(tests_vector_resize)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector;
// set some initial content:
for (int i = 1; i < 10; i++) myvector.push_back(VAL(i));
myvector.resize(5);
myvector.resize(8,VAL(100));
myvector.resize(12);
PRINT(myvector);
// title
TITLE(test size and capacity 1 :)
ft::vector<T> vector2;
std::cout << "size: " << vector2.size() << " - capacity: " << vector2.capacity() << "\n\n";
std::cout << "assign(10, 1)\n";
vector2.assign(10, VAL(1));
PRINT(vector2);
std::cout << "\n";
std::cout << "resize(15)\n";
vector2.resize(15);
PRINT(vector2);
std::cout << "\n";
std::cout << "resize(10)\n";
vector2.resize(10);
PRINT(vector2);
std::cout << "\n";
std::cout << "resize(19)\n";
vector2.resize(19);
PRINT(vector2);
std::cout << "\n";
std::cout << "resize(20)\n";
vector2.resize(20);
PRINT(vector2);
std::cout << "\n";
std::cout << "resize(21)\n";
vector2.resize(21);
PRINT(vector2);
std::cout << "\n";
// title
TITLE(test size and capacity 2 :)
ft::vector<T> vector3;
std::cout << "size: " << vector3.size() << " - capacity: " << vector3.capacity() << "\n\n";
std::cout << "assign(10, 1)\n";
vector3.assign(10, VAL(1));
PRINT(vector3);
std::cout << "\n";
std::cout << "resize(21)\n";
vector3.resize(21);
PRINT(vector3);
std::cout << "\n";
DELETE
}
TEST_V(tests_vector_capacity)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector;
// set some content in the vector:
for (int i=0; i<100; i++) myvector.push_back(VAL(i));
std::cout << "size: " << (int) myvector.size() << '\n';
std::cout << "capacity: " << (int) myvector.capacity() << '\n';
std::cout << "max_size: " << (int) myvector.max_size() << '\n';
DELETE
}
TEST_V(tests_vector_empty)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector;
int sum (0);
for (int i=1;i<=10;i++) myvector.push_back(VAL(i));
while (!myvector.empty())
{
sum+=TOI(myvector.back());
myvector.pop_back();
}
std::cout << "total: " << sum << '\n';
DELETE
}
TEST_V(tests_vector_reserve)
{
// title
TITLE(cplusplus.com reference :)
typename ft::vector<T>::size_type sz;
ft::vector<T> foo;
sz = foo.capacity();
std::cout << "making foo grow:\n";
for (int i=0; i<100; ++i) {
foo.push_back(VAL(i));
if (sz!=foo.capacity()) {
sz = foo.capacity();
std::cout << "capacity changed: " << sz << '\n';
}
}
ft::vector<T> 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(VAL(i));
if (sz!=bar.capacity()) {
sz = bar.capacity();
std::cout << "capacity changed: " << sz << '\n';
}
}
DELETE
}
TEST_V(tests_vector_operator_access)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector (10); // 10 zero-initialized elements
typename ft::vector<T>::size_type sz = myvector.size();
// assign some values:
for (unsigned i=0; i<sz; i++) myvector[i]=VAL(i);
// reverse vector using operator[]:
for (unsigned i=0; i<sz/2; i++)
{
T temp;
temp = myvector[sz-1-i];
myvector[sz-1-i]=myvector[i];
myvector[i]=temp;
}
PRINT(myvector)
DELETE
}
TEST_V(tests_vector_at)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector (10); // 10 zero-initialized ints
// assign some values:
for (unsigned i=0; i<myvector.size(); i++)
myvector.at(i)=VAL(i);
PRINT(myvector)
DELETE
}
TEST_V(tests_vector_front)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector;
myvector.push_back(VAL(78));
myvector.push_back(VAL(16));
// now front equals 78, and back 16
//myvector.front() -= myvector.back();
std::cout << "myvector.front() is now " << myvector.front() << '\n';
PRINT(myvector)
DELETE
}
TEST_V(tests_vector_back)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector;
myvector.push_back(VAL(78));
myvector.push_back(VAL(16));
// now front equals 78, and back 16
// myvector.front() -= myvector.back();
std::cout << "myvector.front() is now " << myvector.front() << '\n';
PRINT(myvector)
DELETE
}
TEST_V(tests_vector_assign)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> first;
ft::vector<T> second;
ft::vector<T> third;
first.assign (7,VAL(100)); // 7 ints with a value of 100
typename ft::vector<T>::iterator it;
it=first.begin()+1;
second.assign (it,first.end()-1); // the 5 central values of first
T myints[] = {VAL(1776),VAL(7),VAL(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';
// title
TITLE(capacity tests of assignation :)
ft::vector<T> myvector;
std::cout << "capacity before assignation : " << myvector.capacity() << "\n";
std::cout << "\nassign 1\n";
myvector.assign(1, VAL(12));
PRINT(myvector)
std::cout << "\nassign 3\n";
myvector.assign(3, VAL(12));
PRINT(myvector)
std::cout << "\nassign 7268\n";
myvector.assign(7268, VAL(12));
PRINT(myvector)
// title
TITLE(tests of iterators :)
ft::vector<T> int_vector_1;
ft::vector<T> int_vector_2;
ft::vector<T> int_vector_3;
ft::vector<T> it_vector;
std::cout << "\nassign 1\n";
int_vector_1.assign(1, VAL(12));
it_vector.assign(int_vector_1.begin(), int_vector_1.end());
PRINT(it_vector)
std::cout << "\nassign 0\n";
int_vector_2.assign(1, VAL(6));
it_vector.assign(int_vector_2.begin(), int_vector_2.end() - 1);
PRINT(it_vector)
std::cout << "\nassign 266 - 13 - 172 = 81\n";
int_vector_3.assign(266, VAL(1));
it_vector.assign(int_vector_3.begin() + 13, int_vector_3.end() - 172);
PRINT(it_vector)
DELETE
}
TEST_V(tests_vector_push_back)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> 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(VAL(myint[i]));
std::cout << "[capacity : "
<< std::setw(2) << myvector.capacity() << "] "
<< myvector[i] << "\n";
}
PRINT(myvector)
// title
TITLE(big push back :)
for (int i = 0; i < 72363; i++)
{
myvector.push_back(VAL(9));
std::cout << "[" << i
<< ":" << myvector.capacity() << "] ";
}
std::cout << " -> size : " << myvector.size() << " , capacity :" << myvector.capacity() << "\n";
DELETE
}
TEST_V(tests_vector_pop_back)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector;
int sum (0);
myvector.push_back (VAL(100));
myvector.push_back (VAL(200));
myvector.push_back (VAL(300));
while (!myvector.empty())
{
sum+=TOI(myvector.back());
myvector.pop_back();
}
std::cout << "The elements of myvector add up to " << sum << '\n';
// title
TITLE(check state :)
std::cout << "size : " << myvector.size() << '\n';
std::cout << "capacity : " << myvector.capacity() << '\n';
DELETE
}
TEST_V(tests_vector_insert)
{
typename ft::vector<T>::iterator it;
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector (3,VAL(100));
it = myvector.begin();
it = myvector.insert ( it , VAL(200) );
myvector.insert (it,2,VAL(300));
ft::vector<T> anothervector (2,VAL(400));
it = myvector.begin();
myvector.insert (it+2,anothervector.begin(),anothervector.end());
T myarray [] = { VAL(501),VAL(502),VAL(503) };
myvector.insert (myvector.begin(), myarray, myarray+3);
PRINT(myvector)
// title
TITLE(tests positions on insert(pos, value) :)
ft::vector<T> myvector2 (3,VAL(100));
it = myvector2.begin();
std::cout << "size:" << myvector2.size() << " capacity:" << myvector2.capacity() << "\n";
myvector2.insert ( it , VAL(200) );
PRINT(myvector2)
ft::vector<T> myvector3 (3,VAL(100));
it = myvector3.end();
std::cout << "\nsize:" << myvector3.size() << " capacity:" << myvector3.capacity() << "\n";
myvector3.insert ( it , VAL(200) );
PRINT(myvector3)
ft::vector<T> myvector4 (3,VAL(100));
it = myvector4.begin() + 2;
std::cout << "\nsize:" << myvector3.size() << " capacity:" << myvector3.capacity() << "\n";
myvector4.insert ( it , VAL(200) );
PRINT(myvector4)
// title
TITLE(tests insert(pos, size, value) :)
ft::vector<T> myvector5;
for (int i = 1; i <= 5; i++)
myvector5.push_back(VAL(i * 100));
it = myvector5.begin() + 1;
myvector5.insert ( it , VAL(150) );
it = myvector5.end();
myvector5.insert (it,2,VAL(600));
it = myvector5.end() - 2;
myvector5.insert (it,2,VAL(550));
PRINT(myvector5)
// title
TITLE(tests insert(pos, first, last) :)
ft::vector<T> myvector6;
myvector6.assign(5, VAL(42));
it = myvector6.begin() + 2;
myvector6.insert ( it, myvector5.begin() + 3, myvector5.end() - 2 );
PRINT(myvector6)
DELETE
}
TEST_V(tests_vector_erase)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector;
// set some values (from 1 to 10)
for (int i=1; i<=10; i++) myvector.push_back(VAL(i));
// erase the 6th element
myvector.erase (myvector.begin()+5);
// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3);
PRINT(myvector)
// title
TITLE(test iterator bigger or equal :)
ft::vector<T> vector2;
for (int i=1; i<=100; i++) vector2.push_back(VAL(i));
PRINT(vector2)
std::cout << "\nerase pos(7,36) :\n";
vector2.erase(vector2.begin() + 7, vector2.begin() + 36);
PRINT(vector2)
std::cout << "\nerase pos(46,54) :\n";
vector2.erase(vector2.begin() + 46, vector2.begin() + 54);
PRINT(vector2)
std::cout << "\nerase pos(7,7) :\n";
vector2.erase(vector2.begin() + 7, vector2.begin() + 7);
PRINT(vector2)
DELETE
}
TEST_V(tests_vector_swap)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> foo (3,VAL(100)); // three ints with a value of 100
ft::vector<T> bar (5,VAL(200)); // five ints with a value of 200
foo.swap(bar);
PRINT(foo)
PRINT(bar)
DELETE
}
TEST_V(tests_vector_clear)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector;
myvector.push_back (VAL(100));
myvector.push_back (VAL(200));
myvector.push_back (VAL(300));
PRINT(myvector)
myvector.clear();
myvector.push_back (VAL(1101));
myvector.push_back (VAL(2202));
PRINT(myvector)
DELETE
}
TEST_V(tests_vector_get_allocator)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> myvector;
T * 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],VAL(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);
DELETE
}
TEST_V(tests_vector_relational_operators)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> foo (3,VAL(100)); // three ints with a value of 100
ft::vector<T> bar (2,VAL(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";
DELETE
}
TEST_V(tests_vector_swap_non_member)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<T> foo (3,VAL(100)); // three ints with a value of 100
ft::vector<T> bar (5,VAL(200)); // five ints with a value of 200
foo.swap(bar);
PRINT(foo)
PRINT(bar)
DELETE
}
TEST_V(tests_vector_reverse_iterators)
{
// title
TITLE(cplusplus.com reference)
TITLE(::constructor ::operator* ::operator++(val))
ft::vector<T> myvector;
for (int i=0; i<10; i++) myvector.push_back(VAL(i));
typedef typename ft::vector<T>::iterator iter_type;
// ? 0 1 2 3 4 5 6 7 8 9 ?
iter_type from (myvector.begin()); // ^
iter_type until (myvector.end()); // ^
ft::reverse_iterator<iter_type> rev_until (from); // ^
ft::reverse_iterator<iter_type> rev_from (until); // ^
std::cout << "myvector:";
while (rev_from != rev_until)
std::cout << ' ' << *rev_from++;
std::cout << '\n';
// title
TITLE(::operator++)
std::cout << "myvector:";
while (rev_from != rev_until) {
std::cout << ' ' << *rev_from;
++rev_from;
}
std::cout << '\n';
// title
TITLE(::base)
ft::reverse_iterator<iter_type> rev_end (myvector.begin());
ft::reverse_iterator<iter_type> rev_begin (myvector.end());
std::cout << "myvector:";
for (iter_type it = rev_end.base(); it != rev_begin.base(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
// title
TITLE(::operator+)
ft::reverse_iterator<iter_type> rev_it_add;
rev_it_add = myvector.rbegin() +3;
std::cout << "The fourth element from the end is: " << *rev_it_add << '\n';
// title
TITLE(::operator+=)
ft::reverse_iterator<iter_type> rev_it_add_equal = myvector.rbegin();
rev_it_add_equal += 2;
std::cout << "The third element from the end is: " << *rev_it_add_equal << '\n';
// title
TITLE(::operator-)
ft::reverse_iterator<iter_type> rev_it_minus;
rev_it_minus = myvector.rend() - 3;
std::cout << "myvector.rend()-3 points to: " << *rev_it_minus << '\n';
// title
TITLE(::operator--)
ft::reverse_iterator<iter_type> rev_it_minus_minus = rev_begin;
while ( rev_it_minus_minus != rev_end )
std::cout << *rev_it_minus_minus++ << ' ';
std::cout << '\n';
while ( rev_it_minus_minus != rev_begin )
std::cout << *(--rev_it_minus_minus) << ' ';
std::cout << '\n';
// title
TITLE(::operator-=)
ft::reverse_iterator<iter_type> rev_it_minus_equal = myvector.rend();
rev_it_minus_equal -= 4;
std::cout << "rev_it_minus_equal now points to: " << *rev_it_minus_equal << '\n';
// title
TITLE(::operator->)
std::map<int,std::string> numbers;
numbers.insert (std::make_pair(1,"one"));
numbers.insert (std::make_pair(2,"two"));
numbers.insert (std::make_pair(3,"three"));
typedef std::map<int,std::string>::iterator map_iter;
ft::reverse_iterator<map_iter> rev_map_end (numbers.begin());
ft::reverse_iterator<map_iter> rev_map_ite (numbers.end());
for ( ; rev_map_ite != rev_map_end ; ++rev_map_ite )
std::cout << rev_map_ite->first << ' ' << rev_map_ite->second << '\n';
// title
TITLE(::operator[])
ft::reverse_iterator<iter_type> rev_it_at = myvector.rbegin();
std::cout << "The fourth element from the end is: " << rev_it_at[3] << '\n';
// title
TITLE(::operator- non-member)
ft::reverse_iterator<typename ft::vector<T>::iterator> rev_it_from,rev_it_until;
rev_it_from = myvector.rbegin();
rev_it_until = myvector.rend();
std::cout << "myvector has " << (rev_it_until-rev_it_from) << " elements.\n";
// title
TITLE(::operator+ non-member)
ft::reverse_iterator<iter_type> rev_it_n_memb;
rev_it_n_memb = 3 + myvector.rbegin();
std::cout << "The fourth element from the end is: " << *rev_it_n_memb << '\n';
DELETE
}
#endif