
#include "tests_map.hpp"


/**/ // UTILS for some tests
/**/ bool fncomp (char lhs, char rhs) {return lhs<rhs;}
/**/ 
/**/ struct classcomp {
/**/ 	bool operator() (const char& lhs, const char& rhs) const
/**/ 	{return lhs<rhs;}
/**/ };
/**/ ///////////////////////

TEST_M(tests_map_simple)
{
	// title
	TITLE(simple test)

	typename ft::map<T, U>				mp;
	typename std::map<T, U>::iterator	it;

	mp[VALT('a')] = VALU(10);

	PRINT(mp)

	DELETE
}

TEST_M(tests_map_constructor)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> first;

	first[VALT('a')]=VALU(10);
	first[VALT('b')]=VALU(30);
	first[VALT('c')]=VALU(50);
	first[VALT('d')]=VALU(70);

	ft::map<T,U> second (first.begin(),first.end());

	ft::map<T,U> third (second);

	ft::map<T,U,classcomp> fourth;                 // class as Compare

	bool(*fn_pt)(char,char) = fncomp;
	ft::map<T,U,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare

	PRINT(first)
	PRINT(second)
	PRINT(third)

	DELETE
}

TEST_M(tests_map_operator_assignation)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> first;
	ft::map<T,U> second;

	first[VALT('x')]=VALU(8);
	first[VALT('y')]=VALU(16);
	first[VALT('z')]=VALU(32);

	PRINT(first)
	PRINT(second)

	second=first;                // second now contains 3 ints
	first=ft::map<T,U>();  // and first is now empty

	std::cout << "Size of first: " << first.size() << '\n';
	std::cout << "Size of second: " << second.size() << '\n';

	PRINT(first)
	PRINT(second)

	DELETE
}

TEST_M(tests_map_begin)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;

	mymap[VALT('b')] = VALU(100);
	mymap[VALT('a')] = VALU(200);
	mymap[VALT('c')] = VALU(300);

	PRINT(mymap)

	DELETE
}

TEST_M(tests_map_end)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;

	mymap[VALT('b')] = VALU(100);
	mymap[VALT('a')] = VALU(200);
	mymap[VALT('c')] = VALU(300);

	PRINT(mymap)

	DELETE
}

TEST_M(tests_map_rbegin)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;

	mymap[VALT('x')] = VALU(100);
	mymap[VALT('y')] = VALU(200);
	mymap[VALT('z')] = VALU(300);

	// show content:
	typename ft::map<T,U>::reverse_iterator rit;
	for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit)
		std::cout << rit->first << " => " << rit->second << '\n';

	DELETE
}

TEST_M(tests_map_rend)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;

	mymap[VALT('x')] = VALU(100);
	mymap[VALT('y')] = VALU(200);
	mymap[VALT('z')] = VALU(300);

	// show content:
	typename ft::map<T,U>::reverse_iterator rit;
	for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit)
		std::cout << rit->first << " => " << rit->second << '\n';

	DELETE
}

TEST_M(tests_map_empty)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;

	mymap[VALT('a')]=VALU(10);
	mymap[VALT('b')]=VALU(20);
	mymap[VALT('c')]=VALU(30);

	while (!mymap.empty())
	{
		std::cout << mymap.begin()->first << " => " << mymap.begin()->second << '\n';
		mymap.erase(mymap.begin());
	}

	DELETE
}

TEST_M(tests_map_size)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;
	mymap[VALT('a')]=VALU(101);
	mymap[VALT('b')]=VALU(202);
	mymap[VALT('c')]=VALU(302);

	std::cout << "mymap.size() is " << mymap.size() << '\n';

	PRINT(mymap)

	DELETE
}

TEST_M(tests_map_max_size)
{
	// title
	TITLE(cplusplus.com reference)

	int i;
	std::map<T,U> mymap;

	if (mymap.max_size()>1000)
	{
		for (i=0; i<1000; i++) mymap[i]=VALU(0);
		std::cout << "The map contains 1000 elements.\n";
	}
	else std::cout << "The map could not hold 1000 elements.\n";

	DELETE
}

TEST_M(tests_map_operator_access)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;

	mymap[VALT('a')]=VALU("An element");
	mymap[VALT('b')]=VALU("another element");
	mymap[VALT('c')]=mymap[VAL('b')];

	std::cout << "mymap['a'] is " << mymap[VALT('a')] << '\n';
	std::cout << "mymap['b'] is " << mymap[VALT('b')] << '\n';
	std::cout << "mymap['c'] is " << mymap[VALT('c')] << '\n';
	std::cout << "mymap['d'] is " << mymap[VALT('d')] << '\n';

	std::cout << "mymap now contains " << mymap.size() << " elements.\n";

	DELETE
}

