00001 #include <boost/test/unit_test.hpp> 00002 #include <boost/filesystem/fstream.hpp> 00003 #include "MPHashFunction.hpp" 00004 #include "Vocabulary.hpp" 00005 #include "Converter.hpp" 00006 #include <vector> 00007 00008 using namespace aitools::invertedindex; 00009 00010 BOOST_AUTO_TEST_SUITE(mp_hash_function); 00011 00012 BOOST_AUTO_TEST_CASE(default_ctor) 00013 { 00014 MPHashFunction mphf; 00015 BOOST_CHECK_EQUAL(mphf.size(), 0); 00016 BOOST_CHECK_EQUAL(mphf.hash("key"), 0); 00017 } 00018 00019 BOOST_AUTO_TEST_CASE(create_a) 00020 { 00021 bfs::path vocabulary("aitools.invertedindex.vocabulary.txt"); 00022 bfs::ofstream ofs(vocabulary); 00023 unsigned key_count(100); 00024 for (unsigned i(0); i != key_count; ++i) 00025 { 00026 ofs << "key" << i << '\n'; 00027 } 00028 ofs.close(); 00029 00030 uint32_t hash; 00031 MPHashFunction mphf; 00032 mphf.create(vocabulary, MPHashFunction::BDZ); 00033 BOOST_CHECK_EQUAL(mphf.size(), key_count); 00034 std::vector<bool> slots(key_count, true); 00035 for (unsigned i(0); i != key_count; ++i) 00036 { 00037 hash = mphf.hash("key" + Converter::ui32_to_str(i)); 00038 BOOST_CHECK(slots.at(hash)); 00039 slots.at(hash) = false; 00040 } 00041 bfs::remove(vocabulary); 00042 } 00043 00044 BOOST_AUTO_TEST_CASE(create_b) 00045 { 00046 Vocabulary vocabulary; 00047 unsigned key_count(100); 00048 for (unsigned i(0); i != key_count; ++i) 00049 { 00050 vocabulary.insert("key" + Converter::ui32_to_str(i)); 00051 } 00052 00053 uint32_t hash; 00054 MPHashFunction mphf; 00055 mphf.create(vocabulary, MPHashFunction::BDZ); 00056 BOOST_CHECK_EQUAL(mphf.size(), key_count); 00057 std::vector<bool> slots(key_count, true); 00058 for (unsigned i(0); i != key_count; ++i) 00059 { 00060 hash = mphf.hash("key" + Converter::ui32_to_str(i)); 00061 BOOST_CHECK(slots.at(hash)); 00062 slots.at(hash) = false; 00063 } 00064 } 00065 00066 BOOST_AUTO_TEST_CASE(save_load) 00067 { 00068 Vocabulary vocabulary; 00069 unsigned key_count(100); 00070 for (unsigned i(0); i != key_count; ++i) 00071 { 00072 vocabulary.insert("key" + Converter::ui32_to_str(i)); 00073 } 00074 00075 uint32_t hash; 00076 MPHashFunction mphf; 00077 mphf.create(vocabulary, MPHashFunction::BDZ); 00078 00079 bfs::path invalid_path("/directory/does/not/exist"); 00080 BOOST_CHECK_THROW(mphf.save(invalid_path), std::invalid_argument); 00081 bfs::path path("aitools.invertedindex.mphf"); 00082 mphf.save(path); 00083 00084 mphf = MPHashFunction(); 00085 BOOST_CHECK_THROW(mphf.load(invalid_path), std::invalid_argument); 00086 mphf.load(path); 00087 BOOST_CHECK_EQUAL(mphf.size(), key_count); 00088 std::vector<bool> slots(key_count, true); 00089 for (unsigned i(0); i != key_count; ++i) 00090 { 00091 hash = mphf.hash("key" + Converter::ui32_to_str(i)); 00092 BOOST_CHECK(slots.at(hash)); 00093 slots.at(hash) = false; 00094 } 00095 bfs::remove(path); 00096 } 00097 00098 BOOST_AUTO_TEST_SUITE_END();