2012-05-28 07:45:12 +03:00
|
|
|
#ifndef __CXXMPH_MAP_TEST_HELPER_H__
|
|
|
|
#define __CXXMPH_MAP_TEST_HELPER_H__
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
2012-06-01 23:49:00 +03:00
|
|
|
#include "string_util.h"
|
2012-06-09 06:58:52 +03:00
|
|
|
#include <check.h>
|
2012-06-01 23:49:00 +03:00
|
|
|
|
2012-05-28 07:45:12 +03:00
|
|
|
namespace cxxmph {
|
|
|
|
|
2012-06-03 04:29:46 +03:00
|
|
|
using namespace cxxmph;
|
2012-05-28 07:45:12 +03:00
|
|
|
using namespace std;
|
|
|
|
|
2012-06-01 23:49:00 +03:00
|
|
|
template <template<typename...> class map_type>
|
2012-06-03 03:47:18 +03:00
|
|
|
struct MapTester {
|
2012-06-16 00:09:54 +03:00
|
|
|
static bool empty_find() {
|
|
|
|
map_type<int64_t, int64_t> m;
|
|
|
|
for (int i = 0; i < 1000; ++i) {
|
|
|
|
if (m.find(i) != m.end()) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
static bool empty_erase() {
|
|
|
|
map_type<int64_t, int64_t> m;
|
|
|
|
for (int i = 0; i < 1000; ++i) {
|
|
|
|
m.erase(i);
|
|
|
|
if (m.size()) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-05-28 07:45:12 +03:00
|
|
|
static bool small_insert() {
|
2012-06-01 23:49:00 +03:00
|
|
|
map_type<int64_t, int64_t> m;
|
2012-05-28 07:45:12 +03:00
|
|
|
// Start counting from 1 to not touch default constructed value bugs
|
|
|
|
for (int i = 1; i < 12; ++i) m.insert(make_pair(i, i));
|
|
|
|
return m.size() == 11;
|
|
|
|
}
|
|
|
|
static bool large_insert() {
|
2012-06-01 23:49:00 +03:00
|
|
|
map_type<int64_t, int64_t> m;
|
2012-05-28 07:45:12 +03:00
|
|
|
// Start counting from 1 to not touch default constructed value bugs
|
2012-06-03 04:29:46 +03:00
|
|
|
int nkeys = 12 * 256 * 256;
|
|
|
|
for (int i = 1; i < nkeys; ++i) m.insert(make_pair(i, i));
|
2012-06-09 06:58:52 +03:00
|
|
|
return static_cast<int>(m.size()) == nkeys - 1;
|
2012-05-28 07:45:12 +03:00
|
|
|
}
|
|
|
|
static bool small_search() {
|
2012-06-01 23:49:00 +03:00
|
|
|
map_type<int64_t, int64_t> m;
|
2012-05-28 07:45:12 +03:00
|
|
|
// Start counting from 1 to not touch default constructed value bugs
|
|
|
|
for (int i = 1; i < 12; ++i) m.insert(make_pair(i, i));
|
|
|
|
for (int i = 1; i < 12; ++i) if (m.find(i) == m.end()) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
static bool default_search() {
|
2012-06-01 23:49:00 +03:00
|
|
|
map_type<int64_t, int64_t> m;
|
2012-05-28 07:45:12 +03:00
|
|
|
if (m.find(0) != m.end()) return false;
|
|
|
|
for (int i = 1; i < 256; ++i) m.insert(make_pair(i, i));
|
|
|
|
if (m.find(0) != m.end()) return false;
|
|
|
|
for (int i = 0; i < 256; ++i) m.insert(make_pair(i, i));
|
|
|
|
if (m.find(0) == m.end()) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
static bool large_search() {
|
|
|
|
int nkeys = 10 * 1000;
|
2012-06-01 23:49:00 +03:00
|
|
|
map_type<int64_t, int64_t> m;
|
2012-05-28 07:45:12 +03:00
|
|
|
for (int i = 0; i < nkeys; ++i) m.insert(make_pair(i, i));
|
|
|
|
for (int i = 0; i < nkeys; ++i) if (m.find(i) == m.end()) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
static bool string_search() {
|
|
|
|
int nkeys = 10 * 1000;
|
|
|
|
vector<string> keys;
|
|
|
|
for (int i = 0; i < nkeys; ++i) {
|
2012-06-03 04:29:46 +03:00
|
|
|
keys.push_back(format("%v", i));
|
2012-05-28 07:45:12 +03:00
|
|
|
}
|
2012-06-01 23:49:00 +03:00
|
|
|
map_type<string, int64_t> m;
|
2012-05-28 07:45:12 +03:00
|
|
|
for (int i = 0; i < nkeys; ++i) m.insert(make_pair(keys[i], i));
|
|
|
|
for (int i = 0; i < nkeys; ++i) {
|
|
|
|
auto it = m.find(keys[i]);
|
|
|
|
if (it == m.end()) return false;
|
|
|
|
if (it->second != i) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-06-03 04:29:46 +03:00
|
|
|
static bool rehash_zero() {
|
|
|
|
map_type<int64_t, int64_t> m;
|
|
|
|
m.rehash(0);
|
|
|
|
return m.size() == 0;
|
|
|
|
}
|
|
|
|
static bool rehash_size() {
|
|
|
|
map_type<int64_t, int64_t> m;
|
|
|
|
int nkeys = 10 * 1000;
|
|
|
|
for (int i = 0; i < nkeys; ++i) { m.insert(make_pair(i, i)); }
|
|
|
|
m.rehash(nkeys);
|
|
|
|
for (int i = 0; i < nkeys; ++i) { if (m.find(i) == m.end()) return false; }
|
|
|
|
for (int i = nkeys; i < nkeys * 2; ++i) {
|
|
|
|
if (m.find(i) != m.end()) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
static bool erase_iterator() {
|
|
|
|
map_type<int64_t, int64_t> m;
|
|
|
|
int nkeys = 10 * 1000;
|
|
|
|
for (int i = 0; i < nkeys; ++i) { m.insert(make_pair(i, i)); }
|
2012-06-09 06:58:52 +03:00
|
|
|
for (int i = 0; i < nkeys; ++i) {
|
|
|
|
if (m.find(i) == m.end()) return false;
|
|
|
|
}
|
|
|
|
for (int i = nkeys - 1; i >= 0; --i) { if (m.find(i) == m.end()) return false; }
|
|
|
|
for (int i = nkeys - 1; i >= 0; --i) {
|
|
|
|
fail_unless(m.find(i) != m.end(), "after erase %d cannot be found", i);
|
|
|
|
fail_unless(m.find(i)->first == i, "after erase key %d cannot be found", i);
|
|
|
|
}
|
|
|
|
for (int i = nkeys - 1; i >= 0; --i) {
|
|
|
|
fail_unless(m.find(i) != m.end(), "after erase %d cannot be found", i);
|
|
|
|
fail_unless(m.find(i)->first == i, "after erase key %d cannot be found", i);
|
|
|
|
if (!(m.find(i)->first == i)) return false;
|
2012-06-03 04:29:46 +03:00
|
|
|
m.erase(m.find(i));
|
|
|
|
if (static_cast<int>(m.size()) != i) return false;
|
|
|
|
}
|
|
|
|
return true;
|
2012-06-09 08:27:39 +03:00
|
|
|
}
|
2012-06-03 04:29:46 +03:00
|
|
|
static bool erase_value() {
|
|
|
|
map_type<int64_t, int64_t> m;
|
|
|
|
int nkeys = 10 * 1000;
|
|
|
|
for (int i = 0; i < nkeys; ++i) { m.insert(make_pair(i, i)); }
|
2012-06-09 06:58:52 +03:00
|
|
|
for (int i = nkeys - 1; i >= 0; --i) {
|
|
|
|
fail_unless(m.find(i) != m.end());
|
2012-06-03 04:29:46 +03:00
|
|
|
m.erase(i);
|
|
|
|
if (static_cast<int>(m.size()) != i) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-05-28 07:45:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace cxxxmph
|
|
|
|
|
|
|
|
#endif // __CXXMPH_MAP_TEST_HELPER_H__
|