223 lines
5.8 KiB
C++
223 lines
5.8 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
|
|
#include <deque>
|
|
|
|
|
|
// toogle between test ft and stl
|
|
// *************************
|
|
#include <vector>
|
|
#include <map>
|
|
#include <stack>
|
|
#ifdef STL
|
|
namespace ft = std;
|
|
#else
|
|
#include "vector.hpp"
|
|
#include "map.hpp"
|
|
#include "stack.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 VALT(n) val<T>(n)
|
|
# define VALU(n) val<U>(n)
|
|
# define TOI(n) toi<T>(n)
|
|
# define PRINT(n) print<>(n, #n);
|
|
# define DELETE delete_structs();
|
|
|
|
|
|
// prototypes
|
|
// *********************************************
|
|
// abstract class test -----------------------
|
|
struct A_test
|
|
{
|
|
virtual ~A_test(){};
|
|
std::string title;
|
|
std::string type;
|
|
virtual void func() = 0;
|
|
};
|
|
// mystruct ----------------------------------
|
|
struct mystruct {
|
|
public:
|
|
mystruct(int data = 0);
|
|
~mystruct();
|
|
int * get_data() const;
|
|
private:
|
|
int * _val;
|
|
};
|
|
std::ostream & operator<<(std::ostream & o, mystruct const * rhs);
|
|
// functions ---------------------------------
|
|
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) TEST_V(f_name)
|
|
|
|
/*
|
|
*/
|
|
|
|
#define TEST_V(f_name) \
|
|
template <class T> 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 <int>));\
|
|
add_to_list(#f_name, "char", new(s_ ## f_name <char>));\
|
|
add_to_list(#f_name, "std::string", new(s_ ## f_name <std::string>));\
|
|
add_to_list(#f_name, "mystruct*", new(s_ ## f_name <mystruct*>));\
|
|
}\
|
|
template <class T>\
|
|
void s_ ## f_name <T>::func()
|
|
|
|
#define TEST_M(f_name) \
|
|
template <class T, class U> struct s_ ## f_name : public A_test\
|
|
{ void func(); };\
|
|
void f_name () {\
|
|
add_to_list("", "", NULL);\
|
|
add_to_list(#f_name, "char, int", new(s_ ## f_name <char, int>));\
|
|
add_to_list(#f_name, "char, char", new(s_ ## f_name <char, char>));\
|
|
add_to_list(#f_name, "char, std::string", new(s_ ## f_name <char, std::string>));\
|
|
add_to_list(#f_name, "int, int", new(s_ ## f_name <int, int>));\
|
|
add_to_list(#f_name, "int, char", new(s_ ## f_name <int, char>));\
|
|
add_to_list(#f_name, "int, std::string", new(s_ ## f_name <int, std::string>));\
|
|
}\
|
|
template <class T, class U>\
|
|
void s_ ## f_name <T, U>::func()
|
|
/*
|
|
add_to_list(#f_name, "char, mystruct*", new(s_ ## f_name <char, mystruct*>));\
|
|
add_to_list(#f_name, "int, mystruct*", new(s_ ## f_name <int, mystruct*>));\
|
|
*/
|
|
|
|
// templates print
|
|
// *****************************************
|
|
template <class T>
|
|
void print(ft::vector<T>& vec, std::string name) {
|
|
|
|
int i = 0;
|
|
typename ft::vector<T>::iterator it;
|
|
typename ft::vector<T>::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 <class T, class U>
|
|
void print(ft::map<T, U>& mp, std::string name) {
|
|
|
|
int i = 0;
|
|
typename ft::map<T, U>::iterator it;
|
|
typename ft::map<T, U>::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";
|
|
}
|
|
template <class T, class cont>
|
|
void print(ft::stack<T,cont>& st, std::string name) {
|
|
|
|
std::cout << "\n" << name << ":(map)\n";
|
|
for (int i = st.size(); i > 0 ; i--, st.pop())
|
|
std::cout << "[" << i << "]" << st.top() << " ";
|
|
std::cout << "\nsize:" << st.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) {
|
|
|
|
if (n <= 126 && n >= 33)
|
|
return 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 );
|
|
}
|
|
template <class T>
|
|
T val(std::string str) { (void)str; return (T()); }
|
|
template <>
|
|
inline int val(std::string str) { int i = str[0]; return (val<int>(i)); }
|
|
template <>
|
|
inline char val(std::string str) { int i = str[0]; return (val<char>(i)); }
|
|
template <>
|
|
inline std::string val(std::string str) { return (str); }
|
|
template <>
|
|
inline mystruct* val(std::string str) { int i = str[0]; return (val<mystruct*>(i)); }
|
|
|
|
|
|
// 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
|
|
|