2012-07-19 10:45:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
2013-02-12 07:15:47 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2012-07-19 10:45:20 +00:00
|
|
|
*
|
2013-02-12 07:15:47 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-07-19 10:45:20 +00:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-02-12 07:15:47 +00:00
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2012-07-19 10:45:20 +00:00
|
|
|
*/
|
|
|
|
|
2014-10-29 02:37:21 +00:00
|
|
|
package com.android.inputmethod.latin.common;
|
2012-07-19 10:45:20 +00:00
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import static org.junit.Assert.assertNotSame;
|
|
|
|
import static org.junit.Assert.assertSame;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
|
|
|
import android.support.test.filters.SmallTest;
|
|
|
|
import android.support.test.runner.AndroidJUnit4;
|
2012-07-19 10:45:20 +00:00
|
|
|
|
2013-05-23 09:06:56 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
2013-02-04 23:25:24 +00:00
|
|
|
@SmallTest
|
2018-07-06 17:10:54 +00:00
|
|
|
@RunWith(AndroidJUnit4.class)
|
|
|
|
public class ResizableIntArrayTests {
|
2012-07-19 10:45:20 +00:00
|
|
|
private static final int DEFAULT_CAPACITY = 48;
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2012-07-19 10:45:20 +00:00
|
|
|
public void testNewInstance() {
|
|
|
|
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
|
|
|
|
final int[] array = src.getPrimitiveArray();
|
2012-07-20 03:01:33 +00:00
|
|
|
assertEquals("new instance length", 0, src.getLength());
|
|
|
|
assertNotNull("new instance array", array);
|
|
|
|
assertEquals("new instance array length", DEFAULT_CAPACITY, array.length);
|
2012-07-19 10:45:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2012-07-19 10:45:20 +00:00
|
|
|
public void testAdd() {
|
|
|
|
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
|
2012-07-20 03:01:33 +00:00
|
|
|
final int[] array = src.getPrimitiveArray();
|
|
|
|
int[] array2 = null, array3 = null;
|
|
|
|
final int limit = DEFAULT_CAPACITY * 2 + 10;
|
2012-07-19 10:45:20 +00:00
|
|
|
for (int i = 0; i < limit; i++) {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int value = i;
|
|
|
|
src.add(value);
|
2012-07-19 10:45:20 +00:00
|
|
|
assertEquals("length after add " + i, i + 1, src.getLength());
|
2012-07-20 03:45:15 +00:00
|
|
|
if (i == DEFAULT_CAPACITY) {
|
|
|
|
array2 = src.getPrimitiveArray();
|
|
|
|
}
|
|
|
|
if (i == DEFAULT_CAPACITY * 2) {
|
|
|
|
array3 = src.getPrimitiveArray();
|
|
|
|
}
|
2012-07-20 03:01:33 +00:00
|
|
|
if (i < DEFAULT_CAPACITY) {
|
|
|
|
assertSame("array after add " + i, array, src.getPrimitiveArray());
|
|
|
|
} else if (i < DEFAULT_CAPACITY * 2) {
|
|
|
|
assertSame("array after add " + i, array2, src.getPrimitiveArray());
|
|
|
|
} else if (i < DEFAULT_CAPACITY * 3) {
|
|
|
|
assertSame("array after add " + i, array3, src.getPrimitiveArray());
|
|
|
|
}
|
2012-07-19 10:45:20 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < limit; i++) {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int value = i;
|
|
|
|
assertEquals("value at " + i, value, src.get(i));
|
2012-07-19 10:45:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2012-07-19 10:45:20 +00:00
|
|
|
public void testAddAt() {
|
|
|
|
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
|
2012-07-20 03:01:33 +00:00
|
|
|
final int limit = DEFAULT_CAPACITY * 10, step = DEFAULT_CAPACITY * 2;
|
2012-07-19 10:45:20 +00:00
|
|
|
for (int i = 0; i < limit; i += step) {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int value = i;
|
2014-01-08 05:11:56 +00:00
|
|
|
src.addAt(i, value);
|
2012-07-19 10:45:20 +00:00
|
|
|
assertEquals("length after add at " + i, i + 1, src.getLength());
|
|
|
|
}
|
|
|
|
for (int i = 0; i < limit; i += step) {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int value = i;
|
|
|
|
assertEquals("value at " + i, value, src.get(i));
|
2012-07-20 03:01:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2012-07-20 03:01:33 +00:00
|
|
|
public void testGet() {
|
|
|
|
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
|
|
|
|
try {
|
2014-10-20 05:48:56 +00:00
|
|
|
src.get(0);
|
2012-07-20 03:01:33 +00:00
|
|
|
fail("get(0) shouldn't succeed");
|
|
|
|
} catch (ArrayIndexOutOfBoundsException e) {
|
|
|
|
// success
|
|
|
|
}
|
|
|
|
try {
|
2014-10-20 05:48:56 +00:00
|
|
|
src.get(DEFAULT_CAPACITY);
|
2012-07-20 03:01:33 +00:00
|
|
|
fail("get(DEFAULT_CAPACITY) shouldn't succeed");
|
|
|
|
} catch (ArrayIndexOutOfBoundsException e) {
|
|
|
|
// success
|
|
|
|
}
|
|
|
|
|
|
|
|
final int index = DEFAULT_CAPACITY / 2;
|
2014-01-07 08:43:44 +00:00
|
|
|
final int valueAddAt = 100;
|
2014-01-08 05:11:56 +00:00
|
|
|
src.addAt(index, valueAddAt);
|
2012-07-20 03:01:33 +00:00
|
|
|
assertEquals("legth after add at " + index, index + 1, src.getLength());
|
2014-01-07 08:43:44 +00:00
|
|
|
assertEquals("value after add at " + index, valueAddAt, src.get(index));
|
2012-07-20 03:01:33 +00:00
|
|
|
assertEquals("value after add at 0", 0, src.get(0));
|
|
|
|
try {
|
2014-10-20 05:48:56 +00:00
|
|
|
src.get(src.getLength());
|
2012-07-20 03:01:33 +00:00
|
|
|
fail("get(length) shouldn't succeed");
|
|
|
|
} catch (ArrayIndexOutOfBoundsException e) {
|
|
|
|
// success
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2012-07-20 03:01:33 +00:00
|
|
|
public void testReset() {
|
|
|
|
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
|
|
|
|
final int[] array = src.getPrimitiveArray();
|
|
|
|
for (int i = 0; i < DEFAULT_CAPACITY; i++) {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int value = i;
|
|
|
|
src.add(value);
|
2012-07-20 03:01:33 +00:00
|
|
|
assertEquals("length after add " + i, i + 1, src.getLength());
|
|
|
|
}
|
|
|
|
|
|
|
|
final int smallerLength = DEFAULT_CAPACITY / 2;
|
|
|
|
src.reset(smallerLength);
|
|
|
|
final int[] array2 = src.getPrimitiveArray();
|
|
|
|
assertEquals("length after reset", 0, src.getLength());
|
|
|
|
assertNotSame("array after reset", array, array2);
|
|
|
|
|
|
|
|
int[] array3 = null;
|
|
|
|
for (int i = 0; i < DEFAULT_CAPACITY; i++) {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int value = i;
|
|
|
|
src.add(value);
|
2012-07-20 03:01:33 +00:00
|
|
|
assertEquals("length after add " + i, i + 1, src.getLength());
|
2012-07-20 03:45:15 +00:00
|
|
|
if (i == smallerLength) {
|
|
|
|
array3 = src.getPrimitiveArray();
|
|
|
|
}
|
2012-07-20 03:01:33 +00:00
|
|
|
if (i < smallerLength) {
|
|
|
|
assertSame("array after add " + i, array2, src.getPrimitiveArray());
|
|
|
|
} else if (i < smallerLength * 2) {
|
|
|
|
assertSame("array after add " + i, array3, src.getPrimitiveArray());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2012-07-20 03:01:33 +00:00
|
|
|
public void testSetLength() {
|
|
|
|
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
|
|
|
|
final int[] array = src.getPrimitiveArray();
|
|
|
|
for (int i = 0; i < DEFAULT_CAPACITY; i++) {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int value = i;
|
|
|
|
src.add(value);
|
2012-07-20 03:01:33 +00:00
|
|
|
assertEquals("length after add " + i, i + 1, src.getLength());
|
|
|
|
}
|
|
|
|
|
|
|
|
final int largerLength = DEFAULT_CAPACITY * 2;
|
|
|
|
src.setLength(largerLength);
|
|
|
|
final int[] array2 = src.getPrimitiveArray();
|
|
|
|
assertEquals("length after larger setLength", largerLength, src.getLength());
|
|
|
|
assertNotSame("array after larger setLength", array, array2);
|
|
|
|
assertEquals("array length after larger setLength", largerLength, array2.length);
|
2012-07-20 03:45:15 +00:00
|
|
|
for (int i = 0; i < largerLength; i++) {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int value = i;
|
2012-07-20 03:45:15 +00:00
|
|
|
if (i < DEFAULT_CAPACITY) {
|
2014-01-07 08:43:44 +00:00
|
|
|
assertEquals("value at " + i, value, src.get(i));
|
2012-07-20 03:45:15 +00:00
|
|
|
} else {
|
2014-01-07 08:43:44 +00:00
|
|
|
assertEquals("value at " + i, 0, src.get(i));
|
2012-07-20 03:45:15 +00:00
|
|
|
}
|
2012-07-20 03:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final int smallerLength = DEFAULT_CAPACITY / 2;
|
|
|
|
src.setLength(smallerLength);
|
|
|
|
final int[] array3 = src.getPrimitiveArray();
|
|
|
|
assertEquals("length after smaller setLength", smallerLength, src.getLength());
|
|
|
|
assertSame("array after smaller setLength", array2, array3);
|
|
|
|
assertEquals("array length after smaller setLength", largerLength, array3.length);
|
|
|
|
for (int i = 0; i < smallerLength; i++) {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int value = i;
|
|
|
|
assertEquals("value at " + i, value, src.get(i));
|
2012-07-19 10:45:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2012-07-19 10:45:20 +00:00
|
|
|
public void testSet() {
|
|
|
|
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
|
2012-07-20 03:01:33 +00:00
|
|
|
final int limit = DEFAULT_CAPACITY * 2 + 10;
|
2012-07-19 10:45:20 +00:00
|
|
|
for (int i = 0; i < limit; i++) {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int value = i;
|
|
|
|
src.add(value);
|
2012-07-19 10:45:20 +00:00
|
|
|
}
|
2012-07-20 03:01:33 +00:00
|
|
|
|
2012-07-19 10:45:20 +00:00
|
|
|
final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY);
|
|
|
|
dst.set(src);
|
|
|
|
assertEquals("length after set", dst.getLength(), src.getLength());
|
|
|
|
assertSame("array after set", dst.getPrimitiveArray(), src.getPrimitiveArray());
|
|
|
|
}
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2012-07-19 10:45:20 +00:00
|
|
|
public void testCopy() {
|
|
|
|
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
|
2012-07-20 03:01:33 +00:00
|
|
|
for (int i = 0; i < DEFAULT_CAPACITY; i++) {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int value = i;
|
|
|
|
src.add(value);
|
2012-07-19 10:45:20 +00:00
|
|
|
}
|
2012-07-20 03:01:33 +00:00
|
|
|
|
2012-07-19 10:45:20 +00:00
|
|
|
final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY);
|
2012-07-20 03:01:33 +00:00
|
|
|
final int[] array = dst.getPrimitiveArray();
|
2012-07-19 10:45:20 +00:00
|
|
|
dst.copy(src);
|
|
|
|
assertEquals("length after copy", dst.getLength(), src.getLength());
|
2012-07-20 03:01:33 +00:00
|
|
|
assertSame("array after copy", array, dst.getPrimitiveArray());
|
2012-07-19 10:45:20 +00:00
|
|
|
assertNotSame("array after copy", dst.getPrimitiveArray(), src.getPrimitiveArray());
|
2013-05-23 09:06:56 +00:00
|
|
|
assertIntArrayEquals("values after copy",
|
2012-07-20 03:01:33 +00:00
|
|
|
dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength());
|
|
|
|
|
|
|
|
final int smallerLength = DEFAULT_CAPACITY / 2;
|
|
|
|
dst.reset(smallerLength);
|
|
|
|
final int[] array2 = dst.getPrimitiveArray();
|
|
|
|
dst.copy(src);
|
|
|
|
final int[] array3 = dst.getPrimitiveArray();
|
|
|
|
assertEquals("length after copy to smaller", dst.getLength(), src.getLength());
|
|
|
|
assertNotSame("array after copy to smaller", array2, array3);
|
|
|
|
assertNotSame("array after copy to smaller", array3, src.getPrimitiveArray());
|
2013-05-23 09:06:56 +00:00
|
|
|
assertIntArrayEquals("values after copy to smaller",
|
2012-07-20 03:01:33 +00:00
|
|
|
dst.getPrimitiveArray(), 0, src.getPrimitiveArray(), 0, dst.getLength());
|
2012-07-19 10:45:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2012-07-19 10:45:20 +00:00
|
|
|
public void testAppend() {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int srcLength = DEFAULT_CAPACITY;
|
|
|
|
final ResizableIntArray src = new ResizableIntArray(srcLength);
|
|
|
|
for (int i = 0; i < srcLength; i++) {
|
|
|
|
final int value = i;
|
|
|
|
src.add(value);
|
2012-07-19 10:45:20 +00:00
|
|
|
}
|
2012-07-20 03:01:33 +00:00
|
|
|
final ResizableIntArray dst = new ResizableIntArray(DEFAULT_CAPACITY * 2);
|
|
|
|
final int[] array = dst.getPrimitiveArray();
|
2014-01-07 08:43:44 +00:00
|
|
|
final int dstLength = DEFAULT_CAPACITY / 2;
|
|
|
|
for (int i = 0; i < dstLength; i++) {
|
2012-07-19 10:45:20 +00:00
|
|
|
final int value = -i - 1;
|
|
|
|
dst.add(value);
|
|
|
|
}
|
|
|
|
final ResizableIntArray dstCopy = new ResizableIntArray(dst.getLength());
|
|
|
|
dstCopy.copy(dst);
|
|
|
|
|
2014-01-07 08:43:44 +00:00
|
|
|
final int startPos = 0;
|
|
|
|
dst.append(src, startPos, 0 /* length */);
|
|
|
|
assertEquals("length after append zero", dstLength, dst.getLength());
|
2012-07-20 03:01:33 +00:00
|
|
|
assertSame("array after append zero", array, dst.getPrimitiveArray());
|
2014-01-07 08:43:44 +00:00
|
|
|
assertIntArrayEquals("values after append zero", dstCopy.getPrimitiveArray(), startPos,
|
|
|
|
dst.getPrimitiveArray(), startPos, dstLength);
|
2012-07-19 10:45:20 +00:00
|
|
|
|
2014-01-07 08:43:44 +00:00
|
|
|
dst.append(src, startPos, srcLength);
|
|
|
|
assertEquals("length after append", dstLength + srcLength, dst.getLength());
|
2012-07-20 03:01:33 +00:00
|
|
|
assertSame("array after append", array, dst.getPrimitiveArray());
|
2012-07-19 10:45:20 +00:00
|
|
|
assertTrue("primitive length after append",
|
2014-01-07 08:43:44 +00:00
|
|
|
dst.getPrimitiveArray().length >= dstLength + srcLength);
|
|
|
|
assertIntArrayEquals("original values after append", dstCopy.getPrimitiveArray(), startPos,
|
|
|
|
dst.getPrimitiveArray(), startPos, dstLength);
|
|
|
|
assertIntArrayEquals("appended values after append", src.getPrimitiveArray(), startPos,
|
|
|
|
dst.getPrimitiveArray(), dstLength, srcLength);
|
2012-07-20 03:01:33 +00:00
|
|
|
|
2014-01-07 08:43:44 +00:00
|
|
|
dst.append(src, startPos, srcLength);
|
|
|
|
assertEquals("length after 2nd append", dstLength + srcLength * 2, dst.getLength());
|
2012-07-20 03:01:33 +00:00
|
|
|
assertNotSame("array after 2nd append", array, dst.getPrimitiveArray());
|
|
|
|
assertTrue("primitive length after 2nd append",
|
2014-01-07 08:43:44 +00:00
|
|
|
dst.getPrimitiveArray().length >= dstLength + srcLength * 2);
|
2013-05-23 09:06:56 +00:00
|
|
|
assertIntArrayEquals("original values after 2nd append",
|
2014-01-07 08:43:44 +00:00
|
|
|
dstCopy.getPrimitiveArray(), startPos, dst.getPrimitiveArray(), startPos,
|
|
|
|
dstLength);
|
2013-05-23 09:06:56 +00:00
|
|
|
assertIntArrayEquals("appended values after 2nd append",
|
2014-01-07 08:43:44 +00:00
|
|
|
src.getPrimitiveArray(), startPos, dst.getPrimitiveArray(), dstLength,
|
|
|
|
srcLength);
|
2013-05-23 09:06:56 +00:00
|
|
|
assertIntArrayEquals("appended values after 2nd append",
|
2014-01-07 08:43:44 +00:00
|
|
|
src.getPrimitiveArray(), startPos, dst.getPrimitiveArray(), dstLength + srcLength,
|
|
|
|
srcLength);
|
2012-07-19 10:45:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2012-07-20 03:45:15 +00:00
|
|
|
public void testFill() {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int srcLength = DEFAULT_CAPACITY;
|
|
|
|
final ResizableIntArray src = new ResizableIntArray(srcLength);
|
|
|
|
for (int i = 0; i < srcLength; i++) {
|
|
|
|
final int value = i;
|
|
|
|
src.add(value);
|
2012-07-20 03:45:15 +00:00
|
|
|
}
|
|
|
|
final int[] array = src.getPrimitiveArray();
|
|
|
|
|
2014-01-07 08:43:44 +00:00
|
|
|
final int startPos = srcLength / 3;
|
|
|
|
final int length = srcLength / 3;
|
2012-07-20 03:45:15 +00:00
|
|
|
final int endPos = startPos + length;
|
|
|
|
assertTrue(startPos >= 1);
|
2014-01-07 08:43:44 +00:00
|
|
|
final int fillValue = 123;
|
2012-07-20 03:45:15 +00:00
|
|
|
try {
|
2014-01-07 08:43:44 +00:00
|
|
|
src.fill(fillValue, -1 /* startPos */, length);
|
2012-07-20 03:45:15 +00:00
|
|
|
fail("fill from -1 shouldn't succeed");
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
// success
|
|
|
|
}
|
|
|
|
try {
|
2014-01-07 08:43:44 +00:00
|
|
|
src.fill(fillValue, startPos, -1 /* length */);
|
2012-07-20 03:45:15 +00:00
|
|
|
fail("fill negative length shouldn't succeed");
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
// success
|
|
|
|
}
|
|
|
|
|
2014-01-07 08:43:44 +00:00
|
|
|
src.fill(fillValue, startPos, length);
|
|
|
|
assertEquals("length after fill", srcLength, src.getLength());
|
2012-07-20 03:45:15 +00:00
|
|
|
assertSame("array after fill", array, src.getPrimitiveArray());
|
2014-01-07 08:43:44 +00:00
|
|
|
for (int i = 0; i < srcLength; i++) {
|
|
|
|
final int value = i;
|
2012-07-20 03:45:15 +00:00
|
|
|
if (i >= startPos && i < endPos) {
|
2014-01-07 08:43:44 +00:00
|
|
|
assertEquals("new values after fill at " + i, fillValue, src.get(i));
|
2012-07-20 03:45:15 +00:00
|
|
|
} else {
|
2014-01-07 08:43:44 +00:00
|
|
|
assertEquals("unmodified values after fill at " + i, value, src.get(i));
|
2012-07-20 03:45:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 08:43:44 +00:00
|
|
|
final int length2 = srcLength * 2 - startPos;
|
2012-07-20 03:45:15 +00:00
|
|
|
final int largeEnd = startPos + length2;
|
2014-01-07 08:43:44 +00:00
|
|
|
assertTrue(largeEnd > srcLength);
|
|
|
|
final int fillValue2 = 456;
|
|
|
|
src.fill(fillValue2, startPos, length2);
|
2012-07-20 03:45:15 +00:00
|
|
|
assertEquals("length after large fill", largeEnd, src.getLength());
|
|
|
|
assertNotSame("array after large fill", array, src.getPrimitiveArray());
|
|
|
|
for (int i = 0; i < largeEnd; i++) {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int value = i;
|
2012-07-20 03:45:15 +00:00
|
|
|
if (i >= startPos && i < largeEnd) {
|
2014-01-07 08:43:44 +00:00
|
|
|
assertEquals("new values after large fill at " + i, fillValue2, src.get(i));
|
2012-07-20 03:45:15 +00:00
|
|
|
} else {
|
2014-01-07 08:43:44 +00:00
|
|
|
assertEquals("unmodified values after large fill at " + i, value, src.get(i));
|
2012-07-20 03:45:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final int startPos2 = largeEnd + length2;
|
|
|
|
final int endPos2 = startPos2 + length2;
|
2014-01-07 08:43:44 +00:00
|
|
|
final int fillValue3 = 789;
|
|
|
|
src.fill(fillValue3, startPos2, length2);
|
2012-07-20 03:45:15 +00:00
|
|
|
assertEquals("length after disjoint fill", endPos2, src.getLength());
|
|
|
|
for (int i = 0; i < endPos2; i++) {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int value = i;
|
2012-07-20 03:45:15 +00:00
|
|
|
if (i >= startPos2 && i < endPos2) {
|
2014-01-07 08:43:44 +00:00
|
|
|
assertEquals("new values after disjoint fill at " + i, fillValue3, src.get(i));
|
2012-07-20 03:45:15 +00:00
|
|
|
} else if (i >= startPos && i < largeEnd) {
|
2014-01-07 08:43:44 +00:00
|
|
|
assertEquals("unmodified values after disjoint fill at " + i,
|
|
|
|
fillValue2, src.get(i));
|
2012-07-20 03:45:15 +00:00
|
|
|
} else if (i < startPos) {
|
2014-01-07 08:43:44 +00:00
|
|
|
assertEquals("unmodified values after disjoint fill at " + i, value, src.get(i));
|
2012-07-20 03:45:15 +00:00
|
|
|
} else {
|
2014-01-07 08:43:44 +00:00
|
|
|
assertEquals("gap values after disjoint fill at " + i, 0, src.get(i));
|
2012-07-20 03:45:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-23 09:06:56 +00:00
|
|
|
private static void assertIntArrayEquals(final String message, final int[] expecteds,
|
|
|
|
final int expectedPos, final int[] actuals, final int actualPos, final int length) {
|
|
|
|
if (expecteds == actuals) {
|
2012-07-19 10:45:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (expecteds == null || actuals == null) {
|
2013-05-23 09:06:56 +00:00
|
|
|
assertEquals(message, Arrays.toString(expecteds), Arrays.toString(actuals));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (expecteds.length < expectedPos + length || actuals.length < actualPos + length) {
|
|
|
|
fail(message + ": insufficient length: expecteds=" + Arrays.toString(expecteds)
|
|
|
|
+ " actuals=" + Arrays.toString(actuals));
|
|
|
|
return;
|
2012-07-19 10:45:20 +00:00
|
|
|
}
|
|
|
|
for (int i = 0; i < length; i++) {
|
2013-05-23 09:06:56 +00:00
|
|
|
assertEquals(message + " [" + i + "]",
|
2012-07-19 10:45:20 +00:00
|
|
|
expecteds[i + expectedPos], actuals[i + actualPos]);
|
|
|
|
}
|
|
|
|
}
|
2013-09-10 08:48:00 +00:00
|
|
|
|
2018-07-06 17:10:54 +00:00
|
|
|
@Test
|
2013-09-10 08:48:00 +00:00
|
|
|
public void testShift() {
|
|
|
|
final ResizableIntArray src = new ResizableIntArray(DEFAULT_CAPACITY);
|
|
|
|
final int limit = DEFAULT_CAPACITY * 10;
|
|
|
|
final int shiftAmount = 20;
|
|
|
|
for (int i = 0; i < limit; ++i) {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int value = i;
|
2014-01-08 05:11:56 +00:00
|
|
|
src.addAt(i, value);
|
2013-09-10 08:48:00 +00:00
|
|
|
assertEquals("length after add at " + i, i + 1, src.getLength());
|
|
|
|
}
|
|
|
|
src.shift(shiftAmount);
|
|
|
|
for (int i = 0; i < limit - shiftAmount; ++i) {
|
2014-01-07 08:43:44 +00:00
|
|
|
final int oldValue = i + shiftAmount;
|
|
|
|
assertEquals("value at " + i, oldValue, src.get(i));
|
2013-09-10 08:48:00 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-19 10:45:20 +00:00
|
|
|
}
|