summaryrefslogtreecommitdiffstats
path: root/libutils
diff options
context:
space:
mode:
authorSergio Giro <sgiro@google.com>2015-11-20 17:17:38 +0000
committerandroid-build-merger <android-build-merger@google.com>2015-11-20 17:17:38 +0000
commit43f13d0d689b96d5b3b995d727e672864250919a (patch)
tree97a6f586e68c2eeca777912b9289b06195ef781c /libutils
parent3e83beff9df25b8f7c49ebeb8add115946847a38 (diff)
parenta96cd36afbff682e24365092dd85bbe9c9165afc (diff)
downloadsystem_core-43f13d0d689b96d5b3b995d727e672864250919a.tar.gz
system_core-43f13d0d689b96d5b3b995d727e672864250919a.tar.bz2
system_core-43f13d0d689b96d5b3b995d727e672864250919a.zip
Merge "libutils: fix cache removal when callback invalidates the key" am: b0df9dca7c am: e483305e47
am: a96cd36afb * commit 'a96cd36afbff682e24365092dd85bbe9c9165afc': libutils: fix cache removal when callback invalidates the key
Diffstat (limited to 'libutils')
-rw-r--r--libutils/tests/LruCache_test.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/libutils/tests/LruCache_test.cpp b/libutils/tests/LruCache_test.cpp
index 580b98058..dd95c576d 100644
--- a/libutils/tests/LruCache_test.cpp
+++ b/libutils/tests/LruCache_test.cpp
@@ -73,6 +73,13 @@ struct ComplexValue {
ssize_t ComplexValue::instanceCount = 0;
+struct KeyWithPointer {
+ int *ptr;
+ bool operator ==(const KeyWithPointer& other) const {
+ return *ptr == *other.ptr;
+ }
+};
+
} // namespace
@@ -84,6 +91,10 @@ template<> inline android::hash_t hash_type(const ComplexKey& value) {
return hash_type(value.k);
}
+template<> inline android::hash_t hash_type(const KeyWithPointer& value) {
+ return hash_type(*value.ptr);
+}
+
class EntryRemovedCallback : public OnEntryRemoved<SimpleKey, StringValue> {
public:
EntryRemovedCallback() : callbackCount(0), lastKey(-1), lastValue(NULL) { }
@@ -98,6 +109,14 @@ public:
StringValue lastValue;
};
+class InvalidateKeyCallback : public OnEntryRemoved<KeyWithPointer, StringValue> {
+public:
+ void operator()(KeyWithPointer& k, StringValue&) {
+ delete k.ptr;
+ k.ptr = nullptr;
+ }
+};
+
class LruCacheTest : public testing::Test {
protected:
virtual void SetUp() {
@@ -293,6 +312,25 @@ TEST_F(LruCacheTest, CallbackOnClear) {
EXPECT_EQ(3, callback.callbackCount);
}
+TEST_F(LruCacheTest, CallbackRemovesKeyWorksOK) {
+ LruCache<KeyWithPointer, StringValue> cache(1);
+ InvalidateKeyCallback callback;
+ cache.setOnEntryRemovedListener(&callback);
+ KeyWithPointer key1;
+ key1.ptr = new int(1);
+ KeyWithPointer key2;
+ key2.ptr = new int(2);
+
+ cache.put(key1, "one");
+ // As the size of the cache is 1, the put will call the callback.
+ // Make sure everything goes smoothly even if the callback invalidates
+ // the key (b/24785286)
+ cache.put(key2, "two");
+ EXPECT_EQ(1U, cache.size());
+ EXPECT_STREQ("two", cache.get(key2));
+ cache.clear();
+}
+
TEST_F(LruCacheTest, IteratorCheck) {
LruCache<int, int> cache(100);