re-organisation of files

This commit is contained in:
hugogogo
2022-06-17 15:30:58 +02:00
parent a939bfc66f
commit 72762a79cb
11 changed files with 1165 additions and 812 deletions

View File

@@ -1,67 +1,68 @@
#include <iostream>
#include <string>
#include "colors.h"
#include <iomanip> // std::setw()
#include "tests_proto.hpp"
#include "colors.h"
#include "A_test.hpp"
//#include "tests_proto.hpp"
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_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_erase();
tests_vector_swap();
tests_vector_clear();
tests_vector_get_allocator();
tests_vector_relational_operators();
tests_vector_swap_non_member();
tests_vector_reverse_iterators();
// 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_erase();
// tests_vector_swap();
// tests_vector_clear();
// tests_vector_get_allocator();
// tests_vector_relational_operators();
// tests_vector_swap_non_member();
// tests_vector_reverse_iterators();
// MAP
// tests_map_simple();
// tests_map_constructor();
// tests_map_operator_assignation();
// tests_map_begin();
// tests_map_end();
// tests_map_rbegin();
// tests_map_rend();
// tests_map_empty();
// tests_map_size();
// tests_map_max_size();
// tests_map_resize();
// tests_map_capacity();
// tests_map_empty();
// tests_map_reserve();
// tests_map_operator_access();
// tests_map_at();
// tests_map_front();
// tests_map_back();
// tests_map_assign();
// tests_map_push_back();
// tests_map_pop_back();
// tests_map_insert();
// tests_map_erase();
// tests_map_swap();
// tests_map_clear();
// tests_map_key_comp();
// tests_map_value_comp();
// tests_map_find();
// tests_map_count();
// tests_map_lower_bound();
// tests_map_upper_bound();
// tests_map_equal_range();
// tests_map_get_allocator();
// STACK
// tests_stack_constructor();
// tests_stack_operator_assignation();

551
tests/tests_map.cpp Normal file
View File

