all vector tests pass

This commit is contained in:
hugogogo
2022-06-17 01:20:46 +02:00
parent da4ebc13fd
commit df6332b496
2 changed files with 235 additions and 280 deletions

View File

@@ -28,13 +28,13 @@ int main() {
tests_vector_push_back(); tests_vector_push_back();
tests_vector_pop_back(); tests_vector_pop_back();
tests_vector_insert(); tests_vector_insert();
// tests_vector_erase(); tests_vector_erase();
// tests_vector_swap(); tests_vector_swap();
// tests_vector_clear(); tests_vector_clear();
// tests_vector_get_allocator(); tests_vector_get_allocator();
// tests_vector_relational_operators(); tests_vector_relational_operators();
// tests_vector_swap_non_member(); tests_vector_swap_non_member();
// tests_vector_reverse_iterators(); tests_vector_reverse_iterators();
// MAP // MAP
// tests_map_constructor(); // tests_map_constructor();

View File

@@ -488,7 +488,6 @@ TEST(tests_vector_push_back)
TITLE(big push back :) TITLE(big push back :)
for (int i = 0; i < 72363; i++) for (int i = 0; i < 72363; i++)
// for (int i = 0; i < 363; i++)
{ {
myvector.push_back(VAL(9)); myvector.push_back(VAL(9));
std::cout << "[" << i std::cout << "[" << i
@@ -616,324 +615,280 @@ TEST(tests_vector_insert)
DELETE DELETE
} }
/* TEST(tests_vector_erase)
void tests_vector_erase()
{ {
TEST(vector::erase) // title
{ TITLE(cplusplus.com reference :)
// title
TITLE(cplusplus.com reference :)
ft::vector<int> myvector; ft::vector<T> myvector;
// set some values (from 1 to 10) // set some values (from 1 to 10)
for (int i=1; i<=10; i++) myvector.push_back(i); for (int i=1; i<=10; i++) myvector.push_back(VAL(i));
// erase the 6th element // erase the 6th element
myvector.erase (myvector.begin()+5); myvector.erase (myvector.begin()+5);
// erase the first 3 elements: // erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3); myvector.erase (myvector.begin(),myvector.begin()+3);
std::cout << "myvector contains:"; std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); ++i) PRINT(myvector)
std::cout << ' ' << myvector[i];
std::cout << '\n';
// title
TITLE(test iterator bigger or equal :)
ft::vector<T> vector2;
// title for (int i=1; i<=100; i++) vector2.push_back(VAL(i));
TITLE(test iterator bigger or equal :) PRINT(vector2)
ft::vector<int> vector2;
int size;
for (int i=1; i<=100; i++) vector2.push_back(i); std::cout << "\nerase pos(7,36) :\n";
size = vector2.size(); vector2.erase(vector2.begin() + 7, vector2.begin() + 36);
for (int i = 0; i < size; i++) PRINT(vector2)
std::cout << "[" << i << "] " << vector2[i] << " ";
std::cout << "\nsize:" << size << " capacity:" << vector2.capacity() << "\n";
std::cout << "\nerase pos(7,36) :\n"; std::cout << "\nerase pos(46,54) :\n";
vector2.erase(vector2.begin() + 7, vector2.begin() + 36); vector2.erase(vector2.begin() + 46, vector2.begin() + 54);
size = vector2.size(); PRINT(vector2)
for (int i = 0; i < size; i++)
std::cout << "[" << i << "] " << vector2[i] << " ";
std::cout << "\nsize:" << size << " capacity:" << vector2.capacity() << "\n";
std::cout << "\nerase pos(46,54) :\n"; std::cout << "\nerase pos(7,7) :\n";
vector2.erase(vector2.begin() + 46, vector2.begin() + 54); vector2.erase(vector2.begin() + 7, vector2.begin() + 7);
size = vector2.size(); PRINT(vector2)
for (int i = 0; i < size; i++)
std::cout << "[" << i << "] " << vector2[i] << " ";
std::cout << "\nsize:" << size << " capacity:" << vector2.capacity() << "\n";
std::cout << "\nerase pos(7,7) :\n"; DELETE
vector2.erase(vector2.begin() + 7, vector2.begin() + 7);
size = vector2.size();
for (int i = 0; i < size; i++)
std::cout << "[" << i << "] " << vector2[i] << " ";
std::cout << "\nsize:" << size << " capacity:" << vector2.capacity() << "\n";
}
TESTEND
} }
void tests_vector_swap() TEST(tests_vector_swap)
{ {
TEST(vector::swap) // title
{ TITLE(cplusplus.com reference :)
// title
TITLE(cplusplus.com reference :)
ft::vector<int> foo (3,100); // three ints with a value of 100 ft::vector<T> foo (3,VAL(100)); // three ints with a value of 100
ft::vector<int> bar (5,200); // five ints with a value of 200 ft::vector<T> bar (5,VAL(200)); // five ints with a value of 200
foo.swap(bar); foo.swap(bar);
std::cout << "foo contains:"; std::cout << "foo contains:";
for (unsigned i=0; i<foo.size(); i++) PRINT(foo)
std::cout << ' ' << foo[i];
std::cout << '\n';
std::cout << "bar contains:"; std::cout << "bar contains:";
for (unsigned i=0; i<bar.size(); i++) PRINT(bar)
std::cout << ' ' << bar[i];
std::cout << '\n'; DELETE
}
TESTEND
} }
void tests_vector_clear() TEST(tests_vector_clear)
{ {
TEST(vector::clear) // title
{ TITLE(cplusplus.com reference :)
// title
TITLE(cplusplus.com reference :)
ft::vector<int> myvector; ft::vector<T> myvector;
myvector.push_back (100); myvector.push_back (VAL(100));
myvector.push_back (200); myvector.push_back (VAL(200));
myvector.push_back (300); myvector.push_back (VAL(300));
std::cout << "myvector contains:"; std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); i++) PRINT(myvector)
std::cout << ' ' << myvector[i];
std::cout << '\n';
myvector.clear(); myvector.clear();
myvector.push_back (1101); myvector.push_back (VAL(1101));
myvector.push_back (2202); myvector.push_back (VAL(2202));
std::cout << "myvector contains:"; std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); i++) PRINT(myvector)
std::cout << ' ' << myvector[i];
std::cout << '\n'; DELETE
}
TESTEND
} }
void tests_vector_get_allocator() TEST(tests_vector_get_allocator)
{ {
TEST(vector::get_allocator) // title
{ TITLE(cplusplus.com reference :)
// title
TITLE(cplusplus.com reference :)
ft::vector<int> myvector; ft::vector<T> myvector;
int * p; T * p;
unsigned int i; unsigned int i;
// allocate an array with space for 5 elements using vector's allocator: // allocate an array with space for 5 elements using vector's allocator:
p = myvector.get_allocator().allocate(5); p = myvector.get_allocator().allocate(5);
// construct values in-place on the array: // construct values in-place on the array:
for (i=0; i<5; i++) myvector.get_allocator().construct(&p[i],i); for (i=0; i<5; i++) myvector.get_allocator().construct(&p[i],VAL(i));
std::cout << "The allocated array contains:"; std::cout << "The allocated array contains:";
for (i=0; i<5; i++) std::cout << ' ' << p[i]; for (i=0; i<5; i++) std::cout << ' ' << p[i];
std::cout << '\n'; std::cout << '\n';
// destroy and deallocate: // destroy and deallocate:
for (i=0; i<5; i++) myvector.get_allocator().destroy(&p[i]); for (i=0; i<5; i++) myvector.get_allocator().destroy(&p[i]);
myvector.get_allocator().deallocate(p,5); myvector.get_allocator().deallocate(p,5);
}
TESTEND DELETE
} }
void tests_vector_relational_operators() TEST(tests_vector_relational_operators)
{ {
TEST(ft::relational operators) // title
{ TITLE(cplusplus.com reference :)
// title
TITLE(cplusplus.com reference :)
ft::vector<int> foo (3,100); // three ints with a value of 100 ft::vector<T> foo (3,VAL(100)); // three ints with a value of 100
ft::vector<int> bar (2,200); // two ints with a value of 200 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 equal\n";
if (foo!=bar) std::cout << "foo and bar are not 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 less than bar\n";
if (foo> bar) std::cout << "foo is greater 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 less than or equal to bar\n";
if (foo>=bar) std::cout << "foo is greater 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 DELETE
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(tests_vector_swap_non_member)
{ {
TEST(ft::reverse_iterator) // title
{ TITLE(cplusplus.com reference :)
// title
TITLE(cplusplus.com reference)
TITLE(::constructor ::operator* ::operator++(int))
ft::vector<int> myvector; ft::vector<T> foo (3,VAL(100)); // three ints with a value of 100
for (int i=0; i<10; i++) myvector.push_back(i); ft::vector<T> bar (5,VAL(200)); // five ints with a value of 200
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:"; foo.swap(bar);
while (rev_from != rev_until)
std::cout << ' ' << *rev_from++;
std::cout << '\n';
std::cout << "foo contains:";
PRINT(foo)
// title std::cout << "bar contains:";
TITLE(::operator++) PRINT(bar)
std::cout << "myvector:"; DELETE
while (rev_from != rev_until) { }
std::cout << ' ' << *rev_from;
++rev_from; TEST(tests_vector_reverse_iterators)
} {
std::cout << '\n'; // title
TITLE(cplusplus.com reference)
TITLE(::constructor ::operator* ::operator++(val))
// title
TITLE(::base) ft::vector<T> myvector;
for (int i=0; i<10; i++) myvector.push_back(VAL(i));
ft::reverse_iterator<iter_type> rev_end (myvector.begin()); typedef typename ft::vector<T>::iterator iter_type;
ft::reverse_iterator<iter_type> rev_begin (myvector.end()); // ? 0 1 2 3 4 5 6 7 8 9 ?
std::cout << "myvector:"; iter_type from (myvector.begin()); // ^
for (iter_type it = rev_end.base(); it != rev_begin.base(); ++it) iter_type until (myvector.end()); // ^
std::cout << ' ' << *it; ft::reverse_iterator<iter_type> rev_until (from); // ^
std::cout << '\n'; ft::reverse_iterator<iter_type> rev_from (until); // ^
std::cout << "myvector:";
// title while (rev_from != rev_until)
TITLE(::operator+) std::cout << ' ' << *rev_from++;
std::cout << '\n';
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++)
// title std::cout << "myvector:";
TITLE(::operator+=) while (rev_from != rev_until) {
std::cout << ' ' << *rev_from;
ft::reverse_iterator<iter_type> rev_it_add_equal = myvector.rbegin(); ++rev_from;
rev_it_add_equal += 2; }
std::cout << "The third element from the end is: " << *rev_it_add_equal << '\n'; std::cout << '\n';
// title // title
TITLE(::operator-) TITLE(::base)
ft::reverse_iterator<iter_type> rev_it_minus; ft::reverse_iterator<iter_type> rev_end (myvector.begin());
rev_it_minus = myvector.rend() - 3; ft::reverse_iterator<iter_type> rev_begin (myvector.end());
std::cout << "myvector.rend()-3 points to: " << *rev_it_minus << '\n'; std::cout << "myvector:";
for (iter_type it = rev_end.base(); it != rev_begin.base(); ++it)
std::cout << ' ' << *it;
// title std::cout << '\n';
TITLE(::operator--)
ft::reverse_iterator<iter_type> rev_it_minus_minus = rev_begin; // title
while ( rev_it_minus_minus != rev_end ) TITLE(::operator+)
std::cout << *rev_it_minus_minus++ << ' ';
std::cout << '\n'; ft::reverse_iterator<iter_type> rev_it_add;
rev_it_add = myvector.rbegin() +3;
while ( rev_it_minus_minus != rev_begin ) std::cout << "The fourth element from the end is: " << *rev_it_add << '\n';
std::cout << *(--rev_it_minus_minus) << ' ';
std::cout << '\n';
// title
TITLE(::operator+=)
// title
TITLE(::operator-=) ft::reverse_iterator<iter_type> rev_it_add_equal = myvector.rbegin();
rev_it_add_equal += 2;
ft::reverse_iterator<iter_type> rev_it_minus_equal = myvector.rend(); std::cout << "The third element from the end is: " << *rev_it_add_equal << '\n';
rev_it_minus_equal -= 4;
std::cout << "rev_it_minus_equal now points to: " << *rev_it_minus_equal << '\n';
// title
TITLE(::operator-)
// title
TITLE(::operator->) ft::reverse_iterator<iter_type> rev_it_minus;
rev_it_minus = myvector.rend() - 3;
std::map<int,std::string> numbers; std::cout << "myvector.rend()-3 points to: " << *rev_it_minus << '\n';
numbers.insert (std::make_pair(1,"one"));
numbers.insert (std::make_pair(2,"two"));
numbers.insert (std::make_pair(3,"three")); // title
TITLE(::operator--)
typedef std::map<int,std::string>::iterator map_iter;
ft::reverse_iterator<map_iter> rev_map_end (numbers.begin()); ft::reverse_iterator<iter_type> rev_it_minus_minus = rev_begin;
ft::reverse_iterator<map_iter> rev_map_ite (numbers.end()); while ( rev_it_minus_minus != rev_end )
std::cout << *rev_it_minus_minus++ << ' ';
for ( ; rev_map_ite != rev_map_end ; ++rev_map_ite ) std::cout << '\n';
std::cout << rev_map_ite->first << ' ' << rev_map_ite->second << '\n';
while ( rev_it_minus_minus != rev_begin )
std::cout << *(--rev_it_minus_minus) << ' ';
// title std::cout << '\n';
TITLE(::operator[])
ft::reverse_iterator<iter_type> rev_it_at = myvector.rbegin(); // title
std::cout << "The fourth element from the end is: " << rev_it_at[3] << '\n'; TITLE(::operator-=)
ft::reverse_iterator<iter_type> rev_it_minus_equal = myvector.rend();
// title rev_it_minus_equal -= 4;
TITLE(::operator- non-member) std::cout << "rev_it_minus_equal now points to: " << *rev_it_minus_equal << '\n';
ft::reverse_iterator<ft::vector<int>::iterator> rev_it_from,rev_it_until;
rev_it_from = myvector.rbegin(); // title
rev_it_until = myvector.rend(); TITLE(::operator->)
std::cout << "myvector has " << (rev_it_until-rev_it_from) << " elements.\n";
std::map<int,std::string> numbers;
numbers.insert (std::make_pair(1,"one"));
// title numbers.insert (std::make_pair(2,"two"));
TITLE(::operator+ non-member) numbers.insert (std::make_pair(3,"three"));
ft::reverse_iterator<iter_type> rev_it_n_memb; typedef std::map<int,std::string>::iterator map_iter;
rev_it_n_memb = 3 + myvector.rbegin(); ft::reverse_iterator<map_iter> rev_map_end (numbers.begin());
std::cout << "The fourth element from the end is: " << *rev_it_n_memb << '\n'; ft::reverse_iterator<map_iter> rev_map_ite (numbers.end());
} for ( ; rev_map_ite != rev_map_end ; ++rev_map_ite )
TESTEND 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 #endif