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