ByteArrayDiskWriterTest.cc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "ByteArrayDiskWriter.h"
  2. #include <string>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. namespace aria2 {
  5. class ByteArrayDiskWriterTest : public CppUnit::TestFixture {
  6. CPPUNIT_TEST_SUITE(ByteArrayDiskWriterTest);
  7. CPPUNIT_TEST(testWriteAndRead);
  8. CPPUNIT_TEST(testWriteAndRead2);
  9. CPPUNIT_TEST_SUITE_END();
  10. private:
  11. public:
  12. void setUp() {}
  13. void testWriteAndRead();
  14. void testWriteAndRead2();
  15. };
  16. CPPUNIT_TEST_SUITE_REGISTRATION(ByteArrayDiskWriterTest);
  17. void ByteArrayDiskWriterTest::testWriteAndRead()
  18. {
  19. ByteArrayDiskWriter bw;
  20. std::string msg1 = "Hello";
  21. bw.writeData((const unsigned char*)msg1.c_str(), msg1.size(), 0);
  22. // write at the end of stream
  23. std::string msg2 = " World";
  24. bw.writeData((const unsigned char*)msg2.c_str(), msg2.size(), 5);
  25. // write at the end of stream +1
  26. std::string msg3 = "!!";
  27. bw.writeData((const unsigned char*)msg3.c_str(), msg3.size(), 12);
  28. // write space at the 'hole'
  29. std::string msg4 = " ";
  30. bw.writeData((const unsigned char*)msg4.c_str(), msg4.size(), 11);
  31. char buf[100];
  32. int32_t c = bw.readData((unsigned char*)buf, sizeof(buf), 1);
  33. buf[c] = '\0';
  34. CPPUNIT_ASSERT_EQUAL(std::string("ello World !!"), std::string(buf));
  35. CPPUNIT_ASSERT_EQUAL((int64_t)14, bw.size());
  36. }
  37. void ByteArrayDiskWriterTest::testWriteAndRead2()
  38. {
  39. ByteArrayDiskWriter bw;
  40. std::string msg1 = "Hello World";
  41. bw.writeData((const unsigned char*)msg1.c_str(), msg1.size(), 0);
  42. std::string msg2 = "From Mars";
  43. bw.writeData((const unsigned char*)msg2.c_str(), msg2.size(), 6);
  44. char buf[100];
  45. int32_t c = bw.readData((unsigned char*)buf, sizeof(buf), 0);
  46. buf[c] = '\0';
  47. CPPUNIT_ASSERT_EQUAL(std::string("Hello From Mars"), std::string(buf));
  48. CPPUNIT_ASSERT_EQUAL((int64_t)15, bw.size());
  49. }
  50. } // namespace aria2