File: WrDiskCacheTest.cc

package info (click to toggle)
aria2 1.37.0%2Bdebian-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,128 kB
  • sloc: cpp: 115,006; ansic: 9,140; makefile: 1,466; ruby: 475; python: 373; sh: 260; xml: 176
file content (77 lines) | stat: -rw-r--r-- 2,132 bytes parent folder | download | duplicates (6)
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
66
67
68
69
70
71
72
73
74
75
76
77
#include "WrDiskCache.h"

#include <cstring>

#include <cppunit/extensions/HelperMacros.h>

#include "TestUtil.h"
#include "DirectDiskAdaptor.h"
#include "ByteArrayDiskWriter.h"

namespace aria2 {

class WrDiskCacheTest : public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(WrDiskCacheTest);
  CPPUNIT_TEST(testAdd);
  CPPUNIT_TEST_SUITE_END();

  std::shared_ptr<DirectDiskAdaptor> adaptor_;
  ByteArrayDiskWriter* writer_;

public:
  void setUp()
  {
    adaptor_ = std::make_shared<DirectDiskAdaptor>();
    auto dw = make_unique<ByteArrayDiskWriter>();
    writer_ = dw.get();
    adaptor_->setDiskWriter(std::move(dw));
  }

  void testAdd();
};

CPPUNIT_TEST_SUITE_REGISTRATION(WrDiskCacheTest);

void WrDiskCacheTest::testAdd()
{
  WrDiskCache dc(20);
  CPPUNIT_ASSERT_EQUAL((size_t)0, dc.getSize());
  WrDiskCacheEntry e1(adaptor_);
  e1.cacheData(createDataCell(0, "who knows?"));
  CPPUNIT_ASSERT(dc.add(&e1));
  CPPUNIT_ASSERT_EQUAL((size_t)10, dc.getSize());

  WrDiskCacheEntry e2(adaptor_);
  e2.cacheData(createDataCell(21, "seconddata"));
  CPPUNIT_ASSERT(dc.add(&e2));
  CPPUNIT_ASSERT_EQUAL((size_t)20, dc.getSize());

  WrDiskCacheEntry e3(adaptor_);
  e3.cacheData(createDataCell(10, "hello"));
  CPPUNIT_ASSERT(dc.add(&e3));
  CPPUNIT_ASSERT_EQUAL((size_t)15, dc.getSize());
  // e1 is flushed to the disk
  CPPUNIT_ASSERT_EQUAL(std::string("who knows?"), writer_->getString());
  CPPUNIT_ASSERT_EQUAL((size_t)0, e1.getSize());

  e3.cacheData(createDataCell(15, " world"));
  CPPUNIT_ASSERT(dc.update(&e3, 6));

  // e3 is flushed to the disk
  CPPUNIT_ASSERT_EQUAL(std::string("who knows?hello world"),
                       writer_->getString());
  CPPUNIT_ASSERT_EQUAL((size_t)0, e3.getSize());
  CPPUNIT_ASSERT_EQUAL((size_t)10, dc.getSize());

  e2.cacheData(createDataCell(31, "01234567890"));
  CPPUNIT_ASSERT(dc.update(&e2, 11));
  // e2 is flushed to the disk
  CPPUNIT_ASSERT_EQUAL(
      std::string("who knows?hello worldseconddata01234567890"),
      writer_->getString());
  CPPUNIT_ASSERT_EQUAL((size_t)0, e2.getSize());
  CPPUNIT_ASSERT_EQUAL((size_t)0, dc.getSize());
}

} // namespace aria2