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
CC = clang++
CC = g++
EXT = cpp
CFLAGS = -Wall -Wextra -Werror $(INCLUDES)
@@ -80,8 +80,8 @@ endif
all: $(NAME)
stl: CFLAGS += -D STL
stl: all
ft: all
stl: re
ft: re
$(D_OBJS)/%.o: %.$(EXT) | $(D_OBJS)
@echo $(CYAN)"compilation (objects.o) :"$(RESET)

View File

@@ -3,18 +3,6 @@
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
{
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 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
// then the function template with direct matching are considered
// https://stackoverflow.com/questions/12877546/how-do-i-avoid-implicit-conversions-on
@@ -95,19 +38,6 @@ template <class T> struct is_integral
} // namespace ft
// "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
// SFINAE : https://jguegant.github.io/blogs/tech/sfinae-introduction.html

View File

@@ -99,17 +99,11 @@ public:
template <class InputIterator>
typename enable_if< !is_integral<InputIterator>::value,void >::type
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);
// push_back ---------------------------------
void push_back(const value_type & val);
// pop_back ----------------------------------
//void pop_back();
void pop_back();
// insert ------------------------------------
//iterator insert(iterator position, 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[] --------------------------------
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;
// at ----------------------------------------
//reference at(size_type n);
@@ -175,35 +178,25 @@ VT_TPL template <class InputIterator>
typename enable_if< !is_integral<InputIterator>::value,void >::type VT::
assign( InputIterator first, InputIterator last)
{
// TMP
std::cout << B_RED "inside assign(first, last) " RESET;
// TMP END
InputIterator tmp = first;
int range;
int range = 0;
clear();
for (range = 1; tmp != last; range++)
tmp++;
while (tmp++ != last)
range++;
_size += range;
if (_size >= _capacity)
reserve(_size);
while (first != last)
{
_allocator.construct(&_mem_ptr[_size], first);
_allocator.construct(&_mem_ptr[_size], *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::
assign( size_type n, const T & val )
{
// TMP
std::cout << B_RED "inside assign(n, val) " RESET;
// TMP END
if (n > _allocator.max_size())
throw std::length_error("assign: n > max_size");
@@ -237,7 +230,12 @@ VT_TPL void VT::
_size++;
}
// pop_back ----------------------------------
//void pop_back();
VT_TPL void VT::
pop_back()
{
_allocator.destroy(end() - 1);
_size--;
}
// insert ------------------------------------
//iterator insert(iterator position, 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)
{
// title
TITLE(cplusplus.com reference :)
std::vector<int> first;
std::vector<int> second;
std::vector<int> third;
ft::vector<int> first;
ft::vector<int> second;
ft::vector<int> third;
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;
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 third: " << int (third.size()) << '\n';
// title
TITLE(capacity tests of assignation :)
ft::vector<int> myvector;
@@ -339,17 +342,50 @@ int main() {
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";
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;
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
/*
TEST(vector::push_back)
{
ft::vector<int> myvector;
@@ -369,17 +405,15 @@ int main() {
std::cout << " -> size : " << myvector.size() << " , capacity :" << myvector.capacity() << "\n";
// second test
// 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";
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
*/
/*
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