621 lines
13 KiB
C++
621 lines
13 KiB
C++
|
|
#ifndef TESTS_MAP_CPP
|
|
#define TESTS_MAP_CPP
|
|
|
|
#include "tests_utils.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)
|
|
|
|
std::map<char,int> mymap;
|
|
|
|
// first insert function version (single parameter):
|
|
mymap.insert ( std::pair<char,int>('a',100) );
|
|
mymap.insert ( std::pair<char,int>('z',200) );
|
|
|
|
std::pair<std::map<char,int>::iterator,bool> ret;
|
|
ret = mymap.insert ( std::pair<char,int>('z',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):
|
|
std::map<char,int>::iterator it = mymap.begin();
|
|
mymap.insert (it, std::pair<char,int>('b',300)); // max efficiency inserting
|
|
mymap.insert (it, std::pair<char,int>('c',400)); // no max efficiency inserting
|
|
|
|
// third insert function version (range insertion):
|
|
std::map<char,int> anothermap;
|
|
anothermap.insert(mymap.begin(),mymap.find('c'));
|
|
|
|
// showing contents:
|
|
std::cout << "mymap contains:\n";
|
|
for (it=mymap.begin(); it!=mymap.end(); ++it)
|
|
std::cout << it->first << " => " << it->second << '\n';
|
|
|
|
std::cout << "anothermap contains:\n";
|
|
for (it=anothermap.begin(); it!=anothermap.end(); ++it)
|
|
std::cout << it->first << " => " << it->second << '\n';
|
|
|
|
DELETE
|
|
}
|
|
|
|
TEST_M(tests_map_erase)
|
|
{
|
|
// title
|
|
TITLE(cplusplus.com reference)
|
|
|
|
std::map<char,int> mymap;
|
|
std::map<char,int>::iterator it;
|
|
|
|
// insert some values:
|
|
mymap['a']=10;
|
|
mymap['b']=20;
|
|
mymap['c']=30;
|
|
mymap['d']=40;
|
|
mymap['e']=50;
|
|
mymap['f']=60;
|
|
|
|
it=mymap.find('b');
|
|
mymap.erase (it); // erasing by iterator
|
|
|
|
mymap.erase ('c'); // erasing by key
|
|
|
|
it=mymap.find ('e');
|
|
mymap.erase ( it, mymap.end() ); // erasing by range
|
|
|
|
// show content:
|
|
for (it=mymap.begin(); it!=mymap.end(); ++it)
|
|
std::cout << it->first << " => " << it->second << '\n';
|
|
|
|
DELETE
|
|
}
|
|
|
|
TEST_M(tests_map_swap)
|
|
{
|
|
// title
|
|
TITLE(cplusplus.com reference)
|
|
|
|
std::map<char,int> foo,bar;
|
|
|
|
foo['x']=100;
|
|
foo['y']=200;
|
|
|
|
bar['a']=11;
|
|
bar['b']=22;
|
|
bar['c']=33;
|
|
|
|
foo.swap(bar);
|
|
|
|
std::cout << "foo contains:\n";
|
|
for (std::map<char,int>::iterator it=foo.begin(); it!=foo.end(); ++it)
|
|
std::cout << it->first << " => " << it->second << '\n';
|
|
|
|
std::cout << "bar contains:\n";
|
|
for (std::map<char,int>::iterator it=bar.begin(); it!=bar.end(); ++it)
|
|
std::cout << it->first << " => " << it->second << '\n';
|
|
|
|
DELETE
|
|
}
|
|
|
|
TEST_M(tests_map_clear)
|
|
{
|
|
// title
|
|
TITLE(cplusplus.com reference)
|
|
|
|
std::map<char,int> mymap;
|
|
|
|
mymap['x']=100;
|
|
mymap['y']=200;
|
|
mymap['z']=300;
|
|
|
|
std::cout << "mymap contains:\n";
|
|
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
|
|
std::cout << it->first << " => " << it->second << '\n';
|
|
|
|
mymap.clear();
|
|
mymap['a']=1101;
|
|
mymap['b']=2202;
|
|
|
|
std::cout << "mymap contains:\n";
|
|
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
|
|
std::cout << it->first << " => " << it->second << '\n';
|
|
|
|
DELETE
|
|
}
|
|
|
|
TEST_M(tests_map_key_comp)
|
|
{
|
|
// title
|
|
TITLE(cplusplus.com reference)
|
|
|
|
std::map<char,int> mymap;
|
|
|
|
std::map<char,int>::key_compare mycomp = mymap.key_comp();
|
|
|
|
mymap['a']=100;
|
|
mymap['b']=200;
|
|
mymap['c']=300;
|
|
|
|
std::cout << "mymap contains:\n";
|
|
|
|
char highest = mymap.rbegin()->first; // key value of last element
|
|
|
|
std::map<char,int>::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)
|
|
|
|
std::map<char,int> mymap;
|
|
|
|
mymap['x']=1001;
|
|
mymap['y']=2002;
|
|
mymap['z']=3003;
|
|
|
|
std::cout << "mymap contains:\n";
|
|
|
|
std::pair<char,int> highest = *mymap.rbegin(); // last element
|
|
|
|
std::map<char,int>::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)
|
|
|
|
std::map<char,int> mymap;
|
|
std::map<char,int>::iterator it;
|
|
|
|
mymap['a']=50;
|
|
mymap['b']=100;
|
|
mymap['c']=150;
|
|
mymap['d']=200;
|
|
|
|
it = mymap.find('b');
|
|
if (it != mymap.end())
|
|
mymap.erase (it);
|
|
|
|
// print content:
|
|
std::cout << "elements in mymap:" << '\n';
|
|
std::cout << "a => " << mymap.find('a')->second << '\n';
|
|
std::cout << "c => " << mymap.find('c')->second << '\n';
|
|
std::cout << "d => " << mymap.find('d')->second << '\n';
|
|
|
|
DELETE
|
|
}
|
|
|
|
TEST_M(tests_map_count)
|
|
{
|
|
// title
|
|
TITLE(cplusplus.com reference)
|
|
|
|
std::map<char,int> mymap;
|
|
char c;
|
|
|
|
mymap ['a']=101;
|
|
mymap ['c']=202;
|
|
mymap ['f']=303;
|
|
|
|
for (c='a'; c<'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)
|
|
|
|
std::map<char,int> mymap;
|
|
std::map<char,int>::iterator itlow,itup;
|
|
|
|
mymap['a']=20;
|
|
mymap['b']=40;
|
|
mymap['c']=60;
|
|
mymap['d']=80;
|
|
mymap['e']=100;
|
|
|
|
itlow=mymap.lower_bound ('b'); // itlow points to b
|
|
itup=mymap.upper_bound ('d'); // itup points to e (not d!)
|
|
|
|
mymap.erase(itlow,itup); // erases [itlow,itup)
|
|
|
|
// print content:
|
|
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
|
|
std::cout << it->first << " => " << it->second << '\n';
|
|
|
|
DELETE
|
|
}
|
|
|
|
TEST_M(tests_map_upper_bound)
|
|
{
|
|
// title
|
|
TITLE(cplusplus.com reference)
|
|
|
|
std::map<char,int> mymap;
|
|
std::map<char,int>::iterator itlow,itup;
|
|
|
|
mymap['a']=20;
|
|
mymap['b']=40;
|
|
mymap['c']=60;
|
|
mymap['d']=80;
|
|
mymap['e']=100;
|
|
|
|
itlow=mymap.lower_bound ('b'); // itlow points to b
|
|
itup=mymap.upper_bound ('d'); // itup points to e (not d!)
|
|
|
|
mymap.erase(itlow,itup); // erases [itlow,itup)
|
|
|
|
// print content:
|
|
for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
|
|
std::cout << it->first << " => " << it->second << '\n';
|
|
|
|
DELETE
|
|
}
|
|
|
|
TEST_M(tests_map_equal_range)
|
|
{
|
|
// title
|
|
TITLE(cplusplus.com reference)
|
|
|
|
std::map<char,int> mymap;
|
|
|
|
mymap['a']=10;
|
|
mymap['b']=20;
|
|
mymap['c']=30;
|
|
|
|
std::pair<std::map<char,int>::iterator,std::map<char,int>::iterator> ret;
|
|
ret = mymap.equal_range('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;
|
|
std::map<char,int> mymap;
|
|
std::pair<const char,int>* p;
|
|
|
|
// allocate an array of 5 elements using mymap's allocator:
|
|
p=mymap.get_allocator().allocate(5);
|
|
|
|
// assign some values to array
|
|
psize = sizeof(std::map<char,int>::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)
|
|
|
|
std::map<int, char> alice{{1, 'a'}, {2, 'b'}, {3, 'c'}};
|
|
std::map<int, char> bob{{7, 'Z'}, {8, 'Y'}, {9, 'X'}, {10, 'W'}};
|
|
std::map<int, char> eve{{1, 'a'}, {2, 'b'}, {3, '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)
|
|
|
|
std::map<int, char> alice{{1, 'a'}, {2, 'b'}, {3, 'c'}};
|
|
std::map<int, char> bob{{7, 'Z'}, {8, 'Y'}, {9, 'X'}, {10, 'W'}};
|
|
|
|
auto print = [](std::pair<const int, char>& n) {
|
|
std::cout << " " << n.first << '(' << n.second << ')';
|
|
};
|
|
|
|
// Print state before swap
|
|
std::cout << "alice:";
|
|
std::for_each(alice.begin(), alice.end(), print);
|
|
std::cout << "\n" "bob :";
|
|
std::for_each(bob.begin(), bob.end(), print);
|
|
std::cout << '\n';
|
|
|
|
std::cout << "-- SWAP\n";
|
|
std::swap(alice, bob);
|
|
|
|
// Print state after swap
|
|
std::cout << "alice:";
|
|
std::for_each(alice.begin(), alice.end(), print);
|
|
std::cout << "\n" "bob :";
|
|
std::for_each(bob.begin(), bob.end(), print);
|
|
std::cout << '\n';
|
|
|
|
DELETE
|
|
}
|
|
*/
|
|
|
|
#endif
|
|
|