correction of core dump

This commit is contained in:
hugogogo
2022-06-17 01:00:53 +02:00
parent e3151bce4e
commit da4ebc13fd
6 changed files with 1177 additions and 548 deletions

View File

@@ -92,6 +92,10 @@ stl: CFLAGS += -D STL
stl: re
ft: re
leakstl: CFLAGS += -D STL
leakstl: fclean leaks
leakft: fclean leaks
$(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS)
@echo $(CYAN)"compilation " $@ $(RESET)
@$(CC) $(CFLAGS) -c $< -o $@

View File

@@ -71,6 +71,7 @@ std::vector<mystruct*> mem_list;
// ****************************************
# define TITLE(s) std::cout << "\n" B_PURPLE #s RESET "\n\n";
# define VAL(n) val<T>(n)
# define TOI(n) toi<T>(n)
# define PRINT(n) print_vector<T>(n);
# define DELETE delete_structs();
@@ -106,6 +107,35 @@ template <>
}
// convert a value
// *****************************************
template <class T>
int toi(T t) {(void)t; return (0);
}
template <>
int toi(int i) {return (i);
}
template <>
int toi(char c) {return (c);
}
template <>
int toi(std::string str) {
int i;
std::stringstream stream;
stream << str;
stream >> i;
stream.clear();
return (i);
}
template <>
int toi(mystruct* s) {
return ( s->get_data()[0] );
}
// get a value
// *********************************************
template <class T>

View File

@@ -1,4 +1,3 @@
#ifndef VECTOR_HPP
# define VECTOR_HPP

View File

