|
|
@@ -28,12 +28,15 @@ public class ArrayAllocatorTest {
|
|
|
|
|
|
assertArray(Object[].class, 0, ArrayAllocator.forPrototype(new Object[0], 0));
|
|
|
assertSame(ArrayAllocator.EMPTY_ARRAY, ArrayAllocator.forPrototype(new Object[0], 0));
|
|
|
-
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testForPrototype_A() {
|
|
|
assertArray(float[].class, 4, ArrayAllocator.forPrototype(new float[0], 4));
|
|
|
assertArray(int[].class, 4, ArrayAllocator.forPrototype(new int[0], 4));
|
|
|
assertArray(float[].class, 0, ArrayAllocator.forPrototype(new float[0], 0));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Test
|
|
|
public void testForComponent() {
|
|
|
assertArray(Float[].class, 4, ArrayAllocator.forComponent(Float.class, 4));
|
|
|
@@ -45,7 +48,10 @@ public class ArrayAllocatorTest {
|
|
|
|
|
|
assertArray(Object[].class, 0, ArrayAllocator.forComponent(Object.class, 0));
|
|
|
assertSame(ArrayAllocator.EMPTY_ARRAY, ArrayAllocator.forComponent(Object.class, 0));
|
|
|
-
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testForComponent_A() {
|
|
|
assertArray(float[].class, 4, ArrayAllocator.forComponent(float.class, 4));
|
|
|
assertArray(int[].class, 4, ArrayAllocator.forComponent(int.class, 4));
|
|
|
assertArray(float[].class, 0, ArrayAllocator.forComponent(float.class, 0));
|
|
|
@@ -63,6 +69,19 @@ public class ArrayAllocatorTest {
|
|
|
assertNotSame(array1, array2);
|
|
|
assertArrayEquals(new Integer[]{1,2,3,4,5,6,null,null}, array2);
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testEnsureCapacity_A() {
|
|
|
+ byte[] array1 = new byte[]{ 1,2,3,4,5,6 };
|
|
|
+
|
|
|
+ assertSame(array1, ArrayAllocator.ensureCapacity(array1, 0));
|
|
|
+ assertSame(array1, ArrayAllocator.ensureCapacity(array1, 3));
|
|
|
+ assertSame(array1, ArrayAllocator.ensureCapacity(array1, 6));
|
|
|
+
|
|
|
+ byte[] array2 = ArrayAllocator.ensureCapacity(array1, 8);
|
|
|
+ assertNotSame(array1, array2);
|
|
|
+ assertArrayEquals(new byte[]{1,2,3,4,5,6,0,0}, array2);
|
|
|
+ }
|
|
|
|
|
|
@Test
|
|
|
public void testEnsureCapacity_Preserve() {
|
|
|
@@ -84,6 +103,27 @@ public class ArrayAllocatorTest {
|
|
|
ArrayAllocator.ensureCapacity(array1, 8, 7);
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testEnsureCapacity_Preserve_A() {
|
|
|
+ byte[] array1 = new byte[]{ 1,2,3,4,5,6 };
|
|
|
+
|
|
|
+ assertSame(array1, ArrayAllocator.ensureCapacity(array1, 0, 0));
|
|
|
+ assertSame(array1, ArrayAllocator.ensureCapacity(array1, 3, 0));
|
|
|
+ assertSame(array1, ArrayAllocator.ensureCapacity(array1, 6, 0));
|
|
|
+
|
|
|
+ byte[] array2 = ArrayAllocator.ensureCapacity(array1, 8, 6);
|
|
|
+ assertNotSame(array1, array2);
|
|
|
+ assertArrayEquals(new byte[]{1,2,3,4,5,6,0,0}, array2);
|
|
|
+
|
|
|
+ byte[] array3 = ArrayAllocator.ensureCapacity(array1, 8, 4);
|
|
|
+ assertNotSame(array1, array3);
|
|
|
+ assertArrayEquals(new byte[]{1,2,3,4,0,0,0,0}, array3);
|
|
|
+
|
|
|
+ QAssert.assertThrows(ArrayIndexOutOfBoundsException.class, ()-> {
|
|
|
+ ArrayAllocator.ensureCapacity(array1, 8, 7);
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
@Test
|
|
|
public void testEnsureOffsetLength() {
|
|
|
@@ -109,6 +149,31 @@ public class ArrayAllocatorTest {
|
|
|
ArrayAllocator.ensureOffsetLength(array1, 3, 10);
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testEnsureOffsetLength_A() {
|
|
|
+ byte[] array1 = new byte[4];
|
|
|
+ ArrayAllocator.ensureOffsetLength(array1, 1, 2);
|
|
|
+ ArrayAllocator.ensureOffsetLength(array1, 0, 4);
|
|
|
+ ArrayAllocator.ensureOffsetLength(array1, 1, 3);
|
|
|
+
|
|
|
+ QAssert.assertThrows(IllegalArgumentException.class, ()-> {
|
|
|
+ ArrayAllocator.ensureOffsetLength(array1, 1, -1);
|
|
|
+ });
|
|
|
+ QAssert.assertThrows(IllegalArgumentException.class, ()-> {
|
|
|
+ ArrayAllocator.ensureOffsetLength(array1, 8, -1);
|
|
|
+ });
|
|
|
+
|
|
|
+ QAssert.assertThrows(ArrayIndexOutOfBoundsException.class, ()-> {
|
|
|
+ ArrayAllocator.ensureOffsetLength(array1, -1, 1);
|
|
|
+ });
|
|
|
+ QAssert.assertThrows(ArrayIndexOutOfBoundsException.class, ()-> {
|
|
|
+ ArrayAllocator.ensureOffsetLength(array1, 8, 1);
|
|
|
+ });
|
|
|
+ QAssert.assertThrows(ArrayIndexOutOfBoundsException.class, ()-> {
|
|
|
+ ArrayAllocator.ensureOffsetLength(array1, 3, 10);
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
@Test
|
|
|
public void testEnsureOffsetLength_N() {
|
|
|
@@ -133,7 +198,7 @@ public class ArrayAllocatorTest {
|
|
|
ArrayAllocator.ensureOffsetLength(4, 3, 10);
|
|
|
});
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Test
|
|
|
public void testEnsureFromTo() {
|
|
|
Integer[] array1 = new Integer[4];
|
|
|
@@ -155,6 +220,28 @@ public class ArrayAllocatorTest {
|
|
|
ArrayAllocator.ensureFromTo(array1, 5, 6);
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testEnsureFromTo_A() {
|
|
|
+ byte[] array1 = new byte[4];
|
|
|
+ ArrayAllocator.ensureFromTo(array1, 1, 3);
|
|
|
+ ArrayAllocator.ensureFromTo(array1, 0, 4);
|
|
|
+ ArrayAllocator.ensureFromTo(array1, 1, 4);
|
|
|
+
|
|
|
+ QAssert.assertThrows(IllegalArgumentException.class, ()-> {
|
|
|
+ ArrayAllocator.ensureFromTo(array1, 3, 1);
|
|
|
+ });
|
|
|
+
|
|
|
+ QAssert.assertThrows(ArrayIndexOutOfBoundsException.class, ()-> {
|
|
|
+ ArrayAllocator.ensureFromTo(array1, -1, 8);
|
|
|
+ });
|
|
|
+ QAssert.assertThrows(ArrayIndexOutOfBoundsException.class, ()-> {
|
|
|
+ ArrayAllocator.ensureFromTo(array1, 2, 8);
|
|
|
+ });
|
|
|
+ QAssert.assertThrows(ArrayIndexOutOfBoundsException.class, ()-> {
|
|
|
+ ArrayAllocator.ensureFromTo(array1, 5, 6);
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
@Test
|
|
|
public void testEnsureFromTo_N() {
|
|
|
@@ -189,6 +276,18 @@ public class ArrayAllocatorTest {
|
|
|
assertArrayEquals(new Integer[]{1,2,3,null,null,null,null,null,null}, ArrayAllocator.grow(array1, 9));
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void testGrow_A() {
|
|
|
+ byte[] array1 = new byte[]{1,2,3};
|
|
|
+ assertSame(array1, ArrayAllocator.grow(array1, 2));
|
|
|
+ assertSame(array1, ArrayAllocator.grow(array1, 3));
|
|
|
+ assertArrayEquals(new byte[]{1,2,3,0,0,0}, ArrayAllocator.grow(array1, 4));
|
|
|
+ assertArrayEquals(new byte[]{1,2,3,0,0,0}, ArrayAllocator.grow(array1, 5));
|
|
|
+ assertArrayEquals(new byte[]{1,2,3,0,0,0}, ArrayAllocator.grow(array1, 6));
|
|
|
+ assertArrayEquals(new byte[]{1,2,3,0,0,0,0}, ArrayAllocator.grow(array1, 7));
|
|
|
+ assertArrayEquals(new byte[]{1,2,3,0,0,0,0,0,0}, ArrayAllocator.grow(array1, 9));
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
public void testGrow_preserve() {
|
|
|
Integer[] array1 = new Integer[]{1,2,3};
|
|
|
@@ -201,6 +300,18 @@ public class ArrayAllocatorTest {
|
|
|
assertArrayEquals(new Integer[]{1,2,null,null,null,null,null,null,null}, ArrayAllocator.grow(array1, 9, 2));
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void testGrow_preserve_A() {
|
|
|
+ byte[] array1 = new byte[]{1,2,3};
|
|
|
+ assertSame(array1, ArrayAllocator.grow(array1, 2, 2));
|
|
|
+ assertSame(array1, ArrayAllocator.grow(array1, 3, 2));
|
|
|
+ assertArrayEquals(new byte[]{1,2,0,0,0,0}, ArrayAllocator.grow(array1, 4, 2));
|
|
|
+ assertArrayEquals(new byte[]{1,2,0,0,0,0}, ArrayAllocator.grow(array1, 5, 2));
|
|
|
+ assertArrayEquals(new byte[]{1,2,0,0,0,0}, ArrayAllocator.grow(array1, 6, 2));
|
|
|
+ assertArrayEquals(new byte[]{1,2,0,0,0,0,0}, ArrayAllocator.grow(array1, 7, 2));
|
|
|
+ assertArrayEquals(new byte[]{1,2,0,0,0,0,0,0,0}, ArrayAllocator.grow(array1, 9, 2));
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
public void testTrim() {
|
|
|
Integer[] array1 = new Integer[]{1,2,3};
|
|
|
@@ -211,6 +322,17 @@ public class ArrayAllocatorTest {
|
|
|
assertArrayEquals(new Integer[]{1}, ArrayAllocator.trim(array1, 1));
|
|
|
assertArrayEquals(new Integer[]{}, ArrayAllocator.trim(array1, 0));
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testTrim_A() {
|
|
|
+ byte[] array1 = new byte[]{1,2,3};
|
|
|
+
|
|
|
+ assertSame(array1, ArrayAllocator.trim(array1, 5));
|
|
|
+ assertSame(array1, ArrayAllocator.trim(array1, 3));
|
|
|
+ assertArrayEquals(new byte[]{1,2}, ArrayAllocator.trim(array1, 2));
|
|
|
+ assertArrayEquals(new byte[]{1}, ArrayAllocator.trim(array1, 1));
|
|
|
+ assertArrayEquals(new byte[]{}, ArrayAllocator.trim(array1, 0));
|
|
|
+ }
|
|
|
|
|
|
private void assertArray(Class<?> type, int size, Object array) {
|
|
|
assertEquals(type, array.getClass());
|