MultiFileAllocationIteratorTest.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "MultiFileAllocationIterator.h"
  2. #include "File.h"
  3. #include "MultiDiskAdaptor.h"
  4. #include "FileEntry.h"
  5. #include "Exception.h"
  6. #include <algorithm>
  7. #include <iostream>
  8. #include <cppunit/extensions/HelperMacros.h>
  9. namespace aria2 {
  10. class MultiFileAllocationIteratorTest:public CppUnit::TestFixture {
  11. CPPUNIT_TEST_SUITE(MultiFileAllocationIteratorTest);
  12. CPPUNIT_TEST(testAllocate);
  13. CPPUNIT_TEST(testMakeDiskWriterEntries);
  14. CPPUNIT_TEST_SUITE_END();
  15. private:
  16. public:
  17. void setUp() {}
  18. void testAllocate();
  19. void testMakeDiskWriterEntries();
  20. };
  21. CPPUNIT_TEST_SUITE_REGISTRATION( MultiFileAllocationIteratorTest );
  22. void MultiFileAllocationIteratorTest::testMakeDiskWriterEntries()
  23. {
  24. SharedHandle<FileEntry> fs[] = {
  25. SharedHandle<FileEntry>(new FileEntry("file1", 1536, 0)),
  26. SharedHandle<FileEntry>(new FileEntry("file2", 2048, 1536)),
  27. SharedHandle<FileEntry>(new FileEntry("file3", 1024, 3584)),
  28. SharedHandle<FileEntry>(new FileEntry("file4", 1024, 4608)),
  29. SharedHandle<FileEntry>(new FileEntry("file5", 1024, 5632)),
  30. SharedHandle<FileEntry>(new FileEntry("file6", 1024, 6656)),
  31. SharedHandle<FileEntry>(new FileEntry("file7", 256, 7680)),
  32. SharedHandle<FileEntry>(new FileEntry("file8", 768, 7936)),
  33. SharedHandle<FileEntry>(new FileEntry("file9", 256, 8704)),
  34. SharedHandle<FileEntry>(new FileEntry("fileA", 256, 8960)),
  35. };
  36. fs[1]->setRequested(false);
  37. fs[3]->setRequested(false);
  38. fs[4]->setRequested(false);
  39. fs[5]->setRequested(false);
  40. fs[6]->setRequested(false);
  41. fs[8]->setRequested(false);
  42. fs[9]->setRequested(false);
  43. std::string storeDir = "/tmp/aria2_MultiFileAllocationIteratorTest_testMakeDiskWriterEntries";
  44. SharedHandle<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor());
  45. diskAdaptor->setFileEntries(std::deque<SharedHandle<FileEntry> >(&fs[0], &fs[10]));
  46. diskAdaptor->setPieceLength(1024);
  47. diskAdaptor->setStoreDir(storeDir);
  48. diskAdaptor->openFile();
  49. SharedHandle<MultiFileAllocationIterator> itr
  50. (dynamic_pointer_cast<MultiFileAllocationIterator>(diskAdaptor->fileAllocationIterator()));
  51. DiskWriterEntries entries = itr->getDiskWriterEntries();
  52. std::sort(entries.begin(), entries.end());
  53. CPPUNIT_ASSERT_EQUAL((size_t)6, entries.size());
  54. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file1"), entries[0]->getFilePath(storeDir));
  55. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file2"), entries[1]->getFilePath(storeDir));
  56. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file3"), entries[2]->getFilePath(storeDir));
  57. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file6"), entries[3]->getFilePath(storeDir));
  58. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file7"), entries[4]->getFilePath(storeDir));
  59. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file8"), entries[5]->getFilePath(storeDir));
  60. }
  61. void MultiFileAllocationIteratorTest::testAllocate()
  62. {
  63. std::string dir = "/tmp";
  64. std::string topDir = "aria2_MultiFileAllocationIteratorTest_testAllocate";
  65. std::string fname1 = "file1";
  66. std::string fname2 = "file2";
  67. std::string fname3 = "file3";
  68. std::string fname4 = "file4";
  69. std::string fname5 = "file5";
  70. std::string fname6 = "file6";
  71. int64_t length1 = 32769;
  72. int64_t length2 = 0;
  73. int64_t length3 = 8;
  74. int64_t length4 = 10;
  75. int64_t length5 = 20;
  76. int64_t length6 = 30;
  77. try {
  78. SharedHandle<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor());
  79. diskAdaptor->setStoreDir(dir);
  80. diskAdaptor->setTopDir(topDir);
  81. int64_t offset = 0;
  82. SharedHandle<FileEntry> fileEntry1(new FileEntry(fname1,
  83. length1,
  84. offset));
  85. offset += length1;
  86. SharedHandle<FileEntry> fileEntry2(new FileEntry(fname2,
  87. length2,
  88. offset));
  89. offset += length2;
  90. SharedHandle<FileEntry> fileEntry3(new FileEntry(fname3,
  91. length3,
  92. offset));
  93. offset += length3;
  94. SharedHandle<FileEntry> fileEntry4(new FileEntry(fname4,
  95. length4,
  96. offset));
  97. fileEntry4->setRequested(false);
  98. offset += length4;
  99. SharedHandle<FileEntry> fileEntry5(new FileEntry(fname5,
  100. length5,
  101. offset));
  102. offset += length5;
  103. SharedHandle<FileEntry> fileEntry6(new FileEntry(fname6,
  104. length6,
  105. offset));
  106. fileEntry6->setRequested(false);
  107. std::deque<SharedHandle<FileEntry> > fs;
  108. fs.push_back(fileEntry1);
  109. fs.push_back(fileEntry2);
  110. fs.push_back(fileEntry3);
  111. fs.push_back(fileEntry4);
  112. fs.push_back(fileEntry5);
  113. fs.push_back(fileEntry6);
  114. diskAdaptor->setFileEntries(fs);
  115. // we have to open file first.
  116. diskAdaptor->initAndOpenFile();
  117. SharedHandle<MultiFileAllocationIterator> itr
  118. (dynamic_pointer_cast<MultiFileAllocationIterator>(diskAdaptor->fileAllocationIterator()));
  119. while(!itr->finished()) {
  120. itr->allocateChunk();
  121. }
  122. CPPUNIT_ASSERT_EQUAL((uint64_t)length1, File(dir+"/"+topDir+"/"+fname1).size());
  123. CPPUNIT_ASSERT_EQUAL((uint64_t)length2, File(dir+"/"+topDir+"/"+fname2).size());
  124. CPPUNIT_ASSERT_EQUAL((uint64_t)length3, File(dir+"/"+topDir+"/"+fname3).size());
  125. CPPUNIT_ASSERT_EQUAL(0ULL, File(dir+"/"+topDir+"/"+fname4).size());
  126. CPPUNIT_ASSERT_EQUAL((uint64_t)length5, File(dir+"/"+topDir+"/"+fname5).size());
  127. CPPUNIT_ASSERT_EQUAL(0ULL, File(dir+"/"+topDir+"/"+fname6).size());
  128. } catch(Exception& e) {
  129. CPPUNIT_FAIL(e.stackTrace());
  130. }
  131. }
  132. } // namespace aria2