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 =
  24. A2_TEST_OUT_DIR "/aria2_FallocFileAllocationIteratorTest_testAllocate";
  25. std::ofstream of(fn.c_str(), std::ios::binary);
  26. of << "0123456789";
  27. of.close();
  28. File f(fn);
  29. CPPUNIT_ASSERT_EQUAL((int64_t)10, f.size());
  30. DefaultDiskWriter writer(fn);
  31. int64_t offset = 10;
  32. int64_t totalLength = 40_k;
  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((int64_t)40_k, f.size());
  39. #endif // !HAVE_FALLOCATE
  40. }
  41. } // namespace aria2