diff --git a/headers/map.hpp b/headers/map.hpp index e4c51fa..76bea02 100644 --- a/headers/map.hpp +++ b/headers/map.hpp @@ -163,9 +163,10 @@ public: // get_allocator ----------------------------- allocator_type get_allocator() const; + // TMP non privat + bst_map _bst; private: - bst_map _bst; allocator_type _allocator; key_compare _comp; diff --git a/templates/map.tpp b/templates/map.tpp index 506a818..354fa0f 100644 --- a/templates/map.tpp +++ b/templates/map.tpp @@ -238,22 +238,28 @@ MP_TPL typename MP::allocator_type MP:: ************************/ // operator == ------------------------------- template< class Key, class T, class Compare, class Alloc > bool operator== - ( 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._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._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 <= ------------------------------- 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 > -------------------------------- 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 >= ------------------------------- 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) ----------------------------- template< class Key, class T, class Compare, class Alloc > void swap ( const MP& lhs, const MP& rhs ) { lhs.swap(rhs); } diff --git a/tests/includes/main.hpp b/tests/includes/main.hpp index fafcf6a..d9d8da6 100644 --- a/tests/includes/main.hpp +++ b/tests/includes/main.hpp @@ -69,6 +69,8 @@ void tests_map_lower_bound(); void tests_map_upper_bound(); void tests_map_equal_range(); void tests_map_get_allocator(); +void tests_map_relational_operators(); +void tests_map_swap_non_member(); #endif diff --git a/tests/includes/tests_utils.hpp b/tests/includes/tests_utils.hpp index ee20cfa..bed6a71 100644 --- a/tests/includes/tests_utils.hpp +++ b/tests/includes/tests_utils.hpp @@ -70,10 +70,11 @@ extern std::vector< mystruct* > mem_list; template struct s_ ## f_name : public A_test\ { void func(); };\ void f_name () {\ + add_to_list("", "", NULL);\ add_to_list(#f_name, "int", new(s_ ## f_name ));\ - add_to_list("", "char", new(s_ ## f_name ));\ - add_to_list("", "std::string", new(s_ ## f_name ));\ - add_to_list("", "mystruct*", new(s_ ## f_name ));\ + add_to_list(#f_name, "char", new(s_ ## f_name ));\ + add_to_list(#f_name, "std::string", new(s_ ## f_name ));\ + add_to_list(#f_name, "mystruct*", new(s_ ## f_name ));\ }\ template \ void s_ ## f_name ::func() @@ -82,14 +83,23 @@ extern std::vector< mystruct* > mem_list; template struct s_ ## f_name : public A_test\ { void func(); };\ void f_name () {\ + add_to_list("", "", NULL);\ + add_to_list(#f_name, "char, mystruct*", new(s_ ## f_name ));\ + add_to_list(#f_name, "int, mystruct*", new(s_ ## f_name ));\ + }\ + template \ + void s_ ## f_name ::func() + +/* add_to_list(#f_name, "char, int", new(s_ ## f_name ));\ add_to_list(#f_name, "char, char", new(s_ ## f_name ));\ add_to_list(#f_name, "char, std::string", new(s_ ## f_name ));\ add_to_list(#f_name, "char, mystruct*", new(s_ ## f_name ));\ - }\ - template \ - void s_ ## f_name ::func() - + add_to_list(#f_name, "int, int", new(s_ ## f_name ));\ + add_to_list(#f_name, "int, char", new(s_ ## f_name ));\ + add_to_list(#f_name, "int, std::string", new(s_ ## f_name ));\ + add_to_list(#f_name, "int, mystruct*", new(s_ ## f_name ));\ +*/ // templates print // ***************************************** diff --git a/tests/main.cpp b/tests/main.cpp index cbf0a51..05fe950 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -45,20 +45,20 @@ int main() { tests_map_size(); tests_map_max_size(); tests_map_operator_access(); -// 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(); + 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(); // tests_map_relational_operators(); -// tests_map_swap_non_member(); + tests_map_swap_non_member(); // STACK diff --git a/tests/tests_definitions.cpp b/tests/tests_definitions.cpp index 511015c..ed3fbd4 100644 --- a/tests/tests_definitions.cpp +++ b/tests/tests_definitions.cpp @@ -10,8 +10,11 @@ void add_to_list(std::string title, std::string type, A_test* test) { std::vector< std::vector >::iterator it; // title != NULL for the first element - if (!title.empty()) + if (test == NULL) + { test_list.push_back(test_sub_list); + return; + } test->title = title; test->type = type; diff --git a/tests/tests_map.cpp b/tests/tests_map.cpp index b74ab47..1472110 100644 --- a/tests/tests_map.cpp +++ b/tests/tests_map.cpp @@ -229,42 +229,36 @@ TEST_M(tests_map_operator_access) DELETE } -/* TEST_M(tests_map_insert) { // title TITLE(cplusplus.com reference) - std::map mymap; + ft::map mymap; // first insert function version (single parameter): - mymap.insert ( std::pair('a',100) ); - mymap.insert ( std::pair('z',200) ); + mymap.insert ( ft::pair(VALT('a'),VALU(100)) ); + mymap.insert ( ft::pair(VALT('z'),VALU(200)) ); - std::pair::iterator,bool> ret; - ret = mymap.insert ( std::pair('z',500) ); + ft::pair::iterator, bool> ret; + ret = mymap.insert ( ft::pair(VALT('z'),VALU(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::iterator it = mymap.begin(); - mymap.insert (it, std::pair('b',300)); // max efficiency inserting - mymap.insert (it, std::pair('c',400)); // no max efficiency inserting + typename ft::map::iterator it = mymap.begin(); + mymap.insert (it, ft::pair(VALT('b'),VALU(300))); // max efficiency inserting + mymap.insert (it, ft::pair(VALT('c'),VALU(400))); // no max efficiency inserting // third insert function version (range insertion): - std::map anothermap; + ft::map 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'; + PRINT(mymap) + PRINT(anothermap) DELETE } @@ -274,28 +268,26 @@ TEST_M(tests_map_erase) // title TITLE(cplusplus.com reference) - std::map mymap; - std::map::iterator it; + ft::map mymap; + typename ft::map::iterator it; // insert some values: - mymap['a']=10; - mymap['b']=20; - mymap['c']=30; - mymap['d']=40; - mymap['e']=50; - mymap['f']=60; + mymap[VALT('a')]=VALU(10); + mymap[VALT('b')]=VALU(20); + mymap[VALT('c')]=VALU(30); + mymap[VALT('d')]=VALU(40); + mymap[VALT('e')]=VALU(50); + mymap[VALT('f')]=VALU(60); - it=mymap.find('b'); - mymap.erase (it); // erasing by iterator + it=mymap.find(VALT('b')); + mymap.erase (it); // erasing by iterator - mymap.erase ('c'); // erasing by key + mymap.erase (VALT('c')); // erasing by key - it=mymap.find ('e'); - mymap.erase ( it, mymap.end() ); // erasing by range + it=mymap.find (VALT('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'; + PRINT(mymap) DELETE } @@ -305,24 +297,19 @@ TEST_M(tests_map_swap) // title TITLE(cplusplus.com reference) - std::map foo,bar; + ft::map foo,bar; - foo['x']=100; - foo['y']=200; + foo[VALT('x')]=VALU(100); + foo[VALT('y')]=VALU(200); - bar['a']=11; - bar['b']=22; - bar['c']=33; + bar[VALT('a')]=VALU(11); + bar[VALT('b')]=VALU(22); + bar[VALT('c')]=VALU(33); foo.swap(bar); - std::cout << "foo contains:\n"; - for (std::map::iterator it=foo.begin(); it!=foo.end(); ++it) - std::cout << it->first << " => " << it->second << '\n'; - - std::cout << "bar contains:\n"; - for (std::map::iterator it=bar.begin(); it!=bar.end(); ++it) - std::cout << it->first << " => " << it->second << '\n'; + PRINT(foo) + PRINT(bar) DELETE } @@ -332,23 +319,19 @@ TEST_M(tests_map_clear) // title TITLE(cplusplus.com reference) - std::map mymap; + ft::map mymap; - mymap['x']=100; - mymap['y']=200; - mymap['z']=300; + mymap[VALT('x')]=VALU(100); + mymap[VALT('y')]=VALU(200); + mymap[VALT('z')]=VALU(300); - std::cout << "mymap contains:\n"; - for (std::map::iterator it=mymap.begin(); it!=mymap.end(); ++it) - std::cout << it->first << " => " << it->second << '\n'; + PRINT(mymap) mymap.clear(); - mymap['a']=1101; - mymap['b']=2202; + mymap[VALT('a')]=VALU(1101); + mymap[VALT('b')]=VALU(2202); - std::cout << "mymap contains:\n"; - for (std::map::iterator it=mymap.begin(); it!=mymap.end(); ++it) - std::cout << it->first << " => " << it->second << '\n'; + PRINT(mymap) DELETE } @@ -358,19 +341,19 @@ TEST_M(tests_map_key_comp) // title TITLE(cplusplus.com reference) - std::map mymap; + ft::map mymap; - std::map::key_compare mycomp = mymap.key_comp(); + typename ft::map::key_compare mycomp = mymap.key_comp(); - mymap['a']=100; - mymap['b']=200; - mymap['c']=300; + mymap[VALT('a')]=VALU(100); + mymap[VALT('b')]=VALU(200); + mymap[VALT('c')]=VALU(300); 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::iterator it = mymap.begin(); + typename ft::map::iterator it = mymap.begin(); do { std::cout << it->first << " => " << it->second << '\n'; } while ( mycomp((*it++).first, highest) ); @@ -385,17 +368,17 @@ TEST_M(tests_map_value_comp) // title TITLE(cplusplus.com reference) - std::map mymap; + ft::map mymap; - mymap['x']=1001; - mymap['y']=2002; - mymap['z']=3003; + mymap[VALT('x')]=VALU(1001); + mymap[VALT('y')]=VALU(2002); + mymap[VALT('z')]=VALU(3003); std::cout << "mymap contains:\n"; - std::pair highest = *mymap.rbegin(); // last element + ft::pair highest = *mymap.rbegin(); // last element - std::map::iterator it = mymap.begin(); + typename ft::map::iterator it = mymap.begin(); do { std::cout << it->first << " => " << it->second << '\n'; } while ( mymap.value_comp()(*it++, highest) ); @@ -408,23 +391,23 @@ TEST_M(tests_map_find) // title TITLE(cplusplus.com reference) - std::map mymap; - std::map::iterator it; + ft::map mymap; + typename ft::map::iterator it; - mymap['a']=50; - mymap['b']=100; - mymap['c']=150; - mymap['d']=200; + mymap[VALT('a')]=VALU(50); + mymap[VALT('b')]=VALU(100); + mymap[VALT('c')]=VALU(150); + mymap[VALT('d')]=VALU(200); - it = mymap.find('b'); + it = mymap.find(VALT('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'; + std::cout << "a => " << mymap.find(VALT('a'))->second << '\n'; + std::cout << "c => " << mymap.find(VALT('c'))->second << '\n'; + std::cout << "d => " << mymap.find(VALT('d'))->second << '\n'; DELETE } @@ -434,14 +417,15 @@ TEST_M(tests_map_count) // title TITLE(cplusplus.com reference) - std::map mymap; - char c; + ft::map mymap; + T c; - mymap ['a']=101; - mymap ['c']=202; - mymap ['f']=303; + mymap [VALT('a')]=VALU(101); + mymap [VALT('c')]=VALU(202); + 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'); c0) @@ -458,23 +442,21 @@ TEST_M(tests_map_lower_bound) // title TITLE(cplusplus.com reference) - std::map mymap; - std::map::iterator itlow,itup; + ft::map mymap; + typename ft::map::iterator itlow,itup; - mymap['a']=20; - mymap['b']=40; - mymap['c']=60; - mymap['d']=80; - mymap['e']=100; + mymap[VALT('a')]=VALU(20); + mymap[VALT('b')]=VALU(40); + mymap[VALT('c')]=VALU(60); + mymap[VALT('d')]=VALU(80); + mymap[VALT('e')]=VALU(100); - itlow=mymap.lower_bound ('b'); // itlow points to b - itup=mymap.upper_bound ('d'); // itup points to e (not d!) + itlow=mymap.lower_bound (VALT('b')); // itlow points to b + itup=mymap.upper_bound (VALT('d')); // itup points to e (not d!) mymap.erase(itlow,itup); // erases [itlow,itup) - // print content: - for (std::map::iterator it=mymap.begin(); it!=mymap.end(); ++it) - std::cout << it->first << " => " << it->second << '\n'; + PRINT(mymap) DELETE } @@ -484,23 +466,21 @@ TEST_M(tests_map_upper_bound) // title TITLE(cplusplus.com reference) - std::map mymap; - std::map::iterator itlow,itup; + ft::map mymap; + typename ft::map::iterator itlow,itup; - mymap['a']=20; - mymap['b']=40; - mymap['c']=60; - mymap['d']=80; - mymap['e']=100; + mymap[VALT('a')]=VALU(20); + mymap[VALT('b')]=VALU(40); + mymap[VALT('c')]=VALU(60); + mymap[VALT('d')]=VALU(80); + mymap[VALT('e')]=VALU(100); - itlow=mymap.lower_bound ('b'); // itlow points to b - itup=mymap.upper_bound ('d'); // itup points to e (not d!) + itlow=mymap.lower_bound (VALT('b')); // itlow points to b + 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: - for (std::map::iterator it=mymap.begin(); it!=mymap.end(); ++it) - std::cout << it->first << " => " << it->second << '\n'; + PRINT(mymap) DELETE } @@ -510,14 +490,14 @@ TEST_M(tests_map_equal_range) // title TITLE(cplusplus.com reference) - std::map mymap; + ft::map mymap; - mymap['a']=10; - mymap['b']=20; - mymap['c']=30; + mymap[VALT('a')]=VALU(10); + mymap[VALT('b')]=VALU(20); + mymap[VALT('c')]=VALU(30); - std::pair::iterator,std::map::iterator> ret; - ret = mymap.equal_range('b'); + ft::pair::iterator,typename ft::map::iterator> ret; + ret = mymap.equal_range(VALT('b')); std::cout << "lower bound points to: "; std::cout << ret.first->first << " => " << ret.first->second << '\n'; @@ -534,14 +514,14 @@ TEST_M(tests_map_get_allocator) TITLE(cplusplus.com reference) int psize; - std::map mymap; - std::pair* p; + ft::map mymap; + ft::pair* 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::value_type)*5; + psize = sizeof(typename ft::map::value_type)*5; std::cout << "The allocated array has a size of " << psize << " bytes.\n"; @@ -555,29 +535,45 @@ TEST_M(tests_map_relational_operators) // title TITLE(cplusplus.com reference) - std::map alice{{1, 'a'}, {2, 'b'}, {3, 'c'}}; - std::map bob{{7, 'Z'}, {8, 'Y'}, {9, 'X'}, {10, 'W'}}; - std::map eve{{1, 'a'}, {2, 'b'}, {3, 'c'}}; - - std::cout << std::boolalpha; - - // Compare non equal containers - std::cout << "alice == bob returns " << (alice == bob) << '\n'; - std::cout << "alice != bob returns " << (alice != bob) << '\n'; - std::cout << "alice < bob returns " << (alice < bob) << '\n'; - std::cout << "alice <= bob returns " << (alice <= bob) << '\n'; - std::cout << "alice > bob returns " << (alice > bob) << '\n'; - std::cout << "alice >= bob returns " << (alice >= bob) << '\n'; - - std::cout << '\n'; - - // Compare equal containers - std::cout << "alice == eve returns " << (alice == eve) << '\n'; - std::cout << "alice != eve returns " << (alice != eve) << '\n'; - std::cout << "alice < eve returns " << (alice < eve) << '\n'; - std::cout << "alice <= eve returns " << (alice <= eve) << '\n'; - std::cout << "alice > eve returns " << (alice > eve) << '\n'; - std::cout << "alice >= eve returns " << (alice >= eve) << '\n'; + ft::map alice; + ft::map bob; + ft::map 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; + + // Compare non equal containers + std::cout << "alice == bob returns " << (alice == bob) << '\n'; + std::cout << "alice != bob returns " << (alice != bob) << '\n'; + std::cout << "alice < bob returns " << (alice < bob) << '\n'; + std::cout << "alice <= bob returns " << (alice <= bob) << '\n'; + std::cout << "alice > bob returns " << (alice > bob) << '\n'; + std::cout << "alice >= bob returns " << (alice >= bob) << '\n'; + + std::cout << '\n'; + + // Compare equal containers + std::cout << "alice == eve returns " << (alice == eve) << '\n'; + std::cout << "alice != eve returns " << (alice != eve) << '\n'; + std::cout << "alice < eve returns " << (alice < eve) << '\n'; + std::cout << "alice <= eve returns " << (alice <= eve) << '\n'; + std::cout << "alice > eve returns " << (alice > eve) << '\n'; + std::cout << "alice >= eve returns " << (alice >= eve) << '\n'; DELETE } @@ -588,33 +584,31 @@ TEST_M(tests_map_swap_non_member) // title TITLE(cplusplus.com reference) - std::map alice{{1, 'a'}, {2, 'b'}, {3, 'c'}}; - std::map bob{{7, 'Z'}, {8, 'Y'}, {9, 'X'}, {10, 'W'}}; - - auto print = [](std::pair& n) { - std::cout << " " << n.first << '(' << n.second << ')'; - }; - - // Print state before swap - std::cout << "alice:"; - std::for_each(alice.begin(), alice.end(), print); - std::cout << "\n" "bob :"; - std::for_each(bob.begin(), bob.end(), print); - std::cout << '\n'; - - std::cout << "-- SWAP\n"; - std::swap(alice, bob); - - // Print state after swap - std::cout << "alice:"; - std::for_each(alice.begin(), alice.end(), print); - std::cout << "\n" "bob :"; - std::for_each(bob.begin(), bob.end(), print); - std::cout << '\n'; + ft::map alice; + ft::map bob; + + 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'); + + // Print state before swap + PRINT(alice) + PRINT(bob) + + std::cout << "-- SWAP\n"; + std::swap(alice, bob); + + // Print state after swap + PRINT(alice) + PRINT(bob) DELETE } -*/ #endif