Files
42_INT_11_ft_containers/headers/tests_utils.hpp
2022-06-16 18:05:28 +02:00

138 lines
3.1 KiB
C++

#ifndef TESTS_UTILS_HPP
# define TESTS_UTILS_HPP
#include "colors.h"
#include "A_test.hpp"
#include <iostream>
#include <string>
#include <vector>
#include <iomanip> // std::setw()
#include <iterator> // std::reverse_iterator
#include <utility> // std::make_pair
#include <map> // std::map
#include <sstream>
// toogle between test ft and stl
// *************************
#ifdef STL
namespace ft = std;
#else
#include "vector.hpp"
#include "reverse_iterator.hpp"
#endif
// struct for tests
// ***************************************
struct mystruct {
public:
mystruct(int data = 0) {_val = new int[2]; _val[0] = data; _val[1] = data;}
~mystruct() {delete[] _val;}
int * get_data() const {return _val;}
private:
int * _val;
};
//extern std::ostream & operator<<(std::ostream & o, mystruct const & rhs);
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);
}
// global declarations
// ************************************
extern std::vector<A_test*> test_list;
extern void add_to_list(std::string s, A_test* s1, A_test* s2, A_test* s3, A_test* s4);
std::vector<mystruct*> mem_list_struct;
// 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,\
new(s_ ## f_name <int>),\
new(s_ ## f_name <char>),\
new(s_ ## f_name <std::string>),\
new(s_ ## f_name <mystruct*>)\
);}\
template <class T>\
void s_ ## f_name <T>::func()
// defines
// ****************************************
# define TITLE(s) std::cout << "\n" B_PURPLE #s RESET "\n\n";
# define VAL(n) val<T>(n)
# define PRINT(n) print_vector<T>(n);
# define DELETE delete_structs();
// 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_struct.push_back(s);
return ( s );
}
// get a value
// *********************************************
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";
}
// delete vector elements
// **********************************
void delete_structs() {
std::vector<mystruct*>::iterator it;
std::vector<mystruct*>::iterator it_end = mem_list_struct.end();
for (it = mem_list_struct.begin(); it != it_end; ++it)
delete *it;
}
#endif