1 54 55 package junitx.framework; 56 57 import junit.framework.Assert; 58 59 import java.util.ArrayList ; 60 import java.util.Arrays ; 61 62 68 public class ArrayAssert { 69 70 73 private ArrayAssert() { 74 } 75 76 87 static public void assertEquals(String message, 88 Object [] expected, 89 Object [] actual) { 90 if (Arrays.equals(expected, actual)) { 91 return; 92 } 93 94 String formatted = ""; 95 if (message != null) { 96 formatted = message + " "; 97 } 98 99 Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected); 100 Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual); 101 Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length); 102 for (int i = 0; i < expected.length; i++) { 103 Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i]); 104 } 105 } 106 107 111 static public void assertEquals(Object [] expected, 112 Object [] actual) { 113 assertEquals(null, expected, actual); 114 } 115 116 128 public static void assertEquals(double[] expected, 129 double[] actual, 130 double delta) { 131 assertEquals(null, expected, actual, delta); 132 } 133 134 147 public static void assertEquals(String message, 148 double[] expected, 149 double[] actual, 150 double delta) { 151 if (Arrays.equals(expected, actual)) { 152 return; 153 } 154 155 String formatted = ""; 156 if (message != null) { 157 formatted = message + " "; 158 } 159 160 Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected); 161 Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual); 162 Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length); 163 for (int i = 0; i < actual.length; i++) { 164 Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i], delta); 165 } 166 } 167 168 181 public static void assertEquals(String message, 182 float[] expected, 183 float[] actual, 184 float delta) { 185 if (Arrays.equals(expected, actual)) { 186 return; 187 } 188 189 String formatted = ""; 190 if (message != null) { 191 formatted = message + " "; 192 } 193 194 Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected); 195 Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual); 196 Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length); 197 for (int i = 0; i < actual.length; i++) { 198 Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i], delta); 199 } 200 } 201 202 214 public static void assertEquals(float[] expected, 215 float[] actual, 216 float delta) { 217 assertEquals(null, expected, actual, delta); 218 } 219 220 232 public static void assertEquals(String message, 233 long[] expected, 234 long[] actual) { 235 if (Arrays.equals(expected, actual)) { 236 return; 237 } 238 239 String formatted = ""; 240 if (message != null) { 241 formatted = message + " "; 242 } 243 244 Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected); 245 Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual); 246 Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length); 247 for (int i = 0; i < actual.length; i++) { 248 Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i]); 249 } 250 } 251 252 263 public static void assertEquals(long[] expected, 264 long[] actual) { 265 assertEquals(null, expected, actual); 266 } 267 268 280 public static void assertEquals(String message, 281 int[] expected, 282 int[] actual) { 283 if (Arrays.equals(expected, actual)) { 284 return; 285 } 286 287 String formatted = ""; 288 if (message != null) { 289 formatted = message + " "; 290 } 291 292 Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected); 293 Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual); 294 Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length); 295 296 for (int i = 0; i < actual.length; i++) { 297 Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i]); 298 } 299 } 300 301 312 public static void assertEquals(int[] expected, 313 int[] actual) { 314 assertEquals(null, expected, actual); 315 } 316 317 329 public static void assertEquals(String message, 330 short[] expected, 331 short[] actual) { 332 if (Arrays.equals(expected, actual)) { 333 return; 334 } 335 336 String formatted = ""; 337 if (message != null) { 338 formatted = message + " "; 339 } 340 341 Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected); 342 Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual); 343 Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length); 344 345 for (int i = 0; i < actual.length; i++) { 346 Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i]); 347 } 348 } 349 350 361 public static void assertEquals(short[] expected, 362 short[] actual) { 363 assertEquals(null, expected, actual); 364 } 365 366 378 public static void assertEquals(String message, 379 char[] expected, 380 char[] actual) { 381 if (Arrays.equals(expected, actual)) { 382 return; 383 } 384 385 String formatted = ""; 386 if (message != null) { 387 formatted = message + " "; 388 } 389 390 Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected); 391 Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual); 392 Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length); 393 for (int i = 0; i < actual.length; i++) { 394 Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i]); 395 } 396 } 397 398 409 public static void assertEquals(char[] expected, 410 char[] actual) { 411 assertEquals(null, expected, actual); 412 } 413 414 426 public static void assertEquals(String message, 427 boolean[] expected, 428 boolean[] actual) { 429 if (Arrays.equals(expected, actual)) { 430 return; 431 } 432 433 String formatted = ""; 434 if (message != null) { 435 formatted = message + " "; 436 } 437 438 Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected); 439 Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual); 440 Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length); 441 for (int i = 0; i < actual.length; i++) { 442 Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i]); 443 } 444 } 445 446 457 public static void assertEquals(boolean[] expected, 458 boolean[] actual) { 459 assertEquals(null, expected, actual); 460 } 461 462 474 public static void assertEquals(String message, 475 byte[] expected, 476 byte[] actual) { 477 if (Arrays.equals(expected, actual)) { 478 return; 479 } 480 481 String formatted = ""; 482 if (message != null) { 483 formatted = message + " "; 484 } 485 486 Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected); 487 Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual); 488 Assert.assertEquals(formatted + "[array length] ", expected.length, actual.length); 489 for (int i = 0; i < actual.length; i++) { 490 Assert.assertEquals(formatted + "[position " + i + "]", expected[i], actual[i]); 491 } 492 } 493 494 505 public static void assertEquals(byte[] expected, 506 byte[] actual) { 507 assertEquals(null, expected, actual); 508 } 509 510 524 public static void assertEquivalenceArrays(String message, 525 Object [] expected, 526 Object [] actual) { 527 if (Arrays.equals(expected, actual)) { 528 return; 529 } 530 531 String formatted = ""; 532 if (message != null) { 533 formatted = message + " "; 534 } 535 536 Assert.assertNotNull(formatted + "expected array: <not null> but was <null>", expected); 537 Assert.assertNotNull(formatted + "expected array: <null> but was <not null>", actual); 538 539 ArrayList missing = new ArrayList (); 540 for (int i = 0; i < expected.length; i++) { 541 missing.add(expected[i]); 542 } 543 544 ArrayList extra = new ArrayList (); 545 for (int i = 0; i < actual.length; i++) { 546 extra.add(actual[i]); 547 } 548 549 ArrayList missingClone = (ArrayList ) missing.clone(); 550 missing.removeAll(extra); 551 extra.removeAll(missingClone); 552 553 Assert.assertTrue(formatted + "[Missing elements: " + missing + "]", missing.size() == 0); 554 Assert.assertTrue(formatted + "[Extra elements: " + extra + "]", extra.size() == 0); 555 } 556 557 570 public static void assertEquivalenceArrays(Object [] expected, 571 Object [] actual) { 572 assertEquivalenceArrays(null, expected, actual); 573 } 574 575 } 576 | Popular Tags |