diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2022-04-27 21:19:53 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2022-04-27 21:19:53 +0000 |
commit | 4ef89db433d31c00c6d189047143045cd5e92b8b (patch) | |
tree | 726e5607db73d3507341014b4a2fa74c8c243569 | |
parent | 8c3ad61eb5b48e6838e6dfe2377a451bb43ee797 (diff) | |
parent | 55ec1c6652f40592d67c8255fb395a6e2b333b1e (diff) | |
download | support-4ef89db433d31c00c6d189047143045cd5e92b8b.tar.gz |
Merge changes Ida0eac0e,I59be198f into androidx-main
* changes:
Convert LongSparseArrayTest to kotlin
rename and move LongSparseArrayTest
-rw-r--r-- | collection/collection/src/jvmTest/java/androidx/collection/LongSparseArrayTest.java | 293 | ||||
-rw-r--r-- | collection/collection/src/jvmTest/kotlin/androidx/collection/LongSparseArrayTest.kt | 286 |
2 files changed, 286 insertions, 293 deletions
diff --git a/collection/collection/src/jvmTest/java/androidx/collection/LongSparseArrayTest.java b/collection/collection/src/jvmTest/java/androidx/collection/LongSparseArrayTest.java deleted file mode 100644 index b984a12ad0b..00000000000 --- a/collection/collection/src/jvmTest/java/androidx/collection/LongSparseArrayTest.java +++ /dev/null @@ -1,293 +0,0 @@ -/* - * Copyright 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package androidx.collection; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class LongSparseArrayTest { - @Test - public void getOrDefaultPrefersStoredValue() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.put(1L, "1"); - assertEquals("1", map.get(1L, "2")); - } - - @Test - public void getOrDefaultUsesDefaultWhenAbsent() { - LongSparseArray<String> map = new LongSparseArray<>(); - assertEquals("1", map.get(1L, "1")); - } - - @Test - public void getOrDefaultReturnsNullWhenNullStored() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.put(1L, null); - assertNull(map.get(1L, "1")); - } - - @Test - public void getOrDefaultDoesNotPersistDefault() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.get(1L, "1"); - assertFalse(map.containsKey(1L)); - } - - @Test - public void putIfAbsentDoesNotOverwriteStoredValue() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.put(1L, "1"); - map.putIfAbsent(1L, "2"); - assertEquals("1", map.get(1L)); - } - - @Test - public void putIfAbsentReturnsStoredValue() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.put(1L, "1"); - assertEquals("1", map.putIfAbsent(1L, "2")); - } - - @Test - public void putIfAbsentStoresValueWhenAbsent() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.putIfAbsent(1L, "2"); - assertEquals("2", map.get(1L)); - } - - @Test - public void putIfAbsentReturnsNullWhenAbsent() { - LongSparseArray<String> map = new LongSparseArray<>(); - assertNull(map.putIfAbsent(1L, "2")); - } - - @Test - public void replaceWhenAbsentDoesNotStore() { - LongSparseArray<String> map = new LongSparseArray<>(); - assertNull(map.replace(1L, "1")); - assertFalse(map.containsKey(1L)); - } - - @Test - public void replaceStoresAndReturnsOldValue() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.put(1L, "1"); - assertEquals("1", map.replace(1L, "2")); - assertEquals("2", map.get(1L)); - } - - @Test - public void replaceStoresAndReturnsNullWhenMappedToNull() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.put(1L, null); - assertNull(map.replace(1L, "1")); - assertEquals("1", map.get(1L)); - } - - @Test - public void replaceValueKeyAbsent() { - LongSparseArray<String> map = new LongSparseArray<>(); - assertFalse(map.replace(1L, "1", "2")); - assertFalse(map.containsKey(1L)); - } - - @Test - public void replaceValueMismatchDoesNotReplace() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.put(1L, "1"); - assertFalse(map.replace(1L, "2", "3")); - assertEquals("1", map.get(1L)); - } - - @Test - public void replaceValueMismatchNullDoesNotReplace() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.put(1L, "1"); - assertFalse(map.replace(1L, null, "2")); - assertEquals("1", map.get(1L)); - } - - @Test - public void replaceValueMatchReplaces() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.put(1L, "1"); - assertTrue(map.replace(1L, "1", "2")); - assertEquals("2", map.get(1L)); - } - - @Test - public void replaceNullValueMismatchDoesNotReplace() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.put(1L, null); - assertFalse(map.replace(1L, "1", "2")); - assertNull(map.get(1L)); - } - - @Test - public void replaceNullValueMatchRemoves() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.put(1L, null); - assertTrue(map.replace(1L, null, "1")); - assertEquals("1", map.get(1L)); - } - - @Test - public void removeValueKeyAbsent() { - LongSparseArray<String> map = new LongSparseArray<>(); - assertFalse(map.remove(1L, "1")); - } - - @Test - public void removeValueMismatchDoesNotRemove() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.put(1L, "1"); - assertFalse(map.remove(1L, "2")); - assertTrue(map.containsKey(1L)); - } - - @Test - public void removeValueMismatchNullDoesNotRemove() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.put(1L, "1"); - assertFalse(map.remove(1L, null)); - assertTrue(map.containsKey(1L)); - } - - @Test - public void removeValueMatchRemoves() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.put(1L, "1"); - assertTrue(map.remove(1L, "1")); - assertFalse(map.containsKey(1L)); - } - - @Test - public void removeNullValueMismatchDoesNotRemove() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.put(1L, null); - assertFalse(map.remove(1L, "2")); - assertTrue(map.containsKey(1L)); - } - - @Test - public void removeNullValueMatchRemoves() { - LongSparseArray<String> map = new LongSparseArray<>(); - map.put(1L, null); - assertTrue(map.remove(1L, null)); - assertFalse(map.containsKey(1L)); - } - - @Test - public void isEmpty() { - LongSparseArray<String> LongSparseArray = new LongSparseArray<>(); - assertTrue(LongSparseArray.isEmpty()); // Newly created LongSparseArray should be empty - - // Adding elements should change state from empty to not empty. - for (long i = 0L; i < 5L; i++) { - LongSparseArray.put(i, Long.toString(i)); - assertFalse(LongSparseArray.isEmpty()); - } - LongSparseArray.clear(); - assertTrue(LongSparseArray.isEmpty()); // A cleared LongSparseArray should be empty. - - - long key1 = 1L, key2 = 2L; - String value1 = "some value", value2 = "some other value"; - LongSparseArray.append(key1, value1); - assertFalse(LongSparseArray.isEmpty()); // has 1 element. - LongSparseArray.append(key2, value2); - assertFalse(LongSparseArray.isEmpty()); // has 2 elements. - assertFalse(LongSparseArray.isEmpty()); // consecutive calls should be OK. - - LongSparseArray.remove(key1); - assertFalse(LongSparseArray.isEmpty()); // has 1 element. - LongSparseArray.remove(key2); - assertTrue(LongSparseArray.isEmpty()); - } - - @Test - public void containsKey() { - LongSparseArray<String> array = new LongSparseArray<>(); - array.put(1L, "one"); - - assertTrue(array.containsKey(1L)); - assertFalse(array.containsKey(2L)); - } - - @Test - public void containsValue() { - LongSparseArray<String> array = new LongSparseArray<>(); - array.put(1L, "one"); - - assertTrue(array.containsValue("one")); - assertFalse(array.containsValue("two")); - } - - @Test - public void putAll() { - LongSparseArray<String> dest = new LongSparseArray<>(); - dest.put(1L, "one"); - dest.put(3L, "three"); - - LongSparseArray<String> source = new LongSparseArray<>(); - source.put(1L, "uno"); - source.put(2L, "dos"); - - dest.putAll(source); - assertEquals(3, dest.size()); - assertEquals("uno", dest.get(1L)); - assertEquals("dos", dest.get(2L)); - assertEquals("three", dest.get(3L)); - } - - @Test - public void putAllVariance() { - LongSparseArray<Object> dest = new LongSparseArray<>(); - dest.put(1L, 1L); - - LongSparseArray<String> source = new LongSparseArray<>(); - dest.put(2L, "two"); - - dest.putAll(source); - assertEquals(2, dest.size()); - assertEquals(1L, dest.get(1L)); - assertEquals("two", dest.get(2L)); - } - - @Test - public void cloning() { - LongSparseArray<String> source = new LongSparseArray<>(); - source.put(10L, "hello"); - source.put(20L, "world"); - - LongSparseArray<String> dest = source.clone(); - assertNotSame(source, dest); - - for (int i = 0; i < source.size(); i++) { - assertEquals(source.keyAt(i), dest.keyAt(i)); - assertEquals(source.valueAt(i), dest.valueAt(i)); - } - } -} diff --git a/collection/collection/src/jvmTest/kotlin/androidx/collection/LongSparseArrayTest.kt b/collection/collection/src/jvmTest/kotlin/androidx/collection/LongSparseArrayTest.kt new file mode 100644 index 00000000000..8ee96054a98 --- /dev/null +++ b/collection/collection/src/jvmTest/kotlin/androidx/collection/LongSparseArrayTest.kt @@ -0,0 +1,286 @@ +/* + * Copyright 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package androidx.collection + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotSame +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(JUnit4::class) +internal class LongSparseArrayTest { + @Test + fun getOrDefaultPrefersStoredValue() { + val map = LongSparseArray<String>() + map[1L] = "1" + assertEquals("1", map.get(1L, defaultValue = "2")) + } + + @Test + fun getOrDefaultUsesDefaultWhenAbsent() { + val map = LongSparseArray<String>() + assertEquals("1", map.get(1L, defaultValue = "1")) + } + + @Test + fun getOrDefaultReturnsNullWhenNullStored() { + val map = LongSparseArray<String?>() + map.put(1L, null) + assertNull(map.get(1L, defaultValue = "1")) + } + + @Test + fun getOrDefaultDoesNotPersistDefault() { + val map = LongSparseArray<String>() + map.get(1L, "1") + assertFalse(map.containsKey(1L)) + } + + @Test + fun putIfAbsentDoesNotOverwriteStoredValue() { + val map = LongSparseArray<String>() + map.put(1L, "1") + map.putIfAbsent(1L, "2") + assertEquals("1", map[1L]) + } + + @Test + fun putIfAbsentReturnsStoredValue() { + val map = LongSparseArray<String>() + map.put(1L, "1") + assertEquals("1", map.putIfAbsent(1L, "2")) + } + + @Test + fun putIfAbsentStoresValueWhenAbsent() { + val map = LongSparseArray<String>() + map.putIfAbsent(1L, "2") + assertEquals("2", map[1L]) + } + + @Test + fun putIfAbsentReturnsNullWhenAbsent() { + val map = LongSparseArray<String>() + assertNull(map.putIfAbsent(1L, "2")) + } + + @Test + fun replaceWhenAbsentDoesNotStore() { + val map = LongSparseArray<String>() + assertNull(map.replace(1L, "1")) + assertFalse(map.containsKey(1L)) + } + + @Test + fun replaceStoresAndReturnsOldValue() { + val map = LongSparseArray<String>() + map.put(1L, "1") + assertEquals("1", map.replace(1L, "2")) + assertEquals("2", map[1L]) + } + + @Test + fun replaceStoresAndReturnsNullWhenMappedToNull() { + val map = LongSparseArray<String?>() + map.put(1L, null) + assertNull(map.replace(1L, "1")) + assertEquals("1", map[1L]) + } + + @Test + fun replaceValueKeyAbsent() { + val map = LongSparseArray<String>() + assertFalse(map.replace(1L, oldValue = "1", newValue = "2")) + assertFalse(map.containsKey(1L)) + } + + @Test + fun replaceValueMismatchDoesNotReplace() { + val map = LongSparseArray<String>() + map.put(1L, "1") + assertFalse(map.replace(1L, oldValue = "2", newValue = "3")) + assertEquals("1", map[1L]) + } + + @Test + fun replaceValueMismatchNullDoesNotReplace() { + val map = LongSparseArray<String?>() + map.put(1L, "1") + assertFalse(map.replace(1L, oldValue = null, newValue = "2")) + assertEquals("1", map[1L]) + } + + @Test + fun replaceValueMatchReplaces() { + val map = LongSparseArray<String>() + map.put(1L, "1") + assertTrue(map.replace(1L, oldValue = "1", newValue = "2")) + assertEquals("2", map[1L]) + } + + @Test + fun replaceNullValueMismatchDoesNotReplace() { + val map = LongSparseArray<String?>() + map.put(1L, null) + assertFalse(map.replace(1L, oldValue = "1", newValue = "2")) + assertNull(map[1L]) + } + + @Test + fun replaceNullValueMatchRemoves() { + val map = LongSparseArray<String?>() + map.put(1L, null) + assertTrue(map.replace(1L, oldValue = null, newValue = "1")) + assertEquals("1", map[1L]) + } + + @Test + fun removeValueKeyAbsent() { + val map = LongSparseArray<String>() + assertFalse(map.remove(1L, "1")) + } + + @Test + fun removeValueMismatchDoesNotRemove() { + val map = LongSparseArray<String>() + map.put(1L, "1") + assertFalse(map.remove(1L, "2")) + assertTrue(map.containsKey(1L)) + } + + @Test + fun removeValueMismatchNullDoesNotRemove() { + val map = LongSparseArray<String?>() + map.put(1L, "1") + assertFalse(map.remove(1L, null)) + assertTrue(map.containsKey(1L)) + } + + @Test + fun removeValueMatchRemoves() { + val map = LongSparseArray<String>() + map.put(1L, "1") + assertTrue(map.remove(1L, "1")) + assertFalse(map.containsKey(1L)) + } + + @Test + fun removeNullValueMismatchDoesNotRemove() { + val map = LongSparseArray<String?>() + map.put(1L, null) + assertFalse(map.remove(1L, "2")) + assertTrue(map.containsKey(1L)) + } + + @Test + fun removeNullValueMatchRemoves() { + val map = LongSparseArray<String?>() + map.put(1L, null) + assertTrue(map.remove(1L, null)) + assertFalse(map.containsKey(1L)) + } + + @Test + fun isEmpty() { + // Newly created LongSparseArray should be empty + + // Adding elements should change state from empty to not empty. + // A cleared LongSparseArray should be empty. + val map = LongSparseArray<String>() + assertTrue(map.isEmpty()) // Newly created LongSparseArray should be empty + + // Adding elements should change state from empty to not empty. + repeat(5) { i -> + map.put(i.toLong(), "$i") + assertFalse(map.isEmpty()) + } + map.clear() + assertTrue(map.isEmpty()) // A cleared LongSparseArray should be empty. + val key1 = 1L + val key2 = 2L + val value1 = "some value" + val value2 = "some other value" + map.append(key1, value1) + assertFalse(map.isEmpty()) // has 1 element. + map.append(key2, value2) + assertFalse(map.isEmpty()) // has 2 elements. + assertFalse(map.isEmpty()) // consecutive calls should be OK. + map.remove(key1) + assertFalse(map.isEmpty()) // has 1 element. + map.remove(key2) + assertTrue(map.isEmpty()) + } + + @Test + fun containsKey() { + val array = LongSparseArray<String>() + array.put(1L, "one") + assertTrue(array.containsKey(1L)) + assertFalse(array.containsKey(2L)) + } + + @Test + fun containsValue() { + val array = LongSparseArray<String>() + array.put(1L, "one") + assertTrue(array.containsValue("one")) + assertFalse(array.containsValue("two")) + } + + @Test + fun putAll() { + val dest = LongSparseArray<String>() + dest.put(1L, "one") + dest.put(3L, "three") + val source = LongSparseArray<String>() + source.put(1L, "uno") + source.put(2L, "dos") + dest.putAll(source) + assertEquals(3, dest.size()) + assertEquals("uno", dest[1L]) + assertEquals("dos", dest[2L]) + assertEquals("three", dest[3L]) + } + + @Test + fun putAllVariance() { + val dest = LongSparseArray<Any>() + dest.put(1L, 1L) + val source = LongSparseArray<String>() + dest.put(2L, "two") + dest.putAll(source) + assertEquals(2, dest.size()) + assertEquals(1L, dest[1L]) + assertEquals("two", dest[2L]) + } + + @Test + fun cloning() { + val source = LongSparseArray<String>() + source.put(10L, "hello") + source.put(20L, "world") + val dest = source.clone() + assertNotSame(source, dest) + repeat(source.size()) { i -> + assertEquals(source.keyAt(i), dest.keyAt(i)) + assertEquals(source.valueAt(i), dest.valueAt(i)) + } + } +}
\ No newline at end of file |