Debian Codegraph @

core/tests/utiltests/src/android/util/IntArrayTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2017 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.util;
18
19 import static org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21
22 import androidx.test.filters.SmallTest;
23 import androidx.test.runner.AndroidJUnit4;
24
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27
28 @RunWith(AndroidJUnit4.class)
29 @SmallTest
30 public class IntArrayTest {
31
32 @Test
33 public void testIntArray() {
34 IntArray a = new IntArray();
35 a.add(1);
36 a.add(2);
37 a.add(3);
38 verify(new int[]{1, 2, 3}, a);
39
40 IntArray b = IntArray.fromArray(new int[]{4, 5, 6, 7, 8}, 3);
41 a.addAll(b);
42 verify(new int[]{1, 2, 3, 4, 5, 6}, a);
43
44 a.resize(2);
45 verify(new int[]{1, 2}, a);
46
47 a.resize(8);
48 verify(new int[]{1, 2, 0, 0, 0, 0, 0, 0}, a);
49
50 a.set(5, 10);
51 verify(new int[]{1, 2, 0, 0, 0, 10, 0, 0}, a);
52
53 a.add(5, 20);
54 assertEquals(20, a.get(5));
55 assertEquals(5, a.indexOf(20));
56 verify(new int[]{1, 2, 0, 0, 0, 20, 10, 0, 0}, a);
57
58 assertEquals(-1, a.indexOf(99));
59
60 a.resize(15);
61 a.set(14, 30);
62 verify(new int[]{1, 2, 0, 0, 0, 20, 10, 0, 0, 0, 0, 0, 0, 0, 30}, a);
63
64 int[] backingArray = new int[]{1, 2, 3, 4};
65 a = IntArray.wrap(backingArray);
66 a.set(0, 10);
67 assertEquals(10, backingArray[0]);
68 backingArray[1] = 20;
69 backingArray[2] = 30;
70 verify(backingArray, a);
71 assertEquals(2, a.indexOf(30));
72
73 a.resize(2);
74 assertEquals(0, backingArray[2]);
75 assertEquals(0, backingArray[3]);
76
77 a.add(50);
78 verify(new int[]{10, 20, 50}, a);
79 }
80
81 public void verify(int[] expected, IntArray intArray) {
82 assertEquals(expected.length, intArray.size());
83 assertArrayEquals(expected, intArray.toArray());
84 }
85 }