MultiDiskAdaptorTest.cc 3.8 KB

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