diff --git a/tests/includes/main.hpp b/tests/includes/main.hpp index 42a0876..fafcf6a 100644 --- a/tests/includes/main.hpp +++ b/tests/includes/main.hpp @@ -47,28 +47,28 @@ void tests_vector_swap_non_member(); void tests_vector_reverse_iterators(); // map void tests_map_simple(); -//void tests_map_constructor(); -//void tests_map_operator_assignation(); -//void tests_map_begin(); -//void tests_map_end(); -//void tests_map_rbegin(); -//void tests_map_rend(); -//void tests_map_empty(); -//void tests_map_size(); -//void tests_map_max_size(); -//void tests_map_operator_access(); -//void tests_map_insert(); -//void tests_map_erase(); -//void tests_map_swap(); -//void tests_map_clear(); -//void tests_map_key_comp(); -//void tests_map_value_comp(); -//void tests_map_find(); -//void tests_map_count(); -//void tests_map_lower_bound(); -//void tests_map_upper_bound(); -//void tests_map_equal_range(); -//void tests_map_get_allocator(); +void tests_map_constructor(); +void tests_map_operator_assignation(); +void tests_map_begin(); +void tests_map_end(); +void tests_map_rbegin(); +void tests_map_rend(); +void tests_map_empty(); +void tests_map_size(); +void tests_map_max_size(); +void tests_map_operator_access(); +void tests_map_insert(); +void tests_map_erase(); +void tests_map_swap(); +void tests_map_clear(); +void tests_map_key_comp(); +void tests_map_value_comp(); +void tests_map_find(); +void tests_map_count(); +void tests_map_lower_bound(); +void tests_map_upper_bound(); +void tests_map_equal_range(); +void tests_map_get_allocator(); #endif diff --git a/tests/includes/tests_utils.hpp b/tests/includes/tests_utils.hpp index 170c866..ee20cfa 100644 --- a/tests/includes/tests_utils.hpp +++ b/tests/includes/tests_utils.hpp @@ -32,7 +32,7 @@ # define VALT(n) val(n) # define VALU(n) val(n) # define TOI(n) toi(n) -# define PRINT(n) print(n); +# define PRINT(n) print<>(n, #n); # define DELETE delete_structs(); @@ -82,7 +82,10 @@ extern std::vector< mystruct* > mem_list; template struct s_ ## f_name : public A_test\ { void func(); };\ void f_name () {\ - add_to_list(#f_name, "int, int", new(s_ ## f_name ));\ + 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() @@ -91,23 +94,25 @@ extern std::vector< mystruct* > mem_list; // templates print // ***************************************** template - void print(ft::vector vec) { + void print(ft::vector vec, std::string name) { int i = 0; typename ft::vector::iterator it; typename ft::vector::iterator it_end = vec.end(); + std::cout << "\n" << name << ":(vector)\n"; for (it = vec.begin(); it != it_end; ++it, i++) std::cout << "[" << i << "]" << *it << " "; std::cout << "\nsize:" << vec.size() << " capacty:" << vec.capacity() << "\n"; } template - void print(ft::map mp) { + void print(ft::map mp, std::string name) { int i = 0; typename ft::map::iterator it; typename ft::map::iterator it_end = mp.end(); + std::cout << "\n" << name << ":(map)\n"; for (it = mp.begin(); it != it_end; ++it, i++) std::cout << "[" << i << "]" << it->first << ":" << it->second << " "; std::cout << "\nsize:" << mp.size() << "\n"; @@ -118,13 +123,15 @@ template // specialization in header, make it inline : // https://stackoverflow.com/questions/63529059/c-specialized-method-templates-produce-multiple-definition-errors template - T val(int n) {(void)n; return (T()); -} + T val(int n) { (void)n; return (T()); } template <> - inline int val(int n) {return (n); -} + inline int val(int n) { return (n); } 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 <> inline std::string val(int n) { @@ -144,6 +151,16 @@ template <> mem_list.push_back(s); return ( s ); } +template + T val(std::string str) { (void)str; return (T()); } +template <> + inline int val(std::string str) { int i = str[0]; return (val(i)); } +template <> + inline char val(std::string str) { int i = str[0]; return (val(i)); } +template <> + inline std::string val(std::string str) { return (str); } +template <> + inline mystruct* val(std::string str) { int i = str[0]; return (val(i)); } // templates to value diff --git a/tests/main.cpp b/tests/main.cpp index 17a98e1..cbf0a51 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -35,16 +35,16 @@ int main() { // 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_operator_access(); + 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_operator_access(); // tests_map_insert(); // tests_map_erase(); // tests_map_swap(); diff --git a/tests/tests_map.cpp b/tests/tests_map.cpp index ecb4a1d..b74ab47 100644 --- a/tests/tests_map.cpp +++ b/tests/tests_map.cpp @@ -4,6 +4,15 @@ #include "tests_utils.hpp" +/**/ // UTILS for some tests +/**/ bool fncomp (char lhs, char rhs) {return lhs first; - std::map first; + first[VALT('a')]=VALU(10); + first[VALT('b')]=VALU(30); + first[VALT('c')]=VALU(50); + first[VALT('d')]=VALU(70); - first['a']=10; - first['b']=30; - first['c']=50; - first['d']=70; + ft::map second (first.begin(),first.end()); - std::map second (first.begin(),first.end()); + ft::map third (second); - std::map third (second); - - std::map fourth; // class as Compare + ft::map fourth; // class as Compare bool(*fn_pt)(char,char) = fncomp; - std::map fifth (fn_pt); // function pointer as Compare + ft::map fifth (fn_pt); // function pointer as Compare + + PRINT(first) + PRINT(second) + PRINT(third) DELETE } @@ -56,19 +61,25 @@ TEST_M(tests_map_operator_assignation) // title TITLE(cplusplus.com reference) - std::map first; - std::map second; + ft::map first; + ft::map second; - first['x']=8; - first['y']=16; - first['z']=32; + first[VALT('x')]=VALU(8); + first[VALT('y')]=VALU(16); + first[VALT('z')]=VALU(32); + + PRINT(first) + PRINT(second) second=first; // second now contains 3 ints - first=std::map(); // and first is now empty + first=ft::map(); // and first is now empty std::cout << "Size of first: " << first.size() << '\n'; std::cout << "Size of second: " << second.size() << '\n'; + PRINT(first) + PRINT(second) + DELETE } @@ -77,15 +88,13 @@ TEST_M(tests_map_begin) // title TITLE(cplusplus.com reference) - std::map mymap; + ft::map mymap; - mymap['b'] = 100; - mymap['a'] = 200; - mymap['c'] = 300; + mymap[VALT('b')] = VALU(100); + mymap[VALT('a')] = VALU(200); + mymap[VALT('c')] = VALU(300); - // show content: - for (std::map::iterator it=mymap.begin(); it!=mymap.end(); ++it) - std::cout << it->first << " => " << it->second << '\n'; + PRINT(mymap) DELETE } @@ -95,15 +104,13 @@ TEST_M(tests_map_end) // title TITLE(cplusplus.com reference) - std::map mymap; + ft::map mymap; - mymap['b'] = 100; - mymap['a'] = 200; - mymap['c'] = 300; + mymap[VALT('b')] = VALU(100); + mymap[VALT('a')] = VALU(200); + mymap[VALT('c')] = VALU(300); - // show content: - for (std::map::iterator it=mymap.begin(); it!=mymap.end(); ++it) - std::cout << it->first << " => " << it->second << '\n'; + PRINT(mymap) DELETE } @@ -113,14 +120,14 @@ TEST_M(tests_map_rbegin) // 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); // show content: - std::map::reverse_iterator rit; + typename ft::map::reverse_iterator rit; for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit) std::cout << rit->first << " => " << rit->second << '\n'; @@ -132,14 +139,14 @@ TEST_M(tests_map_rend) // 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); // show content: - std::map::reverse_iterator rit; + typename ft::map::reverse_iterator rit; for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit) std::cout << rit->first << " => " << rit->second << '\n'; @@ -151,16 +158,16 @@ TEST_M(tests_map_empty) // 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); while (!mymap.empty()) { - std::cout << mymap.begin()->first << " => " << mymap.begin()->second << '\n'; - mymap.erase(mymap.begin()); + std::cout << mymap.begin()->first << " => " << mymap.begin()->second << '\n'; + mymap.erase(mymap.begin()); } DELETE @@ -171,13 +178,15 @@ TEST_M(tests_map_size) // title TITLE(cplusplus.com reference) - std::map mymap; - mymap['a']=101; - mymap['b']=202; - mymap['c']=302; + ft::map mymap; + mymap[VALT('a')]=VALU(101); + mymap[VALT('b')]=VALU(202); + mymap[VALT('c')]=VALU(302); std::cout << "mymap.size() is " << mymap.size() << '\n'; + PRINT(mymap) + DELETE } @@ -187,11 +196,11 @@ TEST_M(tests_map_max_size) TITLE(cplusplus.com reference) int i; - std::map mymap; + std::map mymap; 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"; } else std::cout << "The map could not hold 1000 elements.\n"; @@ -204,22 +213,23 @@ TEST_M(tests_map_operator_access) // title TITLE(cplusplus.com reference) - std::map mymap; + ft::map mymap; - mymap['a']="an element"; - mymap['b']="another element"; - mymap['c']=mymap['b']; + mymap[VALT('a')]=VALU("An element"); + mymap[VALT('b')]=VALU("another element"); + mymap[VALT('c')]=mymap[VAL('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['a'] is " << mymap[VALT('a')] << '\n'; + std::cout << "mymap['b'] is " << mymap[VALT('b')] << '\n'; + std::cout << "mymap['c'] is " << mymap[VALT('c')] << '\n'; + std::cout << "mymap['d'] is " << mymap[VALT('d')] << '\n'; std::cout << "mymap now contains " << mymap.size() << " elements.\n"; DELETE } +/* TEST_M(tests_map_insert) { // title @@ -604,7 +614,6 @@ TEST_M(tests_map_swap_non_member) DELETE } - */ #endif diff --git a/tests/tests_vector.cpp b/tests/tests_vector.cpp index 86a73d7..87fd466 100644 --- a/tests/tests_vector.cpp +++ b/tests/tests_vector.cpp @@ -42,10 +42,8 @@ TEST_V(tests_vector_operator_assignation) // title TITLE(more informations) - std::cout << "foo:\n"; PRINT(foo); - std::cout << "bar:\n"; PRINT(bar); DELETE @@ -59,7 +57,6 @@ TEST_V(tests_vector_begin) ft::vector myvector; for (int i=1; i<=5; i++) myvector.push_back(VAL(i)); - std::cout << "myvector contains:\n"; PRINT(myvector); DELETE @@ -73,7 +70,6 @@ TEST_V(tests_vector_end) ft::vector myvector; for (int i=1; i<=5; i++) myvector.push_back(VAL(i)); - std::cout << "myvector contains:\n"; PRINT(myvector); DELETE @@ -92,7 +88,6 @@ TEST_V(tests_vector_rbegin) for (; rit!= myvector.rend(); ++rit) *rit = VAL(++i); - std::cout << "myvector contains:"; PRINT(myvector); DELETE @@ -111,7 +106,6 @@ TEST_V(tests_vector_rend) for (rit = myvector.rbegin(); rit!= myvector.rend(); ++rit) *rit = VAL(++i); - std::cout << "myvector contains:"; PRINT(myvector); DELETE @@ -172,7 +166,6 @@ TEST_V(tests_vector_resize) myvector.resize(8,VAL(100)); myvector.resize(12); - std::cout << "myvector contains:"; PRINT(myvector); @@ -334,7 +327,6 @@ TEST_V(tests_vector_operator_access) myvector[i]=temp; } - std::cout << "myvector contains:"; PRINT(myvector) DELETE @@ -351,7 +343,6 @@ TEST_V(tests_vector_at) for (unsigned i=0; i myvector3 (3,VAL(100)); @@ -568,7 +557,6 @@ TEST_V(tests_vector_insert) it = myvector3.end(); std::cout << "\nsize:" << myvector3.size() << " capacity:" << myvector3.capacity() << "\n"; myvector3.insert ( it , VAL(200) ); - std::cout << "myvector contains:"; PRINT(myvector3) ft::vector myvector4 (3,VAL(100)); @@ -576,7 +564,6 @@ TEST_V(tests_vector_insert) it = myvector4.begin() + 2; std::cout << "\nsize:" << myvector3.size() << " capacity:" << myvector3.capacity() << "\n"; myvector4.insert ( it , VAL(200) ); - std::cout << "myvector contains:"; PRINT(myvector4) @@ -597,7 +584,6 @@ TEST_V(tests_vector_insert) it = myvector5.end() - 2; myvector5.insert (it,2,VAL(550)); - std::cout << "myvector contains:"; PRINT(myvector5) @@ -631,7 +617,6 @@ TEST_V(tests_vector_erase) // erase the first 3 elements: myvector.erase (myvector.begin(),myvector.begin()+3); - std::cout << "myvector contains:"; PRINT(myvector) // title @@ -666,10 +651,8 @@ TEST_V(tests_vector_swap) foo.swap(bar); - std::cout << "foo contains:"; PRINT(foo) - std::cout << "bar contains:"; PRINT(bar) DELETE @@ -685,14 +668,12 @@ TEST_V(tests_vector_clear) myvector.push_back (VAL(200)); myvector.push_back (VAL(300)); - std::cout << "myvector contains:"; PRINT(myvector) myvector.clear(); myvector.push_back (VAL(1101)); myvector.push_back (VAL(2202)); - std::cout << "myvector contains:"; PRINT(myvector) DELETE @@ -752,10 +733,8 @@ TEST_V(tests_vector_swap_non_member) foo.swap(bar); - std::cout << "foo contains:"; PRINT(foo) - std::cout << "bar contains:"; PRINT(bar) DELETE