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. }
  14. void testWriteAndRead();
  15. void testWriteAndRead2();
  16. };
  17. CPPUNIT_TEST_SUITE_REGISTRATION( ByteArrayDiskWriterTest );
  18. void ByteArrayDiskWriterTest::testWriteAndRead() {
  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. ByteArrayDiskWriter bw;
  39. std::string msg1 = "Hello World";
  40. bw.writeData((const unsigned char*)msg1.c_str(), msg1.size(), 0);
  41. std::string msg2 = "From Mars";
  42. bw.writeData((const unsigned char*)msg2.c_str(), msg2.size(), 6);
  43. char buf[100];
  44. int32_t c = bw.readData((unsigned char*)buf, sizeof(buf), 0);
  45. buf[c] = '\0';
  46. CPPUNIT_ASSERT_EQUAL(std::string("Hello From Mars"), std::string(buf));
  47. CPPUNIT_ASSERT_EQUAL((int64_t)15, bw.size());
  48. }
  49. } // namespace aria2