FallocFileAllocationIteratorTest.cc 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "FallocFileAllocationIterator.h"
  2. #include <fstream>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. #include "File.h"
  5. #include "DefaultDiskWriter.h"
  6. namespace aria2 {
  7. class FallocFileAllocationIteratorTest:public CppUnit::TestFixture {
  8. CPPUNIT_TEST_SUITE(FallocFileAllocationIteratorTest);
  9. CPPUNIT_TEST(testAllocate);
  10. CPPUNIT_TEST_SUITE_END();
  11. private:
  12. public:
  13. void setUp() {}
  14. void testAllocate();
  15. };
  16. CPPUNIT_TEST_SUITE_REGISTRATION( FallocFileAllocationIteratorTest );
  17. void FallocFileAllocationIteratorTest::testAllocate()
  18. {
  19. // When fallocate is used, test fails if file system does not
  20. // support it. So skip it.
  21. #ifndef HAVE_FALLOCATE
  22. std::string dir = "./";
  23. std::string fname = "aria2_FallocFileAllocationIteratorTest_testAllocate";
  24. std::string fn = dir+"/"+fname;
  25. std::ofstream of(fn.c_str(), std::ios::binary);
  26. of << "0123456789";
  27. of.close();
  28. File f(fn);
  29. CPPUNIT_ASSERT_EQUAL((uint64_t)10, f.size());
  30. DefaultDiskWriter writer(fn);
  31. int64_t offset = 10;
  32. int64_t totalLength = 40960;
  33. // we have to open file first.
  34. writer.openExistingFile();
  35. FallocFileAllocationIterator itr(&writer, offset, totalLength);
  36. itr.allocateChunk();
  37. CPPUNIT_ASSERT(itr.finished());
  38. CPPUNIT_ASSERT_EQUAL((uint64_t)40960, f.size());
  39. #endif // !HAVE_FALLOCATE
  40. }
  41. } // namespace aria2