libmoost
/home/mhx/git/github/libmoost/test/io/file_operations.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/cstdint.hpp>
00037 
00038 #include "../../include/moost/io/file_operations.hpp"
00039 
00040 using namespace boost::filesystem;
00041 using namespace moost::io;
00042 
00043 BOOST_AUTO_TEST_SUITE( file_operations_test )
00044 
00045 struct Fixture
00046 {
00047   struct TempDirectory
00048   {
00049     path Path;
00050     TempDirectory(const std::string & path_name) : Path(path_name) { create_directory(Path); }
00051     ~TempDirectory() { remove_all(Path); }
00052   };
00053   TempDirectory temp_dir;
00054 
00055   Fixture()
00056   : temp_dir("file_operations_test")
00057   {
00058   }
00059   ~Fixture()
00060   {
00061   }
00062 };
00063 
00064 // can we grow?
00065 BOOST_FIXTURE_TEST_CASE( test_grow_size, Fixture )
00066 {
00067   std::ofstream out("file_operations_test/blah.txt");
00068   out << "blah" << std::flush;
00069   file_operations::change_size("file_operations_test/blah.txt", 128);
00070   BOOST_CHECK_EQUAL(file_size(path("file_operations_test/blah.txt")), 128);
00071 }
00072 
00073 // can we grow past the 4 gb limit?
00074 BOOST_FIXTURE_TEST_CASE( test_grow_4gb, Fixture )
00075 {
00076   std::ofstream out("file_operations_test/blah.txt");
00077   out << "blah" << std::flush;
00078   file_operations::change_size("file_operations_test/blah.txt", UINT64_C(4294967396));
00079   BOOST_CHECK_EQUAL(file_size(path("file_operations_test/blah.txt")), UINT64_C(4294967396));
00080 }
00081 
00082 // can we shrink?
00083 BOOST_FIXTURE_TEST_CASE( test_shrink_size, Fixture )
00084 {
00085   std::ofstream out("file_operations_test/blah.txt");
00086   out << "blah blahblahblah blah" << std::flush;
00087   file_operations::change_size("file_operations_test/blah.txt", 4);
00088   BOOST_CHECK_EQUAL(file_size(path("file_operations_test/blah.txt")), 4);
00089 }
00090 
00091 // can we shrink to zero?
00092 BOOST_FIXTURE_TEST_CASE( test_shrink_zero, Fixture )
00093 {
00094   std::ofstream out("file_operations_test/blah.txt");
00095   out << "blah blahblahblah blah" << std::flush;
00096   file_operations::change_size("file_operations_test/blah.txt", 0);
00097   BOOST_CHECK_EQUAL(file_size(path("file_operations_test/blah.txt")), 0);
00098 }
00099 
00100 // what happens if we change something that doesn't exist?
00101 BOOST_FIXTURE_TEST_CASE( test_filenotfound, Fixture )
00102 {
00103   BOOST_CHECK_THROW(file_operations::change_size("file_operations_test/blah.txt", 128), std::runtime_error);
00104 }
00105 
00106 BOOST_AUTO_TEST_SUITE_END()