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 static com.google.common.truth.Truth.assertThat; 20 21 import com.android.tv.testing.constants.ConfigConstants; 22 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 import org.robolectric.RobolectricTestRunner; 26 import org.robolectric.annotation.Config; 27 28 /** Tests for {@link MultiLongSparseArray}. */ 29 @RunWith(RobolectricTestRunner.class) 30 @Config(sdk = ConfigConstants.SDK) 31 public class MultiLongSparseArrayTest { 32 @Test testEmpty()33 public void testEmpty() { 34 MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); 35 assertThat(sparseArray.get(0)).isEmpty(); 36 } 37 38 @Test testOneElement()39 public void testOneElement() { 40 MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); 41 sparseArray.put(0, "foo"); 42 assertThat(sparseArray.get(0)).containsExactly("foo"); 43 } 44 45 @Test testTwoElements()46 public void testTwoElements() { 47 MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); 48 sparseArray.put(0, "foo"); 49 sparseArray.put(0, "bar"); 50 assertThat(sparseArray.get(0)).containsExactly("foo", "bar"); 51 } 52 53 @Test testClearEmptyCache()54 public void testClearEmptyCache() { 55 MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); 56 sparseArray.clearEmptyCache(); 57 assertThat(sparseArray.getEmptyCacheSize()).isEqualTo(0); 58 sparseArray.put(0, "foo"); 59 sparseArray.remove(0, "foo"); 60 assertThat(sparseArray.getEmptyCacheSize()).isEqualTo(1); 61 sparseArray.clearEmptyCache(); 62 assertThat(sparseArray.getEmptyCacheSize()).isEqualTo(0); 63 } 64 65 @Test testMaxEmptyCacheSize()66 public void testMaxEmptyCacheSize() { 67 MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); 68 sparseArray.clearEmptyCache(); 69 assertThat(sparseArray.getEmptyCacheSize()).isEqualTo(0); 70 for (int i = 0; i <= MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT + 2; i++) { 71 sparseArray.put(i, "foo"); 72 } 73 for (int i = 0; i <= MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT + 2; i++) { 74 sparseArray.remove(i, "foo"); 75 } 76 assertThat(sparseArray.getEmptyCacheSize()) 77 .isEqualTo(MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT); 78 sparseArray.clearEmptyCache(); 79 assertThat(sparseArray.getEmptyCacheSize()).isEqualTo(0); 80 } 81 82 @Test testReuseEmptySets()83 public void testReuseEmptySets() { 84 MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); 85 sparseArray.clearEmptyCache(); 86 assertThat(sparseArray.getEmptyCacheSize()).isEqualTo(0); 87 // create a bunch of sets 88 for (int i = 0; i <= MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT + 2; i++) { 89 sparseArray.put(i, "foo"); 90 } 91 // remove them so they are all put in the cache. 92 for (int i = 0; i <= MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT + 2; i++) { 93 sparseArray.remove(i, "foo"); 94 } 95 assertThat(sparseArray.getEmptyCacheSize()) 96 .isEqualTo(MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT); 97 98 // now create elements, that use the cached empty sets. 99 for (int i = 0; i < MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT; i++) { 100 sparseArray.put(10 + i, "bar"); 101 assertThat(sparseArray.getEmptyCacheSize()) 102 .isEqualTo(MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT - i - 1); 103 } 104 } 105 } 106