half tests ok on map

This commit is contained in:
hugogogo
2022-06-21 14:41:24 +02:00
parent 28acc5ef51
commit 844701201f
5 changed files with 135 additions and 130 deletions

View File

@@ -47,28 +47,28 @@ void tests_vector_swap_non_member();
void tests_vector_reverse_iterators(); void tests_vector_reverse_iterators();
// map // map
void tests_map_simple(); void tests_map_simple();
//void tests_map_constructor(); void tests_map_constructor();
//void tests_map_operator_assignation(); void tests_map_operator_assignation();
//void tests_map_begin(); void tests_map_begin();
//void tests_map_end(); void tests_map_end();
//void tests_map_rbegin(); void tests_map_rbegin();
//void tests_map_rend(); void tests_map_rend();
//void tests_map_empty(); void tests_map_empty();
//void tests_map_size(); void tests_map_size();
//void tests_map_max_size(); void tests_map_max_size();
//void tests_map_operator_access(); void tests_map_operator_access();
//void tests_map_insert(); void tests_map_insert();
//void tests_map_erase(); void tests_map_erase();
//void tests_map_swap(); void tests_map_swap();
//void tests_map_clear(); void tests_map_clear();
//void tests_map_key_comp(); void tests_map_key_comp();
//void tests_map_value_comp(); void tests_map_value_comp();
//void tests_map_find(); void tests_map_find();
//void tests_map_count(); void tests_map_count();
//void tests_map_lower_bound(); void tests_map_lower_bound();
//void tests_map_upper_bound(); void tests_map_upper_bound();
//void tests_map_equal_range(); void tests_map_equal_range();
//void tests_map_get_allocator(); void tests_map_get_allocator();
#endif #endif

View File

