SingleFileAllocationIteratorTest.cc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "SingleFileAllocationIterator.h"
  2. #include "File.h"
  3. #include "DefaultDiskWriter.h"
  4. #include "DirectDiskAdaptor.h"
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <fstream>
  9. #include <iomanip>
  10. #include <cppunit/extensions/HelperMacros.h>
  11. class SingleFileAllocationIteratorTest:public CppUnit::TestFixture {
  12. CPPUNIT_TEST_SUITE(SingleFileAllocationIteratorTest);
  13. CPPUNIT_TEST(testAllocate);
  14. CPPUNIT_TEST_SUITE_END();
  15. private:
  16. public:
  17. void setUp() {}
  18. void testAllocate();
  19. };
  20. CPPUNIT_TEST_SUITE_REGISTRATION( SingleFileAllocationIteratorTest );
  21. void SingleFileAllocationIteratorTest::testAllocate()
  22. {
  23. string dir = "/tmp";
  24. string fname = "aria2_SingleFileAllocationIteratorTest_testAllocate";
  25. string fn = dir+"/"+fname;
  26. ofstream of(fn.c_str());
  27. of << "0123456789";
  28. of.close();
  29. File x(fn);
  30. CPPUNIT_ASSERT_EQUAL((int64_t)10, x.size());
  31. DefaultDiskWriterHandle writer = new DefaultDiskWriter();
  32. DirectDiskAdaptorHandle diskAdaptor = new DirectDiskAdaptor();
  33. diskAdaptor->setDiskWriter(writer);
  34. diskAdaptor->setTotalLength(16*1024*2+8*1024);
  35. diskAdaptor->setStoreDir(dir);
  36. FileEntryHandle fileEntry = new FileEntry(fname,
  37. diskAdaptor->getTotalLength(),
  38. 0);
  39. FileEntries fs;
  40. fs.push_back(fileEntry);
  41. diskAdaptor->setFileEntries(fs);
  42. // we have to open file first.
  43. diskAdaptor->openFile();
  44. SingleFileAllocationIteratorHandle itr = diskAdaptor->fileAllocationIterator();
  45. itr->allocateChunk();
  46. CPPUNIT_ASSERT(!itr->finished());
  47. itr->allocateChunk();
  48. CPPUNIT_ASSERT(!itr->finished());
  49. itr->allocateChunk();
  50. CPPUNIT_ASSERT(itr->finished());
  51. File f(fn);
  52. CPPUNIT_ASSERT_EQUAL((int64_t)40960, f.size());
  53. }