Belle II Software development
mrucache.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <framework/core/MRUCache.h>
9#include <gtest/gtest.h>
10#include <iostream>
11#include <string>
12
13using namespace std;
14using namespace Belle2;
15
16namespace {
18 TEST(MRUCache, Constructor)
19 {
20 MRUCache<string, string> cache(100);
21 cache.insert("foo", "bar");
22 string result("boo");
23 EXPECT_FALSE(cache.retrieve("foobar", result));
24 EXPECT_EQ(result, "boo");
25 EXPECT_TRUE(cache.retrieve("foo", result));
26 EXPECT_EQ(result, "bar");
27 }
28
30 TEST(MRUCache, InsertRetrieve)
31 {
32 MRUCache<int, int> cache(5);
33 for (int i = 0; i < 10; ++i) {
34 cache.insert(i, -i);
35 }
36
37 EXPECT_EQ(cache.size(), 5u);
38 MRUCache<int, int>::iterator it = cache.begin();
39 for (int i = 9; i >= 5; --i) {
40 EXPECT_EQ(it->first, i);
41 EXPECT_EQ(it->second, -i);
42 ++it;
43 }
44 int var;
45 EXPECT_TRUE(cache.retrieve(5, var));
46 EXPECT_EQ(cache.begin()->first, 5);
47 EXPECT_EQ(cache.begin()->second, -5);
48
49 cache.clear();
50 EXPECT_EQ(cache.size(), 0u);
51 var = -1;
52 EXPECT_FALSE(cache.retrieve(5, var));
53 EXPECT_EQ(var, -1);
54 }
55
57 TEST(MRUCache, Update)
58 {
59 MRUCache<int, int> cache(5);
60 cache.insert(1, 1);
61 int var;
62 EXPECT_EQ(cache.size(), 1u);
63 EXPECT_TRUE(cache.retrieve(1, var));
64 EXPECT_EQ(var, 1);
65 cache.insert(1, 2);
66 EXPECT_EQ(cache.size(), 1u);
67 EXPECT_TRUE(cache.retrieve(1, var));
68 EXPECT_EQ(var, 2);
69 }
70
72 TEST(MRUCache, Statistics)
73 {
74 MRUCache<int, int> cache(1);
75 int var;
76
77 EXPECT_EQ(cache.getHits(), 0u);
78 EXPECT_EQ(cache.getMisses(), 0u);
79 EXPECT_EQ(cache.getOverflows(), 0u);
80
81 cache.retrieve(0, var);
82 EXPECT_EQ(cache.getMisses(), 1u);
83 cache.retrieve(0, var);
84 EXPECT_EQ(cache.getMisses(), 2u);
85
86 cache.insert(0, 0);
87 EXPECT_EQ(cache.getOverflows(), 0u);
88 cache.insert(1, 0);
89 EXPECT_EQ(cache.getOverflows(), 1u);
90 cache.insert(0, 0);
91 EXPECT_EQ(cache.getOverflows(), 2u);
92
93 cache.retrieve(0, var);
94 EXPECT_EQ(cache.getHits(), 1u);
95 cache.retrieve(0, var);
96 EXPECT_EQ(cache.getHits(), 2u);
97
98 cache.clear();
99 EXPECT_EQ(cache.getHits(), 0u);
100 EXPECT_EQ(cache.getMisses(), 0u);
101 EXPECT_EQ(cache.getOverflows(), 0u);
102 }
103} // namespace
Class implementing a generic Most Recently Used cache.
Definition: MRUCache.h:48
container_type::const_iterator iterator
iterator over all cached items, sorted by access: most recent used first
Definition: MRUCache.h:63
Abstract base class for different kinds of events.
STL namespace.