aboutsummaryrefslogtreecommitdiffstats
path: root/staging_vespalib/src/tests/stllike/cache_test.cpp
blob: 2a070fa8ab2fac6aa2d087dfc9e52fd181a87104 (plain) (blame)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/stllike/string.h>
#include <vespa/vespalib/stllike/cache.hpp>
#include <map>

using namespace vespalib;

template<typename K, typename V>
class Map : public std::map<K, V> {
    typedef typename std::map<K, V>::const_iterator const_iterator;
    typedef std::map<K, V> M;
public:
    bool read(const K & k, V & v) const {
        const_iterator found = M::find(k);
        bool ok(found != this->end());
        if (ok) {
            v = found->second;
        }
        return ok;
    }
    void write(const K & k, const V & v) {
        (*this)[k] = v;
    }
    void erase(const K & k) {
        M::erase(k);
    }
};

class Test : public TestApp
{
public:
    int Main() override;
private:
    typedef LruParam<uint32_t, string> P;
    typedef Map<uint32_t, string> B;
    void testCache();
    void testCacheSize();
    void testCacheSizeDeep();
    void testCacheEntriesHonoured();
    void testCacheMaxSizeHonoured();
    void testThatMultipleRemoveOnOverflowIsFine();
};

int
Test::Main()
{
    TEST_INIT("cache_test");
    testCache();
    testCacheSize();
    testCacheSizeDeep();
    testCacheEntriesHonoured();
    testCacheMaxSizeHonoured();
    testThatMultipleRemoveOnOverflowIsFine();
    TEST_DONE();
}

void Test::testCache()
{
    B m;
    cache< CacheParam<P, B> > cache(m, -1);
    // Verfify start conditions.
    EXPECT_TRUE(cache.size() == 0);
    EXPECT_TRUE( ! cache.hasKey(1) );
    cache.write(1, "First inserted string");
    EXPECT_TRUE( cache.hasKey(1) );
    m[2] = "String inserted beneath";
    EXPECT_TRUE( ! cache.hasKey(2) );
    EXPECT_EQUAL( cache.read(2), "String inserted beneath");
    EXPECT_TRUE( cache.hasKey(2) );
    cache.erase(1);
    EXPECT_TRUE( ! cache.hasKey(1) );
    EXPECT_TRUE(cache.size() == 1);
}

void Test::testCacheSize()
{
    B m;
    cache< CacheParam<P, B> > cache(m, -1);
    cache.write(1, "10 bytes string");
    EXPECT_EQUAL(80u, cache.sizeBytes());
}

void Test::testCacheSizeDeep()
{
    B m;
    cache< CacheParam<P, B, zero<uint32_t>, size<string> > > cache(m, -1);
    cache.write(1, "15 bytes string");
    EXPECT_EQUAL(95u, cache.sizeBytes());
}

void Test::testCacheEntriesHonoured()
{
    B m;
    cache< CacheParam<P, B, zero<uint32_t>, size<string> > > cache(m, -1);
    cache.maxElements(1);
    cache.write(1, "15 bytes string");
    EXPECT_EQUAL(1u, cache.size());
    EXPECT_EQUAL(95u, cache.sizeBytes());
    cache.write(2, "16 bytes stringg");
    EXPECT_EQUAL(1u, cache.size());
    EXPECT_TRUE( cache.hasKey(2) );
    EXPECT_FALSE( cache.hasKey(1) );
    EXPECT_EQUAL(96u, cache.sizeBytes());
}

void Test::testCacheMaxSizeHonoured()
{
    B m;
    cache< CacheParam<P, B, zero<uint32_t>, size<string> > > cache(m, 200);
    cache.write(1, "15 bytes string");
    EXPECT_EQUAL(1u, cache.size());
    EXPECT_EQUAL(95u, cache.sizeBytes());
    cache.write(2, "16 bytes stringg");
    EXPECT_EQUAL(2u, cache.size());
    EXPECT_EQUAL(191u, cache.sizeBytes());
    cache.write(3, "17 bytes stringgg");
    EXPECT_EQUAL(3u, cache.size());
    EXPECT_EQUAL(288u, cache.sizeBytes());
    cache.write(4, "18 bytes stringggg");
    EXPECT_EQUAL(3u, cache.size());
    EXPECT_EQUAL(291u, cache.sizeBytes());
}

void Test::testThatMultipleRemoveOnOverflowIsFine()
{
    B m;
    cache< CacheParam<P, B, zero<uint32_t>, size<string> > > cache(m, 2000);
    
    for (size_t j(0); j < 5; j++) {
        for (size_t i(0); cache.size() == i; i++) {
            cache.write(j*53+i, "a");
        }
    }
    EXPECT_EQUAL(25u, cache.size());
    EXPECT_EQUAL(2025u, cache.sizeBytes());
    EXPECT_FALSE( cache.hasKey(0) );
    string ls("long string aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
              "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
              "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
              "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
              "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
              "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    string vls=ls+ls+ls+ls+ls+ls;
    cache.write(53+5, ls);
    EXPECT_EQUAL(25u, cache.size());
    EXPECT_EQUAL(2498u, cache.sizeBytes());
    EXPECT_FALSE( cache.hasKey(1) );
    cache.write(53*7+5, ls);
    EXPECT_EQUAL(19u, cache.size());
    EXPECT_EQUAL(2485u, cache.sizeBytes());
    EXPECT_FALSE( cache.hasKey(2) );
    cache.write(53*8+5, vls);
    EXPECT_EQUAL(14u, cache.size());
    EXPECT_EQUAL(4923u, cache.sizeBytes());
    cache.write(53*9+6, vls);
    EXPECT_EQUAL(1u, cache.size());
    EXPECT_EQUAL(2924u, cache.sizeBytes());
}


TEST_APPHOOK(Test)