From 50224fb432585cc4e93e63b59bb0942555d4e6e1 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Sun, 19 Jun 2022 11:49:49 +0200 Subject: [PATCH] tests are receving as much argument as necessary, and print works with map --- tests/includes/tests_A.hpp | 15 ------ tests/includes/tests_mystruct.hpp | 21 --------- tests/includes/tests_templates.tpp | 76 ------------------------------ tests/includes/tests_utils.hpp | 31 +++++++----- tests/main.cpp | 4 +- tests/tests_definitions.cpp | 26 +++++----- tests/tests_map.cpp | 13 ++--- tests/tests_mystruct.cpp | 19 -------- 8 files changed, 40 insertions(+), 165 deletions(-) delete mode 100644 tests/includes/tests_A.hpp delete mode 100644 tests/includes/tests_mystruct.hpp delete mode 100644 tests/includes/tests_templates.tpp delete mode 100644 tests/tests_mystruct.cpp diff --git a/tests/includes/tests_A.hpp b/tests/includes/tests_A.hpp deleted file mode 100644 index 5f38488..0000000 --- a/tests/includes/tests_A.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef TESTS_A_HPP -# define TESTS_A_HPP - -#include - -struct A_test -{ - virtual ~A_test(){}; - std::string title; - std::string type; - virtual void func() = 0; -}; - -#endif - diff --git a/tests/includes/tests_mystruct.hpp b/tests/includes/tests_mystruct.hpp deleted file mode 100644 index 8f2883a..0000000 --- a/tests/includes/tests_mystruct.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef TESTS_MYSTRUCT_HPP -# define TESTS_MYSTRUCT_HPP - -#include - -struct mystruct { - -public: - mystruct(int data = 0); - ~mystruct(); - int * get_data() const; - -private: - int * _val; - -}; - -std::ostream & operator<<(std::ostream & o, mystruct const * rhs); - -#endif - diff --git a/tests/includes/tests_templates.tpp b/tests/includes/tests_templates.tpp deleted file mode 100644 index 442378d..0000000 --- a/tests/includes/tests_templates.tpp +++ /dev/null @@ -1,76 +0,0 @@ - -#include "tests_utils.hpp" - -// print vector -// ******************************************** -template - void print_vector(ft::vector vec) { - - int i = 0; - typename ft::vector::iterator it; - typename ft::vector::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"; -} - -// get a value -// ********************************************* -template - 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 - 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] ); -} - diff --git a/tests/includes/tests_utils.hpp b/tests/includes/tests_utils.hpp index 8aefeaa..7060d81 100644 --- a/tests/includes/tests_utils.hpp +++ b/tests/includes/tests_utils.hpp @@ -29,7 +29,7 @@ # define TITLE(s) std::cout << "\n" B_PURPLE #s RESET "\n\n"; # define VAL(n) val(n) # define TOI(n) toi(n) -# define PRINT(n) print_vector(n); +# define PRINT(n) print(n); # define DELETE delete_structs(); @@ -51,7 +51,7 @@ private: int * _val; }; std::ostream & operator<<(std::ostream & o, mystruct const * rhs); -void add_to_list(std::string s, A_test* s1, A_test* s2, A_test* s3, A_test* s4); +void add_to_list(std::string title, std::string type, A_test* test); void delete_structs(); @@ -66,14 +66,12 @@ extern std::vector< mystruct* > mem_list; #define TEST(f_name) \ template struct s_ ## f_name : public A_test\ { void func(); };\ - void f_name ()\ - { add_to_list(\ - #f_name,\ - new(s_ ## f_name ),\ - new(s_ ## f_name ),\ - new(s_ ## f_name ),\ - new(s_ ## f_name )\ - );}\ + void f_name () {\ + 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 ));\ + }\ template \ void s_ ## f_name ::func() @@ -81,7 +79,7 @@ extern std::vector< mystruct* > mem_list; // templates print // ***************************************** template - void print_vector(ft::vector vec) { + void print(ft::vector vec) { int i = 0; typename ft::vector::iterator it; @@ -91,6 +89,17 @@ template std::cout << "[" << i << "]" << *it << " "; std::cout << "\nsize:" << vec.size() << " capacty:" << vec.capacity() << "\n"; } +template + void print(ft::map ma) { + + int i = 0; + typename ft::map::iterator it; + typename ft::map::iterator it_end = ma.end(); + + for (it = ma.begin(); it != it_end; ++it, i++) + std::cout << "[" << i << "]" << it->first << ":" << it->second << " "; + std::cout << "\nsize:" << ma.size() << "\n"; +} // templates get value // ************************************* diff --git a/tests/main.cpp b/tests/main.cpp index 59d518a..544fcbb 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -5,7 +5,7 @@ int main() { // VECTOR -// tests_vector_constructor(); + tests_vector_constructor(); // tests_vector_operator_assignation(); // tests_vector_begin(); // tests_vector_end(); @@ -34,7 +34,7 @@ int main() { // tests_vector_reverse_iterators(); // MAP -// tests_map_simple(); + tests_map_simple(); // tests_map_constructor(); // tests_map_operator_assignation(); // tests_map_begin(); diff --git a/tests/tests_definitions.cpp b/tests/tests_definitions.cpp index 6f278ea..511015c 100644 --- a/tests/tests_definitions.cpp +++ b/tests/tests_definitions.cpp @@ -4,23 +4,19 @@ // functions // ********************************************** -void add_to_list(std::string s, A_test* s1, A_test* s2, A_test* s3, A_test* s4) { +void add_to_list(std::string title, std::string type, A_test* test) { - std::vector test_sub_list; + std::vector test_sub_list; + std::vector< std::vector >::iterator it; - 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); + // title != NULL for the first element + if (!title.empty()) + test_list.push_back(test_sub_list); + + test->title = title; + test->type = type; + it = test_list.end() - 1; + (*it).push_back(test); } void delete_structs() { diff --git a/tests/tests_map.cpp b/tests/tests_map.cpp index 7af511a..c096902 100644 --- a/tests/tests_map.cpp +++ b/tests/tests_map.cpp @@ -9,17 +9,18 @@ TEST(tests_map_simple) // title TITLE(simple test) - std::map first; - std::map::iterator it; + typename std::map first; + typename std::map::iterator it; - first['a']=10; - first['b']=30; - first['c']=50; - first['d']=70; + first[VAL('a')]=10; + first[VAL('b')]=30; + first[VAL('c')]=50; + first[VAL('d')]=70; for(it=first.begin(); it!=first.end(); ++it){ std::cout << it->first << " => " << it->second << '\n'; } + PRINT(first) DELETE } diff --git a/tests/tests_mystruct.cpp b/tests/tests_mystruct.cpp deleted file mode 100644 index 22f488d..0000000 --- a/tests/tests_mystruct.cpp +++ /dev/null @@ -1,19 +0,0 @@ - -#include "tests_mystruct.hpp" - -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); -}