MultiDiskAdaptorTest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "MultiDiskAdaptor.h"
  2. #include <string>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. using namespace std;
  5. class MultiDiskAdaptorTest:public CppUnit::TestFixture {
  6. CPPUNIT_TEST_SUITE(MultiDiskAdaptorTest);
  7. CPPUNIT_TEST(testWriteData);
  8. CPPUNIT_TEST(testReadData);
  9. CPPUNIT_TEST_SUITE_END();
  10. private:
  11. Option* option;
  12. MultiDiskAdaptorHandle adaptor;
  13. public:
  14. MultiDiskAdaptorTest():option(0), adaptor(0) {}
  15. void setUp() {
  16. delete option;
  17. option = new Option();
  18. adaptor = new MultiDiskAdaptor();
  19. adaptor->setPieceLength(2);
  20. adaptor->setOption(new Option());
  21. adaptor->setStoreDir(".");
  22. adaptor->setTopDir(".");
  23. }
  24. void testWriteData();
  25. void testReadData();
  26. };
  27. CPPUNIT_TEST_SUITE_REGISTRATION( MultiDiskAdaptorTest );
  28. FileEntries createEntries() {
  29. FileEntryHandle entry1(new FileEntry("file1.txt", 15, 0));
  30. FileEntryHandle entry2(new FileEntry("file2.txt", 7, 15));
  31. FileEntryHandle entry3(new FileEntry("file3.txt", 3, 22));
  32. unlink("file1.txt");
  33. unlink("file2.txt");
  34. unlink("file3.txt");
  35. FileEntries entries;
  36. entries.push_back(entry1);
  37. entries.push_back(entry2);
  38. entries.push_back(entry3);
  39. return entries;
  40. }
  41. void readFile(const string& filename, char* buf, int bufLength) {
  42. FILE* f = fopen(filename.c_str(), "r");
  43. if(f == NULL) {
  44. abort();
  45. }
  46. int retval = fread(buf, bufLength, 1, f);
  47. fclose(f);
  48. if(retval != 1) {
  49. abort();
  50. }
  51. }
  52. void MultiDiskAdaptorTest::testWriteData() {
  53. try {
  54. adaptor->setFileEntries(createEntries());
  55. adaptor->openFile();
  56. 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, string(buf));
  63. adaptor->openFile();
  64. 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(string("1234567890ABCDE"), string(buf));
  70. readFile("file2.txt", buf, 1);
  71. buf[1] = '\0';
  72. CPPUNIT_ASSERT_EQUAL(string("F"), string(buf));
  73. adaptor->openFile();
  74. 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(string("123456789012345"), string(buf));
  80. readFile("file2.txt", buf, 7);
  81. buf[7] = '\0';
  82. CPPUNIT_ASSERT_EQUAL(string("1234567"), string(buf));
  83. readFile("file3.txt", buf, 2);
  84. buf[2] = '\0';
  85. CPPUNIT_ASSERT_EQUAL(string("12"), string(buf));
  86. } catch(Exception* e) {
  87. CPPUNIT_FAIL(e->getMsg());
  88. }
  89. }
  90. void MultiDiskAdaptorTest::testReadData() {
  91. FileEntryHandle entry1(new FileEntry("file1r.txt", 15, 0));
  92. FileEntryHandle entry2(new FileEntry("file2r.txt", 7, 15));
  93. FileEntryHandle entry3(new FileEntry("file3r.txt", 3, 22));
  94. FileEntries 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(string("1234567890ABCDE"), string((char*)buf));
  104. adaptor->readData(buf, 10, 6);
  105. buf[10] = '\0';
  106. CPPUNIT_ASSERT_EQUAL(string("7890ABCDEF"), string((char*)buf));
  107. adaptor->readData(buf, 4, 20);
  108. buf[4] = '\0';
  109. CPPUNIT_ASSERT_EQUAL(string("KLMN"), string((char*)buf));
  110. adaptor->readData(buf, 25, 0);
  111. buf[25] = '\0';
  112. CPPUNIT_ASSERT_EQUAL(string("1234567890ABCDEFGHIJKLMNO"), string((char*)buf));
  113. }