DownloadContextTest.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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_SUITE_END();
  13. public:
  14. void testFindFileEntryByOffset();
  15. void testGetPieceHash();
  16. void testGetNumPieces();
  17. void testGetBasePath();
  18. };
  19. CPPUNIT_TEST_SUITE_REGISTRATION(DownloadContextTest);
  20. void DownloadContextTest::testFindFileEntryByOffset()
  21. {
  22. DownloadContext ctx;
  23. CPPUNIT_ASSERT(ctx.findFileEntryByOffset(0).isNull());
  24. const SharedHandle<FileEntry> fileEntries[] =
  25. { SharedHandle<FileEntry>(new FileEntry("file1",1000,0)),
  26. SharedHandle<FileEntry>(new FileEntry("file2",0,1000)),
  27. SharedHandle<FileEntry>(new FileEntry("file3",0,1000)),
  28. SharedHandle<FileEntry>(new FileEntry("file4",2000,1000)),
  29. SharedHandle<FileEntry>(new FileEntry("file5",3000,3000)),
  30. SharedHandle<FileEntry>(new FileEntry("file6",0,6000))
  31. };
  32. ctx.setFileEntries(&fileEntries[0],
  33. &fileEntries[arrayLength(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).isNull());
  41. }
  42. void DownloadContextTest::testGetPieceHash()
  43. {
  44. DownloadContext ctx;
  45. const std::string pieceHashes[] = { "hash1","hash2","shash3" };
  46. ctx.setPieceHashes(&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. ctx.setDir("/tmp");
  62. // See dir doesn't effect getBasePath().
  63. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), ctx.getBasePath());
  64. }
  65. } // namespace aria2