File: DownloadContextTest.cc

package info (click to toggle)
aria2 1.37.0%2Bdebian-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,128 kB
  • sloc: cpp: 115,006; ansic: 9,140; makefile: 1,466; ruby: 475; python: 373; sh: 260; xml: 176
file content (101 lines) | stat: -rw-r--r-- 3,366 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "DownloadContext.h"

#include <cppunit/extensions/HelperMacros.h>

#include "FileEntry.h"
#include "array_fun.h"

namespace aria2 {

class DownloadContextTest : public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(DownloadContextTest);
  CPPUNIT_TEST(testFindFileEntryByOffset);
  CPPUNIT_TEST(testGetPieceHash);
  CPPUNIT_TEST(testGetNumPieces);
  CPPUNIT_TEST(testGetBasePath);
  CPPUNIT_TEST(testSetFileFilter);
  CPPUNIT_TEST_SUITE_END();

public:
  void testFindFileEntryByOffset();
  void testGetPieceHash();
  void testGetNumPieces();
  void testGetBasePath();
  void testSetFileFilter();
};

CPPUNIT_TEST_SUITE_REGISTRATION(DownloadContextTest);

void DownloadContextTest::testFindFileEntryByOffset()
{
  DownloadContext ctx;

  CPPUNIT_ASSERT(!ctx.findFileEntryByOffset(0));

  const std::shared_ptr<FileEntry> fileEntries[] = {
      std::shared_ptr<FileEntry>(new FileEntry("file1", 1000, 0)),
      std::shared_ptr<FileEntry>(new FileEntry("file2", 0, 1000)),
      std::shared_ptr<FileEntry>(new FileEntry("file3", 0, 1000)),
      std::shared_ptr<FileEntry>(new FileEntry("file4", 2000, 1000)),
      std::shared_ptr<FileEntry>(new FileEntry("file5", 3000, 3000)),
      std::shared_ptr<FileEntry>(new FileEntry("file6", 0, 6000))};
  ctx.setFileEntries(std::begin(fileEntries), std::end(fileEntries));

  CPPUNIT_ASSERT_EQUAL(std::string("file1"),
                       ctx.findFileEntryByOffset(0)->getPath());
  CPPUNIT_ASSERT_EQUAL(std::string("file4"),
                       ctx.findFileEntryByOffset(1500)->getPath());
  CPPUNIT_ASSERT_EQUAL(std::string("file5"),
                       ctx.findFileEntryByOffset(5999)->getPath());
  CPPUNIT_ASSERT(!ctx.findFileEntryByOffset(6000));
}

void DownloadContextTest::testGetPieceHash()
{
  DownloadContext ctx;
  const std::string pieceHashes[] = {"hash1", "hash2", "shash3"};
  ctx.setPieceHashes("sha-1", &pieceHashes[0], &pieceHashes[3]);
  CPPUNIT_ASSERT_EQUAL(std::string("hash1"), ctx.getPieceHash(0));
  CPPUNIT_ASSERT_EQUAL(std::string(""), ctx.getPieceHash(3));
}

void DownloadContextTest::testGetNumPieces()
{
  DownloadContext ctx(345, 9889, "");
  CPPUNIT_ASSERT_EQUAL((size_t)29, ctx.getNumPieces());
}

void DownloadContextTest::testGetBasePath()
{
  DownloadContext ctx(0, 0, "");
  CPPUNIT_ASSERT_EQUAL(std::string(""), ctx.getBasePath());
  ctx.getFirstFileEntry()->setPath("aria2.tar.bz2");
  CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), ctx.getBasePath());
}

void DownloadContextTest::testSetFileFilter()
{
  DownloadContext ctx;
  std::vector<std::shared_ptr<FileEntry>> files;
  for (int i = 0; i < 10; ++i) {
    files.push_back(std::shared_ptr<FileEntry>(new FileEntry("file", 1, i)));
  }
  ctx.setFileEntries(files.begin(), files.end());
  auto sgl = util::parseIntSegments("6-8,2-4");
  sgl.normalize();
  ctx.setFileFilter(std::move(sgl));
  const std::vector<std::shared_ptr<FileEntry>>& res = ctx.getFileEntries();
  CPPUNIT_ASSERT(!res[0]->isRequested());
  CPPUNIT_ASSERT(res[1]->isRequested());
  CPPUNIT_ASSERT(res[2]->isRequested());
  CPPUNIT_ASSERT(res[3]->isRequested());
  CPPUNIT_ASSERT(!res[4]->isRequested());
  CPPUNIT_ASSERT(res[5]->isRequested());
  CPPUNIT_ASSERT(res[6]->isRequested());
  CPPUNIT_ASSERT(res[7]->isRequested());
  CPPUNIT_ASSERT(!res[8]->isRequested());
  CPPUNIT_ASSERT(!res[9]->isRequested());
}

} // namespace aria2