• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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.inputmethod.latin;
18 
19 import android.test.AndroidTestCase;
20 import android.test.suitebuilder.annotation.SmallTest;
21 
22 @SmallTest
23 public class ResizableIntArrayTests extends AndroidTestCase {
24     private static final int DEFAULT_CAPACITY = 48;
25 
testNewInstance()26     public void testNewInstance() {
27         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
28         final int[] array = src.getPrimitiveArray();
29         assertEquals("new instance length", 0, src.getLength());
30         assertNotNull("new instance array", array);
31         assertEquals("new instance array length", DEFAULT_CAPACITY, array.length);
32     }
33 
testAdd()34     public void testAdd() {
35         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
36         final int[] array = src.getPrimitiveArray();
37         int[] array2 = null, array3 = null;
38         final int limit = DEFAULT_CAPACITY * 2 + 10;
39         for (int i = 0; i < limit; i++) {
40             src.add(i);
41             assertEquals("length after add " + i, i + 1, src.getLength());
42             if (i == DEFAULT_CAPACITY) {
43                 array2 = src.getPrimitiveArray();
44             }
45             if (i == DEFAULT_CAPACITY * 2) {
46                 array3 = src.getPrimitiveArray();
47             }
48             if (i < DEFAULT_CAPACITY) {
49                 assertSame("array after add " + i, array, src.getPrimitiveArray());
50             } else if (i < DEFAULT_CAPACITY * 2) {
51                 assertSame("array after add " + i, array2, src.getPrimitiveArray());
52             } else if (i < DEFAULT_CAPACITY * 3) {
53                 assertSame("array after add " + i, array3, src.getPrimitiveArray());
54             }
55         }
56         for (int i = 0; i < limit; i++) {
57             assertEquals("value at " + i, i, src.get(i));
58         }
59     }
60 
testAddAt()61     public void testAddAt() {
62         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
63         final int limit = DEFAULT_CAPACITY * 10, step = DEFAULT_CAPACITY * 2;
64         for (int i = 0; i < limit; i += step) {
65             src.add(i, i);
66             assertEquals("length after add at " + i, i + 1, src.getLength());
67         }
68         for (int i = 0; i < limit; i += step) {
69             assertEquals("value at " + i, i, src.get(i));
70         }
71     }
72 
testGet()73     public void testGet() {
74         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
75         try {
76             final int value = src.get(0);
77             fail("get(0) shouldn't succeed");
78         } catch (ArrayIndexOutOfBoundsException e) {
79             // success
80         }
81         try {
82             final int value = src.get(DEFAULT_CAPACITY);
83             fail("get(DEFAULT_CAPACITY) shouldn't succeed");
84         } catch (ArrayIndexOutOfBoundsException e) {
85             // success
86         }
87 
88         final int index = DEFAULT_CAPACITY / 2;
89         src.add(index, 100);
90         assertEquals("legth after add at " + index, index + 1, src.getLength());
91         assertEquals("value after add at " + index, 100, src.get(index));
92         assertEquals("value after add at 0", 0, src.get(0));
93         try {
94             final int value = src.get(src.getLength());
95             fail("get(length) shouldn't succeed");
96         } catch (ArrayIndexOutOfBoundsException e) {
97             // success
98         }
99     }
100 
testReset()101     public void testReset() {
102         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
103         final int[] array = src.getPrimitiveArray();
104         for (int i = 0; i < DEFAULT_CAPACITY; i++) {
105             src.add(i);
106             assertEquals("length after add " + i, i + 1, src.getLength());
107         }
108 
109         final int smallerLength = DEFAULT_CAPACITY / 2;
110         src.reset(smallerLength);
111         final int[] array2 = src.getPrimitiveArray();
112         assertEquals("length after reset", 0, src.getLength());
113         assertNotSame("array after reset", array, array2);
114 
115         int[] array3 = null;
116         for (int i = 0; i < DEFAULT_CAPACITY; i++) {
117             src.add(i);
118             assertEquals("length after add " + i, i + 1, src.getLength());
119             if (i == smallerLength) {
120                 array3 = src.getPrimitiveArray();
121             }
122             if (i < smallerLength) {
123                 assertSame("array after add " + i, array2, src.getPrimitiveArray());
124             } else if (i < smallerLength * 2) {
125                 assertSame("array after add " + i, array3, src.getPrimitiveArray());
126             }
127         }
128     }
129 
testSetLength()130     public void testSetLength() {
131         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
132         final int[] array = src.getPrimitiveArray();
133         for (int i = 0; i < DEFAULT_CAPACITY; i++) {
134             src.add(i);
135             assertEquals("length after add " + i, i + 1, src.getLength());
136         }
137 
138         final int largerLength = DEFAULT_CAPACITY * 2;
139         src.setLength(largerLength);
140         final int[] array2 = src.getPrimitiveArray();
141         assertEquals("length after larger setLength", largerLength, src.getLength());
142         assertNotSame("array after larger setLength", array, array2);
143         assertEquals("array length after larger setLength", largerLength, array2.length);
144         for (int i = 0; i < largerLength; i++) {
145             final int v = src.get(i);
146             if (i < DEFAULT_CAPACITY) {
147                 assertEquals("value at " + i, i, v);
148             } else {
149                 assertEquals("value at " + i, 0, v);
150             }
151         }
152 
153         final int smallerLength = DEFAULT_CAPACITY / 2;
154         src.setLength(smallerLength);
155         final int[] array3 = src.getPrimitiveArray();
156         assertEquals("length after smaller setLength", smallerLength, src.getLength());
157         assertSame("array after smaller setLength", array2, array3);
158         assertEquals("array length after smaller setLength", largerLength, array3.length);
159         for (int i = 0; i < smallerLength; i++) {
160             assertEquals("value at " + i, i, src.get(i));
161         }
162     }
163 
testSet()164     public void testSet() {
165         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
166         final int limit = DEFAULT_CAPACITY * 2 + 10;
167         for (int i = 0; i < limit; i++) {
168             src.add(i);
169         }
170 
171         final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY);
172         dst.set(src);
173         assertEquals("length after set", dst.getLength(), src.getLength());
174         assertSame("array after set", dst.getPrimitiveArray(), src.getPrimitiveArray());
175     }
176 
testCopy()177     public void testCopy() {
178         final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
179         for (int i = 0; i < DEFAULT_CAPACITY; i++) {
180             src.add(i);
181         }
182 
183         final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY);
184         final int[] array = dst.getPrimitiveArray();
185         dst.copy(src);
186         assertEquals("length after copy", dst.getLength(), src.getLength());
187         assertSame("array after copy", array, dst.getPrimitiveArray());
188         assertNotSame("array after copy", dst.getPrimitiveArray(), src.getPrimitiveArray());
189         assertArrayEquals("values after copy",
190                 dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength());
191 
192         final int smallerLength = DEFAULT_CAPACITY / 2;
193         dst.reset(smallerLength);
194         final int[] array2 = dst.getPrimitiveArray();
195         dst.copy(src);
196         final int[] array3 = dst.getPrimitiveArray();
197         assertEquals("length after copy to smaller", dst.getLength(), src.getLength());
198         assertNotSame("array after copy to smaller", array2, array3);
199         assertNotSame("array after copy to smaller", array3, src.getPrimitiveArray());
200         assertArrayEquals("values after copy to smaller",
201                 dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength());
202     }
203 
testAppend()204     public void testAppend() {
205         final int srcLen = DEFAULT_CAPACITY;
206         final ResizableIntArray src = new ResizableIntArray(srcLen);
207         for (int i = 0; i < srcLen; i++) {
208             src.add(i);
209         }
210         final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY * 2);
211         final int[] array = dst.getPrimitiveArray();
212         final int dstLen = DEFAULT_CAPACITY / 2;
213         for (int i = 0; i < dstLen; i++) {
214             final int value = -i - 1;
215             dst.add(value);
216         }
217         final ResizableIntArray dstCopy = new ResizableIntArray(dst.getLength());
218         dstCopy.copy(dst);
219 
220         dst.append(src, 0, 0);
221         assertEquals("length after append zero", dstLen, dst.getLength());
222         assertSame("array after append zero", array, dst.getPrimitiveArray());
223         assertArrayEquals("values after append zero",
224                 dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen);
225 
226         dst.append(src, 0, srcLen);
227         assertEquals("length after append", dstLen + srcLen, dst.getLength());
228         assertSame("array after append", array, dst.getPrimitiveArray());
229         assertTrue("primitive length after append",
230                 dst.getPrimitiveArray().length >= dstLen + srcLen);
231         assertArrayEquals("original values after append",
232                 dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen);
233         assertArrayEquals("appended values after append",
234                 src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen, srcLen);
235 
236         dst.append(src, 0, srcLen);
237         assertEquals("length after 2nd append", dstLen + srcLen * 2, dst.getLength());
238         assertNotSame("array after 2nd append", array, dst.getPrimitiveArray());
239         assertTrue("primitive length after 2nd append",
240                 dst.getPrimitiveArray().length >= dstLen + srcLen * 2);
241         assertArrayEquals("original values after 2nd append",
242                 dstCopy.getPrimitiveArray(), 0, dst.getPrimitiveArray(), 0, dstLen);
243         assertArrayEquals("appended values after 2nd append",
244                 src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen, srcLen);
245         assertArrayEquals("appended values after 2nd append",
246                 src.getPrimitiveArray(), 0, dst.getPrimitiveArray(), dstLen + srcLen, srcLen);
247     }
248 
testFill()249     public void testFill() {
250         final int srcLen = DEFAULT_CAPACITY;
251         final ResizableIntArray src = new ResizableIntArray(srcLen);
252         for (int i = 0; i < srcLen; i++) {
253             src.add(i);
254         }
255         final int[] array = src.getPrimitiveArray();
256 
257         final int startPos = srcLen / 3;
258         final int length = srcLen / 3;
259         final int endPos = startPos + length;
260         assertTrue(startPos >= 1);
261         final int value = 123;
262         try {
263             src.fill(value, -1, length);
264             fail("fill from -1 shouldn't succeed");
265         } catch (IllegalArgumentException e) {
266             // success
267         }
268         try {
269             src.fill(value, startPos, -1);
270             fail("fill negative length shouldn't succeed");
271         } catch (IllegalArgumentException e) {
272             // success
273         }
274 
275         src.fill(value, startPos, length);
276         assertEquals("length after fill", srcLen, src.getLength());
277         assertSame("array after fill", array, src.getPrimitiveArray());
278         for (int i = 0; i < srcLen; i++) {
279             final int v = src.get(i);
280             if (i >= startPos && i < endPos) {
281                 assertEquals("new values after fill at " + i, value, v);
282             } else {
283                 assertEquals("unmodified values after fill at " + i, i, v);
284             }
285         }
286 
287         final int length2 = srcLen * 2 - startPos;
288         final int largeEnd = startPos + length2;
289         assertTrue(largeEnd > srcLen);
290         final int value2 = 456;
291         src.fill(value2, startPos, length2);
292         assertEquals("length after large fill", largeEnd, src.getLength());
293         assertNotSame("array after large fill", array, src.getPrimitiveArray());
294         for (int i = 0; i < largeEnd; i++) {
295             final int v = src.get(i);
296             if (i >= startPos && i < largeEnd) {
297                 assertEquals("new values after large fill at " + i, value2, v);
298             } else {
299                 assertEquals("unmodified values after large fill at " + i, i, v);
300             }
301         }
302 
303         final int startPos2 = largeEnd + length2;
304         final int endPos2 = startPos2 + length2;
305         final int value3 = 789;
306         src.fill(value3, startPos2, length2);
307         assertEquals("length after disjoint fill", endPos2, src.getLength());
308         for (int i = 0; i < endPos2; i++) {
309             final int v = src.get(i);
310             if (i >= startPos2 && i < endPos2) {
311                 assertEquals("new values after disjoint fill at " + i, value3, v);
312             } else if (i >= startPos && i < largeEnd) {
313                 assertEquals("unmodified values after disjoint fill at " + i, value2, v);
314             } else if (i < startPos) {
315                 assertEquals("unmodified values after disjoint fill at " + i, i, v);
316             } else {
317                 assertEquals("gap values after disjoint fill at " + i, 0, v);
318             }
319         }
320     }
321 
assertArrayEquals(String message, int[] expecteds, int expectedPos, int[] actuals, int actualPos, int length)322     private static void assertArrayEquals(String message, int[] expecteds, int expectedPos,
323             int[] actuals, int actualPos, int length) {
324         if (expecteds == null && actuals == null) {
325             return;
326         }
327         if (expecteds == null || actuals == null) {
328             fail(message + ": expecteds=" + expecteds + " actuals=" + actuals);
329         }
330         for (int i = 0; i < length; i++) {
331             assertEquals(message + ": element at " + i,
332                     expecteds[i + expectedPos], actuals[i + actualPos]);
333         }
334     }
335 }
336