libmoost
|
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/block_store.hpp" 00040 00041 using namespace boost::filesystem; 00042 using namespace boost::archive; 00043 using namespace moost::io; 00044 00045 BOOST_AUTO_TEST_SUITE( block_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 block_store bs; 00057 00058 Fixture() 00059 : temp_dir("block_store_test"), 00060 bs("block_store_test/128", 128) 00061 { 00062 } 00063 ~Fixture() 00064 { 00065 } 00066 }; 00067 00068 // can we alloc? 00069 BOOST_FIXTURE_TEST_CASE( test_alloc, Fixture ) 00070 { 00071 block_store::scoped_block block(bs); 00072 BOOST_REQUIRE_EQUAL(block.index(), 0); 00073 block_store::scoped_block block2(bs); 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 block_store::scoped_block block(bs); 00084 binary_oarchive(*block) << in; 00085 } 00086 { 00087 block_store::scoped_block block(bs, 0); 00088 binary_iarchive(*block) >> out; 00089 } 00090 BOOST_CHECK_EQUAL(in, out); 00091 } 00092 00093 // can we write/read past the beginning of file? 00094 BOOST_FIXTURE_TEST_CASE( test_readwrite_pbof, Fixture ) 00095 { 00096 std::string in = "hello how are you"; 00097 std::string out; 00098 { 00099 block_store::scoped_block block(bs, 1); 00100 binary_oarchive(*block) << in; 00101 } 00102 { 00103 block_store::scoped_block block(bs, 1); 00104 binary_iarchive(*block) >> out; 00105 } 00106 BOOST_CHECK_EQUAL(in, out); 00107 } 00108 00109 // can we free, and does it happen at the right point? 00110 BOOST_FIXTURE_TEST_CASE( test_free, Fixture ) 00111 { 00112 { 00113 block_store::scoped_block block(bs); 00114 block.free(); 00115 block_store::scoped_block block2(bs); 00116 BOOST_CHECK_EQUAL(block2.index(), 1); 00117 } 00118 block_store::scoped_block block3(bs); 00119 BOOST_CHECK_EQUAL(block3.index(), 0); 00120 } 00121 00122 // can we close a block store, then open it again and see that the end has been truncated 00123 BOOST_FIXTURE_TEST_CASE( test_save_load_end, Fixture ) 00124 { 00125 { 00126 block_store bs2("block_store_test/128", 128); 00127 block_store::scoped_block block(bs2); 00128 block_store::scoped_block block2(bs2); 00129 block2.free(); 00130 } 00131 block_store bs3("block_store_test/128", 128); 00132 block_store::scoped_block block3(bs3); 00133 BOOST_CHECK_EQUAL(block3.index(), 1); 00134 } 00135 00136 // can we close a block store, then open it again and see that the free list has been maintained 00137 BOOST_FIXTURE_TEST_CASE( test_save_load_freelist, Fixture ) 00138 { 00139 { 00140 block_store bs2("block_store_test/128", 128); 00141 block_store::scoped_block block(bs2); 00142 block_store::scoped_block block2(bs2); 00143 block2.free(); 00144 block_store::scoped_block block3(bs2); 00145 } 00146 block_store bs3("block_store_test/128", 128); 00147 block_store::scoped_block block4(bs3); 00148 BOOST_CHECK_EQUAL(block4.index(), 1); 00149 } 00150 00151 BOOST_AUTO_TEST_SUITE_END()