1 /* 2 * Copyright (C) 2024 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.tools.metalava.model.api.surface 18 19 import kotlin.test.assertEquals 20 import kotlin.test.assertFalse 21 import kotlin.test.assertNotEquals 22 import kotlin.test.assertSame 23 import kotlin.test.assertTrue 24 import org.junit.FixMethodOrder 25 import org.junit.Test 26 import org.junit.runners.MethodSorters 27 28 @FixMethodOrder(MethodSorters.NAME_ASCENDING) 29 class ApiVariantSetTest { 30 private val apiSurfaces = ApiSurfaces.create(needsBase = true) 31 32 private val main = apiSurfaces.main 33 private val base = apiSurfaces.base!! 34 35 @Test Test creationnull36 fun `Test creation`() { 37 val mutable = MutableApiVariantSet.setOf(apiSurfaces) 38 39 assertTrue(mutable.isEmpty(), "isEmpty") 40 assertFalse(mutable.isNotEmpty(), "isNotEmpty") 41 42 for (variant in apiSurfaces.variants) { 43 assertFalse(mutable.contains(variant), "contains($variant)") 44 } 45 46 assertEquals("ApiVariantSet[]", mutable.toString(), "empty set") 47 } 48 49 @Test Test mutationsnull50 fun `Test mutations`() { 51 val mutable = MutableApiVariantSet.setOf(apiSurfaces) 52 53 val mainCore = main.variantFor(ApiVariantType.CORE) 54 val baseRemoved = base.variantFor(ApiVariantType.REMOVED) 55 56 mutable.add(mainCore) 57 58 assertEquals("ApiVariantSet[main(C)]", mutable.toString()) 59 assertTrue(mainCore in mutable, "main(C) should contain main(CORE)") 60 assertFalse(baseRemoved in mutable, "main(C) should not contain base(REMOVED)") 61 assertTrue(mutable.containsAny(main), "main(C) should contain something from main") 62 assertFalse(mutable.containsAny(base), "main(C) should not contain anything from base") 63 64 mutable.add(baseRemoved) 65 66 assertEquals("ApiVariantSet[base(R),main(C)]", mutable.toString()) 67 assertTrue(mainCore in mutable, "base(R),main(C) should contain main(CORE)") 68 assertTrue(baseRemoved in mutable, "base(R),main(C) should contain base(REMOVED)") 69 assertTrue(mutable.containsAny(main), "base(R),main(C) should contain something from main") 70 assertTrue(mutable.containsAny(base), "base(R),main(C) should contain something from base") 71 72 mutable.remove(mainCore) 73 74 assertEquals("ApiVariantSet[base(R)]", mutable.toString()) 75 assertFalse(mainCore in mutable, "base(R),main(C) should not contain main(CORE)") 76 assertTrue(baseRemoved in mutable, "base(R),main(C) should contain base(REMOVED)") 77 assertFalse( 78 mutable.containsAny(main), 79 "base(R),main(C) should not contain anything from main" 80 ) 81 assertTrue(mutable.containsAny(base), "base(R),main(C) should contain something from base") 82 } 83 84 @Test Test clearnull85 fun `Test clear`() { 86 val mutable = MutableApiVariantSet.setOf(apiSurfaces) 87 88 val mainCore = main.variantFor(ApiVariantType.CORE) 89 val baseCore = base.variantFor(ApiVariantType.CORE) 90 91 mutable.add(mainCore) 92 mutable.add(baseCore) 93 94 assertFalse(mutable.isEmpty(), "expected not empty before clear") 95 mutable.clear() 96 assertTrue(mutable.isEmpty(), "expected empty before clear") 97 } 98 99 @Test Test mutable and immutablenull100 fun `Test mutable and immutable`() { 101 val mutable = MutableApiVariantSet.setOf(apiSurfaces) 102 103 val mainCore = main.variantFor(ApiVariantType.CORE) 104 val baseCore = base.variantFor(ApiVariantType.CORE) 105 106 mutable.add(mainCore) 107 mutable.add(baseCore) 108 109 // Explicit type is needed to ensure correct type inference. 110 val immutable: BaseApiVariantSet = mutable.toImmutable() 111 assertEquals(mutable, immutable, "mutable and immutable set should be equal") 112 assertEquals( 113 mutable.hashCode(), 114 immutable.hashCode(), 115 "mutable and immutable set hashCode should be equal" 116 ) 117 118 mutable.remove(mainCore) 119 120 assertNotEquals(mutable, immutable, "mutable and immutable set should not be equal") 121 assertNotEquals( 122 mutable.hashCode(), 123 immutable.hashCode(), 124 "mutable and immutable set hashCode should not be equal" 125 ) 126 127 val anotherImmutable: BaseApiVariantSet = mutable.toImmutable() 128 129 assertNotEquals( 130 anotherImmutable, 131 immutable, 132 "anotherImmutable and immutable set should not be equal" 133 ) 134 assertNotEquals( 135 anotherImmutable.hashCode(), 136 immutable.hashCode(), 137 "anotherImmutable and immutable set hashCode should not be equal" 138 ) 139 } 140 141 @Test Test empty variant setnull142 fun `Test empty variant set`() { 143 val empty = apiSurfaces.emptyVariantSet 144 assertEquals(empty.toString(), "ApiVariantSet[]", "empty toString") 145 146 val mutable = MutableApiVariantSet.setOf(apiSurfaces) 147 assertSame(empty, mutable.toImmutable(), "empty mutable to immutable") 148 } 149 } 150