MultiFileAllocationIteratorTest.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "MultiFileAllocationIterator.h"
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <cppunit/extensions/HelperMacros.h>
  5. #include "File.h"
  6. #include "MultiDiskAdaptor.h"
  7. #include "FileEntry.h"
  8. #include "Exception.h"
  9. #include "array_fun.h"
  10. #include "TestUtil.h"
  11. #include "DiskWriter.h"
  12. namespace aria2 {
  13. class MultiFileAllocationIteratorTest:public CppUnit::TestFixture {
  14. CPPUNIT_TEST_SUITE(MultiFileAllocationIteratorTest);
  15. CPPUNIT_TEST(testAllocate);
  16. CPPUNIT_TEST(testMakeDiskWriterEntries);
  17. CPPUNIT_TEST_SUITE_END();
  18. private:
  19. public:
  20. void setUp() {}
  21. void testAllocate();
  22. void testMakeDiskWriterEntries();
  23. };
  24. CPPUNIT_TEST_SUITE_REGISTRATION( MultiFileAllocationIteratorTest );
  25. void MultiFileAllocationIteratorTest::testMakeDiskWriterEntries()
  26. {
  27. std::string storeDir = A2_TEST_OUT_DIR"/aria2_MultiFileAllocationIteratorTest"
  28. "_testMakeDiskWriterEntries";
  29. SharedHandle<FileEntry> fs[] = {
  30. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file1", 1536, 0)),
  31. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file2", 2048, 1536)),// req no
  32. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file3", 1024, 3584)),
  33. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file4", 1024, 4608)),// req no
  34. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file5", 1024, 5632)),// req no
  35. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file6", 1024, 6656)),// req no
  36. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file7", 256, 7680)),// req no
  37. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file8", 255, 7936)),
  38. SharedHandle<FileEntry>(new FileEntry(storeDir+"/file9", 1025, 8191)),// req no
  39. SharedHandle<FileEntry>(new FileEntry(storeDir+"/fileA", 1024, 9216)),// req no
  40. SharedHandle<FileEntry>(new FileEntry(storeDir+"/fileB", 1024, 10240)),
  41. };
  42. fs[1]->setRequested(false); // file2
  43. fs[3]->setRequested(false); // file4
  44. fs[4]->setRequested(false); // file5
  45. fs[5]->setRequested(false); // file6
  46. fs[6]->setRequested(false); // file7
  47. fs[8]->setRequested(false); // file9
  48. fs[9]->setRequested(false); // fileA
  49. // create empty file4
  50. createFile(storeDir+std::string("/file4"), 0);
  51. SharedHandle<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor());
  52. diskAdaptor->setFileEntries(vbegin(fs), vend(fs));
  53. diskAdaptor->setPieceLength(1024);
  54. diskAdaptor->openFile();
  55. SharedHandle<MultiFileAllocationIterator> itr
  56. (dynamic_pointer_cast<MultiFileAllocationIterator>
  57. (diskAdaptor->fileAllocationIterator()));
  58. const std::deque<SharedHandle<DiskWriterEntry> >& entries =
  59. itr->getDiskWriterEntries();
  60. CPPUNIT_ASSERT_EQUAL((size_t)11, entries.size());
  61. // file1
  62. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file1"),
  63. entries[0]->getFilePath());
  64. CPPUNIT_ASSERT(entries[0]->needsFileAllocation());
  65. CPPUNIT_ASSERT(entries[0]->getDiskWriter());
  66. // file2
  67. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file2"),
  68. entries[1]->getFilePath());
  69. CPPUNIT_ASSERT(entries[1]->needsFileAllocation());
  70. CPPUNIT_ASSERT(entries[1]->getDiskWriter());
  71. // file3
  72. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file3"),
  73. entries[2]->getFilePath());
  74. CPPUNIT_ASSERT(entries[2]->needsFileAllocation());
  75. CPPUNIT_ASSERT(entries[2]->getDiskWriter());
  76. // file4, diskWriter is not null, because file exists.
  77. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file4"),
  78. entries[3]->getFilePath());
  79. CPPUNIT_ASSERT(!entries[3]->needsFileAllocation());
  80. CPPUNIT_ASSERT(entries[3]->getDiskWriter());
  81. // file5
  82. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file5"),
  83. entries[4]->getFilePath());
  84. CPPUNIT_ASSERT(!entries[4]->needsFileAllocation());
  85. CPPUNIT_ASSERT(!entries[4]->getDiskWriter());
  86. // file6
  87. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file6"),
  88. entries[5]->getFilePath());
  89. CPPUNIT_ASSERT(entries[5]->needsFileAllocation());
  90. CPPUNIT_ASSERT(entries[5]->getDiskWriter());
  91. // file7
  92. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file7"),
  93. entries[6]->getFilePath());
  94. CPPUNIT_ASSERT(entries[6]->needsFileAllocation());
  95. CPPUNIT_ASSERT(entries[6]->getDiskWriter());
  96. // file8
  97. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file8"),
  98. entries[7]->getFilePath());
  99. CPPUNIT_ASSERT(entries[7]->needsFileAllocation());
  100. CPPUNIT_ASSERT(entries[7]->getDiskWriter());
  101. // file9
  102. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/file9"),
  103. entries[8]->getFilePath());
  104. CPPUNIT_ASSERT(!entries[8]->needsFileAllocation());
  105. CPPUNIT_ASSERT(entries[8]->getDiskWriter());
  106. // fileA
  107. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/fileA"),
  108. entries[9]->getFilePath());
  109. CPPUNIT_ASSERT(!entries[9]->needsFileAllocation());
  110. CPPUNIT_ASSERT(!entries[9]->getDiskWriter());
  111. // fileB
  112. CPPUNIT_ASSERT_EQUAL(storeDir+std::string("/fileB"),
  113. entries[10]->getFilePath());
  114. CPPUNIT_ASSERT(entries[10]->needsFileAllocation());
  115. CPPUNIT_ASSERT(entries[10]->getDiskWriter());
  116. }
  117. void MultiFileAllocationIteratorTest::testAllocate()
  118. {
  119. std::string storeDir =
  120. A2_TEST_OUT_DIR"/aria2_MultiFileAllocationIteratorTest_testAllocate";
  121. std::string fname1 = "file1";
  122. std::string fname2 = "file2";
  123. std::string fname3 = "file3";
  124. std::string fname4 = "file4";
  125. std::string fname5 = "file5";
  126. std::string fname6 = "file6";
  127. int64_t length1 = 32769;
  128. int64_t length2 = 0;
  129. int64_t length3 = 8;
  130. int64_t length4 = 10;
  131. int64_t length5 = 20;
  132. int64_t length6 = 30;
  133. try {
  134. SharedHandle<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor());
  135. diskAdaptor->setPieceLength(1);
  136. int64_t offset = 0;
  137. SharedHandle<FileEntry> fileEntry1(new FileEntry(storeDir+"/"+fname1,
  138. length1,
  139. offset));
  140. offset += length1;
  141. SharedHandle<FileEntry> fileEntry2(new FileEntry(storeDir+"/"+fname2,
  142. length2,
  143. offset));
  144. offset += length2;
  145. SharedHandle<FileEntry> fileEntry3(new FileEntry(storeDir+"/"+fname3,
  146. length3,
  147. offset));
  148. offset += length3;
  149. SharedHandle<FileEntry> fileEntry4(new FileEntry(storeDir+"/"+fname4,
  150. length4,
  151. offset));
  152. fileEntry4->setRequested(false);
  153. offset += length4;
  154. SharedHandle<FileEntry> fileEntry5(new FileEntry(storeDir+"/"+fname5,
  155. length5,
  156. offset));
  157. offset += length5;
  158. SharedHandle<FileEntry> fileEntry6(new FileEntry(storeDir+"/"+fname6,
  159. length6,
  160. offset));
  161. fileEntry6->setRequested(false);
  162. std::vector<SharedHandle<FileEntry> > fs;
  163. fs.push_back(fileEntry1);
  164. fs.push_back(fileEntry2);
  165. fs.push_back(fileEntry3);
  166. fs.push_back(fileEntry4);
  167. fs.push_back(fileEntry5);
  168. fs.push_back(fileEntry6);
  169. diskAdaptor->setFileEntries(fs.begin(), fs.end());
  170. for(std::vector<SharedHandle<FileEntry> >::const_iterator i = fs.begin();
  171. i != fs.end(); ++i) {
  172. File((*i)->getPath()).remove();
  173. }
  174. // we have to open file first.
  175. diskAdaptor->initAndOpenFile();
  176. SharedHandle<MultiFileAllocationIterator> itr
  177. (dynamic_pointer_cast<MultiFileAllocationIterator>
  178. (diskAdaptor->fileAllocationIterator()));
  179. while(!itr->finished()) {
  180. itr->allocateChunk();
  181. }
  182. CPPUNIT_ASSERT_EQUAL((int64_t)length1, File(fileEntry1->getPath()).size());
  183. CPPUNIT_ASSERT_EQUAL((int64_t)length2, File(fileEntry2->getPath()).size());
  184. CPPUNIT_ASSERT_EQUAL((int64_t)length3, File(fileEntry3->getPath()).size());
  185. CPPUNIT_ASSERT(!File(fileEntry4->getPath()).isFile());
  186. CPPUNIT_ASSERT_EQUAL((int64_t)length5, File(fileEntry5->getPath()).size());
  187. CPPUNIT_ASSERT(!File(fileEntry6->getPath()).isFile());
  188. } catch(Exception& e) {
  189. CPPUNIT_FAIL(e.stackTrace());
  190. }
  191. }
  192. } // namespace aria2