FallocFileAllocationIteratorTest.cc 1.3 KB

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