@@ -277,8 +277,8 @@ VT_TPL typename VT::iterator VT::
_allocator.construct(it, *(it - 1));
while (it-- != position)
*(it + 1) = *it;
_allocator.destroy(position);
}
_allocator.destroy(position);
_allocator.construct(position, val);
_size++;
return (position);
@@ -507,3 +507,752 @@ VT_TPL
#undef VT
#undef VT_TPL
/////////////////////////////////////////////////////////////////:
/////////////////////////////////////////////////////////////////:
///////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////:
//////////////////////////////////////////////////////:
/////////////////////////////////////////////////////////////:
///////////////////////////////////////////////////////////
// #define VT_TPL template <class T, class Allocator>
// #define VT vector<T, Allocator>
//
// namespace ft {
//
//
//
// /*********************************************
// * COPLIENS
// *********************************************/
// // constructors ------------------------------
// VT_TPL VT::
// vector( const Allocator & alloc )
// : _size(0)
// , _capacity(0)
// , _mem_ptr(NULL)
// , _allocator(alloc) {
//
// return;
// }
// VT_TPL VT::
// vector( size_type n, const T & val, const Allocator & alloc )
// : _size(0)
// , _capacity(0)
// , _mem_ptr(NULL)
// , _allocator(alloc) {
//
// assign(n, val);
// return;
// }
// VT_TPL template <class InputIterator> VT::
// vector(InputIterator first, InputIterator last, const Allocator & alloc)
// : _size(0)
// , _capacity(0)
// , _mem_ptr(NULL)
// , _allocator(alloc) {
//
// assign(first, last);
// return;
// }
// // copy constructor --------------------------
// VT_TPL VT::
// vector( vector const & src )
// : _size(0)
// , _capacity(0)
// , _mem_ptr(NULL)
// , _allocator(src._allocator) {
//
// *this = src;
// return;
// }
// // destructors -------------------------------
// VT_TPL VT::
// ~vector() { return; }
// // operator= ---------------------------------
// VT_TPL VT & VT::
// operator=( vector const & rhs ) {
//
// vector new_vector;
//
// //Base::operator=(rhs);
// if ( this != &rhs )
// {
// new_vector.reserve(_capacity);
// new_vector.assign(rhs.begin(), rhs.end());
// swap(new_vector);
// //_size = rhs.size();
// }
// return *this;
// }
//
//
//
// /*************
// * iterators :
// *************/
// // begin -------------------------------------
// VT_TPL typename VT::iterator VT::
// begin() { return _mem_ptr; }
// VT_TPL typename VT::const_iterator VT::
// begin() const { return _mem_ptr; }
// // end ---------------------------------------
// VT_TPL typename VT::iterator VT::
// end() { return &_mem_ptr[_size]; }
// VT_TPL typename VT::const_iterator VT::
// end() const { return &_mem_ptr[_size]; }
// // rbegin ------------------------------------
// VT_TPL typename VT::reverse_iterator VT::
// rbegin() { return reverse_iterator(end()); }
// VT_TPL typename VT::const_reverse_iterator VT::
// rbegin() const { return const_reverse_iterator(end()); }
// // rend --------------------------------------
// VT_TPL typename VT::reverse_iterator VT::
// rend() { return reverse_iterator(begin()); }
// VT_TPL typename VT::const_reverse_iterator VT::
// rend() const { return const_reverse_iterator(begin()); }
//
//
//
// /************
// * capacity :
// ************/
// // size --------------------------------------
// VT_TPL typename VT::size_type VT::
// size( ) const { return _size; }
// // max_size ----------------------------------
// VT_TPL typename VT::size_type VT::
// max_size() const { return (_allocator.max_size()); }
// // resize ------------------------------------
// VT_TPL void VT::
// resize(size_type n, value_type val) {
//
// if (n > _size)
// {
// if (n > _capacity)
// _increment_capacity(n);
// while (_size != n)
// {
// _allocator.construct(&_mem_ptr[_size], val);
// ++_size;
// }
// }
// else if (n < _size)
// {
// while (_size != n)
// _allocator.destroy(&_mem_ptr[--_size]);
// }
// }
// // capacity ----------------------------------
// VT_TPL typename VT::size_type VT::
// capacity() const { return _capacity; }
// // empty -------------------------------------
// VT_TPL bool VT::
// empty() const { return (_size == 0); }
// // reserve -----------------------------------
// VT_TPL void VT::
// reserve( size_type new_cap ) {
//
// T* new_arr;
// T* old_arr = _mem_ptr;
//
// if (new_cap > max_size())
// throw std::length_error("vector::reserve");
//
// if (_capacity >= new_cap)
// return ;
// new_arr = _allocator.allocate(new_cap);
//
// if (old_arr)
// {
// iterator first = begin();
// iterator last = end();
// _mem_ptr = new_arr;
// _size = 0;
// assign(first, last);
// _destroy(first, last);
// _allocator.deallocate(old_arr, _capacity);
// }
// else
// _mem_ptr = new_arr;
//
// _capacity = new_cap;
//
//
// // value_type * tmp_ptr;
// // value_type * old_ptr = _mem_ptr;
// // iterator first = begin();
// // iterator last = end();
// //
// // if (new_cap > _allocator.max_size())
// // throw std::length_error("reserve: new_cap > max_size");
// // if (_capacity == _allocator.max_size())
// // throw std::length_error("reserve: capacity == max_size");
// // if (new_cap <= _capacity)
// // return ;
// //
// // _capacity = new_cap;
// // tmp_ptr = _allocator.allocate(new_cap);
// //
// // if (_mem_ptr)
// // {
// // _mem_ptr = tmp_ptr;
// // _size = 0;
// // assign(first, last);
// // _destroy(begin(), end());
// // _allocator.deallocate(old_ptr, _capacity);
// // }
// // _mem_ptr = tmp_ptr;
// }
//
//
//
// /******************
// * element access :
// ******************/
// // operator[] --------------------------------
// VT_TPL typename VT::reference VT::
// operator[](size_type n) { return _mem_ptr[n]; }
// VT_TPL typename VT::const_reference VT::
// operator[](size_type n) const { return _mem_ptr[n]; }
// // at ----------------------------------------
// VT_TPL typename VT::reference VT::
// at(size_type n) {
//
// if (n >= _size)
// throw std::out_of_range("vector out of range");
// return (_mem_ptr[n]);
// }
// VT_TPL typename VT::const_reference VT::
// at(size_type n) const {
//
// if (n >= _size)
// throw std::out_of_range("vector out of range");
// return (_mem_ptr[n]);
// }
// // front -------------------------------------
// VT_TPL typename VT::reference VT::
// front() { return (*_mem_ptr); }
// VT_TPL typename VT::const_reference VT::
// front() const { return (*_mem_ptr); }
// // back --------------------------------------
// VT_TPL typename VT::reference VT::
// back() { return (_mem_ptr[_size - 1]); }
// VT_TPL typename VT::const_reference VT::
// back() const { return (_mem_ptr[_size - 1]); }
//
//
//
// /*************
// * modifiers :
// *************/
// // assign ------------------------------------
// VT_TPL template <class InputIterator>
// typename enable_if< !is_integral<InputIterator>::value,void >::type VT::
// assign( InputIterator first, InputIterator last) {
//
// _assign(first, last, typename iterator_traits<InputIterator>::iterator_category());
//
// // InputIterator tmp = first;
// // unsigned int range = 0;
// //
// // clear();
// //
// // while (tmp++ != last)
// // range++;
// // if (range >= _capacity)
// // _increment_capacity(range);
// // while (first != last)
// // {
// // _allocator.construct(&_mem_ptr[_size], *first);
// // first++;
// // _size++;
// // }
// }
// VT_TPL
// template < typename InputIt >
// void VT::
// _assign(InputIt first, InputIt last, std::input_iterator_tag)
// {
// clear();
//
// while (first != last)
// {
// if (_size + 1 > _capacity)
// _increment_capacity(_size + 1);
// _allocator.construct(&_mem_ptr[_size], *first);
// ++first;
// ++_size;
// }
// }
// VT_TPL
// template < typename ForwardIt >
// void VT::
// _assign(ForwardIt first, ForwardIt last, std::forward_iterator_tag)
// {
// clear();
//
// difference_type diff = std::distance(first, last);
// if (diff < 0)
// throw std::logic_error("Wrong iterator order");
//
// if (static_cast<size_type>(diff) > _capacity)
// _increment_capacity(diff);
//
// while (first != last)
// {
// _allocator.construct(&_mem_ptr[_size], *first);
// ++first;
// ++_size;
// }
// }
//
//
// VT_TPL void VT::
// assign( size_type n, const T & val ) {
//
// if (n > _allocator.max_size())
// throw std::length_error("assign: n > max_size");
//
// value_type * tmp_ptr;
//
// _destroy(begin(), end());
// if (n > _capacity)
// {
// _capacity = n;
// tmp_ptr = _allocator.allocate(n);
// if (_mem_ptr)
// _allocator.deallocate(_mem_ptr, _capacity);
// _mem_ptr = tmp_ptr;
// }
// _size = n;
// while (n)
// _allocator.construct(&_mem_ptr[--n], val);
// }
// // push_back ---------------------------------
// VT_TPL void VT::
// push_back( const value_type & element ) {
//
// if (_size >= _capacity)
// _increment_capacity(1);
// _allocator.construct(&_mem_ptr[_size], element);
// _size++;
// }
// // pop_back ----------------------------------
// VT_TPL void VT::
// pop_back() { _allocator.destroy(end() - 1); _size--; }
// // insert ------------------------------------
// VT_TPL typename VT::iterator VT::
// insert(iterator pos, const value_type& value) {
// // insert(iterator position, const value_type& val) {
//
// if (_size + 1 > _capacity)
// {
// difference_type offset = pos - begin();
// _increment_capacity(_size + 1);
// pos = begin() + offset;
// }
//
// iterator it_end = end();
// if (pos != it_end)
// {
// iterator i = it_end;
// --i;
// _allocator.construct(i + 1, *i);
// while (i != pos)
// {
// --i;
// *(i + 1) = *i;
// }
// _allocator.destroy(pos);
// }
// _allocator.construct(pos, value);
// _size += 1;
// return (pos);
//
//
//
// // difference_type distance;
// // iterator it;
// //
// // if (_size + 1 > _capacity)
// // {
// // distance = position - begin();
// // _increment_capacity(1);
// // position = begin() + distance;
// // }
// // it = end();
// // if (position != it)
// // {
// // _allocator.construct(it, *(it - 1));
// // while (it-- != position)
// // *(it + 1) = *it;
// // }
// // _allocator.destroy(position);
// // _allocator.construct(position, val);
// // _size++;
// // return (position);
// }
// VT_TPL void VT::
// insert(iterator pos, size_type count, const value_type& value) {
// // insert(iterator position, size_type n, const value_type& val) {
//
// if (_size + count > _capacity)
// {
// difference_type offset = pos - begin();
// // _auto_realloc(_size + count);
// _increment_capacity(count);
// pos = begin() + offset;
// }
//
// iterator it_end = end();
// if (pos != it_end)
// {
// iterator i = it_end;
// while (i + count != it_end && i != pos)
// {
// --i;
// _allocator.construct(i + count, *i);
// }
// while (i != pos)
// {
// --i;
// *(i + count) = *i;
// }
// // _destroy_objects(pos, std::min(pos + count, it_end));
// _destroy(pos, std::min(pos + count, it_end));
// }
//
// iterator last = pos + count;
// while (pos != last)
// {
// _allocator.construct(pos, value);
// ++pos;
// }
// _size += count;
//
// // difference_type distance;
// // iterator it_end;
// // iterator it;
// //
// // if (_size + n > _capacity)
// // {
// // distance = position - begin();
// // _increment_capacity(n);
// // position = begin() + distance;
// // }
// //
// // it_end = end();
// // if (position != it_end)
// // {
// // it = it_end;
// // while (it + n != it_end && it != position)
// // {
// // it--;
// // _allocator.construct(it + n, *it);
// // }
// // while (it != position)
// // {
// // it--;
// // *(it + n) = *it;
// // }
// // _destroy(position, std::min(position + n, it_end));
// // }
// //
// // for (size_type i = 0; i < n; i++, position++)
// // _allocator.construct(position, val);
// // _size += n;
// }
// VT_TPL template <class InputIterator>
// typename enable_if< !is_integral<InputIterator>::value,void >::type VT::
// insert(iterator position, InputIterator first, InputIterator last) {
//
// difference_type dist;
// difference_type n;
// iterator it_end;
// iterator it;
//
// n = std::distance(first, last);
// if (_size + n > _capacity)
// {
// dist = position - begin();
// _increment_capacity(n);
// position = begin() + dist;
// }
//
// it_end = end();
// if (position != it_end)
// {
// it = it_end;
// while (it + n != it_end && it != position)
// {
// it--;
// _allocator.construct(it + n, *it);
// }
// while (it != position)
// {
// it--;
// *(it + n) = *it;
// }
// _destroy(position, std::min(position + n, it_end));
// }
//
// while (first != last)
// {
// _allocator.construct(position, *first);
// ++position;
// ++first;
// }
//
// _size += n;
// }
// // erase -------------------------------------
// VT_TPL typename VT::iterator VT::
// erase(iterator position) {
//
// iterator i = position;
// iterator it_end = end() - 1;
//
// while (i != it_end)
// {
// *i = *(i + 1);
// ++i;
// }
// _allocator.destroy(it_end);
// _size -= 1;
// return (position);
// }
// VT_TPL typename VT::iterator VT::
// erase(iterator first, iterator last) {
//
// iterator it_end = end();
// difference_type diff = std::distance(first, last);
//
// if (diff <= 0)
// return (first);
//
// it_end = end();
// while (last != it_end)
// {
// *first = *last;
// first++;
// last++;
// }
// _destroy(it_end - diff, it_end);
// _size -= diff;
//
// return (first);
// }
// // swap --------------------------------------
// VT_TPL void VT::
// swap(vector& x) {
//
// T* tmp_mem_ptr;
// size_type tmp_size;
// size_type tmp_capacity;
//
// tmp_mem_ptr = x._mem_ptr;
// tmp_size = x._size;
// tmp_capacity = x._capacity;
//
// x._mem_ptr = _mem_ptr;
// x._size = _size;
// x._capacity = _capacity;
//
// _mem_ptr = tmp_mem_ptr;
// _size = tmp_size;
// _capacity = tmp_capacity;
// }
// // clear -------------------------------------
// VT_TPL void VT::
// clear() { _destroy(begin(), end()); _size = 0; }
//
//
//
// /*************
// * allocator :
// *************/
// // get_allocator -----------------------------
// VT_TPL typename VT::allocator_type VT::
// get_allocator() const { return (_allocator); }
//
//
//
// /*********************************************
// * PRIVATE MEMBER FUNCTIONS
// *********************************************/
// VT_TPL void VT::
// _destroy(iterator first, iterator last) {
//
// while (first != last)
// {
// _allocator.destroy(first);
// first++;
// }
// }
// VT_TPL void VT::
// _increment_capacity(size_type min_new_cap) {
// // _increment_capacity(size_type n) {
//
// size_type new_cap;
//
// if (_capacity == max_size())
// throw std::length_error("vector::reserve");
// new_cap = std::min(max_size() / 2, _size) * 2;
// new_cap = std::max(new_cap, min_new_cap);
// reserve(new_cap);
//
//
// // size_type res;
// //
// // res = std::max(_size * 2, n);
// // reserve(std::min(res, _allocator.max_size()));
// }
//
//
//
// /*********************************************
// * NESTED CLASS
// *********************************************/
// //void vector::Class::function() {}
//
//
//
// /*********************************************
// * STATICS
// *********************************************/
// //std::string const vector::_bar = "bar";
//
//
//
// /************************
// * non-member functions :
// ************************/
// // operator == -------------------------------
// VT_TPL
// bool operator== (const VT & lhs, const VT & rhs) {
//
// if (lhs.size() != rhs.size())
// return false;
// return ft::equal(lhs.begin(), lhs.end(), rhs.begin());
// }
// // operator < --------------------------------
// VT_TPL
// bool operator< (const VT & lhs, const VT & rhs) {
//
// return ft::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
// }
// // operator != -------------------------------
// VT_TPL
// bool operator!= (const VT & lhs, const VT & rhs) { return !(lhs == rhs); }
// // operator <= -------------------------------
// VT_TPL
// bool operator<= (const VT & lhs, const VT & rhs) { return !(lhs > rhs); }
// // operator > --------------------------------
// VT_TPL
// bool operator> (const VT & lhs, const VT & rhs) { return (rhs < lhs); }
// // operator >= -------------------------------
// VT_TPL
// bool operator>= (const VT & lhs, const VT & rhs) { return !(lhs < rhs); }
// // swap (vector) -------------------------------
// VT_TPL
// void swap (VT & lhs, VT & rhs) { lhs.swap(rhs); }
//
// } // namespace ft
//
// #undef VT
// #undef VT_TPL
//