@@ -0,0 +1,551 @@
#ifndef TESTS_MAP_CPP
#define TESTS_MAP_CPP
#include "tests_utils.hpp"
TEST(tests_map_simple)
{
// title
TITLE(simple test)
std::map<char,int> first;
std::map<char, int>::iterator it;
first['a']=10;
first['b']=30;
first['c']=50;
first['d']=70;
for(it=first.begin(); it!=first.end(); ++it){
std::cout << it->first << " => " << it->second << '\n';
}
DELETE
}
/*
TEST(tests_map_constructor)
{
// title
TITLE(cplusplus.com reference)
// bool fncomp (char lhs, char rhs) {return lhs<rhs;}
//
// struct classcomp {
// bool operator() (const char& lhs, const char& rhs) const
// {return lhs<rhs;}
// };
std::map<char,int> first;
first['a']=10;
first['b']=30;
first['c']=50;
first['d']=70;
std::map<char,int> second (first.begin(),first.end());
std::map<char,int> third (second);
std::map<char,int,classcomp> fourth; // class as Compare
bool(*fn_pt)(char,char) = fncomp;
std::map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare
DELETE
}
TEST(tests_map_operator_assignation)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> first;
std::map<char,int> second;
first['x']=8;
first['y']=16;
first['z']=32;
second=first; // second now contains 3 ints
first=std::map<char,int>(); // and first is now empty
std::cout << "Size of first: " << first.size() << '\n';
std::cout << "Size of second: " << second.size() << '\n';
DELETE
}
TEST(tests_map_begin)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> mymap;
mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;
// show content:
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
DELETE
}
TEST(tests_map_end)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> mymap;
mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;
// show content:
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
DELETE
}
TEST(tests_map_rbegin)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> mymap;
mymap['x'] = 100;
mymap['y'] = 200;
mymap['z'] = 300;
// show content:
std::map<char,int>::reverse_iterator rit;
for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit)
std::cout << rit->first << " => " << rit->second << '\n';
DELETE
}
TEST(tests_map_rend)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> mymap;
mymap['x'] = 100;
mymap['y'] = 200;
mymap['z'] = 300;
// show content:
std::map<char,int>::reverse_iterator rit;
for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit)
std::cout << rit->first << " => " << rit->second << '\n';
DELETE
}
TEST(tests_map_empty)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> mymap;
mymap['a']=10;
mymap['b']=20;
mymap['c']=30;
while (!mymap.empty())
{
std::cout << mymap.begin()->first << " => " << mymap.begin()->second << '\n';
mymap.erase(mymap.begin());
}
DELETE
}
TEST(tests_map_size)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> mymap;
mymap['a']=101;
mymap['b']=202;
mymap['c']=302;
std::cout << "mymap.size() is " << mymap.size() << '\n';
DELETE
}
TEST(tests_map_max_size)
{
// title
TITLE(cplusplus.com reference)
int i;
std::map<int,int> mymap;
if (mymap.max_size()>1000)
{
for (i=0; i<1000; i++) mymap[i]=0;
std::cout << "The map contains 1000 elements.\n";
}
else std::cout << "The map could not hold 1000 elements.\n";
DELETE
}
TEST(tests_map_operator_access)
{
// title
TITLE(cplusplus.com reference)
std::map<char,std::string> mymap;
mymap['a']="an element";
mymap['b']="another element";
mymap['c']=mymap['b'];
std::cout << "mymap['a'] is " << mymap['a'] << '\n';
std::cout << "mymap['b'] is " << mymap['b'] << '\n';
std::cout << "mymap['c'] is " << mymap['c'] << '\n';
std::cout << "mymap['d'] is " << mymap['d'] << '\n';
std::cout << "mymap now contains " << mymap.size() << " elements.\n";
DELETE
}
TEST(tests_map_insert)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> mymap;
// first insert function version (single parameter):
mymap.insert ( std::pair<char,int>('a',100) );
mymap.insert ( std::pair<char,int>('z',200) );
std::pair<std::map<char,int>::iterator,bool> ret;
ret = mymap.insert ( std::pair<char,int>('z',500) );
if (ret.second==false) {
std::cout << "element 'z' already existed";
std::cout << " with a value of " << ret.first->second << '\n';
}
// second insert function version (with hint position):
std::map<char,int>::iterator it = mymap.begin();
mymap.insert (it, std::pair<char,int>('b',300)); // max efficiency inserting
mymap.insert (it, std::pair<char,int>('c',400)); // no max efficiency inserting
// third insert function version (range insertion):
std::map<char,int> anothermap;
anothermap.insert(mymap.begin(),mymap.find('c'));
// showing contents:
std::cout << "mymap contains:\n";
for (it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
std::cout << "anothermap contains:\n";
for (it=anothermap.begin(); it!=anothermap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
DELETE
}
TEST(tests_map_erase)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> mymap;
std::map<char,int>::iterator it;
// insert some values:
mymap['a']=10;
mymap['b']=20;
mymap['c']=30;
mymap['d']=40;
mymap['e']=50;
mymap['f']=60;
it=mymap.find('b');
mymap.erase (it); // erasing by iterator
mymap.erase ('c'); // erasing by key
it=mymap.find ('e');
mymap.erase ( it, mymap.end() ); // erasing by range
// show content:
for (it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
DELETE
}
TEST(tests_map_swap)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> foo,bar;
foo['x']=100;
foo['y']=200;
bar['a']=11;
bar['b']=22;
bar['c']=33;
foo.swap(bar);
std::cout << "foo contains:\n";
for (std::map<char,int>::iterator it=foo.begin(); it!=foo.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
std::cout << "bar contains:\n";
for (std::map<char,int>::iterator it=bar.begin(); it!=bar.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
DELETE
}
TEST(tests_map_clear)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> mymap;
mymap['x']=100;
mymap['y']=200;
mymap['z']=300;
std::cout << "mymap contains:\n";
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
mymap.clear();
mymap['a']=1101;
mymap['b']=2202;
std::cout << "mymap contains:\n";
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
DELETE
}
TEST(tests_map_key_comp)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> mymap;
std::map<char,int>::key_compare mycomp = mymap.key_comp();
mymap['a']=100;
mymap['b']=200;
mymap['c']=300;
std::cout << "mymap contains:\n";
char highest = mymap.rbegin()->first; // key value of last element
std::map<char,int>::iterator it = mymap.begin();
do {
std::cout << it->first << " => " << it->second << '\n';
} while ( mycomp((*it++).first, highest) );
std::cout << '\n';
DELETE
}
TEST(tests_map_value_comp)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> mymap;
mymap['x']=1001;
mymap['y']=2002;
mymap['z']=3003;
std::cout << "mymap contains:\n";
std::pair<char,int> highest = *mymap.rbegin(); // last element
std::map<char,int>::iterator it = mymap.begin();
do {
std::cout << it->first << " => " << it->second << '\n';
} while ( mymap.value_comp()(*it++, highest) );
DELETE
}
TEST(tests_map_find)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> mymap;
std::map<char,int>::iterator it;
mymap['a']=50;
mymap['b']=100;
mymap['c']=150;
mymap['d']=200;
it = mymap.find('b');
if (it != mymap.end())
mymap.erase (it);
// print content:
std::cout << "elements in mymap:" << '\n';
std::cout << "a => " << mymap.find('a')->second << '\n';
std::cout << "c => " << mymap.find('c')->second << '\n';
std::cout << "d => " << mymap.find('d')->second << '\n';
DELETE
}
TEST(tests_map_count)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> mymap;
char c;
mymap ['a']=101;
mymap ['c']=202;
mymap ['f']=303;
for (c='a'; c<'h'; c++)
{
std::cout << c;
if (mymap.count(c)>0)
std::cout << " is an element of mymap.\n";
else
std::cout << " is not an element of mymap.\n";
}
DELETE
}
TEST(tests_map_lower_bound)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> mymap;
std::map<char,int>::iterator itlow,itup;
mymap['a']=20;
mymap['b']=40;
mymap['c']=60;
mymap['d']=80;
mymap['e']=100;
itlow=mymap.lower_bound ('b'); // itlow points to b
itup=mymap.upper_bound ('d'); // itup points to e (not d!)
mymap.erase(itlow,itup); // erases [itlow,itup)
// print content:
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
DELETE
}
TEST(tests_map_upper_bound)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> mymap;
std::map<char,int>::iterator itlow,itup;
mymap['a']=20;
mymap['b']=40;
mymap['c']=60;
mymap['d']=80;
mymap['e']=100;
itlow=mymap.lower_bound ('b'); // itlow points to b
itup=mymap.upper_bound ('d'); // itup points to e (not d!)
mymap.erase(itlow,itup); // erases [itlow,itup)
// print content:
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
DELETE
}
TEST(tests_map_equal_range)
{
// title
TITLE(cplusplus.com reference)
std::map<char,int> mymap;
mymap['a']=10;
mymap['b']=20;
mymap['c']=30;
std::pair<std::map<char,int>::iterator,std::map<char,int>::iterator> ret;
ret = mymap.equal_range('b');
std::cout << "lower bound points to: ";
std::cout << ret.first->first << " => " << ret.first->second << '\n';
std::cout << "upper bound points to: ";
std::cout << ret.second->first << " => " << ret.second->second << '\n';
DELETE
}
TEST(tests_map_get_allocator)
{
// title
TITLE(cplusplus.com reference)
int psize;
std::map<char,int> mymap;
std::pair<const char,int>* p;
// allocate an array of 5 elements using mymap's allocator:
p=mymap.get_allocator().allocate(5);
// assign some values to array
psize = sizeof(std::map<char,int>::value_type)*5;
std::cout << "The allocated array has a size of " << psize << " bytes.\n";
mymap.get_allocator().deallocate(p,5);
DELETE
}
*/
#endif

