File: lru_cache.cpp

package info (click to toggle)
cpptrace 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,996 kB
  • sloc: cpp: 15,646; python: 962; ansic: 155; sh: 103; makefile: 86
file content (104 lines) | stat: -rw-r--r-- 2,654 bytes parent folder | download
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
#include <gtest/gtest.h>

#include "utils/lru_cache.hpp"

using cpptrace::detail::lru_cache;
using cpptrace::detail::nullopt;

namespace {

TEST(LruCacheTest, DefaultConstructor) {
    lru_cache<int, int> cache;
    EXPECT_EQ(cache.size(), 0);
}

TEST(LruCacheTest, MaybeGet) {
    lru_cache<int, int> cache;
    auto result = cache.maybe_get(42);
    EXPECT_FALSE(result.has_value());
}

TEST(LruCacheTest, InsertAndGet) {
    lru_cache<int, int> cache;
    cache.insert(42, 50);
    auto result = cache.maybe_get(42);
    ASSERT_TRUE(result.has_value());
    EXPECT_EQ(result.unwrap(), 50);
}

TEST(LruCacheTest, ConstGet) {
    lru_cache<int, int> cache;
    cache.insert(42, 50);
    const lru_cache<int, int>& cache_ref = cache;
    auto result = cache_ref.maybe_get(42);
    ASSERT_TRUE(result.has_value());
    EXPECT_EQ(result.unwrap(), 50);
}

TEST(LruCacheTest, Set) {
    lru_cache<int, int> cache;
    cache.set(42, 50);
    auto result = cache.maybe_get(42);
    ASSERT_TRUE(result.has_value());
    EXPECT_EQ(result.unwrap(), 50);
    cache.set(42, 60);
    auto result2 = cache.maybe_get(42);
    ASSERT_TRUE(result2.has_value());
    EXPECT_EQ(result2.unwrap(), 60);
}

TEST(LruCacheTest, NoMaxSize) {
    lru_cache<int, int> cache;
    for(int i = 0; i < 1000; i++) {
        cache.insert(i, i + 50);
    }
    EXPECT_EQ(cache.size(), 1000);
    for(int i = 0; i < 1000; i++) {
        EXPECT_EQ(cache.maybe_get(i).unwrap(), i + 50);
    }
}

TEST(LruCacheTest, MaxSize) {
    lru_cache<int, int> cache(20);
    for(int i = 0; i < 1000; i++) {
        cache.insert(i, i + 50);
    }
    EXPECT_EQ(cache.size(), 20);
    for(int i = 0; i < 1000 - 20; i++) {
        EXPECT_FALSE(cache.maybe_get(i).has_value());
    }
    for(int i = 1000 - 20; i < 1000; i++) {
        EXPECT_EQ(cache.maybe_get(i).unwrap(), i + 50);
    }
}

TEST(LruCacheTest, SizeAfterInserts) {
    lru_cache<int, int> cache;
    for(int i = 0; i < 1000; i++) {
        cache.insert(i, i + 50);
    }
    EXPECT_EQ(cache.size(), 1000);
    cache.set_max_size(20);
    EXPECT_EQ(cache.size(), 20);
    for(int i = 0; i < 1000 - 20; i++) {
        EXPECT_FALSE(cache.maybe_get(i).has_value());
    }
    for(int i = 1000 - 20; i < 1000; i++) {
        EXPECT_EQ(cache.maybe_get(i).unwrap(), i + 50);
    }
}

TEST(LruCacheTest, Touch) {
    lru_cache<int, int> cache(20);
    for(int i = 0; i < 1000; i++) {
        cache.maybe_touch(0);
        cache.insert(i, i + 50);
    }
    EXPECT_EQ(cache.size(), 20);
    for(int i = 1000 - 19; i < 1000; i++) {
        EXPECT_EQ(cache.maybe_get(i).unwrap(), i + 50);
    }
    EXPECT_EQ(cache.maybe_get(0).unwrap(), 50);
}

}