168 lines
3.7 KiB
C++
168 lines
3.7 KiB
C++
|
|
#ifndef TESTS_UTILS_HPP
|
|
# define TESTS_UTILS_HPP
|
|
|
|
|
|
#include "colors.h"
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <iomanip> // std::setw()
|
|
#include <iterator> // std::reverse_iterator
|
|
#include <utility> // std::make_pair
|
|
#include <sstream> // std::stringstream
|
|
|
|
|
|
// toogle between test ft and stl
|
|
// *************************
|
|
#include <vector>
|
|
#include <map>
|
|
#ifdef STL
|
|
namespace ft = std;
|
|
#else
|
|
#include "vector.hpp"
|
|
#include "reverse_iterator.hpp"
|
|
#endif
|
|
|
|
|
|
// defines
|
|
// ****************************************
|
|
# 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<T>(n);
|
|
# define DELETE delete_structs();
|
|
|
|
|
|
// prototypes
|
|
// *********************************************
|
|
struct A_test
|
|
{
|
|
virtual ~A_test(){};
|
|
std::string title;
|
|
std::string type;
|
|
virtual void func() = 0;
|
|
};
|
|
struct mystruct {
|
|
public:
|
|
mystruct(int data = 0);
|
|
~mystruct();
|
|
int * get_data() const;
|
|
private:
|
|
int * _val;
|
|
};
|
|
std::ostream & operator<<(std::ostream & o, mystruct const * rhs);
|
|
void add_to_list(std::string title, std::string type, A_test* test);
|
|
void delete_structs();
|
|
|
|
|
|
// global variables
|
|
// ***************************************
|
|
extern std::vector< std::vector<A_test*> > test_list;
|
|
extern std::vector< mystruct* > mem_list;
|
|
|
|
|
|
// adding each test to the 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, "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()
|
|
|
|
|
|
// templates print
|
|
// *****************************************
|
|
template <class T>
|
|
void print(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";
|
|
}
|
|
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
|
|
// *************************************
|
|
// specialization in header, make it inline :
|
|
// https://stackoverflow.com/questions/63529059/c-specialized-method-templates-produce-multiple-definition-errors
|
|
template <class T>
|
|
T val(int n) {(void)n; return (T());
|
|
}
|
|
template <>
|
|
inline int val(int n) {return (n);
|
|
}
|
|
template <>
|
|
inline char val(int n) {return (n % 94 + 33);
|
|
}
|
|
template <>
|
|
inline std::string val(int n) {
|
|
|
|
std::string str;
|
|
std::stringstream stream;
|
|
|
|
stream << n;
|
|
stream >> str;
|
|
stream.clear();
|
|
return (str);
|
|
}
|
|
template <>
|
|
inline mystruct* val(int n) {
|
|
|
|
mystruct *s = new mystruct(n);
|
|
mem_list.push_back(s);
|
|
return ( s );
|
|
}
|
|
|
|
|
|
// templates to value
|
|
// **************************************
|
|
template <class T>
|
|
int toi(T t) {(void)t; return (0);
|
|
}
|
|
template <>
|
|
inline int toi(int i) {return (i);
|
|
}
|
|
template <>
|
|
inline int toi(char c) {return (c);
|
|
}
|
|
template <>
|
|
inline int toi(std::string str) {
|
|
|
|
int i;
|
|
std::stringstream stream;
|
|
|
|
stream << str;
|
|
stream >> i;
|
|
stream.clear();
|
|
return (i);
|
|
}
|
|
template <>
|
|
inline int toi(mystruct* s) {
|
|
|
|
return ( s->get_data()[0] );
|
|
}
|
|
|
|
|
|
#endif
|
|
|