DownloadContextTest.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "DownloadContext.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. #include "FileEntry.h"
  4. #include "array_fun.h"
  5. namespace aria2 {
  6. class DownloadContextTest:public CppUnit::TestFixture {
  7. CPPUNIT_TEST_SUITE(DownloadContextTest);
  8. CPPUNIT_TEST(testFindFileEntryByOffset);
  9. CPPUNIT_TEST(testGetPieceHash);
  10. CPPUNIT_TEST(testGetNumPieces);
  11. CPPUNIT_TEST(testGetBasePath);
  12. CPPUNIT_TEST(testSetFileFilter);
  13. CPPUNIT_TEST_SUITE_END();
  14. public:
  15. void testFindFileEntryByOffset();
  16. void testGetPieceHash();
  17. void testGetNumPieces();
  18. void testGetBasePath();
  19. void testSetFileFilter();
  20. };
  21. CPPUNIT_TEST_SUITE_REGISTRATION(DownloadContextTest);
  22. void DownloadContextTest::testFindFileEntryByOffset()
  23. {
  24. DownloadContext ctx;
  25. CPPUNIT_ASSERT(!ctx.findFileEntryByOffset(0));
  26. const std::shared_ptr<FileEntry> fileEntries[] =
  27. { std::shared_ptr<FileEntry>(new FileEntry("file1",1000,0)),
  28. std::shared_ptr<FileEntry>(new FileEntry("file2",0,1000)),
  29. std::shared_ptr<FileEntry>(new FileEntry("file3",0,1000)),
  30. std::shared_ptr<FileEntry>(new FileEntry("file4",2000,1000)),
  31. std::shared_ptr<FileEntry>(new FileEntry("file5",3000,3000)),
  32. std::shared_ptr<FileEntry>(new FileEntry("file6",0,6000))
  33. };
  34. ctx.setFileEntries(std::begin(fileEntries), std::end(fileEntries));
  35. CPPUNIT_ASSERT_EQUAL(std::string("file1"),
  36. ctx.findFileEntryByOffset(0)->getPath());
  37. CPPUNIT_ASSERT_EQUAL(std::string("file4"),
  38. ctx.findFileEntryByOffset(1500)->getPath());
  39. CPPUNIT_ASSERT_EQUAL(std::string("file5"),
  40. ctx.findFileEntryByOffset(5999)->getPath());
  41. CPPUNIT_ASSERT(!ctx.findFileEntryByOffset(6000));
  42. }
  43. void DownloadContextTest::testGetPieceHash()
  44. {
  45. DownloadContext ctx;
  46. const std::string pieceHashes[] = { "hash1","hash2","shash3" };
  47. ctx.setPieceHashes("sha-1", &pieceHashes[0], &pieceHashes[3]);
  48. CPPUNIT_ASSERT_EQUAL(std::string("hash1"), ctx.getPieceHash(0));
  49. CPPUNIT_ASSERT_EQUAL(std::string(""), ctx.getPieceHash(3));
  50. }
  51. void DownloadContextTest::testGetNumPieces()
  52. {
  53. DownloadContext ctx(345, 9889, "");
  54. CPPUNIT_ASSERT_EQUAL((size_t)29, ctx.getNumPieces());
  55. }
  56. void DownloadContextTest::testGetBasePath()
  57. {
  58. DownloadContext ctx(0, 0, "");
  59. CPPUNIT_ASSERT_EQUAL(std::string(""), ctx.getBasePath());
  60. ctx.getFirstFileEntry()->setPath("aria2.tar.bz2");
  61. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), ctx.getBasePath());
  62. }
  63. void DownloadContextTest::testSetFileFilter()
  64. {
  65. DownloadContext ctx;
  66. std::vector<std::shared_ptr<FileEntry> > files;
  67. for(int i = 0; i < 10; ++i) {
  68. files.push_back(std::shared_ptr<FileEntry>(new FileEntry("file", 1, i)));
  69. }
  70. ctx.setFileEntries(files.begin(), files.end());
  71. SegList<int> sgl;
  72. util::parseIntSegments(sgl, "6-8,2-4");
  73. sgl.normalize();
  74. ctx.setFileFilter(sgl);
  75. const std::vector<std::shared_ptr<FileEntry> >& res = ctx.getFileEntries();
  76. CPPUNIT_ASSERT(!res[0]->isRequested());
  77. CPPUNIT_ASSERT(res[1]->isRequested());
  78. CPPUNIT_ASSERT(res[2]->isRequested());
  79. CPPUNIT_ASSERT(res[3]->isRequested());
  80. CPPUNIT_ASSERT(!res[4]->isRequested());
  81. CPPUNIT_ASSERT(res[5]->isRequested());
  82. CPPUNIT_ASSERT(res[6]->isRequested());
  83. CPPUNIT_ASSERT(res[7]->isRequested());
  84. CPPUNIT_ASSERT(!res[8]->isRequested());
  85. CPPUNIT_ASSERT(!res[9]->isRequested());
  86. }
  87. } // namespace aria2