File: CacheTest_PushAndPop.cpp

package info (click to toggle)
cryfs 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,412 kB
  • sloc: cpp: 150,187; asm: 10,493; python: 1,455; javascript: 65; sh: 50; makefile: 17; xml: 7
file content (133 lines) | stat: -rw-r--r-- 3,756 bytes parent folder | download | duplicates (4)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "testutils/CacheTest.h"

#include "blockstore/implementations/caching/cache/Cache.h"
#include "testutils/MinimalKeyType.h"
#include "testutils/MinimalValueType.h"
#include <cpp-utils/pointer/unique_ref_boost_optional_gtest_workaround.h>


using namespace blockstore::caching;

class CacheTest_PushAndPop: public CacheTest {};

TEST_F(CacheTest_PushAndPop, PopNonExistingEntry_EmptyCache) {
  EXPECT_EQ(boost::none, pop(10));
}

TEST_F(CacheTest_PushAndPop, PopNonExistingEntry_NonEmptyCache) {
  push(9, 10);
  EXPECT_EQ(boost::none, pop(10));
}

TEST_F(CacheTest_PushAndPop, PopNonExistingEntry_FullCache) {
  //Add a lot of even numbered keys
  for (int i = 0; i < static_cast<int>(MAX_ENTRIES); ++i) {
    push(2*i, 2*i);
  }
  //Request an odd numbered key
  EXPECT_EQ(boost::none, pop(9));
}

TEST_F(CacheTest_PushAndPop, OneEntry) {
  push(10, 20);
  EXPECT_EQ(20, pop(10).value());
}

TEST_F(CacheTest_PushAndPop, MultipleEntries) {
  push(10, 20);
  push(20, 30);
  push(30, 40);
  EXPECT_EQ(30, pop(20).value());
  EXPECT_EQ(20, pop(10).value());
  EXPECT_EQ(40, pop(30).value());
}

TEST_F(CacheTest_PushAndPop, FullCache) {
  for(int i = 0; i < static_cast<int>(MAX_ENTRIES); ++i) {
    push(i, 2*i);
  }
  for(int i = 0; i < static_cast<int>(MAX_ENTRIES); ++i) {
    EXPECT_EQ(2*i, pop(i).value());
  }
}

TEST_F(CacheTest_PushAndPop, FullCache_PushNonOrdered_PopOrdered) {
  for(int i = 1; i < static_cast<int>(MAX_ENTRIES); i += 2) {
    push(i, 2*i);
  }
  for(int i = 0; i < static_cast<int>(MAX_ENTRIES); i += 2) {
    push(i, 2*i);
  }
  for(int i = 0; i < static_cast<int>(MAX_ENTRIES); ++i) {
    EXPECT_EQ(2*i, pop(i).value());
  }
}

TEST_F(CacheTest_PushAndPop, FullCache_PushOrdered_PopNonOrdered) {
  for(int i = 0; i < static_cast<int>(MAX_ENTRIES); ++i) {
    push(i, 2*i);
  }
  for(int i = 1; i < static_cast<int>(MAX_ENTRIES); i += 2) {
    EXPECT_EQ(2*i, pop(i).value());
  }
  for(int i = 0; i < static_cast<int>(MAX_ENTRIES); i += 2) {
    EXPECT_EQ(2*i, pop(i).value());
  }
}

int roundDownToEven(int number) {
  if (number % 2 == 0) {
    return number;
  } else {
    return number - 1;
  }
}

int roundDownToOdd(int number) {
  if (number % 2 != 0) {
    return number;
  } else {
    return number - 1;
  }
}

TEST_F(CacheTest_PushAndPop, FullCache_PushNonOrdered_PopNonOrdered) {
  for(int i = roundDownToEven(MAX_ENTRIES - 1); i >= 0; i -= 2) {
    push(i, 2*i);
  }
  for(int i = 1; i < static_cast<int>(MAX_ENTRIES); i += 2) {
    push(i, 2*i);
  }
  for(int i = roundDownToOdd(MAX_ENTRIES-1); i >= 0; i -= 2) {
    EXPECT_EQ(2*i, pop(i).value());
  }
  for(int i = 0; i < static_cast<int>(MAX_ENTRIES); i += 2) {
    EXPECT_EQ(2*i, pop(i).value());
  }
}

TEST_F(CacheTest_PushAndPop, MoreThanFullCache) {
  for(int i = 0; i < static_cast<int>(MAX_ENTRIES + 2); ++i) {
    push(i, 2*i);
  }
  //Check that the oldest two elements got deleted automatically
  EXPECT_EQ(boost::none, pop(0));
  EXPECT_EQ(boost::none, pop(1));
  //Check the other elements are still there
  for(int i = 2; i < static_cast<int>(MAX_ENTRIES + 2); ++i) {
    EXPECT_EQ(2*i, pop(i).value());
  }
}

TEST_F(CacheTest_PushAndPop, AfterTimeout) {
  constexpr double TIMEOUT1_SEC = Cache::MAX_LIFETIME_SEC * 3/4;
  constexpr double TIMEOUT2_SEC = Cache::PURGE_LIFETIME_SEC * 3/4;
  static_assert(TIMEOUT1_SEC + TIMEOUT2_SEC > Cache::MAX_LIFETIME_SEC, "Ensure that our chosen timeouts push the first entry out of the cache");

  push(10, 20);
  boost::this_thread::sleep_for(boost::chrono::milliseconds(static_cast<int>(1000 * TIMEOUT1_SEC)));
  push(20, 30);
  boost::this_thread::sleep_for(boost::chrono::milliseconds(static_cast<int>(1000 * TIMEOUT2_SEC)));
  EXPECT_EQ(boost::none, pop(10));
  EXPECT_EQ(30, pop(20).value());
}