MultiDiskAdaptorTest.cc 3.8 KB

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