vector finish

This commit is contained in:
hugogogo
2022-06-14 21:25:16 +02:00
parent 988d67e908
commit 005382a3ac
11 changed files with 584 additions and 71 deletions

View File

@@ -1,11 +1,7 @@
#ifndef TESTS_VECTORS_CPP
#define TESTS_VECTORS_CPP
#include <iostream>
#include <string>
//#include "colors.h"
#include "tests.hpp"
#include <iomanip> // std::setw()
void tests_vector_constructor()
@@ -106,7 +102,6 @@ void tests_vector_end()
TESTEND
}
/*
void tests_vector_rbegin()
{
TEST(vector::rbegin)
@@ -152,8 +147,6 @@ void tests_vector_rend()
}
TESTEND
}
*/
void tests_vector_size()
{
@@ -967,4 +960,181 @@ void tests_vector_get_allocator()
TESTEND
}
void tests_vector_relational_operators()
{
TEST(ft::relational operators)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<int> foo (3,100); // three ints with a value of 100
ft::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";
}
TESTEND
}
void tests_vector_swap_non_member()
{
TEST(ft::swap vector)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<int> foo (3,100); // three ints with a value of 100
ft::vector<int> bar (5,200); // five ints with a value of 200
foo.swap(bar);
std::cout << "foo contains:";
for (ft::vector<int>::iterator it = foo.begin(); it!=foo.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "bar contains:";
for (ft::vector<int>::iterator it = bar.begin(); it!=bar.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
}
TESTEND
}
void tests_vector_reverse_iterators()
{
TEST(ft::reverse_iterator)
{
// title
TITLE(cplusplus.com reference)
TITLE(::constructor ::operator* ::operator++(int))
ft::vector<int> myvector;
for (int i=0; i<10; i++) myvector.push_back(i);
typedef ft::vector<int>::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<ft::vector<int>::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';
}
TESTEND
}
#endif