1 16 17 package org.apache.commons.lang; 18 19 import java.util.Arrays ; 20 21 import junit.framework.Test; 22 import junit.framework.TestCase; 23 import junit.framework.TestSuite; 24 import junit.textui.TestRunner; 25 26 32 public class ArrayUtilsAddTest extends TestCase { 33 public static void main(String [] args) { 34 TestRunner.run(suite()); 35 } 36 37 public static Test suite() { 38 TestSuite suite = new TestSuite(ArrayUtilsAddTest.class); 39 suite.setName("ArrayUtils add Tests"); 40 return suite; 41 } 42 43 public void testAddObjectArrayBoolean() { 44 boolean[] newArray; 45 newArray = ArrayUtils.add((boolean[])null, false); 46 assertTrue(Arrays.equals(new boolean[]{false}, newArray)); 47 assertEquals(Boolean.TYPE, newArray.getClass().getComponentType()); 48 newArray = ArrayUtils.add((boolean[])null, true); 49 assertTrue(Arrays.equals(new boolean[]{true}, newArray)); 50 assertEquals(Boolean.TYPE, newArray.getClass().getComponentType()); 51 boolean[] array1 = new boolean[]{true, false, true}; 52 newArray = ArrayUtils.add(array1, false); 53 assertTrue(Arrays.equals(new boolean[]{true, false, true, false}, newArray)); 54 assertEquals(Boolean.TYPE, newArray.getClass().getComponentType()); 55 } 56 57 public void testAddObjectArrayByte() { 58 byte[] newArray; 59 newArray = ArrayUtils.add((byte[])null, (byte)0); 60 assertTrue(Arrays.equals(new byte[]{0}, newArray)); 61 assertEquals(Byte.TYPE, newArray.getClass().getComponentType()); 62 newArray = ArrayUtils.add((byte[])null, (byte)1); 63 assertTrue(Arrays.equals(new byte[]{1}, newArray)); 64 assertEquals(Byte.TYPE, newArray.getClass().getComponentType()); 65 byte[] array1 = new byte[]{1, 2, 3}; 66 newArray = ArrayUtils.add(array1, (byte)0); 67 assertTrue(Arrays.equals(new byte[]{1, 2, 3, 0}, newArray)); 68 assertEquals(Byte.TYPE, newArray.getClass().getComponentType()); 69 newArray = ArrayUtils.add(array1, (byte)4); 70 assertTrue(Arrays.equals(new byte[]{1, 2, 3, 4}, newArray)); 71 assertEquals(Byte.TYPE, newArray.getClass().getComponentType()); 72 } 73 74 public void testAddObjectArrayChar() { 75 char[] newArray; 76 newArray = ArrayUtils.add((char[])null, (char)0); 77 assertTrue(Arrays.equals(new char[]{0}, newArray)); 78 assertEquals(Character.TYPE, newArray.getClass().getComponentType()); 79 newArray = ArrayUtils.add((char[])null, (char)1); 80 assertTrue(Arrays.equals(new char[]{1}, newArray)); 81 assertEquals(Character.TYPE, newArray.getClass().getComponentType()); 82 char[] array1 = new char[]{1, 2, 3}; 83 newArray = ArrayUtils.add(array1, (char)0); 84 assertTrue(Arrays.equals(new char[]{1, 2, 3, 0}, newArray)); 85 assertEquals(Character.TYPE, newArray.getClass().getComponentType()); 86 newArray = ArrayUtils.add(array1, (char)4); 87 assertTrue(Arrays.equals(new char[]{1, 2, 3, 4}, newArray)); 88 assertEquals(Character.TYPE, newArray.getClass().getComponentType()); 89 } 90 91 public void testAddObjectArrayDouble() { 92 double[] newArray; 93 newArray = ArrayUtils.add((double[])null, 0); 94 assertTrue(Arrays.equals(new double[]{0}, newArray)); 95 assertEquals(Double.TYPE, newArray.getClass().getComponentType()); 96 newArray = ArrayUtils.add((double[])null, 1); 97 assertTrue(Arrays.equals(new double[]{1}, newArray)); 98 assertEquals(Double.TYPE, newArray.getClass().getComponentType()); 99 double[] array1 = new double[]{1, 2, 3}; 100 newArray = ArrayUtils.add(array1, 0); 101 assertTrue(Arrays.equals(new double[]{1, 2, 3, 0}, newArray)); 102 assertEquals(Double.TYPE, newArray.getClass().getComponentType()); 103 newArray = ArrayUtils.add(array1, 4); 104 assertTrue(Arrays.equals(new double[]{1, 2, 3, 4}, newArray)); 105 assertEquals(Double.TYPE, newArray.getClass().getComponentType()); 106 } 107 108 public void testAddObjectArrayFloat() { 109 float[] newArray; 110 newArray = ArrayUtils.add((float[])null, 0); 111 assertTrue(Arrays.equals(new float[]{0}, newArray)); 112 assertEquals(Float.TYPE, newArray.getClass().getComponentType()); 113 newArray = ArrayUtils.add((float[])null, 1); 114 assertTrue(Arrays.equals(new float[]{1}, newArray)); 115 assertEquals(Float.TYPE, newArray.getClass().getComponentType()); 116 float[] array1 = new float[]{1, 2, 3}; 117 newArray = ArrayUtils.add(array1, 0); 118 assertTrue(Arrays.equals(new float[]{1, 2, 3, 0}, newArray)); 119 assertEquals(Float.TYPE, newArray.getClass().getComponentType()); 120 newArray = ArrayUtils.add(array1, 4); 121 assertTrue(Arrays.equals(new float[]{1, 2, 3, 4}, newArray)); 122 assertEquals(Float.TYPE, newArray.getClass().getComponentType()); 123 } 124 125 public void testAddObjectArrayInt() { 126 int[] newArray; 127 newArray = ArrayUtils.add((int[])null, 0); 128 assertTrue(Arrays.equals(new int[]{0}, newArray)); 129 assertEquals(Integer.TYPE, newArray.getClass().getComponentType()); 130 newArray = ArrayUtils.add((int[])null, 1); 131 assertTrue(Arrays.equals(new int[]{1}, newArray)); 132 assertEquals(Integer.TYPE, newArray.getClass().getComponentType()); 133 int[] array1 = new int[]{1, 2, 3}; 134 newArray = ArrayUtils.add(array1, 0); 135 assertTrue(Arrays.equals(new int[]{1, 2, 3, 0}, newArray)); 136 assertEquals(Integer.TYPE, newArray.getClass().getComponentType()); 137 newArray = ArrayUtils.add(array1, 4); 138 assertTrue(Arrays.equals(new int[]{1, 2, 3, 4}, newArray)); 139 assertEquals(Integer.TYPE, newArray.getClass().getComponentType()); 140 } 141 142 public void testAddObjectArrayLong() { 143 long[] newArray; 144 newArray = ArrayUtils.add((long[])null, 0); 145 assertTrue(Arrays.equals(new long[]{0}, newArray)); 146 assertEquals(Long.TYPE, newArray.getClass().getComponentType()); 147 newArray = ArrayUtils.add((long[])null, 1); 148 assertTrue(Arrays.equals(new long[]{1}, newArray)); 149 assertEquals(Long.TYPE, newArray.getClass().getComponentType()); 150 long[] array1 = new long[]{1, 2, 3}; 151 newArray = ArrayUtils.add(array1, 0); 152 assertTrue(Arrays.equals(new long[]{1, 2, 3, 0}, newArray)); 153 assertEquals(Long.TYPE, newArray.getClass().getComponentType()); 154 newArray = ArrayUtils.add(array1, 4); 155 assertTrue(Arrays.equals(new long[]{1, 2, 3, 4}, newArray)); 156 assertEquals(Long.TYPE, newArray.getClass().getComponentType()); 157 } 158 159 public void testAddObjectArrayShort() { 160 short[] newArray; 161 newArray = ArrayUtils.add((short[])null, (short)0); 162 assertTrue(Arrays.equals(new short[]{0}, newArray)); 163 assertEquals(Short.TYPE, newArray.getClass().getComponentType()); 164 newArray = ArrayUtils.add((short[])null, (short)1); 165 assertTrue(Arrays.equals(new short[]{1}, newArray)); 166 assertEquals(Short.TYPE, newArray.getClass().getComponentType()); 167 short[] array1 = new short[]{1, 2, 3}; 168 newArray = ArrayUtils.add(array1, (short)0); 169 assertTrue(Arrays.equals(new short[]{1, 2, 3, 0}, newArray)); 170 assertEquals(Short.TYPE, newArray.getClass().getComponentType()); 171 newArray = ArrayUtils.add(array1, (short)4); 172 assertTrue(Arrays.equals(new short[]{1, 2, 3, 4}, newArray)); 173 assertEquals(Short.TYPE, newArray.getClass().getComponentType()); 174 } 175 176 public void testAddObjectArrayObject() { 177 Object [] newArray; 178 newArray = ArrayUtils.add((Object [])null, null); 179 assertTrue(Arrays.equals((new Object []{null}), newArray)); 180 assertEquals(Object .class, newArray.getClass().getComponentType()); 181 182 newArray = ArrayUtils.add((Object [])null, "a"); 183 assertTrue(Arrays.equals((new String []{"a"}), newArray)); 184 assertTrue(Arrays.equals((new Object []{"a"}), newArray)); 185 assertEquals(String .class, newArray.getClass().getComponentType()); 186 187 String [] stringArray1 = new String []{"a", "b", "c"}; 188 newArray = ArrayUtils.add(stringArray1, null); 189 assertTrue(Arrays.equals((new String []{"a", "b", "c", null}), newArray)); 190 assertEquals(String .class, newArray.getClass().getComponentType()); 191 192 newArray = ArrayUtils.add(stringArray1, "d"); 193 assertTrue(Arrays.equals((new String []{"a", "b", "c", "d"}), newArray)); 194 assertEquals(String .class, newArray.getClass().getComponentType()); 195 196 Number [] numberArray1 = new Number []{new Integer (1), new Double (2)}; 197 newArray = ArrayUtils.add(numberArray1, new Float (3)); 198 assertTrue(Arrays.equals((new Number []{new Integer (1), new Double (2), new Float (3)}), newArray)); 199 assertEquals(Number .class, newArray.getClass().getComponentType()); 200 201 numberArray1 = null; 202 newArray = ArrayUtils.add(numberArray1, new Float (3)); 203 assertTrue(Arrays.equals((new Float []{new Float (3)}), newArray)); 204 assertEquals(Float .class, newArray.getClass().getComponentType()); 205 206 numberArray1 = null; 207 newArray = ArrayUtils.add(numberArray1, null); 208 assertTrue(Arrays.equals((new Object []{null}), newArray)); 209 assertEquals(Object .class, newArray.getClass().getComponentType()); 210 } 211 212 public void testAddObjectArrayToObjectArray() { 213 assertNull(ArrayUtils.addAll((Object []) null, (Object []) null)); 214 Object [] newArray; 215 String [] stringArray1 = new String []{"a", "b", "c"}; 216 String [] stringArray2 = new String []{"1", "2", "3"}; 217 newArray = ArrayUtils.addAll(stringArray1, null); 218 assertNotSame(stringArray1, newArray); 219 assertTrue(Arrays.equals(stringArray1, newArray)); 220 assertTrue(Arrays.equals((new String []{"a", "b", "c"}), newArray)); 221 assertEquals(String .class, newArray.getClass().getComponentType()); 222 newArray = ArrayUtils.addAll(null, stringArray2); 223 assertNotSame(stringArray2, newArray); 224 assertTrue(Arrays.equals(stringArray2, newArray)); 225 assertTrue(Arrays.equals((new String []{"1", "2", "3"}), newArray)); 226 assertEquals(String .class, newArray.getClass().getComponentType()); 227 newArray = ArrayUtils.addAll(stringArray1, stringArray2); 228 assertTrue(Arrays.equals((new String []{"a", "b", "c", "1", "2", "3"}), newArray)); 229 assertEquals(String .class, newArray.getClass().getComponentType()); 230 newArray = ArrayUtils.addAll(ArrayUtils.EMPTY_STRING_ARRAY, null); 231 assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, newArray)); 232 assertTrue(Arrays.equals((new String []{}), newArray)); 233 assertEquals(String .class, newArray.getClass().getComponentType()); 234 newArray = ArrayUtils.addAll(null, ArrayUtils.EMPTY_STRING_ARRAY); 235 assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, newArray)); 236 assertTrue(Arrays.equals((new String []{}), newArray)); 237 assertEquals(String .class, newArray.getClass().getComponentType()); 238 newArray = ArrayUtils.addAll(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.EMPTY_STRING_ARRAY); 239 assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, newArray)); 240 assertTrue(Arrays.equals((new String []{}), newArray)); 241 assertEquals(String .class, newArray.getClass().getComponentType()); 242 String [] stringArrayNull = new String []{null}; 243 newArray = ArrayUtils.addAll(stringArrayNull, stringArrayNull); 244 assertTrue(Arrays.equals((new String []{null, null}), newArray)); 245 assertEquals(String .class, newArray.getClass().getComponentType()); 246 247 assertTrue( Arrays.equals( new boolean[] { true, false, false, true }, 249 ArrayUtils.addAll( new boolean[] { true, false }, new boolean[] { false, true } ) ) ); 250 251 assertTrue( Arrays.equals( new char[] { 'a', 'b', 'c', 'd' }, 253 ArrayUtils.addAll( new char[] { 'a', 'b' }, new char[] { 'c', 'd' } ) ) ); 254 255 assertTrue( Arrays.equals( new byte[] { (byte) 0, (byte) 1, (byte) 2, (byte) 3 }, 257 ArrayUtils.addAll( new byte[] { (byte) 0, (byte) 1 }, new byte[] { (byte) 2, (byte) 3 } ) ) ); 258 259 assertTrue( Arrays.equals( new short[] { (short) 10, (short) 20, (short) 30, (short) 40 }, 261 ArrayUtils.addAll( new short[] { (short) 10, (short) 20 }, new short[] { (short) 30, (short) 40 } ) ) ); 262 263 assertTrue( Arrays.equals( new int[] { 1, 1000, -1000, -1 }, 265 ArrayUtils.addAll( new int[] { 1, 1000 }, new int[] { -1000, -1 } ) ) ); 266 267 assertTrue( Arrays.equals( new long[] { 1L, -1L, 1000L, -1000L }, 269 ArrayUtils.addAll( new long[] { 1L, -1L }, new long[] { 1000L, -1000L } ) ) ); 270 271 assertTrue( Arrays.equals( new float[] { 10.5f, 10.1f, 1.6f, 0.01f }, 273 ArrayUtils.addAll( new float[] { 10.5f, 10.1f }, new float[] { 1.6f, 0.01f } ) ) ); 274 275 assertTrue( Arrays.equals( new double[] { Math.PI, -Math.PI, 0, 9.99 }, 277 ArrayUtils.addAll( new double[] { Math.PI, -Math.PI }, new double[] { 0, 9.99 } ) ) ); 278 279 } 280 281 public void testAddObjectAtIndex() { 282 Object [] newArray; 283 newArray = ArrayUtils.add((Object [])null, 0, null); 284 assertTrue(Arrays.equals((new Object []{null}), newArray)); 285 assertEquals(Object .class, newArray.getClass().getComponentType()); 286 newArray = ArrayUtils.add((Object [])null, 0, "a"); 287 assertTrue(Arrays.equals((new String []{"a"}), newArray)); 288 assertTrue(Arrays.equals((new Object []{"a"}), newArray)); 289 assertEquals(String .class, newArray.getClass().getComponentType()); 290 String [] stringArray1 = new String []{"a", "b", "c"}; 291 newArray = ArrayUtils.add(stringArray1, 0, null); 292 assertTrue(Arrays.equals((new String []{null, "a", "b", "c"}), newArray)); 293 assertEquals(String .class, newArray.getClass().getComponentType()); 294 newArray = ArrayUtils.add(stringArray1, 1, null); 295 assertTrue(Arrays.equals((new String []{"a", null, "b", "c"}), newArray)); 296 assertEquals(String .class, newArray.getClass().getComponentType()); 297 newArray = ArrayUtils.add(stringArray1, 3, null); 298 assertTrue(Arrays.equals((new String []{"a", "b", "c", null}), newArray)); 299 assertEquals(String .class, newArray.getClass().getComponentType()); 300 newArray = ArrayUtils.add(stringArray1, 3, "d"); 301 assertTrue(Arrays.equals((new String []{"a", "b", "c", "d"}), newArray)); 302 assertEquals(String .class, newArray.getClass().getComponentType()); 303 assertEquals(String .class, newArray.getClass().getComponentType()); 304 305 Object [] o = new Object [] {"1", "2", "4"}; 306 Object [] result = ArrayUtils.add(o, 2, "3"); 307 Object [] result2 = ArrayUtils.add(o, 3, "5"); 308 309 assertNotNull(result); 310 assertEquals(4, result.length); 311 assertEquals("1", result[0]); 312 assertEquals("2", result[1]); 313 assertEquals("3", result[2]); 314 assertEquals("4", result[3]); 315 assertNotNull(result2); 316 assertEquals(4, result2.length); 317 assertEquals("1", result2[0]); 318 assertEquals("2", result2[1]); 319 assertEquals("4", result2[2]); 320 assertEquals("5", result2[3]); 321 322 boolean[] booleanArray = ArrayUtils.add( null, 0, true ); 324 assertTrue( Arrays.equals( new boolean[] { true }, booleanArray ) ); 325 booleanArray = ArrayUtils.add( new boolean[] { true }, 0, false); 326 assertTrue( Arrays.equals( new boolean[] { false, true }, booleanArray ) ); 327 booleanArray = ArrayUtils.add( new boolean[] { false }, 1, true); 328 assertTrue( Arrays.equals( new boolean[] { false, true }, booleanArray ) ); 329 booleanArray = ArrayUtils.add( new boolean[] { true, false }, 1, true); 330 assertTrue( Arrays.equals( new boolean[] { true, true, false }, booleanArray ) ); 331 332 char[] charArray = ArrayUtils.add( (char[]) null, 0, 'a' ); 334 assertTrue( Arrays.equals( new char[] { 'a' }, charArray ) ); 335 charArray = ArrayUtils.add( new char[] { 'a' }, 0, 'b'); 336 assertTrue( Arrays.equals( new char[] { 'b', 'a' }, charArray ) ); 337 charArray = ArrayUtils.add( new char[] { 'a', 'b' }, 0, 'c'); 338 assertTrue( Arrays.equals( new char[] { 'c', 'a', 'b' }, charArray ) ); 339 charArray = ArrayUtils.add( new char[] { 'a', 'b' }, 1, 'k'); 340 assertTrue( Arrays.equals( new char[] { 'a', 'k', 'b' }, charArray ) ); 341 charArray = ArrayUtils.add( new char[] { 'a', 'b', 'c' }, 1, 't'); 342 assertTrue( Arrays.equals( new char[] { 'a', 't', 'b', 'c' }, charArray ) ); 343 344 short[] shortArray = ArrayUtils.add( new short[] { 1 }, 0, (short) 2); 346 assertTrue( Arrays.equals( new short[] { 2, 1 }, shortArray ) ); 347 shortArray = ArrayUtils.add( new short[] { 2, 6 }, 2, (short) 10); 348 assertTrue( Arrays.equals( new short[] { 2, 6, 10 }, shortArray ) ); 349 shortArray = ArrayUtils.add( new short[] { 2, 6 }, 0, (short) -4); 350 assertTrue( Arrays.equals( new short[] { -4, 2, 6 }, shortArray ) ); 351 shortArray = ArrayUtils.add( new short[] { 2, 6, 3 }, 2, (short) 1); 352 assertTrue( Arrays.equals( new short[] { 2, 6, 1, 3 }, shortArray ) ); 353 354 byte[] byteArray = ArrayUtils.add( new byte[] { 1 }, 0, (byte) 2); 356 assertTrue( Arrays.equals( new byte[] { 2, 1 }, byteArray ) ); 357 byteArray = ArrayUtils.add( new byte[] { 2, 6 }, 2, (byte) 3); 358 assertTrue( Arrays.equals( new byte[] { 2, 6, 3 }, byteArray ) ); 359 byteArray = ArrayUtils.add( new byte[] { 2, 6 }, 0, (byte) 1); 360 assertTrue( Arrays.equals( new byte[] { 1, 2, 6 }, byteArray ) ); 361 byteArray = ArrayUtils.add( new byte[] { 2, 6, 3 }, 2, (byte) 1); 362 assertTrue( Arrays.equals( new byte[] { 2, 6, 1, 3 }, byteArray ) ); 363 364 int[] intArray = ArrayUtils.add( new int[] { 1 }, 0, 2); 366 assertTrue( Arrays.equals( new int[] { 2, 1 }, intArray ) ); 367 intArray = ArrayUtils.add( new int[] { 2, 6 }, 2, 10); 368 assertTrue( Arrays.equals( new int[] { 2, 6, 10 }, intArray ) ); 369 intArray = ArrayUtils.add( new int[] { 2, 6 }, 0, -4); 370 assertTrue( Arrays.equals( new int[] { -4, 2, 6 }, intArray ) ); 371 intArray = ArrayUtils.add( new int[] { 2, 6, 3 }, 2, 1); 372 assertTrue( Arrays.equals( new int[] { 2, 6, 1, 3 }, intArray ) ); 373 374 long[] longArray = ArrayUtils.add( new long[] { 1L }, 0, 2L); 376 assertTrue( Arrays.equals( new long[] { 2L, 1L }, longArray ) ); 377 longArray = ArrayUtils.add( new long[] { 2L, 6L }, 2, 10L); 378 assertTrue( Arrays.equals( new long[] { 2L, 6L, 10L }, longArray ) ); 379 longArray = ArrayUtils.add( new long[] { 2L, 6L }, 0, -4L); 380 assertTrue( Arrays.equals( new long[] { -4L, 2L, 6L }, longArray ) ); 381 longArray = ArrayUtils.add( new long[] { 2L, 6L, 3L }, 2, 1L); 382 assertTrue( Arrays.equals( new long[] { 2L, 6L, 1L, 3L }, longArray ) ); 383 384 float[] floatArray = ArrayUtils.add( new float[] { 1.1f }, 0, 2.2f); 386 assertTrue( Arrays.equals( new float[] { 2.2f, 1.1f }, floatArray ) ); 387 floatArray = ArrayUtils.add( new float[] { 2.3f, 6.4f }, 2, 10.5f); 388 assertTrue( Arrays.equals( new float[] { 2.3f, 6.4f, 10.5f }, floatArray ) ); 389 floatArray = ArrayUtils.add( new float[] { 2.6f, 6.7f }, 0, -4.8f); 390 assertTrue( Arrays.equals( new float[] { -4.8f, 2.6f, 6.7f }, floatArray ) ); 391 floatArray = ArrayUtils.add( new float[] { 2.9f, 6.0f, 0.3f }, 2, 1.0f); 392 assertTrue( Arrays.equals( new float[] { 2.9f, 6.0f, 1.0f, 0.3f }, floatArray ) ); 393 394 double[] doubleArray = ArrayUtils.add( new double[] { 1.1 }, 0, 2.2); 396 assertTrue( Arrays.equals( new double[] { 2.2, 1.1 }, doubleArray ) ); 397 doubleArray = ArrayUtils.add( new double[] { 2.3, 6.4 }, 2, 10.5); 398 assertTrue( Arrays.equals( new double[] { 2.3, 6.4, 10.5 }, doubleArray ) ); 399 doubleArray = ArrayUtils.add( new double[] { 2.6, 6.7 }, 0, -4.8); 400 assertTrue( Arrays.equals( new double[] { -4.8, 2.6, 6.7 }, doubleArray ) ); 401 doubleArray = ArrayUtils.add( new double[] { 2.9, 6.0, 0.3 }, 2, 1.0); 402 assertTrue( Arrays.equals( new double[] { 2.9, 6.0, 1.0, 0.3 }, doubleArray ) ); 403 } 404 405 } 406 | Popular Tags |