all tests except one ok on map

This commit is contained in:
hugogogo
2022-06-21 18:38:28 +02:00
parent 844701201f
commit 0d98268a74
7 changed files with 215 additions and 199 deletions

View File

@@ -163,9 +163,10 @@ public:
// get_allocator ----------------------------- // get_allocator -----------------------------
allocator_type get_allocator() const; allocator_type get_allocator() const;
// TMP non privat
bst_map _bst;
private: private:
bst_map _bst;
allocator_type _allocator; allocator_type _allocator;
key_compare _comp; key_compare _comp;

View File

@@ -238,22 +238,28 @@ MP_TPL typename MP::allocator_type MP::
************************/ ************************/
// operator == ------------------------------- // operator == -------------------------------
template< class Key, class T, class Compare, class Alloc > bool operator== template< class Key, class T, class Compare, class Alloc > bool operator==
( const MP& lhs, const MP& rhs ) { return (lhs._bst == rhs._bst); } ( const MP& lhs, const MP& rhs ) {
// operator != -------------------------------
template< class Key, class T, class Compare, class Alloc > bool operator!= return (lhs._bst == rhs._bst);
( const MP& lhs, const MP& rhs ) { return (lhs._bst != rhs._bst); } }
// operator < -------------------------------- // operator < --------------------------------
template< class Key, class T, class Compare, class Alloc > bool operator< template< class Key, class T, class Compare, class Alloc > bool operator<
( const MP& lhs, const MP& rhs ) { return (lhs._bst < rhs._bst); } ( const MP& lhs, const MP& rhs ) {
return (lhs._bst < rhs._bst);
}
// operator != -------------------------------
template< class Key, class T, class Compare, class Alloc > bool operator!=
( const MP& lhs, const MP& rhs ) { return !(lhs == rhs); }
// operator <= ------------------------------- // operator <= -------------------------------
template< class Key, class T, class Compare, class Alloc > bool operator<= template< class Key, class T, class Compare, class Alloc > bool operator<=
( const MP& lhs, const MP& rhs ) { return (lhs._bst <= rhs._bst); } ( const MP& lhs, const MP& rhs ) { return !(lhs > rhs); }
// operator > -------------------------------- // operator > --------------------------------
template< class Key, class T, class Compare, class Alloc > bool operator> template< class Key, class T, class Compare, class Alloc > bool operator>
( const MP& lhs, const MP& rhs ) { return (lhs._bst > rhs._bst); } ( const MP& lhs, const MP& rhs ) { return (rhs < lhs); }
// operator >= ------------------------------- // operator >= -------------------------------
template< class Key, class T, class Compare, class Alloc > bool operator>= template< class Key, class T, class Compare, class Alloc > bool operator>=
( const MP& lhs, const MP& rhs ) { return (lhs._bst >= rhs._bst); } ( const MP& lhs, const MP& rhs ) { return !(lhs < rhs); }
// swap (map) ----------------------------- // swap (map) -----------------------------
template< class Key, class T, class Compare, class Alloc > void swap template< class Key, class T, class Compare, class Alloc > void swap
( const MP& lhs, const MP& rhs ) { lhs.swap(rhs); } ( const MP& lhs, const MP& rhs ) { lhs.swap(rhs); }

View File

@@ -69,6 +69,8 @@ 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();
void tests_map_relational_operators();
void tests_map_swap_non_member();
#endif #endif

View File

