FallocFileAllocationIteratorTest.cc 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 fn = A2_TEST_OUT_DIR"/aria2_FallocFileAllocationIteratorTest_testAllocate";
  23. std::ofstream of(fn.c_str(), std::ios::binary);
  24. of << "0123456789";
  25. of.close();
  26. File f(fn);
  27. CPPUNIT_ASSERT_EQUAL((int64_t)10, f.size());
  28. DefaultDiskWriter writer(fn);
  29. int64_t offset = 10;
  30. int64_t totalLength = 40960;
  31. // we have to open file first.
  32. writer.openExistingFile();
  33. FallocFileAllocationIterator itr(&writer, offset, totalLength);
  34. itr.allocateChunk();
  35. CPPUNIT_ASSERT(itr.finished());
  36. CPPUNIT_ASSERT_EQUAL((int64_t)40960, f.size());
  37. #endif // !HAVE_FALLOCATE
  38. }
  39. } // namespace aria2