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