aria2/test/MultiDiskAdaptorTest.cc

157 lines
4.4 KiB
C++
Raw Normal View History

2007-01-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> To add an ability to pre-allocate file space: * src/DirectDiskAdaptor.h: Rewritten. * src/PieceStorage.h: DiskAdaptor -> DiskAdaptorHandle * src/MultiDiskAdaptor.h: Rewritten. * src/DefaultPieceStorage.cc (MultiDiskWriter.h): Removed include. (PreAllocationDiskWriter.h): Removed include. (FileAllocationMonitor.h): New include. (~DefaultPieceStorage): Removed deletion of diskAdaptor. (initStorage): Rewritten. (getDiskAdaptor): DiskAdaptor -> DiskAdaptorHandle * src/FileAllocator.h: New class. * src/FileAllocator.cc: New class. * src/FileAllocationMonitor.h: New class. * src/FileAllocationMonitor.cc: New class. * src/ConsoleFileAllocationMonitor.h: New class. * src/ConsoleFileAllocationMonitor.cc: New class. * src/AbstractSingleDiskAdaptor.h: New class. * src/AbstractSingleDiskAdaptor.cc: New class. * src/DiskAdaptor.h (Directory.h): Removed include. (DiskWriter.h): Removed include. (FileEntry.h): Removed include. (diskWriter): Removed. (DiskAdaptor): Removed diskWriter. (openFile): Made pure virtual. (closeFile): Made pure virtual. (openExistingFile): Made pure virtual. (initAndOpenFile): Made pure virtual. (writeData): Made pure virtual. (readData): Made pure virtual. (sha1Sum): Made pure virtual. (getStoreDir): Returns const reference of storeDir. (DiskAdaptorHandle): New type definition. * src/main.cc (ConsoleFileAllocationMonitor.h): New include. (showUsage): Added default value description of -s option. Added the description of --file-allocation option. (main): Set default value of --file-allocation option to 'none'. Added --file-allocation command-line option. Setup FileAllocationMonitorFactory. * src/FtpInitiateConnectionCommand.cc (executeInternal): Removed diskWriter related processing, which was moved to FtpNegotiationCommand.cc. * src/DirectDiskAdaptor.cc (getFilePath): Made non-const. * src/CopyDiskAdaptor.h (DiskAdaptor.h): Removed include. (DiskWriter.h): Removed include. (AbstractSingleDiskAdaptor.h): New include. (getFilePath): Made non-const. Added virtual keyword. (CopyDiskAdaptor): Removed diskWriter. (getTempFile): Returns const reference. (CopyDiskAdaptorHandle): New type definition. * src/ByteArrayDiskWriter.cc (clear): Simplified. (initAndOpenFile): Rewritten. (openFile): Call initAndOpenFile() * src/MultiDiskAdaptor.cc: Rewritten. * src/DownloadEngineFactory.cc (FileAllocator.h): New include. (FileAllocationMonitor.h): New include. (newConsoleEngine): Call DefaultDiskWriter::createNewDiskWriter() to create DefaultDiskWriter with file allocator. * src/DiskWriter.h (initAndOpenFile): Added totalLength argument. (openFile): Added totalLength argument. * src/prefs.h (PREF_FILE_ALLOCATION): New definition. (V_PREALLOC): New definition. (V_NONE): New definition. * src/HttpResponseCommand.cc (handleDefaultEncoding): Call DefaultDiskWriter::initAndOpenFile with size. * src/FtpNegotiateCommand.cc (Util.h): New include. (recvSize): Open file here. * src/Util.h (ullitos): New function. * src/CopyDiskWriter.h (getFilePath): Made non-const. * src/DefaultDiskWriter.h (Option.h): New include. (totalLength): Removed. (DefaultDiskWriter): Removed totalLength. (initAndOpenFile): Added totalLength argument. (DefaultDiskWriterHandle): New type definition. (createNewDiskWriter): New function. * src/Util.cc (ullitos): New function. * src/DefaultDiskWriter.cc (message.h): New include. (FileAllocator.h): New include. (prefs.h): New include. (Util.h): New include. (DefaultDiskWriter): Removed totalLength. (initAndOpenFile): Added file allocation. (createNewDiskWriter): New function. Just for temporary solution. It will be rewritten later. * src/DiskAdaptor.cc (DiskAdaptor): Removed diskWriter. * src/AbstractDiskWriter.cc (LogFactory.h): New include. (AbstractDiskWriter): Added fileAllocator, logger. (openFile): Added totalLength argument. * src/AbstractDiskWriter.h (FileAllocator.h): New include. (Logger.h): New include. (fileAllocator): New variable. (logger): New variable. (openFile): Added totalLength argument. Added virtual keyword explicitly. (openExistingFile): Added totalLength argument. Added virtual keyword explicitly. (closeFile): Added virtual keyword explicitly. (sha1Sum): Added virtual keyword explicitly. (writeData): Added virtual keyword explicitly. (readData): Added virtual keyword explicitly. (setFileAllocator): New function. * src/DefaultPieceStorage.h (FileAllocator.h): New include. (diskAdaptor): DiskAdaptor -> DiskAdaptorHandle (getDiskAdaptor): DiskAdaptor -> DiskAdaptorHandle * src/FileProgressMonitor.h: New class. To compile aria2 on PC-BSD: * src/DefaultBtContext.cc (libgen.h): New include. To fix memory leak: * src/Exception.h (~Exception): Delete cause. Fixed memory leak.
2007-01-08 00:13:25 +00:00
#include "MultiDiskAdaptor.h"
#include <string>
#include <cppunit/extensions/HelperMacros.h>
using namespace std;
class MultiDiskAdaptorTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(MultiDiskAdaptorTest);
CPPUNIT_TEST(testWriteData);
CPPUNIT_TEST(testReadData);
CPPUNIT_TEST(testSha1Sum);
CPPUNIT_TEST_SUITE_END();
private:
Option* option;
MultiDiskAdaptorHandle adaptor;
public:
MultiDiskAdaptorTest():option(0), adaptor(0) {}
void setUp() {
delete option;
option = new Option();
adaptor = new MultiDiskAdaptor();
adaptor->setPieceLength(2);
adaptor->setOption(new Option());
adaptor->setStoreDir(".");
adaptor->setTopDir(".");
}
void testWriteData();
void testReadData();
void testSha1Sum();
};
CPPUNIT_TEST_SUITE_REGISTRATION( MultiDiskAdaptorTest );
FileEntries createEntries() {
FileEntryHandle entry1(new FileEntry("file1.txt", 15, 0));
FileEntryHandle entry2(new FileEntry("file2.txt", 7, 15));
FileEntryHandle entry3(new FileEntry("file3.txt", 3, 22));
unlink("file1.txt");
unlink("file2.txt");
unlink("file3.txt");
FileEntries entries;
entries.push_back(entry1);
entries.push_back(entry2);
entries.push_back(entry3);
return entries;
}
void readFile(const string& filename, char* buf, int bufLength) {
FILE* f = fopen(filename.c_str(), "r");
if(f == NULL) {
abort();
}
int retval = fread(buf, bufLength, 1, f);
fclose(f);
if(retval != 1) {
abort();
}
}
void MultiDiskAdaptorTest::testWriteData() {
try {
adaptor->setFileEntries(createEntries());
adaptor->openFile();
string msg = "12345";
adaptor->writeData((const unsigned char*)msg.c_str(), msg.size(), 0);
adaptor->closeFile();
char buf[128];
readFile("file1.txt", buf, 5);
buf[5] = '\0';
CPPUNIT_ASSERT_EQUAL(msg, string(buf));
adaptor->openFile();
string msg2 = "67890ABCDEF";
adaptor->writeData((const unsigned char*)msg2.c_str(), msg2.size(), 5);
adaptor->closeFile();
readFile("file1.txt", buf, 15);
buf[15] = '\0';
CPPUNIT_ASSERT_EQUAL(string("1234567890ABCDE"), string(buf));
readFile("file2.txt", buf, 1);
buf[1] = '\0';
CPPUNIT_ASSERT_EQUAL(string("F"), string(buf));
adaptor->openFile();
string msg3 = "12345123456712";
adaptor->writeData((const unsigned char*)msg3.c_str(), msg3.size(), 10);
adaptor->closeFile();
readFile("file1.txt", buf, 15);
buf[15] = '\0';
CPPUNIT_ASSERT_EQUAL(string("123456789012345"), string(buf));
readFile("file2.txt", buf, 7);
buf[7] = '\0';
CPPUNIT_ASSERT_EQUAL(string("1234567"), string(buf));
readFile("file3.txt", buf, 2);
buf[2] = '\0';
CPPUNIT_ASSERT_EQUAL(string("12"), string(buf));
} catch(Exception* e) {
CPPUNIT_FAIL(e->getMsg());
}
}
void MultiDiskAdaptorTest::testReadData() {
FileEntryHandle entry1(new FileEntry("file1r.txt", 15, 0));
FileEntryHandle entry2(new FileEntry("file2r.txt", 7, 15));
FileEntryHandle entry3(new FileEntry("file3r.txt", 3, 22));
FileEntries entries;
entries.push_back(entry1);
entries.push_back(entry2);
entries.push_back(entry3);
adaptor->setFileEntries(entries);
adaptor->openFile();
unsigned char buf[128];
adaptor->readData(buf, 15, 0);
buf[15] = '\0';
CPPUNIT_ASSERT_EQUAL(string("1234567890ABCDE"), string((char*)buf));
adaptor->readData(buf, 10, 6);
buf[10] = '\0';
CPPUNIT_ASSERT_EQUAL(string("7890ABCDEF"), string((char*)buf));
adaptor->readData(buf, 4, 20);
buf[4] = '\0';
CPPUNIT_ASSERT_EQUAL(string("KLMN"), string((char*)buf));
adaptor->readData(buf, 25, 0);
buf[25] = '\0';
CPPUNIT_ASSERT_EQUAL(string("1234567890ABCDEFGHIJKLMNO"), string((char*)buf));
}
void MultiDiskAdaptorTest::testSha1Sum() {
FileEntryHandle entry1(new FileEntry("file1r.txt", 15, 0));
FileEntryHandle entry2(new FileEntry("file2r.txt", 7, 15));
FileEntryHandle entry3(new FileEntry("file3r.txt", 3, 22));
FileEntries entries;
entries.push_back(entry1);
entries.push_back(entry2);
entries.push_back(entry3);
adaptor->setFileEntries(entries);
adaptor->openFile();
string sha1sum = adaptor->sha1Sum(0, 25);
CPPUNIT_ASSERT_EQUAL(string("76495faf71ca63df66dce99547d2c58da7266d9e"), sha1sum);
sha1sum = adaptor->sha1Sum(15, 7);
CPPUNIT_ASSERT_EQUAL(string("737660d816fb23c2d5bc74f62d9b01b852b2aaca"), sha1sum);
sha1sum = adaptor->sha1Sum(10, 14);
CPPUNIT_ASSERT_EQUAL(string("6238bf61dd8df8f77156b2378e9e39cd3939680c"), sha1sum);
adaptor->closeFile();
}