TEST_M(tests_map_insert)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;

	// first insert function version (single parameter):
	mymap.insert ( ft::pair<T,U>(VALT('a'),VALU(100)) );
	mymap.insert ( ft::pair<T,U>(VALT('z'),VALU(200)) );

	ft::pair<typename ft::map<T,U>::iterator, bool> ret;
	ret = mymap.insert ( ft::pair<T,U>(VALT('z'),VALU(500)) );
	if (ret.second==false) {
		std::cout << "element 'z' already existed";
		std::cout << " with a value of " << ret.first->second << '\n';
	}


	// second insert function version (with hint position):
	typename ft::map<T,U>::iterator it = mymap.begin();
	mymap.insert (it, ft::pair<T,U>(VALT('b'),VALU(300)));	// max efficiency inserting
	mymap.insert (it, ft::pair<T,U>(VALT('c'),VALU(400)));	// no max efficiency inserting

	// third insert function version (range insertion):
	ft::map<T,U> anothermap;
	anothermap.insert(mymap.begin(),mymap.find('c'));

	PRINT(mymap)
	PRINT(anothermap)

	DELETE
}

TEST_M(tests_map_erase)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;
	typename ft::map<T,U>::iterator it;

	// insert some values:
	mymap[VALT('a')]=VALU(10);
	mymap[VALT('b')]=VALU(20);
	mymap[VALT('c')]=VALU(30);
	mymap[VALT('d')]=VALU(40);
	mymap[VALT('e')]=VALU(50);
	mymap[VALT('f')]=VALU(60);

	it=mymap.find(VALT('b'));
	mymap.erase (it);					// erasing by iterator

	mymap.erase (VALT('c'));			// erasing by key

	it=mymap.find (VALT('e'));
	mymap.erase ( it, mymap.end() );	// erasing by range

	PRINT(mymap)

	DELETE
}

TEST_M(tests_map_swap)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> foo,bar;

	foo[VALT('x')]=VALU(100);
	foo[VALT('y')]=VALU(200);

	bar[VALT('a')]=VALU(11);
	bar[VALT('b')]=VALU(22);
	bar[VALT('c')]=VALU(33);

	foo.swap(bar);

	PRINT(foo)
	PRINT(bar)

	DELETE
}

TEST_M(tests_map_clear)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;

	mymap[VALT('x')]=VALU(100);
	mymap[VALT('y')]=VALU(200);
	mymap[VALT('z')]=VALU(300);

	PRINT(mymap)

	mymap.clear();
	mymap[VALT('a')]=VALU(1101);
	mymap[VALT('b')]=VALU(2202);

	PRINT(mymap)

	DELETE
}

TEST_M(tests_map_key_comp)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;

	typename ft::map<T,U>::key_compare mycomp = mymap.key_comp();

	mymap[VALT('a')]=VALU(100);
	mymap[VALT('b')]=VALU(200);
	mymap[VALT('c')]=VALU(300);

	std::cout << "mymap contains:\n";

	T highest = mymap.rbegin()->first;		// key value of last element

	typename ft::map<T,U>::iterator it = mymap.begin();
	do {
		std::cout << it->first << " => " << it->second << '\n';
	} while ( mycomp((*it++).first, highest) );

	std::cout << '\n';

	DELETE
}

TEST_M(tests_map_value_comp)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;

	mymap[VALT('x')]=VALU(1001);
	mymap[VALT('y')]=VALU(2002);
	mymap[VALT('z')]=VALU(3003);

	std::cout << "mymap contains:\n";

	ft::pair<T,U> highest = *mymap.rbegin();          // last element

	typename ft::map<T,U>::iterator it = mymap.begin();
	do {
		std::cout << it->first << " => " << it->second << '\n';
	} while ( mymap.value_comp()(*it++, highest) );

	DELETE
}

TEST_M(tests_map_find)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;
	typename ft::map<T,U>::iterator it;

	mymap[VALT('a')]=VALU(50);
	mymap[VALT('b')]=VALU(100);
	mymap[VALT('c')]=VALU(150);
	mymap[VALT('d')]=VALU(200);

	it = mymap.find(VALT('b'));
	if (it != mymap.end())
		mymap.erase (it);

	// print content:
	std::cout << "elements in mymap:" << '\n';
	std::cout << "a => " << mymap.find(VALT('a'))->second << '\n';
	std::cout << "c => " << mymap.find(VALT('c'))->second << '\n';
	std::cout << "d => " << mymap.find(VALT('d'))->second << '\n';

	DELETE
}

TEST_M(tests_map_count)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;
	T c;

	mymap [VALT('a')]=VALU(101);
	mymap [VALT('c')]=VALU(202);
	mymap [VALT('f')]=VALU(303);

	// to do this test with T as a 'string' or 'mystruct*' we should add overload
	for (c=VALT('a'); c<VALT('h'); c++)
	{
		std::cout << c;
		if (mymap.count(c)>0)
			std::cout << " is an element of mymap.\n";
		else 
			std::cout << " is not an element of mymap.\n";
	}

	DELETE
}

