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