summaryrefslogtreecommitdiffstats
path: root/src/test/common/test_bounded_key_counter.cc
blob: a8739405147788140684a3a64f014fe416a28441 (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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// -*- 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) 2015 Red Hat
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation. See file COPYING.
 *
 */
#include "common/bounded_key_counter.h"
#include <gtest/gtest.h>

namespace {

// call get_highest() and return the number of callbacks
template <typename Key, typename Count>
size_t count_highest(BoundedKeyCounter<Key, Count>& counter, size_t count)
{
  size_t callbacks = 0;
  counter.get_highest(count, [&callbacks] (const Key& key, Count count) {
                        ++callbacks;
                      });
  return callbacks;
}

// call get_highest() and return the key/value pairs as a vector
template <typename Key, typename Count,
          typename Vector = std::vector<std::pair<Key, Count>>>
Vector get_highest(BoundedKeyCounter<Key, Count>& counter, size_t count)
{
  Vector results;
  counter.get_highest(count, [&results] (const Key& key, Count count) {
                        results.emplace_back(key, count);
                      });
  return results;
}

} // anonymous namespace

TEST(BoundedKeyCounter, Insert)
{
  BoundedKeyCounter<int, int> counter(2);
  EXPECT_EQ(1, counter.insert(0)); // insert new key
  EXPECT_EQ(2, counter.insert(0)); // increment counter
  EXPECT_EQ(7, counter.insert(0, 5)); // add 5 to counter
  EXPECT_EQ(1, counter.insert(1)); // insert new key
  EXPECT_EQ(0, counter.insert(2)); // reject new key
}

TEST(BoundedKeyCounter, Erase)
{
  BoundedKeyCounter<int, int> counter(10);

  counter.erase(0); // ok to erase nonexistent key
  EXPECT_EQ(1, counter.insert(1, 1));
  EXPECT_EQ(2, counter.insert(2, 2));
  EXPECT_EQ(3, counter.insert(3, 3));
  counter.erase(2);
  counter.erase(1);
  counter.erase(3);
  counter.erase(3);
  EXPECT_EQ(0u, count_highest(counter, 10));
}

TEST(BoundedKeyCounter, Size)
{
  BoundedKeyCounter<int, int> counter(4);
  EXPECT_EQ(0u, counter.size());
  EXPECT_EQ(1, counter.insert(1, 1));
  EXPECT_EQ(1u, counter.size());
  EXPECT_EQ(2, counter.insert(2, 2));
  EXPECT_EQ(2u, counter.size());
  EXPECT_EQ(3, counter.insert(3, 3));
  EXPECT_EQ(3u, counter.size());
  EXPECT_EQ(4, counter.insert(4, 4));
  EXPECT_EQ(4u, counter.size());
  EXPECT_EQ(0, counter.insert(5, 5)); // reject new key
  EXPECT_EQ(4u, counter.size()); // size unchanged
  EXPECT_EQ(5, counter.insert(4, 1)); // update existing key
  EXPECT_EQ(4u, counter.size()); // size unchanged
  counter.erase(2);
  EXPECT_EQ(3u, counter.size());
  counter.erase(2); // erase duplicate
  EXPECT_EQ(3u, counter.size()); // size unchanged
  counter.erase(4);
  EXPECT_EQ(2u, counter.size());
  counter.erase(1);
  EXPECT_EQ(1u, counter.size());
  counter.erase(3);
  EXPECT_EQ(0u, counter.size());
  EXPECT_EQ(6, counter.insert(6, 6));
  EXPECT_EQ(1u, counter.size());
  counter.clear();
  EXPECT_EQ(0u, counter.size());
}

TEST(BoundedKeyCounter, GetHighest)
{
  BoundedKeyCounter<int, int> counter(10);
  using Vector = std::vector<std::pair<int, int>>;

  EXPECT_EQ(0u, count_highest(counter, 0)); // ok to request 0
  EXPECT_EQ(0u, count_highest(counter, 10)); // empty
  EXPECT_EQ(0u, count_highest(counter, 999)); // ok to request count >> 10

  EXPECT_EQ(1, counter.insert(1, 1));
  EXPECT_EQ(Vector({{1,1}}), get_highest(counter, 10));
  EXPECT_EQ(2, counter.insert(2, 2));
  EXPECT_EQ(Vector({{2,2},{1,1}}), get_highest(counter, 10));
  EXPECT_EQ(3, counter.insert(3, 3));
  EXPECT_EQ(Vector({{3,3},{2,2},{1,1}}), get_highest(counter, 10));
  EXPECT_EQ(3, counter.insert(4, 3)); // insert duplicated count=3
  // still returns 4 entries (but order of {3,3} and {4,3} is unspecified)
  EXPECT_EQ(4u, count_highest(counter, 10));
  counter.erase(3);
  EXPECT_EQ(Vector({{4,3},{2,2},{1,1}}), get_highest(counter, 10));
  EXPECT_EQ(0u, count_highest(counter, 0)); // requesting 0 still returns 0
}

TEST(BoundedKeyCounter, Clear)
{
  BoundedKeyCounter<int, int> counter(2);
  EXPECT_EQ(1, counter.insert(0)); // insert new key
  EXPECT_EQ(1, counter.insert(1)); // insert new key
  EXPECT_EQ(2u, count_highest(counter, 2)); // return 2 entries

  counter.clear();

  EXPECT_EQ(0u, count_highest(counter, 2)); // return 0 entries
  EXPECT_EQ(1, counter.insert(1)); // insert new key
  EXPECT_EQ(1, counter.insert(2)); // insert new unique key
  EXPECT_EQ(2u, count_highest(counter, 2)); // return 2 entries
}

// tests for partial sort and invalidation
TEST(BoundedKeyCounter, GetNumSorted)
{
  struct MockCounter : public BoundedKeyCounter<int, int> {
    using BoundedKeyCounter<int, int>::BoundedKeyCounter;
    // expose as public for testing sort invalidations
    using BoundedKeyCounter<int, int>::get_num_sorted;
  };

  MockCounter counter(10);

  EXPECT_EQ(0u, counter.get_num_sorted());
  EXPECT_EQ(0u, count_highest(counter, 10));
  EXPECT_EQ(0u, counter.get_num_sorted());

  EXPECT_EQ(2, counter.insert(2, 2));
  EXPECT_EQ(3, counter.insert(3, 3));
  EXPECT_EQ(4, counter.insert(4, 4));
  EXPECT_EQ(0u, counter.get_num_sorted());

  EXPECT_EQ(0u, count_highest(counter, 0));
  EXPECT_EQ(0u, counter.get_num_sorted());
  EXPECT_EQ(1u, count_highest(counter, 1));
  EXPECT_EQ(1u, counter.get_num_sorted());
  EXPECT_EQ(2u, count_highest(counter, 2));
  EXPECT_EQ(2u, counter.get_num_sorted());
  EXPECT_EQ(3u, count_highest(counter, 10));
  EXPECT_EQ(3u, counter.get_num_sorted());

  EXPECT_EQ(1, counter.insert(1, 1)); // insert at bottom does not invalidate
  EXPECT_EQ(3u, counter.get_num_sorted());

  EXPECT_EQ(4u, count_highest(counter, 10));
  EXPECT_EQ(4u, counter.get_num_sorted());

  EXPECT_EQ(5, counter.insert(5, 5)); // insert at top invalidates sort
  EXPECT_EQ(0u, counter.get_num_sorted());

  EXPECT_EQ(0u, count_highest(counter, 0));
  EXPECT_EQ(0u, counter.get_num_sorted());
  EXPECT_EQ(1u, count_highest(counter, 1));
  EXPECT_EQ(1u, counter.get_num_sorted());
  EXPECT_EQ(2u, count_highest(counter, 2));
  EXPECT_EQ(2u, counter.get_num_sorted());
  EXPECT_EQ(3u, count_highest(counter, 3));
  EXPECT_EQ(3u, counter.get_num_sorted());
  EXPECT_EQ(4u, count_highest(counter, 4));
  EXPECT_EQ(4u, counter.get_num_sorted());
  EXPECT_EQ(5u, count_highest(counter, 10));
  EXPECT_EQ(5u, counter.get_num_sorted());

  // updating an existing counter only invalidates entries <= that counter
  EXPECT_EQ(2, counter.insert(1)); // invalidates {1,2} and {2,2}
  EXPECT_EQ(3u, counter.get_num_sorted());

  EXPECT_EQ(5u, count_highest(counter, 10));
  EXPECT_EQ(5u, counter.get_num_sorted());

  counter.clear(); // invalidates sort
  EXPECT_EQ(0u, counter.get_num_sorted());
}