1/*
2 * Copyright 2018 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
17package androidx.collection;
18
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
21
22import org.junit.Test;
23
24public class SparseArrayCompatTest {
25    @Test
26    public void isEmpty() throws Exception {
27        SparseArrayCompat<String> sparseArrayCompat = new SparseArrayCompat<>();
28        assertTrue(sparseArrayCompat.isEmpty()); // Newly created SparseArrayCompat should be empty
29
30        // Adding elements should change state from empty to not empty.
31        for (int i = 0; i < 5; i++) {
32            sparseArrayCompat.put(i, Integer.toString(i));
33            assertFalse(sparseArrayCompat.isEmpty());
34        }
35        sparseArrayCompat.clear();
36        assertTrue(sparseArrayCompat.isEmpty()); // A cleared SparseArrayCompat should be empty.
37
38
39        int key1 = 1, key2 = 2;
40        String value1 = "some value", value2 = "some other value";
41        sparseArrayCompat.append(key1, value1);
42        assertFalse(sparseArrayCompat.isEmpty()); // has 1 element.
43        sparseArrayCompat.append(key2, value2);
44        assertFalse(sparseArrayCompat.isEmpty());  // has 2 elements.
45        assertFalse(sparseArrayCompat.isEmpty());  // consecutive calls should be OK.
46
47        sparseArrayCompat.remove(key1);
48        assertFalse(sparseArrayCompat.isEmpty()); // has 1 element.
49        sparseArrayCompat.remove(key2);
50        assertTrue(sparseArrayCompat.isEmpty());
51    }
52}
53