View File

@@ -8,26 +8,26 @@
int main() {
// VECTOR
// tests_vector_constructor();
// tests_vector_operator_assignation();
// tests_vector_begin();
// tests_vector_end();
// tests_vector_rbegin();
// tests_vector_rend();
// tests_vector_size();
tests_vector_constructor();
tests_vector_operator_assignation();
tests_vector_begin();
tests_vector_end();
tests_vector_rbegin();
tests_vector_rend();
tests_vector_size();
tests_vector_max_size();
tests_vector_resize();
// tests_vector_capacity();
// tests_vector_empty();
// tests_vector_reserve();
// tests_vector_operator_access();
// tests_vector_at();
// tests_vector_front();
// tests_vector_back();
// tests_vector_assign();
// tests_vector_push_back();
// tests_vector_pop_back();
// tests_vector_insert();
tests_vector_capacity();
tests_vector_empty();
tests_vector_reserve();
tests_vector_operator_access();
tests_vector_at();
tests_vector_front();
tests_vector_back();
tests_vector_assign();
tests_vector_push_back();
tests_vector_pop_back();
tests_vector_insert();
// tests_vector_erase();
// tests_vector_swap();
// tests_vector_clear();

View File

@@ -1,3 +1,4 @@
#ifndef TESTS_VECTORS_CPP
#define TESTS_VECTORS_CPP
@@ -241,535 +242,381 @@ TEST(tests_vector_resize)
DELETE
}
TEST(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(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(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(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;
}
std::cout << "myvector contains:";
PRINT(myvector)
DELETE
}
TEST(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);
std::cout << "myvector contains:";
PRINT(myvector)
DELETE
}
TEST(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(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(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(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++)
// for (int i = 0; i < 363; i++)
{
myvector.push_back(VAL(9));
std::cout << "[" << i
<< ":" << myvector.capacity() << "] ";
}
std::cout << " -> size : " << myvector.size() << " , capacity :" << myvector.capacity() << "\n";
DELETE
}
TEST(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(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);
std::cout << "myvector contains:";
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) );
std::cout << "myvector contains:";
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) );
std::cout << "myvector contains:";
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) );
std::cout << "myvector contains:";
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));
std::cout << "myvector contains:";
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
}
/*
void tests_vector_capacity()
{
TEST(vector::capacity)
{
// title
TITLE(cplusplus.com reference :)
ft::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';
std::cout << "max_size: " << (int) myvector.max_size() << '\n';
}
TESTEND
}
void tests_vector_empty()
{
TEST(vector::empty)
{
// title
TITLE(cplusplus.com reference :)
ft::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';
}
TESTEND
}
void tests_vector_reserve()
{
TEST(vector::reserve)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<int>::size_type sz;
ft::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';
}
}
ft::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';
}
}
}
TESTEND
}
void tests_vector_operator_access()
{
TEST(vector::operator[])
{
// title
TITLE(cplusplus.com reference :)
ft::vector<int> myvector (10); // 10 zero-initialized elements
ft::vector<int>::size_type sz = myvector.size();
// assign some values:
for (unsigned i=0; i<sz; i++) myvector[i]=i;
// reverse vector using operator[]:
for (unsigned i=0; i<sz/2; i++)
{
int temp;
temp = myvector[sz-1-i];
myvector[sz-1-i]=myvector[i];
myvector[i]=temp;
}
std::cout << "myvector contains:";
for (unsigned i=0; i<sz; i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
}
TESTEND
}
void tests_vector_at()
{
TEST(vector::at)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<int> myvector (10); // 10 zero-initialized ints
// assign some values:
for (unsigned i=0; i<myvector.size(); i++)
myvector.at(i)=i;
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); i++)
std::cout << ' ' << myvector.at(i);
std::cout << '\n';
}
TESTEND
}
void tests_vector_front()
{
TEST(vector::front)
{
// title
TITLE(cplusplus.com reference :)
ft::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';
}
TESTEND
}
void tests_vector_back()
{
TEST(vector::back)
{
// title
TITLE(cplusplus.com reference :)
ft::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';
// title
TITLE(test with negatives :)
myvector.push_back(236);
myvector.push_back(8973);
myvector.front() -= myvector.back();
std::cout << "myvector.front() is now " << myvector.front() << '\n';
// title
TITLE(test with char :)
ft::vector<char> letters;
letters.push_back('o');
letters.push_back('m');
letters.push_back('g');
letters.push_back('w');
letters.push_back('t');
letters.push_back('f');
if (!letters.empty()) {
std::cout << "The first character is '" << letters.front() << "'.\n";
}
}
TESTEND
}
void tests_vector_assign()
{
TEST(vector::assign)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<int> first;
ft::vector<int> second;
ft::vector<int> third;
first.assign (7,100); // 7 ints with a value of 100
ft::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';
// title
TITLE(capacity tests of assignation :)
ft::vector<int> myvector;
int size;
std::cout << "capacity before assignation : " << myvector.capacity() << "\n";
std::cout << "\nassign 1\n";
myvector.assign(1, 12);
size = myvector.size();
for (int i = 0; i < size; i++)
std::cout << "[" << i << "] " << myvector[i] << " - ";
std::cout << "\nsize : " << size << " , capacity : " << myvector.capacity() << "\n";
std::cout << "\nassign 3\n";
myvector.assign(3, 12);
size = myvector.size();
for (int i = 0; i < size; i++)
std::cout << "[" << i << "] " << myvector[i] << " - ";
std::cout << "\nsize : " << size << " , capacity : " << myvector.capacity() << "\n";
std::cout << "\nassign 7268\n";
myvector.assign(7268, 12);
size = myvector.size();
for (int i = 0; i < size; i++)
std::cout << "[" << i << "] " << myvector[i] << " - ";
std::cout << "\nsize : " << size << " , capacity : " << myvector.capacity() << "\n";
// title
TITLE(tests of iterators :)
ft::vector<int> int_vector_1;
ft::vector<int> int_vector_2;
ft::vector<int> int_vector_3;
ft::vector<int> it_vector;
int ssize;
std::cout << "\nassign 1\n";
int_vector_1.assign(1, 12);
it_vector.assign(int_vector_1.begin(), int_vector_1.end());
ssize = it_vector.size();
for (int i = 0; i < ssize; i++)
std::cout << "[" << i << "] " << it_vector[i] << " - ";
std::cout << "\nsize : " << ssize << " , capacity : " << it_vector.capacity() << "\n";
std::cout << "\nassign 0\n";
int_vector_2.assign(1, 6);
it_vector.assign(int_vector_2.begin(), int_vector_2.end() - 1);
ssize = it_vector.size();
for (int i = 0; i < ssize; i++)
std::cout << "[" << i << "] " << it_vector[i] << " - ";
std::cout << "\nsize : " << ssize << " , capacity : " << it_vector.capacity() << "\n";
std::cout << "\nassign 266 - 13 - 172 = 81\n";
int_vector_3.assign(266, 1);
it_vector.assign(int_vector_3.begin() + 13, int_vector_3.end() - 172);
ssize = it_vector.size();
for (int i = 0; i < ssize; i++)
std::cout << "[" << i << "] " << it_vector[i] << " - ";
std::cout << "\nsize : " << ssize << " , capacity : " << it_vector.capacity() << "\n";
}
TESTEND
}
void tests_vector_push_back()
{
TEST(vector::push_back)
{
// title
TITLE(cplusplus.com reference :)
ft::vector<int> 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]);
std::cout << "[capacity : "
<< std::setw(2) << myvector.capacity() << "] "
<< myvector[i] << "\n";
}
for (int i = 0; i < size; i++)
std::cout << "[" << std::setw(2) << i << "] " << myvector[i] << "\n";
std::cout << " -> size : " << myvector.size() << " , capacity :" << myvector.capacity() << "\n";
// title
TITLE(big push back :)
for (int i = 0; i < 72363; i++)
{
myvector.push_back(9);
std::cout << "[" << i
<< ":" << myvector.capacity() << "] ";
}
std::cout << " -> size : " << myvector.size() << " , capacity :" << myvector.capacity() << "\n";
}
TESTEND
}
void tests_vector_pop_back()
{
TEST(vector::pop_back)
{
// title
TITLE(cplusplus.com reference :)
ft::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';
// title
TITLE(check state :)
std::cout << "size : " << myvector.size() << '\n';
std::cout << "capacity : " << myvector.capacity() << '\n';
}
TESTEND
}
void tests_vector_insert()
{
TEST(vector::insert)
{
ft::vector<int>::iterator it;
ft::vector<mystruct*>::iterator its;
int i;
// title
TITLE(cplusplus.com reference :)
ft::vector<int> myvector (3,100);
it = myvector.begin();
it = myvector.insert ( it , 200 );
myvector.insert (it,2,300);
ft::vector<int> anothervector (2,400);
it = myvector.begin();
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.end(); it++)
std::cout << ' ' << *it;
std::cout << '\n';
// title
TITLE(tests positions on insert(pos, value) :)
ft::vector<int> myvector2 (3,100);
it = myvector2.begin();
std::cout << "size:" << myvector2.size() << " capacity:" << myvector2.capacity() << "\n";
myvector2.insert ( it , 200 );
std::cout << "myvector contains:";
for (it = myvector2.begin(); it < myvector2.end(); it++)
std::cout << ' ' << *it;
std::cout << "\nsize:" << myvector2.size() << " capacity:" << myvector2.capacity() << "\n";
ft::vector<int> myvector3 (3,100);
it = myvector3.end();
std::cout << "\nsize:" << myvector3.size() << " capacity:" << myvector3.capacity() << "\n";
myvector3.insert ( it , 200 );
std::cout << "myvector contains:";
for (it = myvector3.begin(); it < myvector3.end(); it++)
std::cout << ' ' << *it;
std::cout << "\nsize:" << myvector3.size() << " capacity:" << myvector3.capacity() << "\n";
// title
TITLE(tests positions on insert(pos, value) with struct :)
ft::vector<mystruct*> myvector4;
mystruct *s;
i = 0;
while (i < 3)
{
s = new mystruct(i++);
myvector4.push_back(s);
}
its = myvector4.begin();
std::cout << "size:" << myvector4.size() << " capacity:" << myvector4.capacity() << "\n";
s = new mystruct(i++);
myvector4.insert ( its , s );
std::cout << "myvector contains:";
for (its = myvector4.begin(); its < myvector4.end(); its++)
std::cout << ' ' << **its;
std::cout << "\nsize:" << myvector4.size() << " capacity:" << myvector4.capacity() << "\n";
its = myvector4.begin() + 2;
std::cout << "\nsize:" << myvector4.size() << " capacity:" << myvector4.capacity() << "\n";
s = new mystruct(i++);
myvector4.insert ( its , s );
std::cout << "myvector contains:";
for (its = myvector4.begin(); its < myvector4.end(); its++)
std::cout << ' ' << **its;
std::cout << "\nsize:" << myvector4.size() << " capacity:" << myvector4.capacity() << "\n";
its = myvector4.end();
std::cout << "\nsize:" << myvector4.size() << " capacity:" << myvector4.capacity() << "\n";
s = new mystruct(i++);
myvector4.insert ( its , s );
std::cout << "myvector contains:";
for (its = myvector4.begin(); its < myvector4.end(); its++)
std::cout << ' ' << **its;
std::cout << "\nsize:" << myvector4.size() << " capacity:" << myvector4.capacity() << "\n";
// title
TITLE(tests insert(pos, size, value) :)
ft::vector<int> myvector5;
for (i = 1; i <= 5; i++)
myvector5.push_back(i * 100);
it = myvector5.begin() + 1;
myvector5.insert ( it , 150 );
it = myvector5.end();
myvector5.insert (it,2,600);
it = myvector5.end() - 2;
myvector5.insert (it,2,550);
std::cout << "myvector contains:";
for (it = myvector5.begin(); it < myvector5.end(); it++)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "\nsize:" << myvector5.size() << " capacity:" << myvector5.capacity() << "\n";
// title
TITLE(tests positions on insert(pos, value) with struct :)
ft::vector<mystruct*> myvector6;
i = 1;
while (i <= 5)
{
s = new mystruct(i++ * 100);
myvector6.push_back(s);
}
its = myvector6.begin() + 1;
s = new mystruct(150);
myvector6.insert ( its , s );
its = myvector6.end();
s = new mystruct(600);
myvector6.insert ( its, 2, s );
its = myvector6.end() - 2;
s = new mystruct(550);
myvector6.insert ( its, 2, s );
std::cout << "myvector contains:";
for (its = myvector6.begin(); its < myvector6.end(); its++)
std::cout << ' ' << **its;
std::cout << "\nsize:" << myvector6.size() << " capacity:" << myvector6.capacity() << "\n";
// title
TITLE(tests insert(pos, first, last) with struct :)
ft::vector<mystruct*> myvector7;
for (i = 0; i < 5; i++)
{
s = new mystruct(42);
myvector7.push_back(s);
}
its = myvector7.begin() + 2;
myvector7.insert ( its, myvector6.begin() + 3, myvector6.end() - 2 );
std::cout << "myvector contains:";
for (its = myvector7.begin(); its < myvector7.end(); its++)
std::cout << ' ' << **its;
std::cout << "\nsize:" << myvector7.size() << " capacity:" << myvector7.capacity() << "\n";
}
TESTEND
}
void tests_vector_erase()
{
TEST(vector::erase)