creation of tests.h

This commit is contained in:
hugogogo
2022-05-31 19:52:03 +02:00
parent 5e65691bc8
commit 0e51cc655c
4 changed files with 190 additions and 160 deletions

318
main.cpp
View File

@@ -1,58 +1,12 @@
#include <iostream>
#include <string>
#include "colors.h"
#include "tests.h"
#include <vector>
/*
#define N_TEST "24"
#define TEST(s) std::cout \
<< "\n" B_YELLOW "[" << ++test << "/" N_TEST "] " \
<< #s " :" RESET "\n";
*/
#define TEST \
{\
struct tester : public testt {\
void func()
#define TESTNAME(s) \
};\
test = new(tester);\
test->title = #s;\
testouille.push_back(test);\
}
struct testt {
std::string title;
virtual void func() {
std::cout << "houre\n";
}
};
int main() {
std::vector<testt *> testouille;
testt *test;
TEST {
std::cout << "here\n";
} TESTNAME(name of first test)
TEST {
std::cout << "here2\n";
} TESTNAME(name of second test)
int size = testouille.size();
for(int i = 0; i < size; i++)
{
std::cout
<< "\n" B_YELLOW "[" << i + 1 << "/" << size << "] " \
<< testouille[i]->title << " :" RESET "\n";
testouille[i]->func();
}
/*
int test = 0;
TEST(vector::vector (constructor))
{
// constructors used in the same order as described above:
@@ -70,6 +24,7 @@ int main() {
std::cout << ' ' << *it;
std::cout << '\n';
}
TESTEND
TEST(vector::=operator)
{
@@ -82,6 +37,7 @@ int main() {
std::cout << "Size of foo: " << int(foo.size()) << '\n';
std::cout << "Size of bar: " << int(bar.size()) << '\n';
}
TESTEND
TEST(vector::begin)
{
@@ -93,6 +49,7 @@ int main() {
std::cout << ' ' << *it;
std::cout << '\n';
}
TESTEND
TEST(vector::end)
{
@@ -104,6 +61,7 @@ int main() {
std::cout << ' ' << *it;
std::cout << '\n';
}
TESTEND
TEST(vector::rbegin)
{
@@ -120,6 +78,7 @@ int main() {
std::cout << ' ' << *it;
std::cout << '\n';
}
TESTEND
TEST(vector::rend)
{
@@ -136,6 +95,7 @@ int main() {
std::cout << ' ' << *it;
std::cout << '\n';
}
TESTEND
TEST(vector::size)
{
@@ -151,6 +111,7 @@ int main() {
myints.pop_back();
std::cout << "3. size: " << myints.size() << '\n';
}
TESTEND
TEST(vector::max_size)
{
@@ -163,6 +124,7 @@ int main() {
std::cout << "capacity: " << myvector.capacity() << "\n";
std::cout << "max_size: " << myvector.max_size() << "\n";
}
TESTEND
TEST(vector::resize)
{
@@ -180,6 +142,7 @@ int main() {
std::cout << ' ' << myvector[i];
std::cout << '\n';
}
TESTEND
TEST(vector::capacity)
{
@@ -192,6 +155,7 @@ int main() {
std::cout << "capacity: " << (int) myvector.capacity() << '\n';
std::cout << "max_size: " << (int) myvector.max_size() << '\n';
}
TESTEND
TEST(vector::empty)
{
@@ -208,6 +172,7 @@ int main() {
std::cout << "total: " << sum << '\n';
}
TESTEND
TEST(vector::reserve)
{
@@ -236,6 +201,7 @@ int main() {
}
}
}
TESTEND
TEST(vector::operator[])
{
@@ -260,6 +226,7 @@ int main() {
std::cout << ' ' << myvector[i];
std::cout << '\n';
}
TESTEND
TEST(vector::at)
{
@@ -274,6 +241,7 @@ int main() {
std::cout << ' ' << myvector.at(i);
std::cout << '\n';
}
TESTEND
TEST(vector::front)
{
@@ -288,6 +256,7 @@ int main() {
std::cout << "myvector.front() is now " << myvector.front() << '\n';
}
TESTEND
TEST(vector::back)
{
@@ -302,6 +271,7 @@ int main() {
std::cout << "myvector.front() is now " << myvector.front() << '\n';
}
TESTEND
TEST(vector::assign)
{
@@ -323,6 +293,7 @@ int main() {
std::cout << "Size of second: " << int (second.size()) << '\n';
std::cout << "Size of third: " << int (third.size()) << '\n';
}
TESTEND
TEST(vector::push_back)
{
@@ -345,6 +316,7 @@ int main() {
std::cout << "myvector stores " << int(myvector.size()) << " numbers.\n";
}
TESTEND
TEST(vector::pop_back)
{
@@ -362,6 +334,7 @@ int main() {
std::cout << "The elements of myvector add up to " << sum << '\n';
}
TESTEND
TEST(vector::insert)
{
@@ -387,6 +360,7 @@ int main() {
std::cout << ' ' << *it;
std::cout << '\n';
}
TESTEND
TEST(vector::erase)
{
@@ -406,6 +380,7 @@ int main() {
std::cout << ' ' << myvector[i];
std::cout << '\n';
}
TESTEND
TEST(vector::swap)
{
@@ -424,6 +399,7 @@ int main() {
std::cout << ' ' << bar[i];
std::cout << '\n';
}
TESTEND
TEST(vector::clear)
{
@@ -446,6 +422,7 @@ int main() {
std::cout << ' ' << myvector[i];
std::cout << '\n';
}
TESTEND
TEST(vector::get_allocator)
{
@@ -467,128 +444,153 @@ int main() {
for (i=0; i<5; i++) myvector.get_allocator().destroy(&p[i]);
myvector.get_allocator().deallocate(p,5);
}
*/
TESTEND
// execute tests and print them :
int size = test_list.size();
for(int i = 0; i < size; i++)
{
std::cout
<< "\n" B_YELLOW "[" << i + 1 << "/" << size << "] "
<< test_list[i]->title << " :" RESET "\n";
test_list[i]->func();
}
return 0;
}
/* original main
#include <iostream>
#include <string>
#include <deque>
#if 1 //CREATE A REAL STL EXAMPLE
#include <map>
#include <stack>
#include <vector>
namespace ft = std;
#else
#include <map.hpp>
#include <stack.hpp>
#include <vector.hpp>
#endif
#include <stdlib.h>
#define MAX_RAM 4294967296
#define BUFFER_SIZE 4096
struct Buffer
{
int idx;
char buff[BUFFER_SIZE];
};
#define COUNT (MAX_RAM / (int)sizeof(Buffer))
template<typename T>
class MutantStack : public ft::stack<T>
{
public:
MutantStack() {}
MutantStack(const MutantStack<T>& src) { *this = src; }
MutantStack<T>& operator=(const MutantStack<T>& rhs)
{
this->c = rhs.c;
return *this;
}
~MutantStack() {}
typedef typename ft::stack<T>::container_type::iterator iterator;
iterator begin() { return this->c.begin(); }
iterator end() { return this->c.end(); }
};
int main(int argc, char** argv) {
if (argc != 2)
{
std::cerr << "Usage: ./test seed" << std::endl;
std::cerr << "Provide a seed please" << std::endl;
std::cerr << "Count value:" << COUNT << std::endl;
return 1;
}
const int seed = atoi(argv[1]);
srand(seed);
ft::vector<std::string> vector_str;
ft::vector<int> vector_int;
ft::stack<int> stack_int;
ft::vector<Buffer> vector_buffer;
ft::stack<Buffer, std::deque<Buffer> > stack_deq_buffer;
ft::map<int, int> map_int;
for (int i = 0; i < COUNT; i++)
{
vector_buffer.push_back(Buffer());
}
for (int i = 0; i < COUNT; i++)
{
const int idx = rand() % COUNT;
vector_buffer[idx].idx = 5;
}
ft::vector<Buffer>().swap(vector_buffer);
try
{
for (int i = 0; i < COUNT; i++)
{
const int idx = rand() % COUNT;
vector_buffer.at(idx);
std::cerr << "Error: THIS VECTOR SHOULD BE EMPTY!!" <<std::endl;
}
}
catch(const std::exception& e)
{
//NORMAL ! :P
}
for (int i = 0; i < COUNT; ++i)
{
map_int.insert(ft::make_pair(rand(), rand()));
}
int sum = 0;
for (int i = 0; i < 10000; i++)
{
int access = rand();
sum += map_int[access];
}
std::cout << "should be constant with the same seed: " << sum << std::endl;
{
ft::map<int, int> copy = map_int;
}
MutantStack<char> iterable_stack;
for (char letter = 'a'; letter <= 'z'; letter++)
iterable_stack.push(letter);
for (MutantStack<char>::iterator it = iterable_stack.begin(); it != iterable_stack.end(); it++)
{
std::cout << *it;
}
std::cout << std::endl;
return (0);
}
*/
//////////////////////////////////////////////////////////////////////////////
//
// original main
//
// #include <iostream>
// #include <string>
// #include <deque>
// #if 1 //CREATE A REAL STL EXAMPLE
// #include <map>
// #include <stack>
// #include <vector>
// namespace ft = std;
// #else
// #include <map.hpp>
// #include <stack.hpp>
// #include <vector.hpp>
// #endif
//
// #include <stdlib.h>
//
// #define MAX_RAM 4294967296
// #define BUFFER_SIZE 4096
// struct Buffer
// {
// int idx;
// char buff[BUFFER_SIZE];
// };
//
//
// #define COUNT (MAX_RAM / (int)sizeof(Buffer))
//
// template<typename T>
// class MutantStack : public ft::stack<T>
// {
// public:
// MutantStack() {}
// MutantStack(const MutantStack<T>& src) { *this = src; }
// MutantStack<T>& operator=(const MutantStack<T>& rhs)
// {
// this->c = rhs.c;
// return *this;
// }
// ~MutantStack() {}
//
// typedef typename ft::stack<T>::container_type::iterator iterator;
//
// iterator begin() { return this->c.begin(); }
// iterator end() { return this->c.end(); }
// };
//
// int main(int argc, char** argv) {
// if (argc != 2)
// {
// std::cerr << "Usage: ./test seed" << std::endl;
// std::cerr << "Provide a seed please" << std::endl;
// std::cerr << "Count value:" << COUNT << std::endl;
// return 1;
// }
// const int seed = atoi(argv[1]);
// srand(seed);
//
// ft::vector<std::string> vector_str;
// ft::vector<int> vector_int;
// ft::stack<int> stack_int;
// ft::vector<Buffer> vector_buffer;
// ft::stack<Buffer, std::deque<Buffer> > stack_deq_buffer;
// ft::map<int, int> map_int;
//
// for (int i = 0; i < COUNT; i++)
// {
// vector_buffer.push_back(Buffer());
// }
//
// for (int i = 0; i < COUNT; i++)
// {
// const int idx = rand() % COUNT;
// vector_buffer[idx].idx = 5;
// }
// ft::vector<Buffer>().swap(vector_buffer);
//
// try
// {
// for (int i = 0; i < COUNT; i++)
// {
// const int idx = rand() % COUNT;
// vector_buffer.at(idx);
// std::cerr << "Error: THIS VECTOR SHOULD BE EMPTY!!" <<std::endl;
// }
// }
// catch(const std::exception& e)
// {
// //NORMAL ! :P
// }
//
// for (int i = 0; i < COUNT; ++i)
// {
// map_int.insert(ft::make_pair(rand(), rand()));
// }
//
// int sum = 0;
// for (int i = 0; i < 10000; i++)
// {
// int access = rand();
// sum += map_int[access];
// }
// std::cout << "should be constant with the same seed: " << sum << std::endl;
//
// {
// ft::map<int, int> copy = map_int;
// }
// MutantStack<char> iterable_stack;
// for (char letter = 'a'; letter <= 'z'; letter++)
// iterable_stack.push(letter);
// for (MutantStack<char>::iterator it = iterable_stack.begin(); it != iterable_stack.end(); it++)
// {
// std::cout << *it;
// }
// std::cout << std::endl;
// return (0);
// }
//
//////////////////////////////////////////////////////////////////////////////