117
tests/tests_utils.cpp Normal file
View File

@@ -0,0 +1,117 @@
#include "tests.hpp"
void add_to_list(std::string s, A_test* s1, A_test* s2, A_test* s3, A_test* s4) {
std::vector<A_test*> test_sub_list;
s1->title = s;
s2->title = s;
s3->title = s;
s4->title = s;
s1->type = "int";
s2->type = "char";
s3->type = "std::string";
s4->type = "mystruct";
test_sub_list.push_back(s1);
test_sub_list.push_back(s2);
test_sub_list.push_back(s3);
test_sub_list.push_back(s4);
test_list.push_back(test_sub_list);
}
void delete_structs() {
std::vector<mystruct*>::iterator it;
std::vector<mystruct*>::iterator it_end = mem_list.end();
for (it = mem_list.begin(); it != it_end; ++it)
delete *it;
mem_list.clear();
}
template <class T>
void print_vector(ft::vector<T> vec) {
int i = 0;
typename ft::vector<T>::iterator it;
typename ft::vector<T>::iterator it_end = vec.end();
for (it = vec.begin(); it != it_end; ++it, i++)
std::cout << "[" << i << "]" << *it << " ";
std::cout << "\nsize:" << vec.size() << " capacty:" << vec.capacity() << "\n";
}
// mystruct
// *********************************************
mystruct::mystruct(int data) {_val = new int[2]; _val[0] = data; _val[1] = data;}
mystruct::~mystruct() {delete[] _val;}
int * mystruct::get_data() const {return _val;}
std::ostream & operator<<(std::ostream & o, mystruct const * rhs) {
if (rhs != NULL)
o << (*rhs).get_data()[0] << "," << (*rhs).get_data()[1];
else
o << "NULL";
return (o);
}
// get a value
// *********************************************
template <class T>
T val(int n) {(void)n; return (T());
}
template <>
int val(int n) {return (n);
}
template <>
char val(int n) {return (n % 94 + 33);
}
template <>
std::string val(int n) {
std::string str;
std::stringstream stream;
stream << n;
stream >> str;
stream.clear();
return (str);
}
template <>
mystruct* val(int n) {
mystruct *s = new mystruct(n);
mem_list.push_back(s);
return ( s );
}
// 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] );
}

View File

@@ -1,6 +1,6 @@
#ifndef TESTS_VECTORS_CPP
#define TESTS_VECTORS_CPP
#ifndef TESTS_VECTOR_CPP
#define TESTS_VECTOR_CPP
#include "tests_utils.hpp"