Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
lru-cache-test.cc
Go to the documentation of this file.
1 // Copyright 2015 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "lru-cache.h"
16 
17 #include <boost/thread.hpp>
18 #include <glog/logging.h>
19 #include <gtest/gtest.h>
20 
21 #include "common/init.h"
22 #include "common/logging.h"
23 #include "util/test-info.h"
24 
25 #include "common/names.h"
26 
27 using boost::thread;
28 using boost::thread_group;
29 
30 using namespace impala;
31 
32 TEST(FifoMultimap, Basic) {
34  int result;
35  ASSERT_EQ(3, c.capacity());
36  c.Put(0, 1);
37  c.Put(0, 2);
38  ASSERT_EQ(2, c.size());
39  ASSERT_FALSE(c.Pop(99, &result));
40  c.Pop(0, &result);
41  c.Pop(0, &result);
42  ASSERT_FALSE(c.Pop(0, &result));
43 }
44 
45 TEST(FifoMultimap, Evict) {
47  int result;
48  c.Put(0, 1);
49  c.Put(1, 2);
50  c.Put(2, 3);
51  ASSERT_EQ(3, c.size());
52  c.Put(3, 4);
53  ASSERT_EQ(3, c.size());
54  ASSERT_FALSE(c.Pop(0, &result));
55  ASSERT_TRUE(c.Pop(1, &result));
56  ASSERT_EQ(2, result);
57  ASSERT_FALSE(c.Pop(1, &result));
58  ASSERT_EQ(2, c.size());
59 }
60 
61 TEST(FifoMultimap, Large) {
62  FifoMultimap<int, int> c(1000);
63  int result;
64  for (int i = 0; i < 1000; ++i) {
65  c.Put(i, i);
66  }
67  ASSERT_EQ(1000, c.size());
68  for (int i = 0; i < 1000; ++i) {
69  c.Pop(rand() % 1000, &result);
70  c.Put(rand() % 1000, i);
71  ASSERT_EQ(1000, c.size());
72  }
73 }
74 
75 static int del_sum = 0;
76 void TestDeleter(int* v) { del_sum += *v; }
77 
78 TEST(FifoMultimap, Deleter) {
80  for (int i = 1; i <= 1000; ++i) {
81  c.Put(i, i);
82  }
83  int n = 1000 - c.capacity();
84  // All values from 1 to n have been evicted, so the sum of all values must match del_sum
85  ASSERT_EQ((n * (n + 1)) / 2, del_sum);
86 }
87 
88 static int del_count = 0;
89 void TestCountDeleter(int* v) { ++del_count; }
90 
91 void ParallelPut(const int start, const int end, FifoMultimap<int, int>* shelf) {
92  int val;
93  for (int i = start; i < end; ++i) {
94  if (i % 3 == 0) {
95  if (shelf->Pop(i, &val)) {
96  shelf->Put(i, i);
97  }
98  }
99  shelf->Put(i, i);
100  }
101 }
102 
103 TEST(FifoMultimap, ParallelEviction) {
104  del_sum = 0;
106 
107  thread_group group;
108  size_t count = 100000;
109  size_t thread_count = 10;
110  size_t part = count / thread_count;
111  for (int i = 0; i < thread_count; ++i) {
112  group.add_thread(new thread(ParallelPut, 0, part, &c));
113  }
114  group.join_all();
115  ASSERT_EQ(10, c.size());
116  ASSERT_EQ(count - c.capacity(), del_count);
117 }
118 
119 TEST(FifoMultimap, PopShouldPopMostRecent) {
121  int result;
122  c.Put(1, 1);
123  c.Put(1, 2);
124  c.Put(1, 3);
125  ASSERT_TRUE(c.Pop(1, &result));
126  ASSERT_EQ(3, result);
127  c.Put(1, 3);
128  c.Put(1, 4);
129  ASSERT_EQ(3, c.size());
130  ASSERT_TRUE(c.Pop(1, &result));
131  ASSERT_EQ(4, result);
132  ASSERT_EQ(2, c.size());
133  ASSERT_TRUE(c.Pop(1, &result));
134  ASSERT_EQ(3, result);
135  ASSERT_EQ(1, c.size());
136  ASSERT_TRUE(c.Pop(1, &result));
137  ASSERT_EQ(2, result);
138  ASSERT_EQ(0, c.size());
139 }
140 
141 int main(int argc, char** argv) {
142  ::testing::InitGoogleTest(&argc, argv);
143  return RUN_ALL_TESTS();
144 }
int main(int argc, char **argv)
void Put(const Key &k, const Value &v)
size_t capacity() const
Returns the capacity of the cache.
Definition: lru-cache.h:98
bool Pop(const Key &k, Value *out)
TEST(AtomicTest, Basic)
Definition: atomic-test.cc:28
static int del_count
void ParallelPut(const int start, const int end, FifoMultimap< int, int > *shelf)
static int del_sum
void TestDeleter(int *v)
size_t size()
Returns the total number of entries in the collection.
Definition: lru-cache.h:92
uint64_t count
void TestCountDeleter(int *v)