tests are receving as much argument as necessary, and print works with map

This commit is contained in:
hugogogo
2022-06-19 11:49:49 +02:00
parent 94745ca8a9
commit 50224fb432
8 changed files with 40 additions and 165 deletions

View File

@@ -1,15 +0,0 @@
#ifndef TESTS_A_HPP
# define TESTS_A_HPP
#include <string>
struct A_test
{
virtual ~A_test(){};
std::string title;
std::string type;
virtual void func() = 0;
};
#endif

View File

@@ -1,21 +0,0 @@
#ifndef TESTS_MYSTRUCT_HPP
# define TESTS_MYSTRUCT_HPP
#include <iostream>
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

View File

@@ -1,76 +0,0 @@
#include "tests_utils.hpp"
// print vector
// ********************************************
template <class T>
void print_vector(ft::vector<T> vec) {
int i = 0;
typename ft::vector<T>::iterator it;
typename ft::vector<T>::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 <class T>
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 <class T>
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] );
}

View File

@@ -29,7 +29,7 @@
# define TITLE(s) std::cout << "\n" B_PURPLE #s RESET "\n\n";
# define VAL(n) val<T>(n)
# define TOI(n) toi<T>(n)
# define PRINT(n) print_vector<T>(n);
# define PRINT(n) print<T>(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 <class T> struct s_ ## f_name : public A_test\
{ void func(); };\
void f_name ()\
{ add_to_list(\
#f_name,\
new(s_ ## f_name <int>),\
new(s_ ## f_name <char>),\
new(s_ ## f_name <std::string>),\
new(s_ ## f_name <mystruct*>)\
);}\
void f_name () {\
add_to_list(#f_name, "int", new(s_ ## f_name <int>));\
add_to_list("", "char", new(s_ ## f_name <char>));\
add_to_list("", "std::string", new(s_ ## f_name <std::string>));\
add_to_list("", "mystruct*", new(s_ ## f_name <mystruct*>));\
}\
template <class T>\
void s_ ## f_name <T>::func()
@@ -81,7 +79,7 @@ extern std::vector< mystruct* > mem_list;
// templates print
// *****************************************
template <class T>
void print_vector(ft::vector<T> vec) {
void print(ft::vector<T> vec) {
int i = 0;
typename ft::vector<T>::iterator it;
@@ -91,6 +89,17 @@ template <class T>
std::cout << "[" << i << "]" << *it << " ";
std::cout << "\nsize:" << vec.size() << " capacty:" << vec.capacity() << "\n";
}
template <class T, class U>
void print(ft::map<T, U> ma) {
int i = 0;
typename ft::map<T, U>::iterator it;
typename ft::map<T, U>::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
// *************************************