00001
00002
00003 #include <string>
00004 #include <ostream>
00005 #include <utility>
00006 #include <boost/test/unit_test.hpp>
00007 #include <boost/type_traits.hpp>
00008
00009 #include "generator.hpp"
00010 #include "core/builder.hpp"
00011 #include "core/big_hash_map.hpp"
00012 #include "util/conversion.hpp"
00013
00014 using namespace aitools;
00015
00016 template <typename T>
00017 struct record_generator
00018 {
00019 typedef std::pair<std::string, T> record_type;
00020
00021 record_generator()
00022 : counter(0)
00023 {}
00024
00025 const record_type operator()()
00026 {
00027 record_type record;
00028 record.first.assign("key");
00029 record.first += util::to_string(counter++);
00030 aitools::generator<T>::randomized(record.second);
00031 return record;
00032 }
00033
00034 unsigned counter;
00035 };
00036
00037 namespace std {
00038
00039 template <typename T>
00040 ostream&
00041 operator<<(ostream& os, const std::pair<std::string, T>& record)
00042 {
00043 if (os)
00044 {
00045 os << record.first << '\t';
00046 value::value_traits<T>::print_to(record.second, os);
00047 }
00048 return os;
00049 }
00050 }
00051
00052 template <typename T>
00053 void
00054 test(aitools::memory_type mode, size_t record_count)
00055 {
00056 typedef std::pair<std::string, T> record_type;
00057 typedef bighashmap::core::big_hash_map<T> hashmap_type;
00058
00059
00060
00061
00062 std::vector<record_type> records(record_count);
00063 std::generate(records.begin(), records.end(), record_generator<T>());
00064 const bfs::path input_file("bhm_test_input/records");
00065 const bfs::path output_dir("bhm_test_output");
00066 BOOST_REQUIRE(bfs::create_directory(input_file.parent_path()));
00067 BOOST_REQUIRE(bfs::create_directory(output_dir));
00068 bfs::ofstream ofs(input_file);
00069 BOOST_REQUIRE(ofs);
00070
00071 ofs.setf(std::ios::fixed, std::ios::floatfield);
00072 std::copy(records.begin(), records.end(),
00073 std::ostream_iterator<record_type>(ofs, "\n"));
00074 ofs.close();
00075
00076
00077
00078 const bfs::path idx_file(bighashmap::core::builder<T>::build(
00079 input_file.parent_path(), output_dir));
00080
00081
00082
00083 typename hashmap_type::value_type value;
00084 hashmap_type* bhm(hashmap_type::open(idx_file, mode));
00085 for (auto it(records.begin()); it != records.end(); ++it)
00086 {
00087 BOOST_REQUIRE(bhm->find(it->first, value));
00088 BOOST_REQUIRE_EQUAL(it->second, value);
00089 }
00090
00091
00092
00093 for (unsigned i(0); i != record_count; ++i)
00094 {
00095 BOOST_REQUIRE(!bhm->find(util::to_string(std::rand()), value));
00096 }
00097 delete bhm;
00098 bfs::remove_all(output_dir);
00099 bfs::remove_all(input_file.parent_path());
00100 }
00101
00102
00103
00104 BOOST_AUTO_TEST_SUITE(test_big_hash_map);
00105
00106 static const size_t k_record_count(100000);
00107
00108 BOOST_AUTO_TEST_CASE(test_i32_external)
00109 {
00110 test<int32_t>(aitools::memory_type::min_required, k_record_count);
00111 }
00112
00113 BOOST_AUTO_TEST_CASE(test_i32_internal)
00114 {
00115 test<int32_t>(aitools::memory_type::mb4096, k_record_count);
00116 }
00117
00118 BOOST_AUTO_TEST_CASE(test_i64_external)
00119 {
00120 test<int64_t>(aitools::memory_type::min_required, k_record_count);
00121 }
00122
00123 BOOST_AUTO_TEST_CASE(test_i64_internal)
00124 {
00125 test<int64_t>(aitools::memory_type::mb4096, k_record_count);
00126 }
00127
00128 BOOST_AUTO_TEST_CASE(test_dbl_external)
00129 {
00130 test<double>(aitools::memory_type::min_required, k_record_count);
00131 }
00132
00133 BOOST_AUTO_TEST_CASE(test_dbl_internal)
00134 {
00135 test<double>(aitools::memory_type::mb4096, k_record_count);
00136 }
00137
00138 BOOST_AUTO_TEST_CASE(test_i32_i64_external)
00139 {
00140 typedef value::pair<int32_t, int64_t> value_type;
00141 test<value_type>(aitools::memory_type::min_required, k_record_count);
00142 }
00143
00144 BOOST_AUTO_TEST_CASE(test_i32_i64_internal)
00145 {
00146 typedef value::pair<int32_t, int64_t> value_type;
00147 test<value_type>(aitools::memory_type::mb4096, k_record_count);
00148 }
00149
00150 BOOST_AUTO_TEST_CASE(test_i32_dbl_external)
00151 {
00152 typedef value::pair<int32_t, double> value_type;
00153 test<value_type>(aitools::memory_type::min_required, k_record_count);
00154 }
00155
00156 BOOST_AUTO_TEST_CASE(test_i32_dbl_internal)
00157 {
00158 typedef value::pair<int32_t, double> value_type;
00159 test<value_type>(aitools::memory_type::mb4096, k_record_count);
00160 }
00161
00162 BOOST_AUTO_TEST_CASE(test_i64_dbl_external)
00163 {
00164 typedef value::pair<int64_t, double> value_type;
00165 test<value_type>(aitools::memory_type::min_required, k_record_count);
00166 }
00167
00168 BOOST_AUTO_TEST_CASE(test_i64_dbl_internal)
00169 {
00170 typedef value::pair<int64_t, double> value_type;
00171 test<value_type>(aitools::memory_type::mb4096, k_record_count);
00172 }
00173
00174
00175 BOOST_AUTO_TEST_CASE(test_i32_dbl_external_large_scale)
00176 {
00177 typedef value::pair<int32_t, double> value_type;
00178 test<value_type>(aitools::memory_type::min_required, 10000000);
00179 }
00180
00181
00182 BOOST_AUTO_TEST_CASE(test_i32_dbl_internal_large_scale)
00183 {
00184 typedef value::pair<int32_t, double> value_type;
00185 test<value_type>(aitools::memory_type::mb4096, 10000000);
00186 }
00187
00188 BOOST_AUTO_TEST_SUITE_END();