@@ -32,7 +32,7 @@
# define VALT(n) val<T>(n) # define VALT(n) val<T>(n)
# define VALU(n) val<U>(n) # define VALU(n) val<U>(n)
# define TOI(n) toi<T>(n) # define TOI(n) toi<T>(n)
# define PRINT(n) print<T>(n); # define PRINT(n) print<>(n, #n);
# define DELETE delete_structs(); # define DELETE delete_structs();
@@ -82,7 +82,10 @@ extern std::vector< mystruct* > mem_list;
template <class T, class U> struct s_ ## f_name : public A_test\ template <class T, class U> struct s_ ## f_name : public A_test\
{ void func(); };\ { void func(); };\
void f_name () {\ void f_name () {\
add_to_list(#f_name, "int, int", new(s_ ## f_name <int, int>));\ add_to_list(#f_name, "char, int", new(s_ ## f_name <char, int>));\
add_to_list(#f_name, "char, char", new(s_ ## f_name <char, char>));\
add_to_list(#f_name, "char, std::string", new(s_ ## f_name <char, std::string>));\
add_to_list(#f_name, "char, mystruct*", new(s_ ## f_name <char, mystruct*>));\
}\ }\
template <class T, class U>\ template <class T, class U>\
void s_ ## f_name <T, U>::func() void s_ ## f_name <T, U>::func()
@@ -91,23 +94,25 @@ extern std::vector< mystruct* > mem_list;
// templates print // templates print
// ***************************************** // *****************************************
template <class T> template <class T>
void print(ft::vector<T> vec) { void print(ft::vector<T> vec, std::string name) {
int i = 0; int i = 0;
typename ft::vector<T>::iterator it; typename ft::vector<T>::iterator it;
typename ft::vector<T>::iterator it_end = vec.end(); typename ft::vector<T>::iterator it_end = vec.end();
std::cout << "\n" << name << ":(vector)\n";
for (it = vec.begin(); it != it_end; ++it, i++) for (it = vec.begin(); it != it_end; ++it, i++)
std::cout << "[" << i << "]" << *it << " "; std::cout << "[" << i << "]" << *it << " ";
std::cout << "\nsize:" << vec.size() << " capacty:" << vec.capacity() << "\n"; std::cout << "\nsize:" << vec.size() << " capacty:" << vec.capacity() << "\n";
} }
template <class T, class U> template <class T, class U>
void print(ft::map<T, U> mp) { void print(ft::map<T, U> mp, std::string name) {
int i = 0; int i = 0;
typename ft::map<T, U>::iterator it; typename ft::map<T, U>::iterator it;
typename ft::map<T, U>::iterator it_end = mp.end(); typename ft::map<T, U>::iterator it_end = mp.end();
std::cout << "\n" << name << ":(map)\n";
for (it = mp.begin(); it != it_end; ++it, i++) for (it = mp.begin(); it != it_end; ++it, i++)
std::cout << "[" << i << "]" << it->first << ":" << it->second << " "; std::cout << "[" << i << "]" << it->first << ":" << it->second << " ";
std::cout << "\nsize:" << mp.size() << "\n"; std::cout << "\nsize:" << mp.size() << "\n";
@@ -118,13 +123,15 @@ template <class T, class U>
// specialization in header, make it inline : // specialization in header, make it inline :
// https://stackoverflow.com/questions/63529059/c-specialized-method-templates-produce-multiple-definition-errors // https://stackoverflow.com/questions/63529059/c-specialized-method-templates-produce-multiple-definition-errors
template <class T> template <class T>
T val(int n) {(void)n; return (T()); T val(int n) { (void)n; return (T()); }
}
template <> template <>
inline int val(int n) {return (n); inline int val(int n) { return (n); }
}
template <> template <>
inline char val(int n) {return (n % 94 + 33); inline char val(int n) {
if (n <= 126 && n >= 33)
return n;
return (n % 94 + 33);
} }
template <> template <>
inline std::string val(int n) { inline std::string val(int n) {
@@ -144,6 +151,16 @@ template <>
mem_list.push_back(s); mem_list.push_back(s);
return ( s ); return ( s );
} }
template <class T>
T val(std::string str) { (void)str; return (T()); }
template <>
inline int val(std::string str) { int i = str[0]; return (val<int>(i)); }
template <>
inline char val(std::string str) { int i = str[0]; return (val<char>(i)); }
template <>
inline std::string val(std::string str) { return (str); }
template <>
inline mystruct* val(std::string str) { int i = str[0]; return (val<mystruct*>(i)); }
// templates to value // templates to value

View File

@@ -35,16 +35,16 @@ int main() {
// MAP // MAP
tests_map_simple(); tests_map_simple();
// tests_map_constructor(); tests_map_constructor();
// tests_map_operator_assignation(); tests_map_operator_assignation();
// tests_map_begin(); tests_map_begin();
// tests_map_end(); tests_map_end();
// tests_map_rbegin(); tests_map_rbegin();
// tests_map_rend(); tests_map_rend();
// tests_map_empty(); tests_map_empty();
// tests_map_size(); tests_map_size();
// tests_map_max_size(); tests_map_max_size();
// tests_map_operator_access(); tests_map_operator_access();
// tests_map_insert(); // tests_map_insert();
// tests_map_erase(); // tests_map_erase();
// tests_map_swap(); // tests_map_swap();

View File

@@ -4,6 +4,15 @@
#include "tests_utils.hpp" #include "tests_utils.hpp"
/**/ // UTILS for some tests
/**/ bool fncomp (char lhs, char rhs) {return lhs<rhs;}
/**/
/**/ struct classcomp {
/**/ bool operator() (const char& lhs, const char& rhs) const
/**/ {return lhs<rhs;}
/**/ };
/**/ ///////////////////////
TEST_M(tests_map_simple) TEST_M(tests_map_simple)
{ {
// title // title
@@ -19,34 +28,30 @@ TEST_M(tests_map_simple)
DELETE DELETE
} }
/*
TEST_M(tests_map_constructor) TEST_M(tests_map_constructor)
{ {
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
// bool fncomp (char lhs, char rhs) {return lhs<rhs;} ft::map<T,U> first;
//
// struct classcomp {
// bool operator() (const char& lhs, const char& rhs) const
// {return lhs<rhs;}
// };
std::map<char,int> first; first[VALT('a')]=VALU(10);
first[VALT('b')]=VALU(30);
first[VALT('c')]=VALU(50);
first[VALT('d')]=VALU(70);
first['a']=10; ft::map<T,U> second (first.begin(),first.end());
first['b']=30;
first['c']=50;
first['d']=70;
std::map<char,int> second (first.begin(),first.end()); ft::map<T,U> third (second);
std::map<char,int> third (second); ft::map<T,U,classcomp> fourth; // class as Compare
std::map<char,int,classcomp> fourth; // class as Compare
bool(*fn_pt)(char,char) = fncomp; bool(*fn_pt)(char,char) = fncomp;
std::map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare ft::map<T,U,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare
PRINT(first)
PRINT(second)
PRINT(third)
DELETE DELETE
} }
@@ -56,19 +61,25 @@ TEST_M(tests_map_operator_assignation)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,int> first; ft::map<T,U> first;
std::map<char,int> second; ft::map<T,U> second;
first['x']=8; first[VALT('x')]=VALU(8);
first['y']=16; first[VALT('y')]=VALU(16);
first['z']=32; first[VALT('z')]=VALU(32);
PRINT(first)
PRINT(second)
second=first; // second now contains 3 ints second=first; // second now contains 3 ints
first=std::map<char,int>(); // and first is now empty first=ft::map<T,U>(); // and first is now empty
std::cout << "Size of first: " << first.size() << '\n'; std::cout << "Size of first: " << first.size() << '\n';
std::cout << "Size of second: " << second.size() << '\n'; std::cout << "Size of second: " << second.size() << '\n';
PRINT(first)
PRINT(second)
DELETE DELETE
} }
@@ -77,15 +88,13 @@ TEST_M(tests_map_begin)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,int> mymap; ft::map<T,U> mymap;
mymap['b'] = 100; mymap[VALT('b')] = VALU(100);
mymap['a'] = 200; mymap[VALT('a')] = VALU(200);
mymap['c'] = 300; mymap[VALT('c')] = VALU(300);
// show content: PRINT(mymap)
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
DELETE DELETE
} }
@@ -95,15 +104,13 @@ TEST_M(tests_map_end)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,int> mymap; ft::map<T,U> mymap;
mymap['b'] = 100; mymap[VALT('b')] = VALU(100);
mymap['a'] = 200; mymap[VALT('a')] = VALU(200);
mymap['c'] = 300; mymap[VALT('c')] = VALU(300);
// show content: PRINT(mymap)
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
DELETE DELETE
} }
@@ -113,14 +120,14 @@ TEST_M(tests_map_rbegin)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,int> mymap; ft::map<T,U> mymap;
mymap['x'] = 100; mymap[VALT('x')] = VALU(100);
mymap['y'] = 200; mymap[VALT('y')] = VALU(200);
mymap['z'] = 300; mymap[VALT('z')] = VALU(300);
// show content: // show content:
std::map<char,int>::reverse_iterator rit; typename ft::map<T,U>::reverse_iterator rit;
for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit) for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit)
std::cout << rit->first << " => " << rit->second << '\n'; std::cout << rit->first << " => " << rit->second << '\n';
@@ -132,14 +139,14 @@ TEST_M(tests_map_rend)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,int> mymap; ft::map<T,U> mymap;
mymap['x'] = 100; mymap[VALT('x')] = VALU(100);
mymap['y'] = 200; mymap[VALT('y')] = VALU(200);
mymap['z'] = 300; mymap[VALT('z')] = VALU(300);
// show content: // show content:
std::map<char,int>::reverse_iterator rit; typename ft::map<T,U>::reverse_iterator rit;
for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit) for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit)
std::cout << rit->first << " => " << rit->second << '\n'; std::cout << rit->first << " => " << rit->second << '\n';
@@ -151,16 +158,16 @@ TEST_M(tests_map_empty)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,int> mymap; ft::map<T,U> mymap;
mymap['a']=10; mymap[VALT('a')]=VALU(10);
mymap['b']=20; mymap[VALT('b')]=VALU(20);
mymap['c']=30; mymap[VALT('c')]=VALU(30);
while (!mymap.empty()) while (!mymap.empty())
{ {
std::cout << mymap.begin()->first << " => " << mymap.begin()->second << '\n'; std::cout << mymap.begin()->first << " => " << mymap.begin()->second << '\n';
mymap.erase(mymap.begin()); mymap.erase(mymap.begin());
} }
DELETE DELETE
@@ -171,13 +178,15 @@ TEST_M(tests_map_size)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,int> mymap; ft::map<T,U> mymap;
mymap['a']=101; mymap[VALT('a')]=VALU(101);
mymap['b']=202; mymap[VALT('b')]=VALU(202);
mymap['c']=302; mymap[VALT('c')]=VALU(302);
std::cout << "mymap.size() is " << mymap.size() << '\n'; std::cout << "mymap.size() is " << mymap.size() << '\n';
PRINT(mymap)
DELETE DELETE
} }
@@ -187,11 +196,11 @@ TEST_M(tests_map_max_size)
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
int i; int i;
std::map<int,int> mymap; std::map<T,U> mymap;
if (mymap.max_size()>1000) if (mymap.max_size()>1000)
{ {
for (i=0; i<1000; i++) mymap[i]=0; for (i=0; i<1000; i++) mymap[i]=VALU(0);
std::cout << "The map contains 1000 elements.\n"; std::cout << "The map contains 1000 elements.\n";
} }
else std::cout << "The map could not hold 1000 elements.\n"; else std::cout << "The map could not hold 1000 elements.\n";
@@ -204,22 +213,23 @@ TEST_M(tests_map_operator_access)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,std::string> mymap; ft::map<T,U> mymap;
mymap['a']="an element"; mymap[VALT('a')]=VALU("An element");
mymap['b']="another element"; mymap[VALT('b')]=VALU("another element");
mymap['c']=mymap['b']; mymap[VALT('c')]=mymap[VAL('b')];
std::cout << "mymap['a'] is " << mymap['a'] << '\n'; std::cout << "mymap['a'] is " << mymap[VALT('a')] << '\n';
std::cout << "mymap['b'] is " << mymap['b'] << '\n'; std::cout << "mymap['b'] is " << mymap[VALT('b')] << '\n';
std::cout << "mymap['c'] is " << mymap['c'] << '\n'; std::cout << "mymap['c'] is " << mymap[VALT('c')] << '\n';
std::cout << "mymap['d'] is " << mymap['d'] << '\n'; std::cout << "mymap['d'] is " << mymap[VALT('d')] << '\n';
std::cout << "mymap now contains " << mymap.size() << " elements.\n"; std::cout << "mymap now contains " << mymap.size() << " elements.\n";
DELETE DELETE
} }
/*
TEST_M(tests_map_insert) TEST_M(tests_map_insert)
{ {
// title // title
@@ -604,7 +614,6 @@ TEST_M(tests_map_swap_non_member)
DELETE DELETE
} }
*/ */
#endif #endif

View File

@@ -42,10 +42,8 @@ TEST_V(tests_vector_operator_assignation)
// title // title
TITLE(more informations) TITLE(more informations)
std::cout << "foo:\n";
PRINT(foo); PRINT(foo);
std::cout << "bar:\n";
PRINT(bar); PRINT(bar);
DELETE DELETE
@@ -59,7 +57,6 @@ TEST_V(tests_vector_begin)
ft::vector<T> myvector; ft::vector<T> myvector;
for (int i=1; i<=5; i++) myvector.push_back(VAL(i)); for (int i=1; i<=5; i++) myvector.push_back(VAL(i));
std::cout << "myvector contains:\n";
PRINT(myvector); PRINT(myvector);
DELETE DELETE
@@ -73,7 +70,6 @@ TEST_V(tests_vector_end)
ft::vector<T> myvector; ft::vector<T> myvector;
for (int i=1; i<=5; i++) myvector.push_back(VAL(i)); for (int i=1; i<=5; i++) myvector.push_back(VAL(i));
std::cout << "myvector contains:\n";
PRINT(myvector); PRINT(myvector);
DELETE DELETE
@@ -92,7 +88,6 @@ TEST_V(tests_vector_rbegin)
for (; rit!= myvector.rend(); ++rit) for (; rit!= myvector.rend(); ++rit)
*rit = VAL(++i); *rit = VAL(++i);
std::cout << "myvector contains:";
PRINT(myvector); PRINT(myvector);
DELETE DELETE
@@ -111,7 +106,6 @@ TEST_V(tests_vector_rend)
for (rit = myvector.rbegin(); rit!= myvector.rend(); ++rit) for (rit = myvector.rbegin(); rit!= myvector.rend(); ++rit)
*rit = VAL(++i); *rit = VAL(++i);
std::cout << "myvector contains:";
PRINT(myvector); PRINT(myvector);
DELETE DELETE
@@ -172,7 +166,6 @@ TEST_V(tests_vector_resize)
myvector.resize(8,VAL(100)); myvector.resize(8,VAL(100));
myvector.resize(12); myvector.resize(12);
std::cout << "myvector contains:";
PRINT(myvector); PRINT(myvector);
@@ -334,7 +327,6 @@ TEST_V(tests_vector_operator_access)
myvector[i]=temp; myvector[i]=temp;
} }
std::cout << "myvector contains:";
PRINT(myvector) PRINT(myvector)
DELETE DELETE
@@ -351,7 +343,6 @@ TEST_V(tests_vector_at)
for (unsigned i=0; i<myvector.size(); i++) for (unsigned i=0; i<myvector.size(); i++)
myvector.at(i)=VAL(i); myvector.at(i)=VAL(i);
std::cout << "myvector contains:";
PRINT(myvector) PRINT(myvector)
DELETE DELETE
@@ -549,7 +540,6 @@ TEST_V(tests_vector_insert)
T myarray [] = { VAL(501),VAL(502),VAL(503) }; T myarray [] = { VAL(501),VAL(502),VAL(503) };
myvector.insert (myvector.begin(), myarray, myarray+3); myvector.insert (myvector.begin(), myarray, myarray+3);
std::cout << "myvector contains:";
PRINT(myvector) PRINT(myvector)
// title // title
@@ -560,7 +550,6 @@ TEST_V(tests_vector_insert)
it = myvector2.begin(); it = myvector2.begin();
std::cout << "size:" << myvector2.size() << " capacity:" << myvector2.capacity() << "\n"; std::cout << "size:" << myvector2.size() << " capacity:" << myvector2.capacity() << "\n";
myvector2.insert ( it , VAL(200) ); myvector2.insert ( it , VAL(200) );
std::cout << "myvector contains:";
PRINT(myvector2) PRINT(myvector2)
ft::vector<T> myvector3 (3,VAL(100)); ft::vector<T> myvector3 (3,VAL(100));
@@ -568,7 +557,6 @@ TEST_V(tests_vector_insert)
it = myvector3.end(); it = myvector3.end();
std::cout << "\nsize:" << myvector3.size() << " capacity:" << myvector3.capacity() << "\n"; std::cout << "\nsize:" << myvector3.size() << " capacity:" << myvector3.capacity() << "\n";
myvector3.insert ( it , VAL(200) ); myvector3.insert ( it , VAL(200) );
std::cout << "myvector contains:";
PRINT(myvector3) PRINT(myvector3)
ft::vector<T> myvector4 (3,VAL(100)); ft::vector<T> myvector4 (3,VAL(100));
@@ -576,7 +564,6 @@ TEST_V(tests_vector_insert)
it = myvector4.begin() + 2; it = myvector4.begin() + 2;
std::cout << "\nsize:" << myvector3.size() << " capacity:" << myvector3.capacity() << "\n"; std::cout << "\nsize:" << myvector3.size() << " capacity:" << myvector3.capacity() << "\n";
myvector4.insert ( it , VAL(200) ); myvector4.insert ( it , VAL(200) );
std::cout << "myvector contains:";
PRINT(myvector4) PRINT(myvector4)
@@ -597,7 +584,6 @@ TEST_V(tests_vector_insert)
it = myvector5.end() - 2; it = myvector5.end() - 2;
myvector5.insert (it,2,VAL(550)); myvector5.insert (it,2,VAL(550));
std::cout << "myvector contains:";
PRINT(myvector5) PRINT(myvector5)
@@ -631,7 +617,6 @@ TEST_V(tests_vector_erase)
// 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:";
PRINT(myvector) PRINT(myvector)
// title // title
@@ -666,10 +651,8 @@ TEST_V(tests_vector_swap)
foo.swap(bar); foo.swap(bar);
std::cout << "foo contains:";
PRINT(foo) PRINT(foo)
std::cout << "bar contains:";
PRINT(bar) PRINT(bar)
DELETE DELETE
@@ -685,14 +668,12 @@ TEST_V(tests_vector_clear)
myvector.push_back (VAL(200)); myvector.push_back (VAL(200));
myvector.push_back (VAL(300)); myvector.push_back (VAL(300));
std::cout << "myvector contains:";
PRINT(myvector) PRINT(myvector)
myvector.clear(); myvector.clear();
myvector.push_back (VAL(1101)); myvector.push_back (VAL(1101));
myvector.push_back (VAL(2202)); myvector.push_back (VAL(2202));
std::cout << "myvector contains:";
PRINT(myvector) PRINT(myvector)
DELETE DELETE
@@ -752,10 +733,8 @@ TEST_V(tests_vector_swap_non_member)
foo.swap(bar); foo.swap(bar);
std::cout << "foo contains:";
PRINT(foo) PRINT(foo)
std::cout << "bar contains:";
PRINT(bar) PRINT(bar)
DELETE DELETE