libmoost
/home/mhx/git/github/libmoost/test/io/variable_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/variable_store.hpp"
00040 
00041 using namespace boost::filesystem;
00042 using namespace boost::archive;
00043 using namespace moost::io;
00044 
00045 BOOST_AUTO_TEST_SUITE( variable_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   variable_store vs;
00057 
00058   Fixture()
00059   : temp_dir("variable_store_test"),
00060     vs("variable_store_test", 128, 1024)
00061   {
00062   }
00063   ~Fixture()
00064   {
00065   }
00066 };
00067 
00068 // can we alloc?
00069 BOOST_FIXTURE_TEST_CASE( test_alloc, Fixture )
00070 {
00071   variable_store::scoped_block block(vs, 64);
00072   BOOST_REQUIRE_EQUAL(block.index(), 0);
00073   variable_store::scoped_block block2(vs, 100);
00074   BOOST_REQUIRE_EQUAL(block2.index(), 1);
00075 }
00076 
00077 // can we write/read?
00078 BOOST_FIXTURE_TEST_CASE( test_readwrite, Fixture )
00079 {
00080   std::string in = "hello how are you";
00081   std::string out;
00082   {
00083     variable_store::scoped_block block(vs, 64);
00084     binary_oarchive(*block) << in;
00085   }
00086   {
00087     variable_store::scoped_block block(vs, 64, 0);
00088     binary_iarchive(*block) >> out;
00089   }
00090   BOOST_CHECK_EQUAL(in, out);
00091 }
00092 
00093 // what happens if we try to get too big a block?
00094 BOOST_FIXTURE_TEST_CASE( test_block_too_big, Fixture )
00095 {
00096   variable_store::scoped_block block(vs, 1024); // this is okay
00097   // but this is too big
00098   BOOST_CHECK_THROW(
00099     variable_store::scoped_block(vs, 1025),
00100     std::invalid_argument
00101   );
00102 }
00103 
00104 // can we write to two different blocks stores?
00105 BOOST_FIXTURE_TEST_CASE( test_different_block_stores, Fixture )
00106 {
00107   std::string in1 = "hello how are you";
00108   std::string in2 = "very well thanks";
00109   std::string out1, out2;
00110   {
00111     variable_store::scoped_block block1(vs, 64);
00112     BOOST_CHECK_EQUAL(block1.index(), 0);
00113     binary_oarchive(*block1) << in1;
00114     variable_store::scoped_block block2(vs, 129);
00115     BOOST_CHECK_EQUAL(block2.index(), 0);
00116     binary_oarchive(*block2) << in2;
00117   }
00118   {
00119     variable_store::scoped_block block1(vs, 64, 0);
00120     binary_iarchive(*block1) >> out1;
00121     variable_store::scoped_block block2(vs, 129, 0);
00122     binary_iarchive(*block2) >> out2;
00123   }
00124   BOOST_CHECK_EQUAL(in1, out1);
00125   BOOST_CHECK_EQUAL(in2, out2);
00126 }
00127 
00128 BOOST_AUTO_TEST_SUITE_END()