DownloadContextTest.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. ctx.setFileEntries(std::begin(fileEntries), std::end(fileEntries));
  34. CPPUNIT_ASSERT_EQUAL(std::string("file1"),
  35. ctx.findFileEntryByOffset(0)->getPath());
  36. CPPUNIT_ASSERT_EQUAL(std::string("file4"),
  37. ctx.findFileEntryByOffset(1500)->getPath());
  38. CPPUNIT_ASSERT_EQUAL(std::string("file5"),
  39. ctx.findFileEntryByOffset(5999)->getPath());
  40. CPPUNIT_ASSERT(!ctx.findFileEntryByOffset(6000));
  41. }
  42. void DownloadContextTest::testGetPieceHash()
  43. {
  44. DownloadContext ctx;
  45. const std::string pieceHashes[] = {"hash1", "hash2", "shash3"};
  46. ctx.setPieceHashes("sha-1", &pieceHashes[0], &pieceHashes[3]);
  47. CPPUNIT_ASSERT_EQUAL(std::string("hash1"), ctx.getPieceHash(0));
  48. CPPUNIT_ASSERT_EQUAL(std::string(""), ctx.getPieceHash(3));
  49. }
  50. void DownloadContextTest::testGetNumPieces()
  51. {
  52. DownloadContext ctx(345, 9889, "");
  53. CPPUNIT_ASSERT_EQUAL((size_t)29, ctx.getNumPieces());
  54. }
  55. void DownloadContextTest::testGetBasePath()
  56. {
  57. DownloadContext ctx(0, 0, "");
  58. CPPUNIT_ASSERT_EQUAL(std::string(""), ctx.getBasePath());
  59. ctx.getFirstFileEntry()->setPath("aria2.tar.bz2");
  60. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), ctx.getBasePath());
  61. }
  62. void DownloadContextTest::testSetFileFilter()
  63. {
  64. DownloadContext ctx;
  65. std::vector<std::shared_ptr<FileEntry>> files;
  66. for (int i = 0; i < 10; ++i) {
  67. files.push_back(std::shared_ptr<FileEntry>(new FileEntry("file", 1, i)));
  68. }
  69. ctx.setFileEntries(files.begin(), files.end());
  70. auto sgl = util::parseIntSegments("6-8,2-4");
  71. sgl.normalize();
  72. ctx.setFileFilter(std::move(sgl));
  73. const std::vector<std::shared_ptr<FileEntry>>& res = ctx.getFileEntries();
  74. CPPUNIT_ASSERT(!res[0]->isRequested());
  75. CPPUNIT_ASSERT(res[1]->isRequested());
  76. CPPUNIT_ASSERT(res[2]->isRequested());
  77. CPPUNIT_ASSERT(res[3]->isRequested());
  78. CPPUNIT_ASSERT(!res[4]->isRequested());
  79. CPPUNIT_ASSERT(res[5]->isRequested());
  80. CPPUNIT_ASSERT(res[6]->isRequested());
  81. CPPUNIT_ASSERT(res[7]->isRequested());
  82. CPPUNIT_ASSERT(!res[8]->isRequested());
  83. CPPUNIT_ASSERT(!res[9]->isRequested());
  84. }
  85. } // namespace aria2