summaryrefslogtreecommitdiffstats
path: root/src/test/mgr/test_ttlcache.cc
blob: a1ee0a8626f42a03b7766d8dade26de60df40db1 (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
66
67
68
69
70
#include <iostream>

#include "mgr/TTLCache.h"
#include "gtest/gtest.h"

using namespace std;

TEST(TTLCache, Get) {
	TTLCache<string, int> c{100};
	c.insert("foo", 1);
	int foo = c.get("foo");
	ASSERT_EQ(foo, 1);
}

TEST(TTLCache, Erase) {
	TTLCache<string, int> c{100};
	c.insert("foo", 1);
	int foo = c.get("foo");
	ASSERT_EQ(foo, 1);
	c.erase("foo");
  try{
    foo = c.get("foo");
    FAIL();
  } catch (std::out_of_range& e) {
    SUCCEED();
  }
}

TEST(TTLCache, Clear) {
	TTLCache<string, int> c{100};
	c.insert("foo", 1);
	c.insert("foo2", 2);
	c.clear();
	ASSERT_FALSE(c.size());
}

TEST(TTLCache, NoTTL) {
	TTLCache<string, int> c{100};
	c.insert("foo", 1);
	int foo = c.get("foo");
	ASSERT_EQ(foo, 1);
	c.set_ttl(0);
	c.insert("foo2", 2);
  try{
    foo = c.get("foo2");
    FAIL();
  } catch (std::out_of_range& e) {
    SUCCEED();
  }
}

TEST(TTLCache, SizeLimit) {
	TTLCache<string, int> c{100, 2};
	c.insert("foo", 1);
	c.insert("foo2", 2);
	c.insert("foo3", 3);
	ASSERT_EQ(c.size(), 2);
}

TEST(TTLCache, HitRatio) {
	TTLCache<string, int> c{100};
	c.insert("foo", 1);
	c.insert("foo2", 2);
	c.insert("foo3", 3);
	c.get("foo2");
	c.get("foo3");
	std::pair<uint64_t, uint64_t> hit_miss_ratio = c.get_hit_miss_ratio();
	ASSERT_EQ(std::get<1>(hit_miss_ratio), 3);
	ASSERT_EQ(std::get<0>(hit_miss_ratio), 2);
}