#include "unordered_map_test.h"
#include "testapp.h"
#include <unordered_map>
ADD_SUITE(UnorderedMapSuite);
template<class T>
T merge_map(T const & a, T const& b)
{
T t(a);
t.insert(b.begin(), b.end());
return t;
}
void UnorderedMapSuite::construct()
{
std::unordered_map<std::string, std::string> a;
std::unordered_map<std::string, std::string> b({ { "apple", "red" }, { "pear", "yellow" }, { "banana", "yellow" }, { "banana", "yellow" } });
std::unordered_map<std::string, std::string> c({ { "mango", "yellow" }, { "strawberry", "red" } });
std::unordered_map<std::string, std::string> d(b);
std::unordered_map<std::string, std::string> e(merge_map(b, c));
std::unordered_map<std::string, std::string> f(e.begin(), e.end());
TEST_ASSERT_EQUALS(true, a.empty())
TEST_ASSERT_EQUALS(3, b.size())
TEST_ASSERT_EQUALS(2, c.size())
TEST_ASSERT_EQUALS(3, d.size())
TEST_ASSERT_EQUALS(5, e.size())
TEST_ASSERT_EQUALS(5, f.size())
}
void UnorderedMapSuite::assigns()
{
std::unordered_map<std::string, std::string> a;
std::unordered_map<std::string, std::string> b;
std::unordered_map<std::string, std::string> c;
std::unordered_map<std::string, std::string> d;
a = { { "apple", "red" }, { "pear", "yellow" }, { "banana", "yellow" }, { "banana", "yellow" } };
b = { { "mango", "yellow" }, { "strawberry", "red" } };
c = merge_map(a, b);
d = c;
TEST_ASSERT_EQUALS(3, a.size())
TEST_ASSERT_EQUALS(2, b.size())
TEST_ASSERT_EQUALS(5, c.size())
TEST_ASSERT_EQUALS(5, d.size())
}
void UnorderedMapSuite::iterators()
{
std::unordered_map<std::string, std::string> names =
{ { "apple", "red" }, { "pear", "yellow" }, { "banana", "yellow" }, { "banana", "yellow" } };
int count = 0;
for(auto it = names.begin(); it != names.end(); ++it)
{
std::cerr << it->first << ":" << it->second << " ";
count++;
}
std::cerr << std::endl;
TEST_ASSERT_EQUALS(3, count)
count = 0;
for(auto it = names.cbegin(); it != names.cend(); ++it)
{
std::cerr << it->first << ":" << it->second << " ";
count++;
}
std::cerr <<std::endl;
TEST_ASSERT_EQUALS(3, count)
}
void UnorderedMapSuite::capacity()
{
std::unordered_map<std::string, std::string> a;
std::unordered_map<std::string, std::string> b(
{ { "apple", "red" }, { "pear", "yellow" }, { "banana", "yellow" }, { "banana", "yellow" } });
TEST_ASSERT_EQUALS(true, a.empty())
TEST_ASSERT_EQUALS(3, b.size())
TEST_ASSERT_EQUALS(a.max_size(), b.max_size())
}
void UnorderedMapSuite::access()
{
std::unordered_map<char, int> a;
std::unordered_map<char, int> b = { { 'a', 10 }, { 'b', 20 }};
bool hasExcpetion = false;
a['a'] = 10;
a['b'] = 20;
b.at('a') = 20;
b.at('b') = 30;
try
{
b.at('c') = 40;
}
catch(...)
{
hasExcpetion = true;
}
TEST_ASSERT_EQUALS(10, a['a'])
TEST_ASSERT_EQUALS(20, a['b'])
TEST_ASSERT_EQUALS(20, b['a'])
TEST_ASSERT_EQUALS(30, b['b'])
TEST_ASSERT_EQUALS(true, hasExcpetion)
}
void UnorderedMapSuite::find()
{
std::unordered_map<std::string, std::string> fruits =
{ { "apple", "red" }, { "pear", "yellow" }, { "banana", "yellow" }, { "banana", "yellow" } };
auto it = fruits.find("apple");
TEST_ASSERT_EQUALS("apple", it->first)
it = fruits.find("God");
TEST_ASSERT_EQUALS(true, it == fruits.end())
}
void UnorderedMapSuite::count()
{
std::unordered_map<std::string, std::string> fruits =
{ { "apple", "red" }, { "pear", "yellow" }, { "banana", "yellow" }, { "banana", "yellow" } };
TEST_ASSERT_EQUALS(1, fruits.count("banana"))
TEST_ASSERT_EQUALS(0, fruits.count("watermelon"))
}
void UnorderedMapSuite::equal_range()
{
std::unordered_map<std::string, std::string> fruits =
{ { "apple", "red" }, { "pear", "yellow" }, { "banana", "yellow" }, { "banana", "yellow" } };
auto range = fruits.equal_range("pear");
TEST_ASSERT_EQUALS("pear", range.first->first)
TEST_ASSERT_EQUALS(true, range.first != range.second)
range = fruits.equal_range("watermelon");
TEST_ASSERT_EQUALS(true, range.first == fruits.end())
TEST_ASSERT_EQUALS(true, range.first == range.second)
}
void UnorderedMapSuite::emplace()
{
std::unordered_map<std::string, std::string> fruits;
auto r = fruits.emplace("apple", "red");
TEST_ASSERT_EQUALS("apple", r.first->first)
TEST_ASSERT_EQUALS(true, r.second)
TEST_ASSERT_EQUALS(1, fruits.size())
r = fruits.emplace("pear", "yellow");
TEST_ASSERT_EQUALS("pear", r.first->first)
TEST_ASSERT_EQUALS(true, r.second)
TEST_ASSERT_EQUALS(2, fruits.size())
r = fruits.emplace("pear", "yellow");
TEST_ASSERT_EQUALS("pear", r.first->first)
TEST_ASSERT_EQUALS(false, r.second)
TEST_ASSERT_EQUALS(2, fruits.size())
}
void UnorderedMapSuite::emplace_hint()
{
std::unordered_map<std::string, std::string> fruits =
{ { "apple", "red" } };
auto it = fruits.emplace_hint(fruits.begin(), "pear", "yellow");
TEST_ASSERT_EQUALS("pear", it->first)
TEST_ASSERT_EQUALS(2, fruits.size())
it = fruits.emplace_hint(fruits.begin(), "pear", "yellow");
TEST_ASSERT_EQUALS("pear", it->first)
TEST_ASSERT_EQUALS(2, fruits.size())
}
void UnorderedMapSuite::insert()
{
std::unordered_map<std::string, std::string> a;
std::unordered_map<std::string, std::string> b;
std::unordered_map<std::string, std::string> c;
auto r = a.insert({ "apple", "red" });
TEST_ASSERT_EQUALS("apple", r.first->first)
TEST_ASSERT_EQUALS(true, r.second)
r = a.insert({ "apple", "red" });//no insert
TEST_ASSERT_EQUALS("apple", r.first->first)
TEST_ASSERT_EQUALS(false, r.second)
auto it = a.insert(a.begin(), { "pear", "yellow" });
TEST_ASSERT_EQUALS("pear", it->first)
TEST_ASSERT_EQUALS(2, a.size())
it = a.insert(a.begin(), { "pear", "yellow" });//no insert
TEST_ASSERT_EQUALS("pear", it->first)
TEST_ASSERT_EQUALS(2, a.size())
std::string yellow = "yellow";
a.insert({ yellow + " banana", "yellow"});
b.insert(a.begin(), a.end());
c.insert({{ "apple", "red" }, { "banana", "yellow" }, { "banana", "yellow" }});
TEST_ASSERT_EQUALS(3, a.size())
TEST_ASSERT_EQUALS(3, b.size())
TEST_ASSERT_EQUALS(2, c.size())
}
void UnorderedMapSuite::erase()
{
std::unordered_map<std::string, std::string> fruits =
{ { "apple", "red" }, { "pear", "yellow" }, { "banana", "yellow" }, { "banana", "yellow" } };
TEST_ASSERT_EQUALS(3, fruits.size())
fruits.erase(fruits.find("apple"));
TEST_ASSERT_EQUALS(2, fruits.size())
auto size = fruits.erase("banana");
TEST_ASSERT_EQUALS(1, size)
TEST_ASSERT_EQUALS(1, fruits.size())
fruits.erase(fruits.begin(), fruits.end());
TEST_ASSERT_EQUALS(0, fruits.size())
}
void UnorderedMapSuite::clear()
{
std::unordered_map<std::string, std::string> fruits =
{ { "apple", "red" }, { "pear", "yellow" }, { "banana", "yellow" }, { "banana", "yellow" } };
TEST_ASSERT_EQUALS(3, fruits.size())
fruits.clear();
TEST_ASSERT_EQUALS(0, fruits.size())
}
void UnorderedMapSuite::swap()
{
std::unordered_map<std::string, std::string> a;
std::unordered_map<std::string, std::string> b =
{ { "apple", "red" }, { "pear", "yellow" }, { "banana", "yellow" }, { "banana", "yellow" } };
TEST_ASSERT_EQUALS(0, a.size())
TEST_ASSERT_EQUALS(3, b.size())
a.swap(b);
TEST_ASSERT_EQUALS(3, a.size())
TEST_ASSERT_EQUALS(0, b.size())
}
void UnorderedMapSuite::bucket_count()
{
std::unordered_map<std::string, std::string> fruits =
{ { "apple", "red" }, { "pear", "yellow" }, { "banana", "yellow" }, { "banana", "yellow" } };
TEST_ASSERT_EQUALS(true, fruits.bucket_count() >= fruits.size())
}
void UnorderedMapSuite::max_bucket_count()
{
std::unordered_map<std::string, std::string> a;
std::unor