SingleFileAllocationIteratorTest.cc 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "SingleFileAllocationIterator.h"
  2. #include "File.h"
  3. #include "DefaultDiskWriter.h"
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <fstream>
  8. #include <iomanip>
  9. #include <cppunit/extensions/HelperMacros.h>
  10. class SingleFileAllocationIteratorTest:public CppUnit::TestFixture {
  11. CPPUNIT_TEST_SUITE(SingleFileAllocationIteratorTest);
  12. CPPUNIT_TEST(testAllocate);
  13. CPPUNIT_TEST_SUITE_END();
  14. private:
  15. public:
  16. void setUp() {}
  17. void testAllocate();
  18. };
  19. CPPUNIT_TEST_SUITE_REGISTRATION( SingleFileAllocationIteratorTest );
  20. void SingleFileAllocationIteratorTest::testAllocate()
  21. {
  22. string dir = "/tmp";
  23. string fname = "aria2_SingleFileAllocationIteratorTest_testAllocate";
  24. string fn = dir+"/"+fname;
  25. ofstream of(fn.c_str());
  26. of << "0123456789";
  27. of.close();
  28. File x(fn);
  29. CPPUNIT_ASSERT_EQUAL((int64_t)10, x.size());
  30. DefaultDiskWriter writer;
  31. int64_t offset = 10;
  32. int64_t totalLength = 16*1024*2+8*1024;
  33. // we have to open file first.
  34. writer.openExistingFile(fn);
  35. SingleFileAllocationIterator itr(&writer, offset, totalLength);
  36. itr.init();
  37. while(!itr.finished()) {
  38. itr.allocateChunk();
  39. }
  40. File f(fn);
  41. CPPUNIT_ASSERT_EQUAL((int64_t)40960, f.size());
  42. }