MultiFileAllocationIteratorTest.cc 5.7 KB

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