MultiDiskAdaptorTest.cc 3.4 KB

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