Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/simulation2/tests/test_EntityMap.h
Show All 28 Lines | public: | ||||
void setUp() | void setUp() | ||||
{ | { | ||||
} | } | ||||
void tearDown() | void tearDown() | ||||
{ | { | ||||
} | } | ||||
void test_insert() | void test_insert_or_assign() | ||||
{ | { | ||||
EntityMap<int> test; | EntityMap<int, 1> test; | ||||
TS_ASSERT(test.empty()); | TS_ASSERT(test.empty()); | ||||
test.insert(1,1); | test.insert_or_assign(1,1); | ||||
test.insert(2,2); | test.insert_or_assign(2,2); | ||||
test.insert(3,3); | test.insert_or_assign(3,3); | ||||
test.insert(4,4); | test.insert_or_assign(4,4); | ||||
test.insert(4,5); | test.insert_or_assign(4,5); | ||||
test.insert(4,6); | test.insert_or_assign(4,6); | ||||
// TS_ASSERT(test.m_Data.size() == 5); | TS_ASSERT(test.m_Data.size() == 5); | ||||
// TS_ASSERT(test.m_Data.back().first != INVALID_ENTITY); | TS_ASSERT(test.m_Data.back().first != INVALID_ENTITY); | ||||
TS_ASSERT(test.size() == 4); | TS_ASSERT(test.size() == 4); | ||||
TS_ASSERT(test.find(3)->second == 3); | TS_ASSERT(test.find(3)->second == 3); | ||||
TS_ASSERT(test.find(4)->second == 6); | TS_ASSERT(test.find(4)->second == 6); | ||||
test.insert(10,7); | test.insert_or_assign(10,7); | ||||
// TS_ASSERT(test.m_Data.size() == 11); | TS_ASSERT(test.m_Data.size() == 11); | ||||
// TS_ASSERT(test.m_Data.back().first != INVALID_ENTITY); | TS_ASSERT(test.m_Data.back().first != INVALID_ENTITY); | ||||
TS_ASSERT(test.size() == 5); | TS_ASSERT(test.size() == 5); | ||||
TS_ASSERT(test.find(4)->second == 6); | TS_ASSERT(test.find(4)->second == 6); | ||||
TS_ASSERT(test.find(5) == test.end()); | TS_ASSERT(test.find(5) == test.end()); | ||||
TS_ASSERT(test.find(6) == test.end()); | TS_ASSERT(test.find(6) == test.end()); | ||||
TS_ASSERT(test.find(7) == test.end()); | TS_ASSERT(test.find(7) == test.end()); | ||||
TS_ASSERT(test.find(8) == test.end()); | TS_ASSERT(test.find(8) == test.end()); | ||||
TS_ASSERT(test.find(9) == test.end()); | TS_ASSERT(test.find(9) == test.end()); | ||||
TS_ASSERT(test.find(10)->second == 7); | TS_ASSERT(test.find(10)->second == 7); | ||||
// EntityMap<int, 5> test2; | EntityMap<int, 5> test2; | ||||
// test2.insert(8,5); | test2.insert_or_assign(8,5); | ||||
// TS_ASSERT(test2.find(8)->second == 5); | TS_ASSERT(test2.find(8)->second == 5); | ||||
// TS_ASSERT(test2.m_Data.size() == 5); | TS_ASSERT(test2.m_Data.size() == 5); | ||||
// TS_ASSERT(test2.size() == 1); | TS_ASSERT(test2.size() == 1); | ||||
} | } | ||||
void test_iterators() | void test_iterators() | ||||
{ | { | ||||
EntityMap<int> test; | EntityMap<int, 1> test; | ||||
test.insert(1,1); | test.insert_or_assign(1,1); | ||||
test.insert(2,2); | test.insert_or_assign(2,2); | ||||
test.insert(3,3); | test.insert_or_assign(3,3); | ||||
test.insert(4,4); | test.insert_or_assign(4,4); | ||||
EntityMap<int>::iterator it = test.begin(); | EntityMap<int, 1>::iterator it = test.begin(); | ||||
TS_ASSERT(it->first == 1); | TS_ASSERT(it->first == 1); | ||||
++it; | it++; | ||||
TS_ASSERT(it->first == 2); | TS_ASSERT(it->first == 2); | ||||
++it; // it++ incorrectly returns a pointer in svn | ++it; | ||||
TS_ASSERT(it->first == 3); | TS_ASSERT(it->first == 3); | ||||
it = test.end(); | it = test.end(); | ||||
// TS_ASSERT(it->first == test.m_Data.back().first); | TS_ASSERT(it->first == test.m_Data.back().first); | ||||
EntityMap<int>::const_iterator cit = test.begin(); | EntityMap<int, 1>::const_iterator cit = test.begin(); | ||||
TS_ASSERT(cit->first == 1); | TS_ASSERT(cit->first == 1); | ||||
cit = test.end(); | cit = test.end(); | ||||
// TS_ASSERT(cit->first == test.m_Data.back().first); | TS_ASSERT(cit->first == test.m_Data.back().first); | ||||
size_t iter = 0; | size_t iter = 0; | ||||
for (EntityMap<int>::value_type& v : test) | for (EntityMap<int, 1>::value_type& v : test) | ||||
{ | { | ||||
++iter; | ++iter; | ||||
TS_ASSERT(test.find(iter)->second == (int)iter); | TS_ASSERT(test.find(iter)->second == (int)iter); | ||||
TS_ASSERT(test.find(iter)->second == v.second); | TS_ASSERT(test.find(iter)->second == v.second); | ||||
} | } | ||||
TS_ASSERT(iter == 4); | TS_ASSERT(iter == 4); | ||||
test.clear(); | test.clear(); | ||||
test.insert(10,1); | test.insert_or_assign(10,1); | ||||
test.insert(20,2); | test.insert_or_assign(20,2); | ||||
test.insert(30,3); | test.insert_or_assign(30,3); | ||||
test.insert(40,4); | test.insert_or_assign(40,4); | ||||
it = test.begin(); | it = test.begin(); | ||||
TS_ASSERT(it->second == 1); | TS_ASSERT(it->second == 1); | ||||
++it; // it++ incorrectly returns a pointer in svn | it++; | ||||
TS_ASSERT(it->second == 2); | TS_ASSERT(it->second == 2); | ||||
++it; | ++it; | ||||
TS_ASSERT(it->second == 3); | TS_ASSERT(it->second == 3); | ||||
it = test.end(); | it = test.end(); | ||||
// TS_ASSERT(it->first == test.m_Data.back().first); | TS_ASSERT(it->first == test.m_Data.back().first); | ||||
} | } | ||||
void test_erase() | void test_erase() | ||||
{ | { | ||||
EntityMap<int> test; | EntityMap<int> test; | ||||
test.insert(1,1); | test.insert_or_assign(1,1); | ||||
test.insert(2,2); | test.insert_or_assign(2,2); | ||||
test.insert(3,3); | test.insert_or_assign(3,3); | ||||
test.insert(4,4); | test.insert_or_assign(4,4); | ||||
test.erase(2); | test.erase(2); | ||||
// TS_ASSERT(test.m_Data.size() == 5); | TS_ASSERT(test.m_Data.size() == 5); | ||||
TS_ASSERT(test.size() == 3); | TS_ASSERT(test.size() == 3); | ||||
TS_ASSERT(test.find(2) == test.end()); | TS_ASSERT(test.find(2) == test.end()); | ||||
test.erase(1); | test.erase(1); | ||||
test.erase(3); | test.erase(3); | ||||
test.erase(4);// TS_ASSERT(test.erase(4) == 1); | TS_ASSERT(test.erase(4) == 1); | ||||
// TS_ASSERT(test.m_Data.size() == 5); | TS_ASSERT(test.m_Data.size() == 5); | ||||
TS_ASSERT(test.size() == 0); | TS_ASSERT(test.size() == 0); | ||||
test.erase(5);// TS_ASSERT(test.erase(5) == 0); | TS_ASSERT(test.erase(5) == 0); | ||||
test.insert(1,1); | test.insert_or_assign(1,1); | ||||
test.insert(2,2); | test.insert_or_assign(2,2); | ||||
test.insert(3,3); | test.insert_or_assign(3,3); | ||||
test.insert(4,4); | test.insert_or_assign(4,4); | ||||
test.erase(test.begin()); | test.erase(test.begin()); | ||||
// TS_ASSERT(test.m_Data.size() == 5); | TS_ASSERT(test.m_Data.size() == 5); | ||||
TS_ASSERT(test.size() == 3); | TS_ASSERT(test.size() == 3); | ||||
TS_ASSERT(test.find(1) == test.end()); | TS_ASSERT(test.find(1) == test.end()); | ||||
// TS_ASSERT(test.erase(test.end()) == 0); | TS_ASSERT(test.erase(test.end()) == 0); | ||||
// TS_ASSERT(test.m_Data.back().first != INVALID_ENTITY); | TS_ASSERT(test.m_Data.back().first != INVALID_ENTITY); | ||||
} | } | ||||
void test_clear() | void test_clear() | ||||
{ | { | ||||
EntityMap<int> test; | EntityMap<int> test; | ||||
test.insert(1,1); | test.insert_or_assign(1,1); | ||||
test.insert(2,2); | test.insert_or_assign(2,2); | ||||
test.insert(3,3); | test.insert_or_assign(3,3); | ||||
test.insert(4,4); | test.insert_or_assign(4,4); | ||||
test.clear(); | test.clear(); | ||||
// TS_ASSERT(test.m_Data.size() == 1); | TS_ASSERT(test.m_Data.size() == 1); | ||||
TS_ASSERT(test.size() == 0); | TS_ASSERT(test.size() == 0); | ||||
} | } | ||||
void test_find() | void test_find() | ||||
{ | { | ||||
EntityMap<int> test; | EntityMap<int> test; | ||||
test.insert(1,1); | test.insert_or_assign(1,1); | ||||
test.insert(2,2); | test.insert_or_assign(2,2); | ||||
test.insert(3,3); | test.insert_or_assign(3,3); | ||||
test.insert(40,4); | test.insert_or_assign(40,4); | ||||
TS_ASSERT(test.find(1)->second == 1); | TS_ASSERT(test.find(1)->second == 1); | ||||
TS_ASSERT(test.find(40)->second == 4); | TS_ASSERT(test.find(40)->second == 4); | ||||
TS_ASSERT(test.find(30) == test.end()); | TS_ASSERT(test.find(30) == test.end()); | ||||
} | } | ||||
void test_perf_DISABLED() | void test_perf_DISABLED() | ||||
{ | { | ||||
EntityMap<int> test; | EntityMap<int> test; | ||||
printf("Testing performance of EntityMap\n"); | printf("Testing performance of EntityMap\n"); | ||||
double t = timer_Time(); | double t = timer_Time(); | ||||
for (int i = 1; i <= 200000; ++i) | for (int i = 1; i <= 200000; ++i) | ||||
test.insert(i,i); | test.insert_or_assign(i,i); | ||||
double tt = timer_Time() - t; | double tt = timer_Time() - t; | ||||
printf("inserting 200K elements in order: %lfs\n", tt); | printf("insert_or_assigning 200K elements in order: %lfs\n", tt); | ||||
t = timer_Time(); | t = timer_Time(); | ||||
for (int i = 1; i <= 200000; ++i) | for (int i = 1; i <= 200000; ++i) | ||||
test.erase(i); | test.erase(i); | ||||
tt = timer_Time() - t; | tt = timer_Time() - t; | ||||
printf("Erasing 200K elements, by key: %lfs\n", tt); | printf("Erasing 200K elements, by key: %lfs\n", tt); | ||||
t = timer_Time(); | t = timer_Time(); | ||||
for (int i = 200000; i >= 1; --i) | for (int i = 200000; i >= 1; --i) | ||||
test.insert(i,i); | test.insert_or_assign(i,i); | ||||
tt = timer_Time() - t; | tt = timer_Time() - t; | ||||
printf("inserting 200K elements in reverse order: %lfs\n", tt); | printf("insert_or_assigning 200K elements in reverse order: %lfs\n", tt); | ||||
t = timer_Time(); | t = timer_Time(); | ||||
for (auto i = test.begin(); i != test.end(); ++i) | for (auto i = test.begin(); i != test.end(); ++i) | ||||
test.erase(i); | test.erase(i); | ||||
tt = timer_Time() - t; | tt = timer_Time() - t; | ||||
printf("Erasing 200K elements, by iterator: %lfs\n", tt); | printf("Erasing 200K elements, by iterator: %lfs\n", tt); | ||||
t = timer_Time(); | t = timer_Time(); | ||||
for (int i = 1; i <= 200000; ++i) | for (int i = 1; i <= 200000; ++i) | ||||
test.erase(i); | test.erase(i); | ||||
tt = timer_Time() - t; | tt = timer_Time() - t; | ||||
printf("Erasing 200K non-existing elements: %lfs\n", tt); | printf("Erasing 200K non-existing elements: %lfs\n", tt); | ||||
// prep random vector | // prep random vector | ||||
std::vector<int> vec; | std::vector<int> vec; | ||||
for (int i = 1; i <= 200000; ++i) | for (int i = 1; i <= 200000; ++i) | ||||
vec.push_back(i); | vec.push_back(i); | ||||
std::random_shuffle(vec.begin(), vec.end()); | std::random_shuffle(vec.begin(), vec.end()); | ||||
for (int i = 1; i <= 200000; ++i) | for (int i = 1; i <= 200000; ++i) | ||||
test.insert(i,i); | test.insert_or_assign(i,i); | ||||
t = timer_Time(); | t = timer_Time(); | ||||
for (int i = 1; i <= 200000; ++i) | for (int i = 1; i <= 200000; ++i) | ||||
test.find(vec[i])->second = 3; | test.find(vec[i])->second = 3; | ||||
tt = timer_Time() - t; | tt = timer_Time() - t; | ||||
printf("200K random lookups in random order: %lfs\n", tt); | printf("200K random lookups in random order: %lfs\n", tt); | ||||
t = timer_Time(); | t = timer_Time(); | ||||
for (auto& p : test) | for (auto& p : test) | ||||
p.second = 3; | p.second = 3; | ||||
tt = timer_Time() - t; | tt = timer_Time() - t; | ||||
printf("auto iteration on 200K continuous entitymap: %lfs\n", tt); | printf("auto iteration on 200K continuous entitymap: %lfs\n", tt); | ||||
test.clear(); | test.clear(); | ||||
for (int i = 1; i <= 200000; ++i) | for (int i = 1; i <= 200000; ++i) | ||||
test.insert(i*5,i); | test.insert_or_assign(i*5,i); | ||||
t = timer_Time(); | t = timer_Time(); | ||||
for (EntityMap<int>::value_type& p : test) | for (EntityMap<int>::value_type& p : test) | ||||
p.second = 3; | p.second = 3; | ||||
tt = timer_Time() - t; | tt = timer_Time() - t; | ||||
printf("auto iteration on 200K sparse (holes of 5): %lfs\n", tt); | printf("auto iteration on 200K sparse (holes of 5): %lfs\n", tt); | ||||
test.clear(); | test.clear(); | ||||
for (int i = 1; i <= 4000; ++i) | for (int i = 1; i <= 4000; ++i) | ||||
test.insert(i*50,i); | test.insert_or_assign(i*50,i); | ||||
t = timer_Time(); | t = timer_Time(); | ||||
for (EntityMap<int>::value_type& p : test) | for (EntityMap<int>::value_type& p : test) | ||||
p.second = 3; | p.second = 3; | ||||
tt = timer_Time() - t; | tt = timer_Time() - t; | ||||
printf("auto iteration on 4K sparse (holes of 50): %lfs\n", tt); | printf("auto iteration on 4K sparse (holes of 50): %lfs\n", tt); | ||||
test.clear(); | test.clear(); | ||||
for (int i = 1; i <= 200000; ++i) | for (int i = 1; i <= 200000; ++i) | ||||
test.insert(i*50,i); | test.insert_or_assign(i*50,i); | ||||
t = timer_Time(); | t = timer_Time(); | ||||
for (EntityMap<int>::value_type& p : test) | for (EntityMap<int>::value_type& p : test) | ||||
p.second = 3; | p.second = 3; | ||||
tt = timer_Time() - t; | tt = timer_Time() - t; | ||||
printf("auto iteration on 200K sparse (holes of 50): %lfs\n", tt); | printf("auto iteration on 200K sparse (holes of 50): %lfs\n", tt); | ||||
test.clear(); | test.clear(); | ||||
for (int i = 1; i <= 2000000; ++i) | for (int i = 1; i <= 2000000; ++i) | ||||
test.insert(i*50,i); | test.insert_or_assign(i*50,i); | ||||
t = timer_Time(); | t = timer_Time(); | ||||
for (EntityMap<int>::iterator i = test.begin(); i != test.end(); ++i) | for (EntityMap<int>::iterator i = test.begin(); i != test.end(); ++i) | ||||
i->second = 3; | i->second = 3; | ||||
tt = timer_Time() - t; | tt = timer_Time() - t; | ||||
printf("manual ++iteration on 2000K sparse (holes of 50) (warmup 1): %lfs\n", tt); | printf("manual ++iteration on 2000K sparse (holes of 50) (warmup 1): %lfs\n", tt); | ||||
t = timer_Time(); | t = timer_Time(); | ||||
for (EntityMap<int>::iterator i = test.begin(); i != test.end(); ++i) | for (EntityMap<int>::iterator i = test.begin(); i != test.end(); i++) | ||||
i->second = 3; | i->second = 3; | ||||
tt = timer_Time() - t; | tt = timer_Time() - t; | ||||
printf("manual ++iteration on 2000K sparse (holes of 50) (warmup 2): %lfs\n", tt); | printf("manual ++iteration on 2000K sparse (holes of 50) (warmup 2): %lfs\n", tt); | ||||
t = timer_Time(); | t = timer_Time(); | ||||
for (EntityMap<int>::iterator i = test.begin(); i != test.end(); ++i) | for (EntityMap<int>::iterator i = test.begin(); i != test.end(); ++i) | ||||
i->second = 3; | i->second = 3; | ||||
tt = timer_Time() - t; | tt = timer_Time() - t; | ||||
printf("manual ++iteration on 2000K sparse (holes of 50): %lfs\n", tt); | printf("manual ++iteration on 2000K sparse (holes of 50): %lfs\n", tt); | ||||
t = timer_Time(); | t = timer_Time(); | ||||
for (EntityMap<int>::iterator i = test.begin(); i != test.end(); ++i) | for (EntityMap<int>::iterator i = test.begin(); i != test.end(); i++) | ||||
i->second = 3; | i->second = 3; | ||||
tt = timer_Time() - t; | tt = timer_Time() - t; | ||||
printf("manual iteration++ on 2000K sparse (holes of 50): %lfs\n", tt); | printf("manual iteration++ on 2000K sparse (holes of 50): %lfs\n", tt); | ||||
} | } | ||||
}; | }; |
Wildfire Games · Phabricator