插件开发工具库,推荐依赖该工具库。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

164 lines
7.9 KiB

package com.fanruan.api.util;
import com.fanruan.api.Prepare;
import java.util.*;
import org.junit.Assert;
import org.junit.Test;
public class ArrayKitTest extends Prepare {
@Test
public void isEmpty() throws Exception{
Object[] emptyArray = new Object[0];
Object[] notEmptyArray = new Object[1];
Boolean[] booleanEmptyArray = new Boolean[0];
Boolean[] booleanNotEmptyArray = new Boolean[1];
Byte[] byteEmptyArray = new Byte[0];
Byte[] byteNotEmptyArray = new Byte[1];
Character[] charEmptyArray = new Character[0];
Character[] charNotEmptyArray = new Character[1];
Double[] doubleEmptyArray = new Double[0];
Double[] doubleNotEmptyArray = new Double[1];
Float[] floatEmptyArray = new Float[0];
Float[] floatNotEmptyArray = new Float[1];
Integer[] intEmptyArray = new Integer[0];
Integer[] intNotEmptyArray = new Integer[1];
Long[] longEmptyArray = new Long[0];
Long[] longNotEmptyArray = new Long[1];
Short[] shortEmptyArray = new Short[0];
Short[] shortNotEmptyArray = new Short[1];
Assert.assertTrue(ArrayKit.isEmpty(emptyArray));
Assert.assertFalse(ArrayKit.isEmpty(notEmptyArray));
Assert.assertTrue(ArrayKit.isEmpty(booleanEmptyArray));
Assert.assertFalse(ArrayKit.isEmpty(booleanNotEmptyArray));
Assert.assertTrue(ArrayKit.isEmpty(byteEmptyArray));
Assert.assertFalse(ArrayKit.isEmpty(byteNotEmptyArray));
Assert.assertTrue(ArrayKit.isEmpty(charEmptyArray));
Assert.assertFalse(ArrayKit.isEmpty(charNotEmptyArray));
Assert.assertTrue(ArrayKit.isEmpty(doubleEmptyArray));
Assert.assertFalse(ArrayKit.isEmpty(doubleNotEmptyArray));
Assert.assertTrue(ArrayKit.isEmpty(floatEmptyArray));
Assert.assertFalse(ArrayKit.isEmpty(floatNotEmptyArray));
Assert.assertTrue(ArrayKit.isEmpty(intEmptyArray));
Assert.assertFalse(ArrayKit.isEmpty(intNotEmptyArray));
Assert.assertTrue(ArrayKit.isEmpty(longEmptyArray));
Assert.assertFalse(ArrayKit.isEmpty(longNotEmptyArray));
Assert.assertTrue(ArrayKit.isEmpty(shortEmptyArray));
Assert.assertFalse(ArrayKit.isEmpty(shortNotEmptyArray));
}
@Test
public void remove() throws Exception{
Integer[] intTestArray = {1, 2, 3, 4};
Integer[] intTestResult = {1, 2, 3};
Boolean[] booleanTestArray = {true, true, true, false};
Boolean[] booleanTestResult = {true, true, true};
Byte[] byteTestArray = {100, 101, 102, 103};
Byte[] byteTestResult = {100, 101, 102};
Character[] charTestArray = {'a', 'b', 'c', 'd'};
Character[] charTestResult = {'a', 'b', 'c'};
Double[] doubleTestArray = {1.1, 2.2, 3.3, 4.4};
Double[] doubleTestResult = {1.1, 2.2, 3.3};
Float[] floatTestArray = {1.1f, 2.2f, 3.3f, 4.4f};
Float[] floatTestResult = {1.1f, 2.2f, 3.3f};
Long[] longTestArray = {1L, 2L, 3L, 4L};
Long[] longTestResult = {1L, 2L, 3L};
Short[] shortTestArray = {1, 2, 3, 4};
Short[] shortTestResult = {1, 2, 3};
Assert.assertEquals(ArrayKit.remove(intTestArray, 3), intTestResult);
Assert.assertEquals(ArrayKit.remove(booleanTestArray, 3), booleanTestResult);
Assert.assertEquals(ArrayKit.remove(byteTestArray, 3), byteTestResult);
Assert.assertEquals(ArrayKit.remove(charTestArray, 3), charTestResult);
Assert.assertEquals(ArrayKit.remove(doubleTestArray, 3), doubleTestResult);
Assert.assertEquals(ArrayKit.remove(floatTestArray, 3), floatTestResult);
Assert.assertEquals(ArrayKit.remove(longTestArray, 3), longTestResult);
Assert.assertEquals(ArrayKit.remove(shortTestArray, 3), shortTestResult);
}
@Test
public void add() throws Exception{
Integer[] intTestArray = {1, 2, 3, 4};
Integer[] intTestResult = {1, 2, 3, 4, 5};
Boolean[] booleanTestArray = {true, true, true, false};
Boolean[] booleanTestResult = {true, true, true, false, true};
Byte[] byteTestArray = {1};
Byte[] byteTestResult = {1, 0};
Character[] charTestArray = {'a', 'b', 'c', 'd'};
Character[] charTestResult = {'a', 'b', 'c', 'd', 'e'};
Double[] doubleTestArray = {1.1, 2.2, 3.3, 4.4};
Double[] doubleTestResult = {1.1, 2.2, 3.3, 4.4, 5.5};
Float[] floatTestArray = {1.1f, 2.2f, 3.3f, 4.4f};
Float[] floatTestResult = {1.1f, 2.2f, 3.3f, 4.4f, 5.5f};
Long[] longTestArray = {1L, 2L, 3L, 4L};
Long[] longTestResult = {1L, 2L, 3L, 4L, 5L};
Short[] shortTestArray = {1, 2, 3, 4};
Short[] shortTestResult = {1, 2, 3, 4, 5};
Assert.assertEquals(ArrayKit.add(intTestArray, 5), intTestResult);
Assert.assertEquals(ArrayKit.add(booleanTestArray, true), booleanTestResult);
Assert.assertEquals(ArrayKit.add(byteTestArray, new Byte("0")), byteTestResult);
Assert.assertEquals(ArrayKit.add(charTestArray, 'e'), charTestResult);
Assert.assertEquals(ArrayKit.add(doubleTestArray, 5.5), doubleTestResult);
Assert.assertEquals(ArrayKit.add(floatTestArray, 5.5f), floatTestResult);
Assert.assertEquals(ArrayKit.add(longTestArray, 5L), longTestResult);
Assert.assertEquals(ArrayKit.add(shortTestArray, new Short("5")), shortTestResult);
}
@Test
public void addAll() throws Exception{
Integer[] intTestArray = {1, 2, 3, 4};
Integer[] intTestResult = {1, 2, 3, 4, 5, 6};
Boolean[] booleanTestArray = {true, true, true, false};
Boolean[] booleanTestResult = {true, true, true, false, true, true};
Byte[] byteTestArray = {1};
Byte[] byteTestResult = {1, 0, 0};
Character[] charTestArray = {'a', 'b', 'c', 'd'};
Character[] charTestResult = {'a', 'b', 'c', 'd', 'e', 'f'};
Double[] doubleTestArray = {1.1, 2.2, 3.3, 4.4};
Double[] doubleTestResult = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6};
Float[] floatTestArray = {1.1f, 2.2f, 3.3f, 4.4f};
Float[] floatTestResult = {1.1f, 2.2f, 3.3f, 4.4f, 5.5f, 6.6f};
Long[] longTestArray = {1L, 2L, 3L, 4L};
Long[] longTestResult = {1L, 2L, 3L, 4L, 5L, 6L};
Short[] shortTestArray = {1, 2, 3, 4};
Short[] shortTestResult = {1, 2, 3, 4, 5, 6};
Assert.assertEquals(ArrayKit.addAll(intTestArray, 5,6), intTestResult);
Assert.assertEquals(ArrayKit.addAll(booleanTestArray, true, true), booleanTestResult);
Assert.assertEquals(ArrayKit.addAll(byteTestArray, new Byte("0"), new Byte("0")), byteTestResult);
Assert.assertEquals(ArrayKit.addAll(charTestArray, 'e','f'), charTestResult);
Assert.assertEquals(ArrayKit.addAll(doubleTestArray, 5.5,6.6), doubleTestResult);
Assert.assertEquals(ArrayKit.addAll(floatTestArray, 5.5f,6.6f), floatTestResult);
Assert.assertEquals(ArrayKit.addAll(longTestArray, 5L,6L), longTestResult);
Assert.assertEquals(ArrayKit.addAll(shortTestArray, new Short("5"),new Short("6")), shortTestResult);
}
@Test
public void toList() throws Exception{
Integer array[] = {1,2,3};
ArrayList<Integer> result = new ArrayList<Integer>();
result.add(1);
result.add(2);
result.add(3);
Assert.assertEquals(ArrayKit.toList(array),result);
}
@Test
public void getLength() {
int[] a = new int[0];
int[] b = new int[2];
Assert.assertEquals(ArrayKit.getLength(a),0);
Assert.assertEquals(ArrayKit.getLength(null),0);
Assert.assertEquals(ArrayKit.getLength(b),2);
}
@Test
public void testInsert() {
Integer[] array1 = new Integer[] {0, 1, 2};
Integer[] result1 = new Integer[] {0, 7, 8, 1, 2};
String[] array2 = new String[] {"1", "2"};
String[] result2 = new String[] {"3", "1", "2"};
Assert.assertEquals(ArrayKit.insert(1, array1, 7, 8), result1);
Assert.assertEquals(ArrayKit.insert(0, array2, "3"), result2);
}
}