123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/28/18.
  7. * @ATTENTION_TO_STUDENTS You are forbidden from modifying this class.
  8. */
  9. public class StringDuplicateDeleterTest {
  10. @Test
  11. public void testRemoveDuplicatesExactly_whenThereIsOnlyOneElement() {
  12. String[] array = new String[]{"a"};
  13. DuplicateDeleter<String> deleter = new StringDuplicateDeleter(array);
  14. String[] expected = new String[]{"a"};
  15. String[] actual = deleter.removeDuplicatesExactly(2);
  16. Assert.assertArrayEquals(expected, actual);
  17. }
  18. @Test
  19. public void testRemoveDuplicatesExactly_whenThereIsOnlyOneElementAndRemoveDumplicationIs1() {
  20. String[] array = new String[]{"b"};
  21. DuplicateDeleter<String> deleter = new StringDuplicateDeleter(array);
  22. String[] expected = new String[]{};
  23. String[] actual = deleter.removeDuplicatesExactly(1);
  24. Assert.assertArrayEquals(expected, actual);
  25. }
  26. @Test
  27. public void testRemoveDuplicatesExactly_withNoElementsToRemove() {
  28. String[] array = new String[]{"aa", "b", "d"};
  29. DuplicateDeleter<String> deleter = new StringDuplicateDeleter(array);
  30. String[] expected = new String[]{"aa", "b", "d"};
  31. String[] actual = deleter.removeDuplicatesExactly(2);
  32. Assert.assertArrayEquals(expected, actual);
  33. }
  34. @Test
  35. public void testRemoveDuplicatesExactly_withMoreThanOneElementToRemove() {
  36. String[] array = new String[]{"cat", "cat", "dog", "dog", "fox"};
  37. DuplicateDeleter<String> deleter = new StringDuplicateDeleter(array);
  38. String[] expected = new String[]{"fox"};
  39. String[] actual = deleter.removeDuplicatesExactly(2);
  40. Assert.assertArrayEquals(expected, actual);
  41. }
  42. @Test
  43. public void testRemoveDuplicatesExactly_withOccuranceAtTheBeginningAndEnd() {
  44. String[] array = new String[]{"cat", "cat", "cat", "deer", "deer", "dog", "fox", "fox", "goat", "goat", "goat"};
  45. DuplicateDeleter<String> deleter = new StringDuplicateDeleter(array);
  46. String[] expected = new String[]{"deer", "deer", "dog", "fox", "fox"};
  47. String[] actual = deleter.removeDuplicatesExactly(3);
  48. Assert.assertArrayEquals(expected, actual);
  49. }
  50. @Test
  51. public void testRemoveDuplicatesExactly_withOccuranceInTheMiddle() {
  52. String[] array = new String[]{"cat", "cat", "cat", "deer", "deer", "dog", "fox", "fox", "goat", "goat", "goat"};
  53. DuplicateDeleter<String> deleter = new StringDuplicateDeleter(array);
  54. String[] expected = new String[]{"cat", "cat", "cat", "dog", "goat", "goat", "goat"};
  55. String[] actual = deleter.removeDuplicatesExactly(2);
  56. Assert.assertArrayEquals(expected, actual);
  57. }
  58. @Test
  59. public void testRemoveDuplicates_withOneElement() {
  60. String[] array = new String[]{"Droolius Caesar"};
  61. DuplicateDeleter<String> deleter = new StringDuplicateDeleter(array);
  62. String[] expected = new String[]{"Droolius Caesar"};
  63. String[] actual = deleter.removeDuplicates(2);
  64. Assert.assertArrayEquals(expected, actual);
  65. }
  66. @Test
  67. public void testRemoveDuplicates_withOneElementToRemove() {
  68. String[] array = new String[]{"Chewbarka"};
  69. DuplicateDeleter<String> deleter = new StringDuplicateDeleter(array);
  70. String[] expected = new String[]{};
  71. String[] actual = deleter.removeDuplicates(1);
  72. Assert.assertArrayEquals(expected, actual);
  73. }
  74. @Test
  75. public void testRemoveDuplicates_withNoElementToRemove() {
  76. String[] array = new String[]{"a", "a", "b", "c", "c", "c", "d"};
  77. DuplicateDeleter<String> deleter = new StringDuplicateDeleter(array);
  78. String[] expected = new String[]{"a", "a", "b", "c", "c", "c", "d"};
  79. String[] actual = deleter.removeDuplicates(4);
  80. Assert.assertArrayEquals(expected, actual);
  81. }
  82. @Test
  83. public void testRemoveDuplicates_whenThereIsMoreThanOneElementWithOccurance() {
  84. String[] array = new String[]{"a", "a", "b", "c", "c", "c", "d"};
  85. DuplicateDeleter<String> deleter = new StringDuplicateDeleter(array);
  86. String[] expected = new String[]{"b", "d"};
  87. String[] actual = deleter.removeDuplicates(2);
  88. Assert.assertArrayEquals(expected, actual);
  89. }
  90. @Test
  91. public void testRemoveDuplicates_withOccuranceInTheMiddle() {
  92. String[] array = new String[]{"aba", "baa", "baa", "bbb", "bbb", "bbb", "cat", "cat", "dog"};
  93. DuplicateDeleter<String> deleter = new StringDuplicateDeleter(array);
  94. String[] expected = new String[]{"aba", "dog"};
  95. String[] actual = deleter.removeDuplicates(2);
  96. Assert.assertArrayEquals(expected, actual);
  97. }
  98. @Test
  99. public void testRemoveDuplicates_withOccuranceAtTheBeginningAndEnd() {
  100. String[] array = new String[]{"aba", "aba", "aba", "bab", "aaa", "bba", "bba", "bbb", "bbb", "bbb"};
  101. DuplicateDeleter<String> deleter = new StringDuplicateDeleter(array);
  102. String[] expected = new String[]{"bab", "aaa", "bba", "bba"};
  103. String[] actual = deleter.removeDuplicates(3);
  104. Assert.assertArrayEquals(expected, actual);
  105. }
  106. @Test
  107. public void testRemoveDuplicatesExactlyIdempotence() {
  108. String[] input = {"aa", "b", "b", "b", "c", "c", "t", "t", "t"};
  109. String[] expected = {"aa", "c", "c"};
  110. DuplicateDeleter<String> deleter = new StringDuplicateDeleter(input);
  111. String[] actual1 = deleter.removeDuplicatesExactly(3);
  112. String[] actual2 = deleter.removeDuplicatesExactly(3);
  113. String[] actual3 = deleter.removeDuplicatesExactly(3);
  114. Assert.assertArrayEquals(expected, actual1);
  115. Assert.assertArrayEquals(expected, actual2);
  116. Assert.assertArrayEquals(expected, actual3);
  117. }
  118. @Test
  119. public void testRemoveDuplicatesIdempotence() {
  120. String[] input = {"aa", "b", "b", "b", "c", "c", "t", "t"};
  121. String[] expected = {"aa"};
  122. DuplicateDeleter<String> deleter = new StringDuplicateDeleter(input);
  123. String[] actual1 = deleter.removeDuplicates(2);
  124. String[] actual2 = deleter.removeDuplicates(2);
  125. String[] actual3 = deleter.removeDuplicates(2);
  126. Assert.assertArrayEquals(expected, actual1);
  127. Assert.assertArrayEquals(expected, actual2);
  128. Assert.assertArrayEquals(expected, actual3);
  129. }
  130. }