1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.tv.util; 18 19 import android.test.MoreAsserts; 20 import android.test.suitebuilder.annotation.SmallTest; 21 22 import junit.framework.TestCase; 23 24 import java.util.Collections; 25 26 /** 27 * Tests for {@link MultiLongSparseArray}. 28 */ 29 @SmallTest 30 public class MultiLongSparseArrayTest extends TestCase { 31 32 public void testEmpty() { 33 MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); 34 assertSame(Collections.EMPTY_SET, sparseArray.get(0)); 35 } 36 37 public void testOneElement() { 38 MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); 39 sparseArray.put(0, "foo"); 40 MoreAsserts.assertContentsInAnyOrder(sparseArray.get(0), "foo"); 41 } 42 43 public void testTwoElements() { 44 MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); 45 sparseArray.put(0, "foo"); 46 sparseArray.put(0, "bar"); 47 MoreAsserts.assertContentsInAnyOrder(sparseArray.get(0), "foo", "bar"); 48 } 49 50 51 public void testClearEmptyCache() { 52 MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); 53 sparseArray.clearEmptyCache(); 54 assertEquals(0, sparseArray.getEmptyCacheSize()); 55 sparseArray.put(0, "foo"); 56 sparseArray.remove(0, "foo"); 57 assertEquals(1, sparseArray.getEmptyCacheSize()); 58 sparseArray.clearEmptyCache(); 59 assertEquals(0, sparseArray.getEmptyCacheSize()); 60 } 61 62 public void testMaxEmptyCacheSize() { 63 MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); 64 sparseArray.clearEmptyCache(); 65 assertEquals(0, sparseArray.getEmptyCacheSize()); 66 for (int i = 0; i <= MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT + 2; i++) { 67 sparseArray.put(i, "foo"); 68 } 69 for (int i = 0; i <= MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT + 2; i++) { 70 sparseArray.remove(i, "foo"); 71 } 72 assertEquals(MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT, 73 sparseArray.getEmptyCacheSize()); 74 sparseArray.clearEmptyCache(); 75 assertEquals(0, sparseArray.getEmptyCacheSize()); 76 } 77 78 public void testReuseEmptySets() { 79 MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); 80 sparseArray.clearEmptyCache(); 81 assertEquals(0, sparseArray.getEmptyCacheSize()); 82 // create a bunch of sets 83 for (int i = 0; i <= MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT + 2; i++) { 84 sparseArray.put(i, "foo"); 85 } 86 // remove them so they are all put in the cache. 87 for (int i = 0; i <= MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT + 2; i++) { 88 sparseArray.remove(i, "foo"); 89 } 90 assertEquals(MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT, 91 sparseArray.getEmptyCacheSize()); 92 93 // now create elements, that use the cached empty sets. 94 for (int i = 0; i < MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT; i++) { 95 sparseArray.put(10 + i, "bar"); 96 assertEquals(MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT - i - 1, 97 sparseArray.getEmptyCacheSize()); 98 } 99 } 100 } 101