1 /* 2 * Copyright (C) 2023 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 android.tools.common 18 19 import org.junit.Assert 20 import org.junit.Before 21 import org.junit.Test 22 23 class CacheTest { 24 @Before setupnull25 fun setup() { 26 Cache.clear() 27 } 28 29 @Test testGetnull30 fun testGet() { 31 val element1 = Cache.get(Dummy(0)) 32 val element2 = Cache.get(Dummy(0)) 33 val element3 = Cache.get(Dummy(1)) 34 Assert.assertSame(element2, element1) 35 Assert.assertNotEquals(element3, element1) 36 Assert.assertEquals(2, Cache.size) 37 } 38 39 @Test testClearnull40 fun testClear() { 41 Cache.get(Dummy(0)) 42 Assert.assertEquals(1, Cache.size) 43 Cache.clear() 44 Assert.assertEquals(0, Cache.size) 45 } 46 47 @Test testBackupnull48 fun testBackup() { 49 Cache.get(Dummy(0)) 50 Cache.get(Dummy(1)) 51 val copy = Cache.backup() 52 Cache.get(Dummy(2)) 53 Assert.assertTrue(copy.cache.containsKey(Dummy(0))) 54 Assert.assertTrue(copy.cache.containsKey(Dummy(1))) 55 Assert.assertFalse(copy.cache.containsKey(Dummy(2))) 56 } 57 58 @Test testRestorenull59 fun testRestore() { 60 Cache.get(Dummy(0)) 61 Cache.get(Dummy(1)) 62 val copy = Cache.backup() 63 Cache.get(Dummy(2)) 64 Assert.assertEquals(3, Cache.size) 65 Cache.restore(copy) 66 Assert.assertTrue(copy.cache.containsKey(Dummy(0))) 67 Assert.assertTrue(copy.cache.containsKey(Dummy(1))) 68 Assert.assertFalse(copy.cache.containsKey(Dummy(2))) 69 } 70 71 data class Dummy(val value: Int) 72 73 companion object { 74 private val Cache.size 75 get() = backup().cache.size 76 } 77 } 78