MultiDiskAdaptorTest.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "MultiDiskAdaptor.h"
  2. #include "FileEntry.h"
  3. #include "Exception.h"
  4. #include <string>
  5. #include <cppunit/extensions/HelperMacros.h>
  6. namespace aria2 {
  7. class MultiDiskAdaptorTest:public CppUnit::TestFixture {
  8. CPPUNIT_TEST_SUITE(MultiDiskAdaptorTest);
  9. CPPUNIT_TEST(testWriteData);
  10. CPPUNIT_TEST(testReadData);
  11. CPPUNIT_TEST_SUITE_END();
  12. private:
  13. SharedHandle<MultiDiskAdaptor> adaptor;
  14. public:
  15. MultiDiskAdaptorTest():adaptor(0) {}
  16. void setUp() {
  17. adaptor = new MultiDiskAdaptor();
  18. adaptor->setPieceLength(2);
  19. adaptor->setStoreDir(".");
  20. adaptor->setTopDir(".");
  21. }
  22. void testWriteData();
  23. void testReadData();
  24. };
  25. CPPUNIT_TEST_SUITE_REGISTRATION( MultiDiskAdaptorTest );
  26. std::deque<SharedHandle<FileEntry> > createEntries() {
  27. SharedHandle<FileEntry> entry1(new FileEntry("file1.txt", 15, 0));
  28. SharedHandle<FileEntry> entry2(new FileEntry("file2.txt", 7, 15));
  29. SharedHandle<FileEntry> entry3(new FileEntry("file3.txt", 3, 22));
  30. unlink("file1.txt");
  31. unlink("file2.txt");
  32. unlink("file3.txt");
  33. std::deque<SharedHandle<FileEntry> > entries;
  34. entries.push_back(entry1);
  35. entries.push_back(entry2);
  36. entries.push_back(entry3);
  37. return entries;
  38. }
  39. void readFile(const std::string& filename, char* buf, int bufLength) {
  40. FILE* f = fopen(filename.c_str(), "r");
  41. if(f == NULL) {
  42. abort();
  43. }
  44. int retval = fread(buf, bufLength, 1, f);
  45. fclose(f);
  46. if(retval != 1) {
  47. abort();
  48. }
  49. }
  50. void MultiDiskAdaptorTest::testWriteData() {
  51. try {
  52. adaptor->setFileEntries(createEntries());
  53. adaptor->openFile();
  54. std::string msg = "12345";
  55. adaptor->writeData((const unsigned char*)msg.c_str(), msg.size(), 0);
  56. adaptor->closeFile();
  57. char buf[128];
  58. readFile("file1.txt", buf, 5);
  59. buf[5] = '\0';
  60. CPPUNIT_ASSERT_EQUAL(msg, std::string(buf));
  61. adaptor->openFile();
  62. std::string msg2 = "67890ABCDEF";
  63. adaptor->writeData((const unsigned char*)msg2.c_str(), msg2.size(), 5);
  64. adaptor->closeFile();
  65. readFile("file1.txt", buf, 15);
  66. buf[15] = '\0';
  67. CPPUNIT_ASSERT_EQUAL(std::string("1234567890ABCDE"), std::string(buf));
  68. readFile("file2.txt", buf, 1);
  69. buf[1] = '\0';
  70. CPPUNIT_ASSERT_EQUAL(std::string("F"), std::string(buf));
  71. adaptor->openFile();
  72. std::string msg3 = "12345123456712";
  73. adaptor->writeData((const unsigned char*)msg3.c_str(), msg3.size(), 10);
  74. adaptor->closeFile();
  75. readFile("file1.txt", buf, 15);
  76. buf[15] = '\0';
  77. CPPUNIT_ASSERT_EQUAL(std::string("123456789012345"), std::string(buf));
  78. readFile("file2.txt", buf, 7);
  79. buf[7] = '\0';
  80. CPPUNIT_ASSERT_EQUAL(std::string("1234567"), std::string(buf));
  81. readFile("file3.txt", buf, 2);
  82. buf[2] = '\0';
  83. CPPUNIT_ASSERT_EQUAL(std::string("12"), std::string(buf));
  84. } catch(Exception* e) {
  85. CPPUNIT_FAIL(e->getMsg());
  86. }
  87. }
  88. void MultiDiskAdaptorTest::testReadData() {
  89. SharedHandle<FileEntry> entry1(new FileEntry("file1r.txt", 15, 0));
  90. SharedHandle<FileEntry> entry2(new FileEntry("file2r.txt", 7, 15));
  91. SharedHandle<FileEntry> entry3(new FileEntry("file3r.txt", 3, 22));
  92. std::deque<SharedHandle<FileEntry> > entries;
  93. entries.push_back(entry1);
  94. entries.push_back(entry2);
  95. entries.push_back(entry3);
  96. adaptor->setFileEntries(entries);
  97. adaptor->openFile();
  98. unsigned char buf[128];
  99. adaptor->readData(buf, 15, 0);
  100. buf[15] = '\0';
  101. CPPUNIT_ASSERT_EQUAL(std::string("1234567890ABCDE"), std::string((char*)buf));
  102. adaptor->readData(buf, 10, 6);
  103. buf[10] = '\0';
  104. CPPUNIT_ASSERT_EQUAL(std::string("7890ABCDEF"), std::string((char*)buf));
  105. adaptor->readData(buf, 4, 20);
  106. buf[4] = '\0';
  107. CPPUNIT_ASSERT_EQUAL(std::string("KLMN"), std::string((char*)buf));
  108. adaptor->readData(buf, 25, 0);
  109. buf[25] = '\0';
  110. CPPUNIT_ASSERT_EQUAL(std::string("1234567890ABCDEFGHIJKLMNO"), std::string((char*)buf));
  111. }
  112. } // namespace aria2