summaryrefslogtreecommitdiffstats
path: root/src/test/common/test_lruset.cc
blob: 64e823e2f5bb899b00a470dfe2e4441250fdeac9 (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
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2013 Inktank <info@inktank.com>
 *
 * LGPL-2.1 (see COPYING-LGPL2.1) or later
 */

#include <iostream>
#include <gtest/gtest.h>

#include "common/LRUSet.h"

struct thing {
  int a;
  thing(int i) : a(i) {}
  friend bool operator==(const thing &a, const thing &b) {
    return a.a == b.a;
  }
  friend std::size_t hash_value(const thing &value) {
    return value.a;
  }
};

namespace std {
  template<> struct hash<thing> {
    size_t operator()(const thing& r) const {
      return r.a;
    }
  };
}


TEST(LRUSet, insert_complex) {
  LRUSet<thing> s;
  s.insert(thing(1));
  s.insert(thing(2));

  ASSERT_TRUE(s.contains(thing(1)));
  ASSERT_TRUE(s.contains(thing(2)));
  ASSERT_FALSE(s.contains(thing(3)));
}

TEST(LRUSet, insert) {
  LRUSet<int> s;
  s.insert(1);
  s.insert(2);

  ASSERT_TRUE(s.contains(1));
  ASSERT_TRUE(s.contains(2));
  ASSERT_FALSE(s.contains(3));
}

TEST(LRUSet, erase) {
  LRUSet<int> s;
  s.insert(1);
  s.insert(2);
  s.insert(3);

  s.erase(2);
  ASSERT_TRUE(s.contains(1));
  ASSERT_FALSE(s.contains(2));
  ASSERT_TRUE(s.contains(3));
  s.prune(1);
  ASSERT_TRUE(s.contains(3));
  ASSERT_FALSE(s.contains(1));
}

TEST(LRUSet, prune) {
  LRUSet<int> s;
  int max = 1000;
  for (int i=0; i<max; ++i) {
    s.insert(i);
    s.prune(max / 10);
  }
  s.prune(0);
  ASSERT_TRUE(s.empty());
}

TEST(LRUSet, lru) {
  LRUSet<int> s;
  s.insert(1);
  s.insert(2);
  s.insert(3);
  s.prune(2);
  ASSERT_FALSE(s.contains(1));
  ASSERT_TRUE(s.contains(2));
  ASSERT_TRUE(s.contains(3));

  s.insert(2);
  s.insert(4);
  s.prune(2);
  ASSERT_FALSE(s.contains(3));
  ASSERT_TRUE(s.contains(2));
  ASSERT_TRUE(s.contains(4));
}

TEST(LRUSet, copy) {
  LRUSet<int> a, b;
  a.insert(1);
  b.insert(2);
  b.insert(3);
  a = b;
  ASSERT_FALSE(a.contains(1));
  ASSERT_TRUE(a.contains(2));
  ASSERT_TRUE(a.contains(3));
}