aria2/test/SegmentManTest.cc
Tatsuhiro Tsujikawa a37aaa9c0c 2007-03-24 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To add the ability to resume downloading a partially downloaded 
file
	which is downloaded from the beginning:
	* src/FileAllocator.h: Made abstract class. New 
DefaultFileAllocator
	takes this role.
	* src/main.cc: Added -c option.
	* src/BitfieldMan.h, src/BitfieldMan.cc (setBitRange): New 
function.
	* src/DiskWriter.h (openExistingFile): Added totalLength 
argument.
	* src/prefs.h (PREF_CONTINUE): New definition.
	* src/SegmentMan.h, src/SegmentMan.cc (markPieceDone): New 
function.
	* src/DefaultDiskWriter.cc
	(createNewDiskWriter): Add GlowFileAllocator to the new object.
	* src/AbstractDiskWriter.h
	(glowFileAllocator): New variable.
	* src/AbstractDiskWriter.cc
	(openExistingFile): Now preallocate file space from the end of 
the
	existing file if totalLength argument is specified and grater 
than 0.
	* src/UrlRequestInfo.cc: If -c option is specified and the file
	to download exists in local, continue the download of the file.
	--allow-overwrite=true is assumed in this context.
	* src/DefaultFileAllocator.h, src/DefaultFileAllocator.cc: New 
class.
	* src/GlowFileAllocator.h, src/GlowFileAllocator.cc: New class.

	Throw exception if --check-integrity=true is specified but chunk
	checksums are not provided:
	* src/UrlRequestInfo.cc
2007-03-24 14:32:49 +00:00

153 lines
4.3 KiB
C++

#include "SegmentMan.h"
#include "File.h"
#include "prefs.h"
#include "Util.h"
#include <cppunit/extensions/HelperMacros.h>
using namespace std;
class SegmentManTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(SegmentManTest);
CPPUNIT_TEST(testSaveAndLoad);
CPPUNIT_TEST(testNullBitfield);
CPPUNIT_TEST(testCancelSegmentOnNullBitfield);
CPPUNIT_TEST(testMarkPieceDone);
CPPUNIT_TEST(testMarkPieceDone_usedSegment);
CPPUNIT_TEST_SUITE_END();
private:
public:
void setUp() {
}
void testSaveAndLoad();
void testNullBitfield();
void testCancelSegmentOnNullBitfield();
void testMarkPieceDone();
void testMarkPieceDone_usedSegment();
};
CPPUNIT_TEST_SUITE_REGISTRATION( SegmentManTest );
void SegmentManTest::testSaveAndLoad() {
string filename = "save-temp";
string aria2filename = filename+".aria2";
File saveFile(aria2filename);
if(saveFile.exists()) {
saveFile.remove();
assert(!saveFile.exists());
}
try {
SegmentMan segmentMan;
segmentMan.totalSize = 10*1024*1024;
segmentMan.ufilename = filename;
segmentMan.initBitfield(1024*1024, segmentMan.totalSize);
SegmentHandle seg1 = segmentMan.getSegment(1);
seg1->writtenLength = seg1->length;
segmentMan.completeSegment(1, seg1);
SegmentHandle seg2 = segmentMan.getSegment(2);
seg2->writtenLength = 512*1024;
//segmentMan.updateSegment(2, seg2);
SegmentHandle seg3 = segmentMan.getSegment(3);
seg3->writtenLength = 512*1024;
//segmentMan.updateSegment(2, seg2);
segmentMan.save();
CPPUNIT_ASSERT(saveFile.exists());
SegmentMan segmentManLoad;
segmentManLoad.ufilename = filename;
segmentManLoad.load();
CPPUNIT_ASSERT_EQUAL(segmentMan.totalSize, segmentManLoad.totalSize);
SegmentHandle seg2Load = segmentManLoad.getSegment(2, seg2->index);
CPPUNIT_ASSERT_EQUAL(seg2, seg2Load);
SegmentHandle seg3Load = segmentManLoad.getSegment(3, seg3->index);
CPPUNIT_ASSERT_EQUAL(seg3, seg3Load);
CPPUNIT_ASSERT_EQUAL(segmentMan.getDownloadLength(), segmentManLoad.getDownloadLength());
} catch(Exception* e) {
cerr << e->getMsg() << endl;
delete e;
}
}
void SegmentManTest::testNullBitfield() {
SegmentMan segmentMan;
Option op;
op.put(PREF_SEGMENT_SIZE, Util::itos(1024*1024));
segmentMan.option = &op;
SegmentHandle segment = segmentMan.getSegment(1);
CPPUNIT_ASSERT(!segment.isNull());
CPPUNIT_ASSERT_EQUAL(0, segment->index);
CPPUNIT_ASSERT_EQUAL(0, segment->length);
CPPUNIT_ASSERT_EQUAL(0, segment->segmentLength);
CPPUNIT_ASSERT_EQUAL(0, segment->writtenLength);
SegmentHandle segment2 = segmentMan.getSegment(2);
CPPUNIT_ASSERT(segment2.isNull());
int64_t totalLength = 1024*1024;
segment->writtenLength = totalLength;
CPPUNIT_ASSERT_EQUAL(totalLength, segmentMan.getDownloadLength());
CPPUNIT_ASSERT(segmentMan.completeSegment(1, segment));
CPPUNIT_ASSERT_EQUAL(totalLength, segmentMan.getDownloadLength());
}
void SegmentManTest::testCancelSegmentOnNullBitfield() {
SegmentMan segmentMan;
SegmentHandle segment = segmentMan.getSegment(1);
CPPUNIT_ASSERT(!segment.isNull());
segmentMan.cancelSegment(1);
CPPUNIT_ASSERT(!segmentMan.getSegment(1).isNull());
}
void SegmentManTest::testMarkPieceDone()
{
SegmentMan segmentMan;
int32_t pieceLength = 1024*1024;
int64_t totalLength = 10*pieceLength;
segmentMan.initBitfield(pieceLength, totalLength);
segmentMan.markPieceDone(5*pieceLength);
for(int32_t i = 0; i < 5; ++i) {
CPPUNIT_ASSERT(segmentMan.hasSegment(i));
}
for(int32_t i = 5; i < 10; ++i) {
CPPUNIT_ASSERT(!segmentMan.hasSegment(i));
}
}
void SegmentManTest::testMarkPieceDone_usedSegment()
{
SegmentMan segmentMan;
int32_t pieceLength = 1024*1024;
int64_t totalLength = 10*pieceLength;
segmentMan.initBitfield(pieceLength, totalLength);
segmentMan.markPieceDone(5*pieceLength+100);
for(int32_t i = 0; i < 5; ++i) {
CPPUNIT_ASSERT(segmentMan.hasSegment(i));
}
for(int32_t i = 5; i < 10; ++i) {
CPPUNIT_ASSERT(!segmentMan.hasSegment(i));
}
SegmentHandle segment = segmentMan.getSegment(0, 5);
CPPUNIT_ASSERT(!segment.isNull());
CPPUNIT_ASSERT_EQUAL(5, segment->index);
CPPUNIT_ASSERT_EQUAL(pieceLength, segment->length);
CPPUNIT_ASSERT_EQUAL(pieceLength, segment->segmentLength);
CPPUNIT_ASSERT_EQUAL(100, segment->writtenLength);
}