libmoost
/home/mhx/git/github/libmoost/test/io/map_store.cpp
Go to the documentation of this file.
00001 /* vim:set ts=3 sw=3 sts=3 et: */
00028 #include <boost/test/unit_test.hpp>
00029 #include <boost/test/test_tools.hpp>
00030 
00031 #include <vector>
00032 #include <fstream>
00033 
00034 #include <boost/filesystem/path.hpp>
00035 #include <boost/filesystem/operations.hpp>
00036 #include <boost/archive/binary_iarchive.hpp>
00037 #include <boost/archive/binary_oarchive.hpp>
00038 
00039 #include "../../include/moost/io/map_store.hpp"
00040 
00041 using namespace boost::filesystem;
00042 using namespace boost::archive;
00043 using namespace moost::io;
00044 
00045 BOOST_AUTO_TEST_SUITE( map_store_test )
00046 
00047 struct Fixture
00048 {
00049   struct TempDirectory
00050   {
00051     path Path;
00052     TempDirectory(const std::string & path_name) : Path(path_name) { create_directory(Path); }
00053     ~TempDirectory() { remove_all(Path); }
00054   };
00055   TempDirectory temp_dir;
00056   TempDirectory temp_dir2;
00057   map_store<std::string> ms;
00058 
00059   Fixture()
00060   : temp_dir("map_store_test"),
00061     temp_dir2("map_store_test2"),
00062     ms("map_store_test", 128, 1024)
00063   {
00064     ms.set_deleted_key("");
00065   }
00066   ~Fixture()
00067   {
00068   }
00069 };
00070 
00071 // can we alloc?
00072 BOOST_FIXTURE_TEST_CASE( test_alloc, Fixture )
00073 {
00074   map_store<std::string>::scoped_block block(ms, "hello", 64);
00075   BOOST_REQUIRE_EQUAL(block.index(), 0);
00076 }
00077 
00078 // can we write/read?
00079 BOOST_FIXTURE_TEST_CASE( test_readwrite, Fixture )
00080 {
00081   std::string in = "hello how are you";
00082   std::string out;
00083   {
00084     map_store<std::string>::scoped_block block(ms, "the key", 64);
00085     binary_oarchive(*block) << in;
00086   }
00087   {
00088     map_store<std::string>::scoped_block block(ms, "the key");
00089     binary_iarchive(*block) >> out;
00090   }
00091   BOOST_CHECK_EQUAL(in, out);
00092 }
00093 
00094 // what happens when we try to realloc?
00095 BOOST_FIXTURE_TEST_CASE( test_realloc, Fixture )
00096 {
00097   map_store<std::string>::scoped_block block(ms, "the key", 64);
00098   BOOST_CHECK_THROW(
00099     map_store<std::string>::scoped_block(ms, "the key", 64);,
00100     std::invalid_argument
00101   );
00102 }
00103 
00104 // okay, are we safe if we delete?
00105 BOOST_FIXTURE_TEST_CASE( test_free, Fixture )
00106 {
00107   {
00108     map_store<std::string>::scoped_block block(ms, "the key", 64);
00109     block.free();
00110   }
00111   {
00112     map_store<std::string>::scoped_block block(ms, "the key", 64);
00113     BOOST_CHECK_EQUAL(block.index(), 0);
00114   }
00115 }
00116 
00117 // can we find stuff if we open and close the map_store ?
00118 BOOST_FIXTURE_TEST_CASE( test_open_close, Fixture )
00119 {
00120   std::string in1 = "hello how are you";
00121   std::string in2 = "very well thanks";
00122   std::string out1, out2;
00123   {
00124     map_store<std::string> ms1("map_store_test2");
00125     map_store<std::string>::scoped_block block1(ms1, "the first key", 128);
00126     binary_oarchive(*block1) << in1;
00127     map_store<std::string>::scoped_block block2(ms1, "the second key", 256);
00128     binary_oarchive(*block2) << in2;
00129   }
00130   {
00131     map_store<std::string> ms2("map_store_test2");
00132     map_store<std::string>::scoped_block block1(ms2, "the first key");
00133     binary_iarchive(*block1) >> out1;
00134     map_store<std::string>::scoped_block block2(ms2, "the second key");
00135     binary_iarchive(*block2) >> out2;
00136   }
00137   BOOST_CHECK_EQUAL(in1, out1);
00138   BOOST_CHECK_EQUAL(in2, out2);
00139 }
00140 
00141 BOOST_AUTO_TEST_SUITE_END()