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
|
#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_SUITE_END();
public:
void testFindFileEntryByOffset();
void testGetPieceHash();
void testGetNumPieces();
void testGetBasePath();
};
CPPUNIT_TEST_SUITE_REGISTRATION(DownloadContextTest);
void DownloadContextTest::testFindFileEntryByOffset()
{
DownloadContext ctx;
CPPUNIT_ASSERT(ctx.findFileEntryByOffset(0).isNull());
const SharedHandle<FileEntry> fileEntries[] =
{ SharedHandle<FileEntry>(new FileEntry("file1",1000,0)),
SharedHandle<FileEntry>(new FileEntry("file2",0,1000)),
SharedHandle<FileEntry>(new FileEntry("file3",0,1000)),
SharedHandle<FileEntry>(new FileEntry("file4",2000,1000)),
SharedHandle<FileEntry>(new FileEntry("file5",3000,3000)),
SharedHandle<FileEntry>(new FileEntry("file6",0,6000))
};
ctx.setFileEntries(vbegin(fileEntries), vend(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).isNull());
}
void DownloadContextTest::testGetPieceHash()
{
DownloadContext ctx;
const std::string pieceHashes[] = { "hash1","hash2","shash3" };
ctx.setPieceHashes(&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());
ctx.setDir("/tmp");
// See dir doesn't effect getBasePath().
CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), ctx.getBasePath());
}
} // namespace aria2
|