add tests for vector assign iterator, and tests script

This commit is contained in:
hugogogo
2022-06-08 20:54:44 +02:00
parent 8c232df375
commit 5d0e631cb7
6 changed files with 82 additions and 116 deletions

View File

@@ -32,7 +32,7 @@ RESET = "\e[0m"
NAME = containers NAME = containers
CC = clang++ CC = g++
EXT = cpp EXT = cpp
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) CFLAGS = -Wall -Wextra -Werror $(INCLUDES)
@@ -80,8 +80,8 @@ endif
all: $(NAME) all: $(NAME)
stl: CFLAGS += -D STL stl: CFLAGS += -D STL
stl: all stl: re
ft: all ft: re
$(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS) $(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS)
@echo $(CYAN)"compilation (objects.o) :"$(RESET) @echo $(CYAN)"compilation (objects.o) :"$(RESET)

View File

@@ -3,18 +3,6 @@
namespace ft { namespace ft {
//struct true_type {
// typedef bool value_type;
// typedef true_type type;
// static const bool value = true;
//};
//
//struct false_type {
// typedef T value_type;
// typedef false_type type;
// static const bool value = false;
//};
template <class T> struct is_integral template <class T> struct is_integral
{ {
typedef char yes[1]; typedef char yes[1];
@@ -39,51 +27,6 @@ template <class T> struct is_integral
static yes& test(unsigned long int); static yes& test(unsigned long int);
static yes& test(unsigned long long int); static yes& test(unsigned long long int);
/*
static yes& test(const bool);
static yes& test(const char);
static yes& test(const wchar_t);
static yes& test(const signed char);
static yes& test(const short int);
static yes& test(const int);
static yes& test(const long int);
static yes& test(const long long int);
static yes& test(const unsigned char);
static yes& test(const unsigned short int);
static yes& test(const unsigned int);
static yes& test(const unsigned long int);
static yes& test(const unsigned long long int);
static yes& test(volatile bool);
static yes& test(volatile char);
static yes& test(volatile wchar_t);
static yes& test(volatile signed char);
static yes& test(volatile short int);
static yes& test(volatile int);
static yes& test(volatile long int);
static yes& test(volatile long long int);
static yes& test(volatile unsigned char);
static yes& test(volatile unsigned short int);
static yes& test(volatile unsigned int);
static yes& test(volatile unsigned long int);
static yes& test(volatile unsigned long long int);
static yes& test(const volatile bool);
static yes& test(const volatile char);
static yes& test(const volatile wchar_t);
static yes& test(const volatile signed char);
static yes& test(const volatile short int);
static yes& test(const volatile int);
static yes& test(const volatile long int);
static yes& test(const volatile long long int);
static yes& test(const volatile unsigned char);
static yes& test(const volatile unsigned short int);
static yes& test(const volatile unsigned int);
static yes& test(const volatile unsigned long int);
static yes& test(const volatile unsigned long long int);
*/
// non-template function with direct matching are always considered first // non-template function with direct matching are always considered first
// then the function template with direct matching are considered // then the function template with direct matching are considered
// https://stackoverflow.com/questions/12877546/how-do-i-avoid-implicit-conversions-on // https://stackoverflow.com/questions/12877546/how-do-i-avoid-implicit-conversions-on
@@ -95,19 +38,6 @@ template <class T> struct is_integral
} // namespace ft } // namespace ft
// "template <>" introduce a total specialization of a template : // "template <>" introduce a total specialization of a template :
//
// template <typename T>
// class A
// {
// // body for the general case
// };
//
// template <>
// class A<bool>
// {
// // body that only applies for T = bool
// };
//
// https://stackoverflow.com/questions/6288812/what-is-the-meaning-of-template-with-empty-angle-brackets-in-c // https://stackoverflow.com/questions/6288812/what-is-the-meaning-of-template-with-empty-angle-brackets-in-c
// SFINAE : https://jguegant.github.io/blogs/tech/sfinae-introduction.html // SFINAE : https://jguegant.github.io/blogs/tech/sfinae-introduction.html

View File

@@ -99,17 +99,11 @@ public:
template <class InputIterator> template <class InputIterator>
typename enable_if< !is_integral<InputIterator>::value,void >::type typename enable_if< !is_integral<InputIterator>::value,void >::type
assign(InputIterator first, InputIterator last); assign(InputIterator first, InputIterator last);
// template <class InputIterator>
// void assign(InputIterator first, InputIterator last
// typename enable_if< !is_integral<InputIterator>::value, bool >::type == true);
// template <class InputIterator>
// typename enable_if< is_integral<InputIterator>::value,void >::type
// assign(size_type n, const value_type& val);
void assign(size_type n, const value_type& val); void assign(size_type n, const value_type& val);
// push_back --------------------------------- // push_back ---------------------------------
void push_back(const value_type & val); void push_back(const value_type & val);
// pop_back ---------------------------------- // pop_back ----------------------------------
//void pop_back(); void pop_back();
// insert ------------------------------------ // insert ------------------------------------
//iterator insert(iterator position, const value_type& val); //iterator insert(iterator position, const value_type& val);
//void insert(iterator position, size_type n, const value_type& val); //void insert(iterator position, size_type n, const value_type& val);

View File

@@ -155,7 +155,10 @@ VT_TPL void VT::
******************/ ******************/
// operator[] -------------------------------- // operator[] --------------------------------
VT_TPL typename VT::reference VT:: VT_TPL typename VT::reference VT::
operator[](size_type n) { return _mem_ptr[n]; } operator[](size_type n)
{
return _mem_ptr[n];
}
//const_reference operator[](size_type n) const; //const_reference operator[](size_type n) const;
// at ---------------------------------------- // at ----------------------------------------
//reference at(size_type n); //reference at(size_type n);
@@ -175,35 +178,25 @@ VT_TPL template <class InputIterator>
typename enable_if< !is_integral<InputIterator>::value,void >::type VT:: typename enable_if< !is_integral<InputIterator>::value,void >::type VT::
assign( InputIterator first, InputIterator last) assign( InputIterator first, InputIterator last)
{ {
// TMP
std::cout << B_RED "inside assign(first, last) " RESET;
// TMP END
InputIterator tmp = first; InputIterator tmp = first;
int range; int range = 0;
clear(); clear();
for (range = 1; tmp != last; range++) while (tmp++ != last)
tmp++; range++;
_size += range; _size += range;
if (_size >= _capacity) if (_size >= _capacity)
reserve(_size); reserve(_size);
while (first != last) while (first != last)
{ {
_allocator.construct(&_mem_ptr[_size], first); _allocator.construct(&_mem_ptr[_size], *first);
first++; first++;
} }
} }
// VT_TPL typename enable_if< isinteger<T>::value,std::string >::type VT::
//VT_TPL template <class InputIterator>
//typename enable_if< is_integral<InputIterator>::value,void >::type VT::
VT_TPL void VT:: VT_TPL void VT::
assign( size_type n, const T & val ) assign( size_type n, const T & val )
{ {
// TMP
std::cout << B_RED "inside assign(n, val) " RESET;
// TMP END
if (n > _allocator.max_size()) if (n > _allocator.max_size())
throw std::length_error("assign: n > max_size"); throw std::length_error("assign: n > max_size");
@@ -237,7 +230,12 @@ VT_TPL void VT::
_size++; _size++;
} }
// pop_back ---------------------------------- // pop_back ----------------------------------
//void pop_back(); VT_TPL void VT::
pop_back()
{
_allocator.destroy(end() - 1);
_size--;
}
// insert ------------------------------------ // insert ------------------------------------
//iterator insert(iterator position, const value_type& val); //iterator insert(iterator position, const value_type& val);
//void insert(iterator position, size_type n, const value_type& val); //void insert(iterator position, size_type n, const value_type& val);

View File

@@ -298,15 +298,16 @@ int main() {
TEST(vector::assign) TEST(vector::assign)
{ {
// title
TITLE(cplusplus.com reference :) TITLE(cplusplus.com reference :)
std::vector<int> first; ft::vector<int> first;
std::vector<int> second; ft::vector<int> second;
std::vector<int> third; ft::vector<int> third;
first.assign (7,100); // 7 ints with a value of 100 first.assign (7,100); // 7 ints with a value of 100
std::vector<int>::iterator it; ft::vector<int>::iterator it;
it=first.begin()+1; it=first.begin()+1;
second.assign (it,first.end()-1); // the 5 central values of first second.assign (it,first.end()-1); // the 5 central values of first
@@ -318,6 +319,8 @@ int main() {
std::cout << "Size of second: " << int (second.size()) << '\n'; std::cout << "Size of second: " << int (second.size()) << '\n';
std::cout << "Size of third: " << int (third.size()) << '\n'; std::cout << "Size of third: " << int (third.size()) << '\n';
// title
TITLE(capacity tests of assignation :) TITLE(capacity tests of assignation :)
ft::vector<int> myvector; ft::vector<int> myvector;
@@ -339,17 +342,50 @@ int main() {
std::cout << "[" << i << "] " << myvector[i] << " - "; std::cout << "[" << i << "] " << myvector[i] << " - ";
std::cout << "\nsize : " << size << " , capacity : " << myvector.capacity() << "\n"; std::cout << "\nsize : " << size << " , capacity : " << myvector.capacity() << "\n";
// std::cout << "\nassign 7268\n"; std::cout << "\nassign 7268\n";
// myvector.assign(7268, 12); myvector.assign(7268, 12);
// size = myvector.size(); size = myvector.size();
// for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
// std::cout << "[" << i << "] " << myvector[i] << " - "; std::cout << "[" << i << "] " << myvector[i] << " - ";
// std::cout << "\nsize : " << size << " , capacity : " << myvector.capacity() << "\n"; 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;
std::cout << "\nassign 1\n";
int_vector_1.assign(1, 12);
it_vector.assign(int_vector_1.begin(), int_vector_1.end());
size = it_vector.size();
for (int i = 0; i < size; i++)
std::cout << "[" << i << "] " << it_vector[i] << " - ";
std::cout << "\nsize : " << size << " , capacity : " << it_vector.capacity() << "\n";
std::cout << "\nassign 1\n";
int_vector_2.assign(1, 6);
it_vector.assign(int_vector_2.begin(), int_vector_2.end() - 1);
size = it_vector.size();
for (int i = 0; i < size; i++)
std::cout << "[" << i << "] " << it_vector[i] << " - ";
std::cout << "\nsize : " << size << " , capacity : " << it_vector.capacity() << "\n";
std::cout << "\nassign 1\n";
int_vector_3.assign(266, 1);
it_vector.assign(int_vector_3.begin() + 13, int_vector_3.end() - 172);
size = it_vector.size();
for (int i = 0; i < size; i++)
std::cout << "[" << i << "] " << it_vector[i] << " - ";
std::cout << "\nsize : " << size << " , capacity : " << it_vector.capacity() << "\n";
} }
TESTEND TESTEND
/*
TEST(vector::push_back) TEST(vector::push_back)
{ {
ft::vector<int> myvector; ft::vector<int> myvector;
@@ -369,17 +405,15 @@ int main() {
std::cout << " -> size : " << myvector.size() << " , capacity :" << myvector.capacity() << "\n"; std::cout << " -> size : " << myvector.size() << " , capacity :" << myvector.capacity() << "\n";
// second test // second test
// for (int i = 0; i < 72363; i++) for (int i = 0; i < 72363; i++)
// { {
// myvector.push_back(9); myvector.push_back(9);
// std::cout << "[" << i std::cout << "[" << i
// << ":" << myvector.capacity() << "] "; << ":" << myvector.capacity() << "] ";
// } }
// std::cout << " -> size : " << myvector.size() << " , capacity :" << myvector.capacity() << "\n"; std::cout << " -> size : " << myvector.size() << " , capacity :" << myvector.capacity() << "\n";
} }
TESTEND TESTEND
*/
/* /*
TEST(vector::pop_back) TEST(vector::pop_back)

10
tests/test.sh Normal file
View File

@@ -0,0 +1,10 @@
#!/bin/bash
# cd $(dirname $0)
make stl > /dev/null
./containers > tests/output_stl.log
make ft > /dev/null
./containers > tests/output_ft.log
diff --context=0 --color=always tests/output_stl.log tests/output_ft.log