turbonss/cxxmph/map_tester.h

89 lines
2.6 KiB
C
Raw Normal View History

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-05-28 07:45:12 +03:00
namespace cxxmph {
using namespace std;
2012-06-01 23:49:00 +03:00
template <template<typename...> class map_type>
2012-05-28 07:45:12 +03:00
class MapTester {
public:
2012-06-01 23:49:00 +03:00
MapTester() {}
~MapTester() {}
2012-05-28 07:45:12 +03:00
bool Run(string* errors) const {
string e;
if (!small_insert()) e += "small insert failed\n";
if (!large_insert()) e += "large insert failed\n";
if (!small_search()) e += "small search failed\n";
if (!default_search()) e += "default search failed\n";
if (!large_search()) e += "large search failed\n";
if (errors) *errors = e;
return !e.empty();
}
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
for (int i = 1; i < 12 * 256 * 256; ++i) m.insert(make_pair(i, i));
return m.size() == 12 * 256 * 256 - 1;
}
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) {
char buf[128];
2012-06-01 23:49:00 +03:00
cxxmph::format("%v", i);
2012-05-28 07:45:12 +03:00
keys.push_back(buf);
}
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;
}
};
} // namespace cxxxmph
#endif // __CXXMPH_MAP_TEST_HELPER_H__