@@ -70,10 +70,11 @@ extern std::vector< mystruct* > mem_list;
template <class T> struct s_ ## f_name : public A_test\ template <class T> struct s_ ## f_name : public A_test\
{ void func(); };\ { void func(); };\
void f_name () {\ void f_name () {\
add_to_list("", "", NULL);\
add_to_list(#f_name, "int", new(s_ ## f_name <int>));\ add_to_list(#f_name, "int", new(s_ ## f_name <int>));\
add_to_list("", "char", new(s_ ## f_name <char>));\ add_to_list(#f_name, "char", new(s_ ## f_name <char>));\
add_to_list("", "std::string", new(s_ ## f_name <std::string>));\ add_to_list(#f_name, "std::string", new(s_ ## f_name <std::string>));\
add_to_list("", "mystruct*", new(s_ ## f_name <mystruct*>));\ add_to_list(#f_name, "mystruct*", new(s_ ## f_name <mystruct*>));\
}\ }\
template <class T>\ template <class T>\
void s_ ## f_name <T>::func() void s_ ## f_name <T>::func()
@@ -82,14 +83,23 @@ 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, "char, int", new(s_ ## f_name <char, int>));\ add_to_list("", "", NULL);\
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*>));\ add_to_list(#f_name, "char, mystruct*", new(s_ ## f_name <char, mystruct*>));\
add_to_list(#f_name, "int, mystruct*", new(s_ ## f_name <int, 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()
/*
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*>));\
add_to_list(#f_name, "int, int", new(s_ ## f_name <int, int>));\
add_to_list(#f_name, "int, char", new(s_ ## f_name <int, char>));\
add_to_list(#f_name, "int, std::string", new(s_ ## f_name <int, std::string>));\
add_to_list(#f_name, "int, mystruct*", new(s_ ## f_name <int, mystruct*>));\
*/
// templates print // templates print
// ***************************************** // *****************************************

View File

@@ -45,20 +45,20 @@ int main() {
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();
// tests_map_clear(); tests_map_clear();
// tests_map_key_comp(); tests_map_key_comp();
// tests_map_value_comp(); tests_map_value_comp();
// tests_map_find(); tests_map_find();
// tests_map_count(); tests_map_count();
// tests_map_lower_bound(); tests_map_lower_bound();
// tests_map_upper_bound(); tests_map_upper_bound();
// tests_map_equal_range(); tests_map_equal_range();
// tests_map_get_allocator(); tests_map_get_allocator();
// tests_map_relational_operators(); // tests_map_relational_operators();
// tests_map_swap_non_member(); tests_map_swap_non_member();
// STACK // STACK

View File

@@ -10,8 +10,11 @@ void add_to_list(std::string title, std::string type, A_test* test) {
std::vector< std::vector<A_test*> >::iterator it; std::vector< std::vector<A_test*> >::iterator it;
// title != NULL for the first element // title != NULL for the first element
if (!title.empty()) if (test == NULL)
{
test_list.push_back(test_sub_list); test_list.push_back(test_sub_list);
return;
}
test->title = title; test->title = title;
test->type = type; test->type = type;

View File

@@ -229,42 +229,36 @@ TEST_M(tests_map_operator_access)
DELETE DELETE
} }
/*
TEST_M(tests_map_insert) TEST_M(tests_map_insert)
{ {
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,int> mymap; ft::map<T,U> mymap;
// first insert function version (single parameter): // first insert function version (single parameter):
mymap.insert ( std::pair<char,int>('a',100) ); mymap.insert ( ft::pair<T,U>(VALT('a'),VALU(100)) );
mymap.insert ( std::pair<char,int>('z',200) ); mymap.insert ( ft::pair<T,U>(VALT('z'),VALU(200)) );
std::pair<std::map<char,int>::iterator,bool> ret; ft::pair<typename ft::map<T,U>::iterator, bool> ret;
ret = mymap.insert ( std::pair<char,int>('z',500) ); ret = mymap.insert ( ft::pair<T,U>(VALT('z'),VALU(500)) );
if (ret.second==false) { if (ret.second==false) {
std::cout << "element 'z' already existed"; std::cout << "element 'z' already existed";
std::cout << " with a value of " << ret.first->second << '\n'; std::cout << " with a value of " << ret.first->second << '\n';
} }
// second insert function version (with hint position): // second insert function version (with hint position):
std::map<char,int>::iterator it = mymap.begin(); typename ft::map<T,U>::iterator it = mymap.begin();
mymap.insert (it, std::pair<char,int>('b',300)); // max efficiency inserting mymap.insert (it, ft::pair<T,U>(VALT('b'),VALU(300))); // max efficiency inserting
mymap.insert (it, std::pair<char,int>('c',400)); // no max efficiency inserting mymap.insert (it, ft::pair<T,U>(VALT('c'),VALU(400))); // no max efficiency inserting
// third insert function version (range insertion): // third insert function version (range insertion):
std::map<char,int> anothermap; ft::map<T,U> anothermap;
anothermap.insert(mymap.begin(),mymap.find('c')); anothermap.insert(mymap.begin(),mymap.find('c'));
// showing contents: PRINT(mymap)
std::cout << "mymap contains:\n"; PRINT(anothermap)
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 DELETE
} }
@@ -274,28 +268,26 @@ TEST_M(tests_map_erase)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,int> mymap; ft::map<T,U> mymap;
std::map<char,int>::iterator it; typename ft::map<T,U>::iterator it;
// insert some values: // insert some values:
mymap['a']=10; mymap[VALT('a')]=VALU(10);
mymap['b']=20; mymap[VALT('b')]=VALU(20);
mymap['c']=30; mymap[VALT('c')]=VALU(30);
mymap['d']=40; mymap[VALT('d')]=VALU(40);
mymap['e']=50; mymap[VALT('e')]=VALU(50);
mymap['f']=60; mymap[VALT('f')]=VALU(60);
it=mymap.find('b'); it=mymap.find(VALT('b'));
mymap.erase (it); // erasing by iterator mymap.erase (it); // erasing by iterator
mymap.erase ('c'); // erasing by key mymap.erase (VALT('c')); // erasing by key
it=mymap.find ('e'); it=mymap.find (VALT('e'));
mymap.erase ( it, mymap.end() ); // erasing by range mymap.erase ( it, mymap.end() ); // erasing by range
// show content: PRINT(mymap)
for (it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
DELETE DELETE
} }
@@ -305,24 +297,19 @@ TEST_M(tests_map_swap)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,int> foo,bar; ft::map<T,U> foo,bar;
foo['x']=100; foo[VALT('x')]=VALU(100);
foo['y']=200; foo[VALT('y')]=VALU(200);
bar['a']=11; bar[VALT('a')]=VALU(11);
bar['b']=22; bar[VALT('b')]=VALU(22);
bar['c']=33; bar[VALT('c')]=VALU(33);
foo.swap(bar); foo.swap(bar);
std::cout << "foo contains:\n"; PRINT(foo)
for (std::map<char,int>::iterator it=foo.begin(); it!=foo.end(); ++it) PRINT(bar)
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 DELETE
} }
@@ -332,23 +319,19 @@ TEST_M(tests_map_clear)
// 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);
std::cout << "mymap contains:\n"; PRINT(mymap)
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
mymap.clear(); mymap.clear();
mymap['a']=1101; mymap[VALT('a')]=VALU(1101);
mymap['b']=2202; mymap[VALT('b')]=VALU(2202);
std::cout << "mymap contains:\n"; PRINT(mymap)
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';
DELETE DELETE
} }
@@ -358,19 +341,19 @@ TEST_M(tests_map_key_comp)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,int> mymap; ft::map<T,U> mymap;
std::map<char,int>::key_compare mycomp = mymap.key_comp(); typename ft::map<T,U>::key_compare mycomp = mymap.key_comp();
mymap['a']=100; mymap[VALT('a')]=VALU(100);
mymap['b']=200; mymap[VALT('b')]=VALU(200);
mymap['c']=300; mymap[VALT('c')]=VALU(300);
std::cout << "mymap contains:\n"; std::cout << "mymap contains:\n";
char highest = mymap.rbegin()->first; // key value of last element T highest = mymap.rbegin()->first; // key value of last element
std::map<char,int>::iterator it = mymap.begin(); typename ft::map<T,U>::iterator it = mymap.begin();
do { do {
std::cout << it->first << " => " << it->second << '\n'; std::cout << it->first << " => " << it->second << '\n';
} while ( mycomp((*it++).first, highest) ); } while ( mycomp((*it++).first, highest) );
@@ -385,17 +368,17 @@ TEST_M(tests_map_value_comp)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,int> mymap; ft::map<T,U> mymap;
mymap['x']=1001; mymap[VALT('x')]=VALU(1001);
mymap['y']=2002; mymap[VALT('y')]=VALU(2002);
mymap['z']=3003; mymap[VALT('z')]=VALU(3003);
std::cout << "mymap contains:\n"; std::cout << "mymap contains:\n";
std::pair<char,int> highest = *mymap.rbegin(); // last element ft::pair<T,U> highest = *mymap.rbegin(); // last element
std::map<char,int>::iterator it = mymap.begin(); typename ft::map<T,U>::iterator it = mymap.begin();
do { do {
std::cout << it->first << " => " << it->second << '\n'; std::cout << it->first << " => " << it->second << '\n';
} while ( mymap.value_comp()(*it++, highest) ); } while ( mymap.value_comp()(*it++, highest) );
@@ -408,23 +391,23 @@ TEST_M(tests_map_find)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,int> mymap; ft::map<T,U> mymap;
std::map<char,int>::iterator it; typename ft::map<T,U>::iterator it;
mymap['a']=50; mymap[VALT('a')]=VALU(50);
mymap['b']=100; mymap[VALT('b')]=VALU(100);
mymap['c']=150; mymap[VALT('c')]=VALU(150);
mymap['d']=200; mymap[VALT('d')]=VALU(200);
it = mymap.find('b'); it = mymap.find(VALT('b'));
if (it != mymap.end()) if (it != mymap.end())
mymap.erase (it); mymap.erase (it);
// print content: // print content:
std::cout << "elements in mymap:" << '\n'; std::cout << "elements in mymap:" << '\n';
std::cout << "a => " << mymap.find('a')->second << '\n'; std::cout << "a => " << mymap.find(VALT('a'))->second << '\n';
std::cout << "c => " << mymap.find('c')->second << '\n'; std::cout << "c => " << mymap.find(VALT('c'))->second << '\n';
std::cout << "d => " << mymap.find('d')->second << '\n'; std::cout << "d => " << mymap.find(VALT('d'))->second << '\n';
DELETE DELETE
} }
@@ -434,14 +417,15 @@ TEST_M(tests_map_count)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,int> mymap; ft::map<T,U> mymap;
char c; T c;
mymap ['a']=101; mymap [VALT('a')]=VALU(101);
mymap ['c']=202; mymap [VALT('c')]=VALU(202);
mymap ['f']=303; mymap [VALT('f')]=VALU(303);
for (c='a'; c<'h'; c++) // to do this test with T as a 'string' or 'mystruct*' we should add overload
for (c=VALT('a'); c<VALT('h'); c++)
{ {
std::cout << c; std::cout << c;
if (mymap.count(c)>0) if (mymap.count(c)>0)
@@ -458,23 +442,21 @@ TEST_M(tests_map_lower_bound)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,int> mymap; ft::map<T,U> mymap;
std::map<char,int>::iterator itlow,itup; typename ft::map<T,U>::iterator itlow,itup;
mymap['a']=20; mymap[VALT('a')]=VALU(20);
mymap['b']=40; mymap[VALT('b')]=VALU(40);
mymap['c']=60; mymap[VALT('c')]=VALU(60);
mymap['d']=80; mymap[VALT('d')]=VALU(80);
mymap['e']=100; mymap[VALT('e')]=VALU(100);
itlow=mymap.lower_bound ('b'); // itlow points to b itlow=mymap.lower_bound (VALT('b')); // itlow points to b
itup=mymap.upper_bound ('d'); // itup points to e (not d!) itup=mymap.upper_bound (VALT('d')); // itup points to e (not d!)
mymap.erase(itlow,itup); // erases [itlow,itup) mymap.erase(itlow,itup); // erases [itlow,itup)
// print 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
} }
@@ -484,23 +466,21 @@ TEST_M(tests_map_upper_bound)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<char,int> mymap; ft::map<T,U> mymap;
std::map<char,int>::iterator itlow,itup; typename ft::map<T,U>::iterator itlow,itup;
mymap['a']=20; mymap[VALT('a')]=VALU(20);
mymap['b']=40; mymap[VALT('b')]=VALU(40);
mymap['c']=60; mymap[VALT('c')]=VALU(60);
mymap['d']=80; mymap[VALT('d')]=VALU(80);
mymap['e']=100; mymap[VALT('e')]=VALU(100);
itlow=mymap.lower_bound ('b'); // itlow points to b itlow=mymap.lower_bound (VALT('b')); // itlow points to b
itup=mymap.upper_bound ('d'); // itup points to e (not d!) itup=mymap.upper_bound (VALT('d')); // itup points to e (not d!)
mymap.erase(itlow,itup); // erases [itlow,itup) mymap.erase(itlow,itup); // erases [itlow,itup)
// print 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
} }
@@ -510,14 +490,14 @@ TEST_M(tests_map_equal_range)
// 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);
std::pair<std::map<char,int>::iterator,std::map<char,int>::iterator> ret; ft::pair<typename ft::map<T,U>::iterator,typename ft::map<T,U>::iterator> ret;
ret = mymap.equal_range('b'); ret = mymap.equal_range(VALT('b'));
std::cout << "lower bound points to: "; std::cout << "lower bound points to: ";
std::cout << ret.first->first << " => " << ret.first->second << '\n'; std::cout << ret.first->first << " => " << ret.first->second << '\n';
@@ -534,14 +514,14 @@ TEST_M(tests_map_get_allocator)
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
int psize; int psize;
std::map<char,int> mymap; ft::map<T,U> mymap;
std::pair<const char,int>* p; ft::pair<const T,U>* p;
// allocate an array of 5 elements using mymap's allocator: // allocate an array of 5 elements using mymap's allocator:
p=mymap.get_allocator().allocate(5); p=mymap.get_allocator().allocate(5);
// assign some values to array // assign some values to array
psize = sizeof(std::map<char,int>::value_type)*5; psize = sizeof(typename ft::map<T,U>::value_type)*5;
std::cout << "The allocated array has a size of " << psize << " bytes.\n"; std::cout << "The allocated array has a size of " << psize << " bytes.\n";
@@ -555,9 +535,25 @@ TEST_M(tests_map_relational_operators)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<int, char> alice{{1, 'a'}, {2, 'b'}, {3, 'c'}}; ft::map<T,U> alice;
std::map<int, char> bob{{7, 'Z'}, {8, 'Y'}, {9, 'X'}, {10, 'W'}}; ft::map<T,U> bob;
std::map<int, char> eve{{1, 'a'}, {2, 'b'}, {3, 'c'}}; ft::map<T,U> eve;
(void)alice;
(void)bob;
(void)eve;
alice[VALT(1)]=VALU('a');
alice[VALT(2)]=VALU('b');
alice[VALT(3)]=VALU('c');
bob[VALT(7)]=VALU('Z');
bob[VALT(8)]=VALU('Y');
bob[VALT(9)]=VALU('X');
bob[VALT(10)]=VALU('W');
eve[VALT(1)]=VALU('a');
eve[VALT(2)]=VALU('b');
eve[VALT(3)]=VALU('c');
std::cout << std::boolalpha; std::cout << std::boolalpha;
@@ -588,33 +584,31 @@ TEST_M(tests_map_swap_non_member)
// title // title
TITLE(cplusplus.com reference) TITLE(cplusplus.com reference)
std::map<int, char> alice{{1, 'a'}, {2, 'b'}, {3, 'c'}}; ft::map<T, U> alice;
std::map<int, char> bob{{7, 'Z'}, {8, 'Y'}, {9, 'X'}, {10, 'W'}}; ft::map<T, U> bob;
auto print = [](std::pair<const int, char>& n) { alice[VALT(1)]=VALU('a');
std::cout << " " << n.first << '(' << n.second << ')'; alice[VALT(2)]=VALU('b');
}; alice[VALT(3)]=VALU('c');
bob[VALT(7)]=VALU('Z');
bob[VALT(8)]=VALU('Y');
bob[VALT(9)]=VALU('X');
bob[VALT(10)]=VALU('W');
// Print state before swap // Print state before swap
std::cout << "alice:"; PRINT(alice)
std::for_each(alice.begin(), alice.end(), print); PRINT(bob)
std::cout << "\n" "bob :";
std::for_each(bob.begin(), bob.end(), print);
std::cout << '\n';
std::cout << "-- SWAP\n"; std::cout << "-- SWAP\n";
std::swap(alice, bob); std::swap(alice, bob);
// Print state after swap // Print state after swap
std::cout << "alice:"; PRINT(alice)
std::for_each(alice.begin(), alice.end(), print); PRINT(bob)
std::cout << "\n" "bob :";
std::for_each(bob.begin(), bob.end(), print);
std::cout << '\n';
DELETE DELETE
} }
*/
#endif #endif