summaryrefslogtreecommitdiff
path: root/test/ByteArrayDiskWriterTest.cc
blob: 6630f970c5c3a1c10b4c69ed1037ac2d172f106a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "ByteArrayDiskWriter.h"
#include <string>
#include <cppunit/extensions/HelperMacros.h>

namespace aria2 {

class ByteArrayDiskWriterTest:public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(ByteArrayDiskWriterTest);
  CPPUNIT_TEST(testWriteAndRead);
  CPPUNIT_TEST(testWriteAndRead2);
  CPPUNIT_TEST_SUITE_END();
private:

public:
  void setUp() {
  }

  void testWriteAndRead();
  void testWriteAndRead2();
};


CPPUNIT_TEST_SUITE_REGISTRATION( ByteArrayDiskWriterTest );

void ByteArrayDiskWriterTest::testWriteAndRead() {
  ByteArrayDiskWriter bw;

  std::string msg1 = "Hello";
  bw.writeData((const unsigned char*)msg1.c_str(), msg1.size(), 0);
  // write at the end of stream
  std::string msg2 = " World";
  bw.writeData((const unsigned char*)msg2.c_str(), msg2.size(), 5);
  // write at the end of stream +1
  std::string msg3 = "!!";
  bw.writeData((const unsigned char*)msg3.c_str(), msg3.size(), 12);
  // write space at the 'hole'
  std::string msg4 = " ";
  bw.writeData((const unsigned char*)msg4.c_str(), msg4.size(), 11);

  char buf[100];
  int32_t c = bw.readData((unsigned char*)buf, sizeof(buf), 1);
  buf[c] = '\0';

  CPPUNIT_ASSERT_EQUAL(std::string("ello World !!"), std::string(buf));
  CPPUNIT_ASSERT_EQUAL((off_t)14, bw.size());
}

void ByteArrayDiskWriterTest::testWriteAndRead2() {
  ByteArrayDiskWriter bw;

  std::string msg1 = "Hello World";
  bw.writeData((const unsigned char*)msg1.c_str(), msg1.size(), 0);
  std::string msg2 = "From Mars";
  bw.writeData((const unsigned char*)msg2.c_str(), msg2.size(), 6);

  char buf[100];
  int32_t c = bw.readData((unsigned char*)buf, sizeof(buf), 0);
  buf[c] = '\0';

  CPPUNIT_ASSERT_EQUAL(std::string("Hello From Mars"), std::string(buf));
  CPPUNIT_ASSERT_EQUAL((off_t)15, bw.size());
}

} // namespace aria2