TEST_M(tests_map_lower_bound)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;
	typename ft::map<T,U>::iterator itlow,itup;

	mymap[VALT('a')]=VALU(20);
	mymap[VALT('b')]=VALU(40);
	mymap[VALT('c')]=VALU(60);
	mymap[VALT('d')]=VALU(80);
	mymap[VALT('e')]=VALU(100);

	itlow=mymap.lower_bound (VALT('b'));  // itlow points to b
	itup=mymap.upper_bound (VALT('d'));   // itup points to e (not d!)

	mymap.erase(itlow,itup);        // erases [itlow,itup)

	PRINT(mymap)

	DELETE
}

TEST_M(tests_map_upper_bound)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;
	typename ft::map<T,U>::iterator itlow,itup;

	mymap[VALT('a')]=VALU(20);
	mymap[VALT('b')]=VALU(40);
	mymap[VALT('c')]=VALU(60);
	mymap[VALT('d')]=VALU(80);
	mymap[VALT('e')]=VALU(100);

	itlow=mymap.lower_bound (VALT('b'));	// itlow points to b
	itup=mymap.upper_bound (VALT('d'));		// itup points to e (not d!)

	mymap.erase(itlow,itup);				// erases [itlow,itup)

	PRINT(mymap)

	DELETE
}

TEST_M(tests_map_equal_range)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> mymap;

	mymap[VALT('a')]=VALU(10);
	mymap[VALT('b')]=VALU(20);
	mymap[VALT('c')]=VALU(30);

	ft::pair<typename ft::map<T,U>::iterator,typename ft::map<T,U>::iterator> ret;
	ret = mymap.equal_range(VALT('b'));

	std::cout << "lower bound points to: ";
	std::cout << ret.first->first << " => " << ret.first->second << '\n';

	std::cout << "upper bound points to: ";
	std::cout << ret.second->first << " => " << ret.second->second << '\n';

	DELETE
}

TEST_M(tests_map_get_allocator)
{
	// title
	TITLE(cplusplus.com reference)

	int psize;
	ft::map<T,U> mymap;
	ft::pair<const T,U>* p;

	// allocate an array of 5 elements using mymap's allocator:
	p=mymap.get_allocator().allocate(5);

	// assign some values to array
	psize = sizeof(typename ft::map<T,U>::value_type)*5;

	std::cout << "The allocated array has a size of " << psize << " bytes.\n";

	mymap.get_allocator().deallocate(p,5);

	DELETE
}

TEST_M(tests_map_relational_operators)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T,U> alice;
	ft::map<T,U>   bob;
	ft::map<T,U>   eve;
	(void)alice;
	(void)bob;
	(void)eve;

	alice[VALT(1)]=VALU('a');
	alice[VALT(2)]=VALU('b');
	alice[VALT(3)]=VALU('c');

	bob[VALT(7)]=VALU('Z');
	bob[VALT(8)]=VALU('Y');
	bob[VALT(9)]=VALU('X');
	bob[VALT(10)]=VALU('W');

	eve[VALT(1)]=VALU('a');
	eve[VALT(2)]=VALU('b');
	eve[VALT(3)]=VALU('c');

	std::cout << std::boolalpha;

	// Compare non equal containers
	std::cout << "alice == bob returns " << (alice == bob) << '\n';
	std::cout << "alice != bob returns " << (alice != bob) << '\n';
	std::cout << "alice <  bob returns " << (alice < bob) << '\n';
	std::cout << "alice <= bob returns " << (alice <= bob) << '\n';
	std::cout << "alice >  bob returns " << (alice > bob) << '\n';
	std::cout << "alice >= bob returns " << (alice >= bob) << '\n';

	std::cout << '\n';

	// Compare equal containers
	std::cout << "alice == eve returns " << (alice == eve) << '\n';
	std::cout << "alice != eve returns " << (alice != eve) << '\n';
	std::cout << "alice <  eve returns " << (alice < eve) << '\n';
	std::cout << "alice <= eve returns " << (alice <= eve) << '\n';
	std::cout << "alice >  eve returns " << (alice > eve) << '\n';
	std::cout << "alice >= eve returns " << (alice >= eve) << '\n';

	DELETE
}


TEST_M(tests_map_swap_non_member)
{
	// title
	TITLE(cplusplus.com reference)

	ft::map<T, U> alice;
	ft::map<T, U> bob;

	alice[VALT(1)]=VALU('a');
	alice[VALT(2)]=VALU('b');
	alice[VALT(3)]=VALU('c');

	bob[VALT(7)]=VALU('Z');
	bob[VALT(8)]=VALU('Y');
	bob[VALT(9)]=VALU('X');
	bob[VALT(10)]=VALU('W');

	// Print state before swap
	PRINT(alice)
	PRINT(bob)

	std::cout << "-- SWAP\n";
	std::swap(alice, bob);

	// Print state after swap
	PRINT(alice)
	PRINT(bob)

	DELETE
}

