IntegerDuplicateDeleterTest.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 testIsNotDuplicatedXNumberOfTimes(){
  28. Integer[] intArray = new Integer[]{9};
  29. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  30. Integer[] expected = new Integer[]{};
  31. }
  32. @Test
  33. public void testRemoveDuplicatesExactly_withNoElementsWithExactDumplication() {
  34. Integer[] intArray = new Integer[]{51, 4, 4, 4, 28, 38, 38, 38, 38, 38};
  35. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  36. Integer[] expected = new Integer[]{51, 4, 4, 4, 28, 38, 38, 38, 38, 38};
  37. Integer[] actual = deleter.removeDuplicatesExactly(2);
  38. Assert.assertArrayEquals(expected, actual);
  39. }
  40. @Test
  41. public void testRemoveDuplicatesExactly_withMoreThanOneElement() {
  42. Integer[] intArray = new Integer[]{5, 5, 1, 2, 2, 3, 3, 3};
  43. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  44. Integer[] expected = new Integer[]{1, 3, 3, 3};
  45. Integer[] actual = deleter.removeDuplicatesExactly(2);
  46. Assert.assertArrayEquals(expected, actual);
  47. }
  48. @Test
  49. public void testRemoveDuplicatesExactly_withOccuranceAtTheBeginningAndEnd() {
  50. Integer[] intArray = new Integer[]{0, 0, 0, 1, 2, 2, 4, 4, 5, 5, 5, 6, 9, 9, 9};
  51. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  52. Integer[] expected = new Integer[]{1, 2, 2, 4, 4, 6};
  53. Integer[] actual = deleter.removeDuplicatesExactly(3);
  54. Assert.assertArrayEquals(expected, actual);
  55. }
  56. @Test
  57. public void testRemoveDuplicatesExactly_withOccuranceInTheMiddle() {
  58. Integer[] intArray = new Integer[]{0, 0, 0, 1, 2, 2, 4, 4, 5, 5, 5, 6, 9, 9, 9};
  59. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  60. Integer[] expected = new Integer[]{0, 0, 0, 1, 5, 5, 5, 6, 9, 9, 9};
  61. Integer[] actual = deleter.removeDuplicatesExactly(2);
  62. Assert.assertArrayEquals(expected, actual);
  63. }
  64. @Test
  65. public void testRemoveDuplicates_whenThereIsOnlyOneElement() {
  66. Integer[] intArray = new Integer[]{5};
  67. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  68. Integer[] expected = new Integer[]{5};
  69. Integer[] actual = deleter.removeDuplicates(2);
  70. Assert.assertArrayEquals(expected, actual);
  71. }
  72. @Test
  73. public void testRemoveDuplicates_whenThereIsOnlyOneElementAndRemoveDumplicationIs1() {
  74. Integer[] intArray = new Integer[]{3};
  75. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  76. Integer[] expected = new Integer[]{};
  77. Integer[] actual = deleter.removeDuplicates(1);
  78. Assert.assertArrayEquals(expected, actual);
  79. }
  80. @Test
  81. public void testRemoveDuplicates_whenThereIsMoreThanOneElement() {
  82. Integer[] intArray = new Integer[]{4, 1, 2};
  83. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  84. Integer[] expected = new Integer[]{4, 1, 2};
  85. Integer[] actual = deleter.removeDuplicates(2);
  86. Assert.assertArrayEquals(expected, actual);
  87. }
  88. @Test
  89. public void testRemoveDuplicates_whenThereIsMoreThanOneElementWithOccurance() {
  90. Integer[] intArray = new Integer[]{4, 4, 1, 2, 2, 2};
  91. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(intArray);
  92. Integer[] expected = new Integer[]{1};
  93. Integer[] actual = deleter.removeDuplicates(2);
  94. Assert.assertArrayEquals(expected, actual);
  95. }
  96. @Test
  97. public void testRemoveDuplicates_removesNothing() {
  98. Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
  99. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
  100. Integer[] expected = new Integer[]{};
  101. Integer[] actual = deleter.removeDuplicates(0);
  102. Assert.assertArrayEquals(expected, actual);
  103. }
  104. @Test
  105. public void testRemoveDuplicates_withOccuranceInTheMiddle() {
  106. Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
  107. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
  108. Integer[] expected = new Integer[]{2};
  109. Integer[] actual = deleter.removeDuplicates(2);
  110. Assert.assertArrayEquals(expected, actual);
  111. }
  112. @Test
  113. public void testRemoveDuplicates_withOccuranceAtTheBeginningAndEnd() {
  114. Integer[] array = new Integer[]{0, 0, 0, 1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5};
  115. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(array);
  116. Integer[] expected = new Integer[]{1, 1, 2, 4, 4};
  117. Integer[] actual = deleter.removeDuplicates(3);
  118. Assert.assertArrayEquals(expected, actual);
  119. }
  120. @Test
  121. public void testRemoveDuplicatesExactlyIdempotence() {
  122. Integer[] input = {11, 3, 3, 3, 4, 4, 4, 4};
  123. Integer[] expected = {11, 4, 4, 4, 4};
  124. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(input);
  125. Integer[] actual1 = deleter.removeDuplicatesExactly(3);
  126. Integer[] actual2 = deleter.removeDuplicatesExactly(3);
  127. Integer[] actual3 = deleter.removeDuplicatesExactly(3);
  128. Assert.assertArrayEquals(expected, actual1);
  129. Assert.assertArrayEquals(expected, actual2);
  130. Assert.assertArrayEquals(expected, actual3);
  131. }
  132. @Test
  133. public void testRemoveDuplicatesIdempotence() {
  134. Integer[] input = {1, 2, 2, 2, 3, 4, 4};
  135. Integer[] expected = {1, 3};
  136. DuplicateDeleter<Integer> deleter = new IntegerDuplicateDeleter(input);
  137. Integer[] actual1 = deleter.removeDuplicates(2);
  138. Integer[] actual2 = deleter.removeDuplicates(2);
  139. Integer[] actual3 = deleter.removeDuplicates(2);
  140. Assert.assertArrayEquals(expected, actual1);
  141. Assert.assertArrayEquals(expected, actual2);
  142. Assert.assertArrayEquals(expected, actual3);
  143. }
  144. }