IntegerDuplicateDeleterTest.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import org.junit.Assert;
  2. import org.junit.Before;
  3. import org.junit.Test;
  4. import java.util.Arrays;
  5. /**
  6. * Created by leon on 1/25/18.
  7. * @ATTENTION_TO_STUDENTS You are forbidden from modifying this class.
  8. */
  9. public class IntegerDuplicateDeleterTest {
  10. @Test
  11. public void testRemoveDuplicatesExactly_whenThereIsOnlyOneElement() {
  12. Integer[] intArray = new Integer[]{0};
  13. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  14. Integer[] expected = new Integer[]{0};
  15. Integer[] actual = deleter.removeDuplicatesExactly(2);
  16. Assert.assertArrayEquals(expected, actual);
  17. }
  18. @Test
  19. public void testRemoveDuplicatesExactly_whenThereIsOnlyOneElementAndRemoveDumplicationIs1() {
  20. Integer[] intArray = new Integer[]{9};
  21. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  22. Integer[] expected = new Integer[]{};
  23. Integer[] actual = deleter.removeDuplicatesExactly(1);
  24. Assert.assertArrayEquals(expected, actual);
  25. }
  26. @Test
  27. public void testRemoveDuplicatesExactly_withNoElementsWithExactDumplication() {
  28. Integer[] intArray = new Integer[]{51, 4, 4, 4, 28, 38, 38, 38, 38, 38};
  29. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  30. Integer[] expected = new Integer[]{51, 4, 4, 4, 28, 38, 38, 38, 38, 38};
  31. Integer[] actual = deleter.removeDuplicatesExactly(2);
  32. Assert.assertArrayEquals(expected, actual);
  33. }
  34. @Test
  35. public void testRemoveDuplicatesExactly_withMoreThanOneElement() {
  36. Integer[] intArray = new Integer[]{5, 5, 1, 2, 2, 3, 3, 3};
  37. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  38. Integer[] expected = new Integer[]{1, 3, 3, 3};
  39. Integer[] actual = deleter.removeDuplicatesExactly(2);
  40. Assert.assertArrayEquals(expected, actual);
  41. }
  42. @Test
  43. public void testRemoveDuplicatesExactly_withOccuranceAtTheBeginningAndEnd() {
  44. Integer[] intArray = new Integer[]{0, 0, 0, 1, 2, 2, 4, 4, 5, 5, 5, 6, 9, 9, 9};
  45. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  46. Integer[] expected = new Integer[]{1, 2, 2, 4, 4, 6};
  47. Integer[] actual = deleter.removeDuplicatesExactly(3);
  48. Assert.assertArrayEquals(expected, actual);
  49. }
  50. @Test
  51. public void testRemoveDuplicatesExactly_withOccuranceInTheMiddle() {
  52. Integer[] intArray = new Integer[]{0, 0, 0, 1, 2, 2, 4, 4, 5, 5, 5, 6, 9, 9, 9};
  53. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  54. Integer[] expected = new Integer[]{0, 0, 0, 1, 5, 5, 5, 6, 9, 9, 9};
  55. Integer[] actual = deleter.removeDuplicatesExactly(2);
  56. Assert.assertArrayEquals(expected, actual);
  57. }
  58. @Test
  59. public void testRemoveDuplicates_whenThereIsOnlyOneElement() {
  60. Integer[] intArray = new Integer[]{5};
  61. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  62. Integer[] expected = new Integer[]{5};
  63. Integer[] actual = deleter.removeDuplicates(2);
  64. Assert.assertArrayEquals(expected, actual);
  65. }
  66. @Test
  67. public void testRemoveDuplicates_whenThereIsOnlyOneElementAndRemoveDumplicationIs1() {
  68. Integer[] intArray = new Integer[]{3};
  69. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  70. Integer[] expected = new Integer[]{};
  71. Integer[] actual = deleter.removeDuplicatesExactly(1);
  72. Assert.assertArrayEquals(expected, actual);
  73. }
  74. @Test
  75. public void testRemoveDuplicates_whenThereIsMoreThanOneElement() {
  76. Integer[] intArray = new Integer[]{4, 1, 2};
  77. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  78. Integer[] expected = new Integer[]{4, 1, 2};
  79. Integer[] actual = deleter.removeDuplicates(2);
  80. Assert.assertArrayEquals(expected, actual);
  81. }
  82. @Test
  83. public void testRemoveDuplicates_whenThereIsMoreThanOneElementWithOccurance() {
  84. Integer[] intArray = new Integer[]{4, 4, 1, 2, 2, 2};
  85. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  86. Integer[] expected = new Integer[]{1};
  87. Integer[] actual = deleter.removeDuplicates(2);
  88. Assert.assertArrayEquals(expected, actual);
  89. }
  90. @Test
  91. public void testRemoveDuplicates_removesNothing() {
  92. Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
  93. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
  94. Integer[] expected = new Integer[]{};
  95. Integer[] actual = deleter.removeDuplicates(0);
  96. Assert.assertArrayEquals(expected, actual);
  97. }
  98. @Test
  99. public void testRemoveDuplicates_withOccuranceInTheMiddle() {
  100. Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
  101. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
  102. Integer[] expected = new Integer[]{2};
  103. Integer[] actual = deleter.removeDuplicates(2);
  104. Assert.assertArrayEquals(expected, actual);
  105. }
  106. @Test
  107. public void testRemoveDuplicates_withOccuranceAtTheBeginningAndEnd() {
  108. Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
  109. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
  110. Integer[] expected = new Integer[]{1, 1, 2, 4, 4};
  111. Integer[] actual = deleter.removeDuplicates(3);
  112. Assert.assertArrayEquals(expected, actual);
  113. }
  114. @Test
  115. public void testRemoveDuplicatesExactlyIdempotence() {
  116. Integer[] input = {11, 3, 3, 3, 4, 4, 4, 4};
  117. Integer[] expected = {11, 4, 4, 4, 4};
  118. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(input);
  119. Integer[] actual1 = deleter.removeDuplicatesExactly(3);
  120. Integer[] actual2 = deleter.removeDuplicatesExactly(3);
  121. Integer[] actual3 = deleter.removeDuplicatesExactly(3);
  122. Assert.assertArrayEquals(expected, actual1);
  123. Assert.assertArrayEquals(expected, actual2);
  124. Assert.assertArrayEquals(expected, actual3);
  125. }
  126. @Test
  127. public void testRemoveDuplicatesIdempotence() {
  128. Integer[] input = {1, 2, 2, 2, 3, 4, 4};
  129. Integer[] expected = {1, 3};
  130. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(input);
  131. Integer[] actual1 = deleter.removeDuplicates(2);
  132. Integer[] actual2 = deleter.removeDuplicates(2);
  133. Integer[] actual3 = deleter.removeDuplicates(2);
  134. Assert.assertArrayEquals(expected, actual1);
  135. Assert.assertArrayEquals(expected, actual2);
  136. Assert.assertArrayEquals(expected, actual3);
  137. }
  138. }