|
@@ -2,7 +2,8 @@ import org.junit.Assert;
|
2
|
2
|
import org.junit.Before;
|
3
|
3
|
import org.junit.Test;
|
4
|
4
|
|
5
|
|
-import static org.junit.Assert.*;
|
|
5
|
+import java.util.ArrayList;
|
|
6
|
+import java.util.List;
|
6
|
7
|
|
7
|
8
|
public class ArrazTest {
|
8
|
9
|
|
|
@@ -277,16 +278,20 @@ public class ArrazTest {
|
277
|
278
|
|
278
|
279
|
@Test
|
279
|
280
|
public void findMax() {
|
280
|
|
- int expected = 5;
|
281
|
|
- int actual = arraz.findMax(intArray);
|
|
281
|
+ int[] spiffyHandyIntArray = new int[]{4, 5, 102, 6, -7, 12, -32, 92, 8};
|
|
282
|
+
|
|
283
|
+ int expected = 102;
|
|
284
|
+ int actual = arraz.findMax(spiffyHandyIntArray);
|
282
|
285
|
|
283
|
286
|
Assert.assertEquals(expected, actual);
|
284
|
287
|
}
|
285
|
288
|
|
286
|
289
|
@Test
|
287
|
290
|
public void findMin() {
|
288
|
|
- int expected = 1;
|
289
|
|
- int actual = arraz.findMin(intArray);
|
|
291
|
+ int[] spiffyHandyIntArray = new int[]{4, 5, 102, 6, -7, 12, -32, 92, 8};
|
|
292
|
+
|
|
293
|
+ int expected = -32;
|
|
294
|
+ int actual = arraz.findMin(spiffyHandyIntArray);
|
290
|
295
|
|
291
|
296
|
Assert.assertEquals(expected, actual);
|
292
|
297
|
}
|
|
@@ -313,45 +318,361 @@ public class ArrazTest {
|
313
|
318
|
|
314
|
319
|
////////// ////////// //////////
|
315
|
320
|
|
|
321
|
+
|
316
|
322
|
@Test
|
317
|
323
|
public void removeDupesFromArray1() {
|
|
324
|
+ Integer[] original = new Integer[]{2, 2, 1, 3, 4, 5, 5, 6, 2, 1, 8};
|
318
|
325
|
|
|
326
|
+ Integer[] expected = new Integer[]{2, 1, 3, 4, 5, 6, 8};
|
|
327
|
+ Integer[] actual = arraz.removeDupesFromArray(original);
|
319
|
328
|
|
320
|
|
-
|
|
329
|
+ Assert.assertEquals(expected, actual);
|
321
|
330
|
}
|
322
|
331
|
|
323
|
332
|
@Test
|
324
|
333
|
public void removeDupesFromArray2() {
|
|
334
|
+ Integer[] original = new Integer[]{10, 6, 20, 22, 28, 5, 999, 5};
|
|
335
|
+
|
|
336
|
+ Integer[] expected = new Integer[]{10, 6, 20, 22, 28, 5, 999};
|
|
337
|
+ Integer[] actual = arraz.removeDupesFromArray(original);
|
|
338
|
+
|
|
339
|
+ Assert.assertEquals(expected, actual);
|
325
|
340
|
}
|
326
|
341
|
|
327
|
342
|
////////// ////////// //////////
|
328
|
343
|
|
329
|
344
|
@Test
|
330
|
345
|
public void find2ndLargestValueFromArray1() {
|
|
346
|
+ Double[] original = new Double[]{2.4, 3.0, 6.5, 7.1, 2.8, 1.1, 0.5};
|
|
347
|
+
|
|
348
|
+ Double expected = 6.5;
|
|
349
|
+ Double actual = arraz.find2ndLargestValueFromArray(original);
|
|
350
|
+
|
|
351
|
+ Assert.assertEquals(expected, actual, 1e-15);
|
331
|
352
|
}
|
332
|
353
|
|
333
|
354
|
|
334
|
355
|
@Test
|
335
|
356
|
public void find2ndLargestValueFromArray2() {
|
|
357
|
+ Double[] original = new Double[]{1.5, 1.3, 1.4, 1.2, 1.1};
|
|
358
|
+
|
|
359
|
+ Double expected = 1.4;
|
|
360
|
+ Double actual = arraz.find2ndLargestValueFromArray(original);
|
|
361
|
+
|
|
362
|
+ Assert.assertEquals(expected, actual, 1e-15);
|
336
|
363
|
}
|
337
|
364
|
|
338
|
365
|
////////// ////////// //////////
|
339
|
366
|
|
340
|
367
|
@Test
|
341
|
368
|
public void makeMeAnArrayListFromArray1() {
|
|
369
|
+ Integer[] original = new Integer[]{1, 2, 3};
|
|
370
|
+
|
|
371
|
+ ArrayList<Integer> expected = new ArrayList<>();
|
|
372
|
+ expected.add(1);
|
|
373
|
+ expected.add(2);
|
|
374
|
+ expected.add(3);
|
|
375
|
+
|
|
376
|
+ ArrayList<Integer> actual = arraz.makeMeAnArrayListFromArray(original);
|
|
377
|
+
|
|
378
|
+ Assert.assertEquals(expected, actual);
|
342
|
379
|
}
|
343
|
380
|
|
344
|
381
|
@Test
|
345
|
382
|
public void makeMeAnArrayListFromArray2() {
|
|
383
|
+ String[] original = new String[]{"I", "am", "very", "hungry", "!"};
|
|
384
|
+
|
|
385
|
+ ArrayList<String> expected = new ArrayList<>();
|
|
386
|
+ expected.add("I");
|
|
387
|
+ expected.add("am");
|
|
388
|
+ expected.add("very");
|
|
389
|
+ expected.add("hungry");
|
|
390
|
+ expected.add("!");
|
|
391
|
+
|
|
392
|
+ ArrayList<String> actual = arraz.makeMeAnArrayListFromArray(original);
|
|
393
|
+
|
|
394
|
+ Assert.assertEquals(expected, actual);
|
346
|
395
|
}
|
347
|
396
|
|
348
|
397
|
////////// ////////// //////////
|
349
|
398
|
|
350
|
399
|
@Test
|
351
|
400
|
public void makeMeAnArrayFromArrayList1() {
|
|
401
|
+ ArrayList<Integer> original = new ArrayList<>();
|
|
402
|
+ original.add(6);
|
|
403
|
+ original.add(20);
|
|
404
|
+ original.add(92);
|
|
405
|
+
|
|
406
|
+ Integer[] expected = new Integer[]{6, 20, 92};
|
|
407
|
+ Integer[] actual = (Integer[]) arraz.makeMeAnArrayFromArrayList(original, new Integer[0]);
|
|
408
|
+
|
|
409
|
+ Assert.assertEquals(expected, actual);
|
352
|
410
|
}
|
353
|
411
|
|
354
|
412
|
@Test
|
355
|
413
|
public void makeMeAnArrayFromArrayList2() {
|
|
414
|
+ ArrayList<String> original = new ArrayList<>();
|
|
415
|
+ original.add("hello");
|
|
416
|
+ original.add("goodbye");
|
|
417
|
+
|
|
418
|
+ String[] expected = new String[]{"hello", "goodbye"};
|
|
419
|
+ String[] actual = (String[]) arraz.makeMeAnArrayFromArrayList(original, new String[0]);
|
|
420
|
+
|
|
421
|
+ Assert.assertEquals(expected, actual);
|
|
422
|
+ }
|
|
423
|
+
|
|
424
|
+ ////////// ////////// //////////
|
|
425
|
+
|
|
426
|
+ @Test
|
|
427
|
+ public void check2ArraysForEquals1() {
|
|
428
|
+ String[] string1 = "I am starting to hate arrays".split(" ");
|
|
429
|
+ String[] string2 = new String[]{"I", "am", "starting", "to", "hate", "arrays"};
|
|
430
|
+
|
|
431
|
+ Assert.assertTrue(arraz.check2ArraysForEquals(string1, string2));
|
|
432
|
+ }
|
|
433
|
+
|
|
434
|
+ @Test
|
|
435
|
+ public void check2ArraysForEquals2() {
|
|
436
|
+ Double[] double1 = new Double[]{1.0, 0.5, 3.6, 38.4, 17.3, 62.0, 9.0, 3.375, 0.0, 3.14159};
|
|
437
|
+ Double[] double2 = new Double[]{1.0, 0.5, 3.6, 38.4, 17.3, 62.1, 9.0, 3.375, 0.0, 3.14159};
|
|
438
|
+
|
|
439
|
+ Assert.assertFalse(arraz.check2ArraysForEquals(double1, double2));
|
|
440
|
+ }
|
|
441
|
+
|
|
442
|
+ ////////// ////////// //////////
|
|
443
|
+
|
|
444
|
+ @Test
|
|
445
|
+ public void averageArrayWithoutMaxMin1() {
|
|
446
|
+ String expected = "3.00";
|
|
447
|
+ String actual = String.format("%.2f", arraz.averageArrayWithoutMaxMin(intArray));
|
|
448
|
+
|
|
449
|
+ Assert.assertEquals(expected, actual);
|
|
450
|
+ }
|
|
451
|
+
|
|
452
|
+ @Test
|
|
453
|
+ public void averageArrayWithoutMaxMin2() {
|
|
454
|
+ int[] original = new int[]{9, 9, 2, 5, -18, 5, 2, 35, 291, 291, 291, -18};
|
|
455
|
+
|
|
456
|
+ String expected = "9.57";
|
|
457
|
+ String actual = String.format("%.2f", arraz.averageArrayWithoutMaxMin(original));
|
|
458
|
+
|
|
459
|
+ Assert.assertEquals(expected, actual);
|
|
460
|
+ }
|
|
461
|
+
|
|
462
|
+ ////////// ////////// //////////
|
|
463
|
+
|
|
464
|
+ @Test
|
|
465
|
+ public void arrayHas65And77_1() {
|
|
466
|
+ int[] original = new int[]{1, 1, 2, 65, 64, 98, 78};
|
|
467
|
+
|
|
468
|
+ Assert.assertFalse(arraz.arrayHas65And77(original));
|
|
469
|
+ }
|
|
470
|
+
|
|
471
|
+ @Test
|
|
472
|
+ public void arrayHas65And77_2() {
|
|
473
|
+ int[] original = new int[]{1, 1, 2, 65, 64, 98, 77};
|
|
474
|
+
|
|
475
|
+ Assert.assertTrue(arraz.arrayHas65And77(original));
|
|
476
|
+ }
|
|
477
|
+
|
|
478
|
+ ////////// ////////// //////////
|
|
479
|
+
|
|
480
|
+ @Test
|
|
481
|
+ public void theTotalofTensIs30_1() {
|
|
482
|
+ int[] original = new int[]{10, 1, 2, 65, 10, 64, 10, 98, 77};
|
|
483
|
+
|
|
484
|
+ Assert.assertTrue(arraz.theTotalofTensIs30(original));
|
|
485
|
+ }
|
|
486
|
+
|
|
487
|
+ @Test
|
|
488
|
+ public void theTotalofTensIs30_2() {
|
|
489
|
+ int[] original = new int[]{10, 1, 2, 10, 65, 10, 64, 10, 98, 77};
|
|
490
|
+
|
|
491
|
+ Assert.assertFalse(arraz.theTotalofTensIs30(original));
|
|
492
|
+ }
|
|
493
|
+
|
|
494
|
+ ////////// ////////// //////////
|
|
495
|
+
|
|
496
|
+ @Test
|
|
497
|
+ public void findSmallestOfArray1() {
|
|
498
|
+ int expected = 1;
|
|
499
|
+ int actual = arraz.findSmallestOfArray(intArray);
|
|
500
|
+
|
|
501
|
+ Assert.assertEquals(expected, actual);
|
|
502
|
+ }
|
|
503
|
+
|
|
504
|
+ @Test
|
|
505
|
+ public void findSmallestOfArray2() {
|
|
506
|
+ int[] spiffyHandyIntArray = new int[]{4, 5, 102, 6, -7, 12, -32, 92, 8};
|
|
507
|
+
|
|
508
|
+ int expected = -32;
|
|
509
|
+ int actual = arraz.findSmallestOfArray(spiffyHandyIntArray);
|
|
510
|
+
|
|
511
|
+ Assert.assertEquals(expected, actual);
|
|
512
|
+ }
|
|
513
|
+
|
|
514
|
+ ////////// ////////// //////////
|
|
515
|
+
|
|
516
|
+ @Test
|
|
517
|
+ public void findSecondSmallestOfArray1() {
|
|
518
|
+ int expected = 2;
|
|
519
|
+ int actual = arraz.findSecondSmallestOfArray(intArray);
|
|
520
|
+
|
|
521
|
+ Assert.assertEquals(expected, actual);
|
|
522
|
+ }
|
|
523
|
+
|
|
524
|
+ @Test
|
|
525
|
+ public void findSecondSmallestOfArray2() {
|
|
526
|
+ int[] spiffyHandyIntArray = new int[]{4, 5, 102, 6, -7, 12, -32, 92, 8};
|
|
527
|
+
|
|
528
|
+ int expected = -7;
|
|
529
|
+ int actual = arraz.findSecondSmallestOfArray(spiffyHandyIntArray);
|
|
530
|
+
|
|
531
|
+ Assert.assertEquals(expected, actual);
|
|
532
|
+ }
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+ ////////// ////////// //////////
|
|
536
|
+
|
|
537
|
+ @Test
|
|
538
|
+ public void makeMeACopyPlease1() {
|
|
539
|
+ int[] expected = new int[]{5, 4, 3, 2, 1};
|
|
540
|
+ int[] actual = arraz.makeMeACopyPlease(intArray);
|
|
541
|
+
|
|
542
|
+ for (int i = 0; i < expected.length; i++) {
|
|
543
|
+ Assert.assertEquals(expected[i], actual[i]);
|
|
544
|
+ }
|
|
545
|
+ }
|
|
546
|
+
|
|
547
|
+ @Test
|
|
548
|
+ public void makeMeACopyPlease2() {
|
|
549
|
+ int[] spiffyHandyIntArray = new int[]{4, 5, 102, 6, -7, 12, -32, 92, 8};
|
|
550
|
+
|
|
551
|
+ int[] expected = new int[]{8, 92, -32, 12, -7, 6, 102, 5, 4};
|
|
552
|
+ int[] actual = arraz.makeMeACopyPlease(spiffyHandyIntArray);
|
|
553
|
+
|
|
554
|
+ for (int i = 0; i < expected.length; i++) {
|
|
555
|
+ Assert.assertEquals(expected[i], actual[i]);
|
|
556
|
+ }
|
|
557
|
+ }
|
|
558
|
+
|
|
559
|
+ ////////// ////////// //////////
|
|
560
|
+
|
|
561
|
+ @Test
|
|
562
|
+ public void removeLastItemAndCopy1() {
|
|
563
|
+ Integer[] original = new Integer[]{10, 6, 20, 22, 28, 5, 999, 5};
|
|
564
|
+
|
|
565
|
+ Integer[] expected = new Integer[]{10, 6, 20, 22, 28, 5, 999};
|
|
566
|
+ Integer[] actual = (Integer[]) arraz.removeLastItemAndCopy(original);
|
|
567
|
+
|
|
568
|
+ Assert.assertEquals(expected, actual);
|
|
569
|
+ }
|
|
570
|
+
|
|
571
|
+ @Test
|
|
572
|
+ public void removeLastItemAndCopy2() {
|
|
573
|
+ String[] original = new String[]{"My", "name", "is", "Jenn", "Chao"};
|
|
574
|
+
|
|
575
|
+ String[] expected = new String[]{"My", "name", "is", "Jenn"};
|
|
576
|
+ String[] actual = (String[]) arraz.removeLastItemAndCopy(original);
|
|
577
|
+
|
|
578
|
+ Assert.assertEquals(expected, actual);
|
|
579
|
+ }
|
|
580
|
+
|
|
581
|
+ ////////// ////////// //////////
|
|
582
|
+
|
|
583
|
+ @Test
|
|
584
|
+ public void removeFirstItemAndCopy1() {
|
|
585
|
+ Integer[] original = new Integer[]{10, 6, 20, 22, 28, 5, 999, 5};
|
|
586
|
+
|
|
587
|
+ Integer[] expected = new Integer[]{6, 20, 22, 28, 5, 999, 5};
|
|
588
|
+ Integer[] actual = (Integer[]) arraz.removeFirstItemAndCopy(original);
|
|
589
|
+
|
|
590
|
+ Assert.assertEquals(expected, actual);
|
|
591
|
+ }
|
|
592
|
+
|
|
593
|
+ @Test
|
|
594
|
+ public void removeFirstItemAndCopy2() {
|
|
595
|
+ String[] original = new String[]{"My", "name", "is", "Jenn", "Chao"};
|
|
596
|
+
|
|
597
|
+ String[] expected = new String[]{"name", "is", "Jenn", "Chao"};
|
|
598
|
+ String[] actual = (String[]) arraz.removeFirstItemAndCopy(original);
|
|
599
|
+
|
|
600
|
+ Assert.assertEquals(expected, actual);
|
|
601
|
+ }
|
|
602
|
+
|
|
603
|
+ ////////// ////////// //////////
|
|
604
|
+
|
|
605
|
+ @Test
|
|
606
|
+ public void insertAtStartAndCopy1() {
|
|
607
|
+ Integer[] original = new Integer[]{1, 2, 3, 4, 5};
|
|
608
|
+
|
|
609
|
+ Integer[] expected = new Integer[]{99, 1, 2, 3, 4, 5};
|
|
610
|
+ Integer[] actual = (Integer[]) arraz.insertAtStartAndCopy(original, 99);
|
|
611
|
+
|
|
612
|
+ Assert.assertEquals(expected, actual);
|
|
613
|
+ }
|
|
614
|
+
|
|
615
|
+ @Test
|
|
616
|
+ public void insertAtStartAndCopy2() {
|
|
617
|
+ String[] original = new String[]{"I", "want", "fruit", "snacks"};
|
|
618
|
+
|
|
619
|
+ String[] expected = new String[]{"Seriously", "I", "want", "fruit", "snacks"};
|
|
620
|
+ String[] actual = (String[]) arraz.insertAtStartAndCopy(original, "Seriously");
|
|
621
|
+
|
|
622
|
+ Assert.assertEquals(expected, actual);
|
|
623
|
+
|
|
624
|
+ }
|
|
625
|
+
|
|
626
|
+ ////////// ////////// //////////
|
|
627
|
+
|
|
628
|
+ @Test
|
|
629
|
+ public void insertAtEndAndCopy1() {
|
|
630
|
+ Integer[] original = new Integer[]{1, 2, 3, 4, 5};
|
|
631
|
+
|
|
632
|
+ Integer[] expected = new Integer[]{1, 2, 3, 4, 5, 99};
|
|
633
|
+ Integer[] actual = (Integer[]) arraz.insertAtEndAndCopy(original, 99);
|
|
634
|
+
|
|
635
|
+ Assert.assertEquals(expected, actual);
|
|
636
|
+
|
|
637
|
+ }
|
|
638
|
+
|
|
639
|
+ @Test
|
|
640
|
+ public void insertAtEndAndCopy2() {
|
|
641
|
+ String[] original = new String[]{"I", "want", "fruit", "snacks"};
|
|
642
|
+
|
|
643
|
+ String[] expected = new String[]{"I", "want", "fruit", "snacks", "!"};
|
|
644
|
+ String[] actual = (String[]) arraz.insertAtEndAndCopy(original, "!");
|
|
645
|
+
|
|
646
|
+ Assert.assertEquals(expected, actual);
|
|
647
|
+ }
|
|
648
|
+
|
|
649
|
+ ////////// ////////// //////////
|
|
650
|
+
|
|
651
|
+ @Test
|
|
652
|
+ public void sortArrayIntoEvensThenOdds1() {
|
|
653
|
+ int[] spiffyHandyIntArray = new int[] {4, 5, 102, 6, -7, 12, -32, 92, 8};
|
|
654
|
+
|
|
655
|
+ List<Integer> evens = new ArrayList<>();
|
|
656
|
+ evens.add(4); evens.add(102); evens.add(6); evens.add(12); evens.add(-32); evens.add(92); evens.add(8);
|
|
657
|
+ List<Integer> odds = new ArrayList<>();
|
|
658
|
+ odds.add(5); odds.add(-7);
|
|
659
|
+
|
|
660
|
+ List[] expected = new List[] {evens, odds};
|
|
661
|
+ List[] actual = arraz.sortArrayIntoEvensThenOdds(spiffyHandyIntArray);
|
|
662
|
+
|
|
663
|
+ Assert.assertEquals(expected, actual);
|
|
664
|
+ }
|
|
665
|
+
|
|
666
|
+ @Test
|
|
667
|
+ public void sortArrayIntoEvensThenOdds2() {
|
|
668
|
+ List<Integer> evens = new ArrayList<>();
|
|
669
|
+ evens.add(2); evens.add(4);
|
|
670
|
+ List<Integer> odds = new ArrayList<>();
|
|
671
|
+ odds.add(1); odds.add(3); odds.add(5);
|
|
672
|
+
|
|
673
|
+ List[] expected = new List[] {evens, odds};
|
|
674
|
+ List[] actual = arraz.sortArrayIntoEvensThenOdds(intArray);
|
|
675
|
+
|
|
676
|
+ Assert.assertEquals(expected, actual);
|
356
|
677
|
}
|
357
|
678
|
}
|