summaryrefslogtreecommitdiff
path: root/libutils
diff options
context:
space:
mode:
authorSergio Giro <sgiro@google.com>2015-10-13 07:45:36 +0000
committerAndroid Git Automerger <android-git-automerger@android.com>2015-10-13 07:45:36 +0000
commitd155004e78e3426fbff3c6936e29c1016b02aa71 (patch)
tree655d89762ac37239d2d9cb61543fd49cb94e1df5 /libutils
parentd05b4af6593107ec01228f9c2d8f98b857cf054d (diff)
parent51209a8b93d85f8b7f3b6a106c41c1aa03190f2c (diff)
am 51209a8b: am 9443fd94: am f8376607: Merge "system/core: fix iterator for LruCache"
* commit '51209a8b93d85f8b7f3b6a106c41c1aa03190f2c': system/core: fix iterator for LruCache
Diffstat (limited to 'libutils')
-rw-r--r--libutils/tests/LruCache_test.cpp106
1 files changed, 106 insertions, 0 deletions
diff --git a/libutils/tests/LruCache_test.cpp b/libutils/tests/LruCache_test.cpp
index 2ed84d700..580b98058 100644
--- a/libutils/tests/LruCache_test.cpp
+++ b/libutils/tests/LruCache_test.cpp
@@ -293,4 +293,110 @@ TEST_F(LruCacheTest, CallbackOnClear) {
EXPECT_EQ(3, callback.callbackCount);
}
+TEST_F(LruCacheTest, IteratorCheck) {
+ LruCache<int, int> cache(100);
+
+ cache.put(1, 4);
+ cache.put(2, 5);
+ cache.put(3, 6);
+ EXPECT_EQ(3U, cache.size());
+
+ LruCache<int, int>::Iterator it(cache);
+ std::unordered_set<int> returnedValues;
+ while (it.next()) {
+ int v = it.value();
+ // Check we haven't seen the value before.
+ EXPECT_TRUE(returnedValues.find(v) == returnedValues.end());
+ returnedValues.insert(v);
+ }
+ EXPECT_EQ(std::unordered_set<int>({4, 5, 6}), returnedValues);
+}
+
+TEST_F(LruCacheTest, EmptyCacheIterator) {
+ // Check that nothing crashes...
+ LruCache<int, int> cache(100);
+
+ LruCache<int, int>::Iterator it(cache);
+ std::unordered_set<int> returnedValues;
+ while (it.next()) {
+ returnedValues.insert(it.value());
+ }
+ EXPECT_EQ(std::unordered_set<int>(), returnedValues);
+}
+
+TEST_F(LruCacheTest, OneElementCacheIterator) {
+ // Check that nothing crashes...
+ LruCache<int, int> cache(100);
+ cache.put(1, 2);
+
+ LruCache<int, int>::Iterator it(cache);
+ std::unordered_set<int> returnedValues;
+ while (it.next()) {
+ returnedValues.insert(it.value());
+ }
+ EXPECT_EQ(std::unordered_set<int>({ 2 }), returnedValues);
+}
+
+TEST_F(LruCacheTest, OneElementCacheRemove) {
+ LruCache<int, int> cache(100);
+ cache.put(1, 2);
+
+ cache.remove(1);
+
+ LruCache<int, int>::Iterator it(cache);
+ std::unordered_set<int> returnedValues;
+ while (it.next()) {
+ returnedValues.insert(it.value());
+ }
+ EXPECT_EQ(std::unordered_set<int>({ }), returnedValues);
+}
+
+TEST_F(LruCacheTest, Remove) {
+ LruCache<int, int> cache(100);
+ cache.put(1, 4);
+ cache.put(2, 5);
+ cache.put(3, 6);
+
+ cache.remove(2);
+
+ LruCache<int, int>::Iterator it(cache);
+ std::unordered_set<int> returnedValues;
+ while (it.next()) {
+ returnedValues.insert(it.value());
+ }
+ EXPECT_EQ(std::unordered_set<int>({ 4, 6 }), returnedValues);
+}
+
+TEST_F(LruCacheTest, RemoveYoungest) {
+ LruCache<int, int> cache(100);
+ cache.put(1, 4);
+ cache.put(2, 5);
+ cache.put(3, 6);
+
+ cache.remove(3);
+
+ LruCache<int, int>::Iterator it(cache);
+ std::unordered_set<int> returnedValues;
+ while (it.next()) {
+ returnedValues.insert(it.value());
+ }
+ EXPECT_EQ(std::unordered_set<int>({ 4, 5 }), returnedValues);
+}
+
+TEST_F(LruCacheTest, RemoveNonMember) {
+ LruCache<int, int> cache(100);
+ cache.put(1, 4);
+ cache.put(2, 5);
+ cache.put(3, 6);
+
+ cache.remove(7);
+
+ LruCache<int, int>::Iterator it(cache);
+ std::unordered_set<int> returnedValues;
+ while (it.next()) {
+ returnedValues.insert(it.value());
+ }
+ EXPECT_EQ(std::unordered_set<int>({ 4, 5, 6 }), returnedValues);
+}
+
}