|
| 1 | +package com.baeldung.list; |
| 2 | + |
| 3 | +import static com.baeldung.list.RemoveAll.removeWithCallingRemoveUntilModifies; |
| 4 | +import static com.baeldung.list.RemoveAll.removeWithCollectingAndReturningRemainingElements; |
| 5 | +import static com.baeldung.list.RemoveAll.removeWithCollectingRemainingElementsAndAddingToOriginalList; |
| 6 | +import static com.baeldung.list.RemoveAll.removeWithForEachLoop; |
| 7 | +import static com.baeldung.list.RemoveAll.removeWithForLoopDecrementOnRemove; |
| 8 | +import static com.baeldung.list.RemoveAll.removeWithForLoopIncrementIfRemains; |
| 9 | +import static com.baeldung.list.RemoveAll.removeWithIterator; |
| 10 | +import static com.baeldung.list.RemoveAll.removeWithRemoveIf; |
| 11 | +import static com.baeldung.list.RemoveAll.removeWithStandardForLoopUsingIndex; |
| 12 | +import static com.baeldung.list.RemoveAll.removeWithStreamFilter; |
| 13 | +import static com.baeldung.list.RemoveAll.*; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.*; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.ConcurrentModificationException; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +public class RemoveAllUnitTest { |
| 25 | + |
| 26 | + private List<Integer> list(Integer... elements) { |
| 27 | + return new ArrayList<>(Arrays.asList(elements)); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void givenAList_whenRemovingElementsWithWhileLoopUsingPrimitiveElement_thenTheResultCorrect() { |
| 32 | + // given |
| 33 | + List<Integer> list = list(1, 2, 3); |
| 34 | + int valueToRemove = 1; |
| 35 | + |
| 36 | + // when |
| 37 | + assertThatThrownBy(() -> removeWithWhileLoopPrimitiveElement(list, valueToRemove)) |
| 38 | + .isInstanceOf(IndexOutOfBoundsException.class); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void givenAList_whenRemovingElementsWithWhileLoopUsingNonPrimitiveElement_thenTheResultCorrect() { |
| 43 | + // given |
| 44 | + List<Integer> list = list(1, 2, 3); |
| 45 | + int valueToRemove = 1; |
| 46 | + |
| 47 | + // when |
| 48 | + removeWithWhileLoopNonPrimitiveElement(list, valueToRemove); |
| 49 | + |
| 50 | + // then |
| 51 | + assertThat(list).isEqualTo(list(2, 3)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenAList_whenRemovingElementsWithWhileLoopStoringFirstOccurrenceIndex_thenTheResultCorrect() { |
| 56 | + // given |
| 57 | + List<Integer> list = list(1, 2, 3); |
| 58 | + int valueToRemove = 1; |
| 59 | + |
| 60 | + // when |
| 61 | + removeWithWhileLoopStoringFirstOccurrenceIndex(list, valueToRemove); |
| 62 | + |
| 63 | + // then |
| 64 | + assertThat(list).isEqualTo(list(2, 3)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void givenAList_whenRemovingElementsWithCallingRemoveUntilModifies_thenTheResultIsCorrect() { |
| 69 | + // given |
| 70 | + List<Integer> list = list(1, 1, 2, 3); |
| 71 | + int valueToRemove = 1; |
| 72 | + |
| 73 | + // when |
| 74 | + removeWithCallingRemoveUntilModifies(list, valueToRemove); |
| 75 | + |
| 76 | + // then |
| 77 | + assertThat(list).isEqualTo(list(2, 3)); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void givenAListWithoutDuplication_whenRemovingElementsWithStandardForLoopUsingIndex_thenTheResultIsCorrect() { |
| 82 | + // given |
| 83 | + List<Integer> list = list(1, 2, 3); |
| 84 | + int valueToRemove = 1; |
| 85 | + |
| 86 | + // when |
| 87 | + removeWithStandardForLoopUsingIndex(list, valueToRemove); |
| 88 | + |
| 89 | + // then |
| 90 | + assertThat(list).isEqualTo(list(2, 3)); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void givenAListWithAdjacentElements_whenRemovingElementsWithStandardForLoop_thenTheResultIsInCorrect() { |
| 95 | + // given |
| 96 | + List<Integer> list = list(1, 1, 2, 3); |
| 97 | + int valueToRemove = 1; |
| 98 | + |
| 99 | + // when |
| 100 | + removeWithStandardForLoopUsingIndex(list, valueToRemove); |
| 101 | + |
| 102 | + // then |
| 103 | + assertThat(list).isEqualTo(list(1, 2, 3)); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void givenAListWithAdjacentElements_whenRemovingElementsWithForLoopAndDecrementOnRemove_thenTheResultIsCorrect() { |
| 108 | + // given |
| 109 | + List<Integer> list = list(1, 1, 2, 3); |
| 110 | + int valueToRemove = 1; |
| 111 | + |
| 112 | + // when |
| 113 | + removeWithForLoopDecrementOnRemove(list, valueToRemove); |
| 114 | + |
| 115 | + // then |
| 116 | + assertThat(list).isEqualTo(list(2, 3)); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void givenAListWithAdjacentElements_whenRemovingElementsWithForLoopAndIncrementIfRemains_thenTheResultIsCorrect() { |
| 121 | + // given |
| 122 | + List<Integer> list = list(1, 1, 2, 3); |
| 123 | + int valueToRemove = 1; |
| 124 | + |
| 125 | + // when |
| 126 | + removeWithForLoopIncrementIfRemains(list, valueToRemove); |
| 127 | + |
| 128 | + // then |
| 129 | + assertThat(list).isEqualTo(list(2, 3)); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void givenAList_whenRemovingElementsWithForEachLoop_thenExceptionIsThrown() { |
| 134 | + // given |
| 135 | + List<Integer> list = list(1, 1, 2, 3); |
| 136 | + int valueToRemove = 1; |
| 137 | + |
| 138 | + // when |
| 139 | + assertThatThrownBy(() -> removeWithForEachLoop(list, valueToRemove)) |
| 140 | + .isInstanceOf(ConcurrentModificationException.class); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void givenAList_whenRemovingElementsWithIterator_thenTheResultIsCorrect() { |
| 145 | + // given |
| 146 | + List<Integer> list = list(1, 1, 2, 3); |
| 147 | + int valueToRemove = 1; |
| 148 | + |
| 149 | + // when |
| 150 | + removeWithIterator(list, valueToRemove); |
| 151 | + |
| 152 | + // then |
| 153 | + assertThat(list).isEqualTo(list(2, 3)); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void givenAList_whenRemovingElementsWithCollectingAndReturningRemainingElements_thenTheResultIsCorrect() { |
| 158 | + // given |
| 159 | + List<Integer> list = list(1, 1, 2, 3); |
| 160 | + int valueToRemove = 1; |
| 161 | + |
| 162 | + // when |
| 163 | + List<Integer> result = removeWithCollectingAndReturningRemainingElements(list, valueToRemove); |
| 164 | + |
| 165 | + // then |
| 166 | + assertThat(result).isEqualTo(list(2, 3)); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void givenAList_whenRemovingElementsWithCollectingRemainingAndAddingToOriginalList_thenTheResultIsCorrect() { |
| 171 | + // given |
| 172 | + List<Integer> list = list(1, 1, 2, 3); |
| 173 | + int valueToRemove = 1; |
| 174 | + |
| 175 | + // when |
| 176 | + removeWithCollectingRemainingElementsAndAddingToOriginalList(list, valueToRemove); |
| 177 | + |
| 178 | + // then |
| 179 | + assertThat(list).isEqualTo(list(2, 3)); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void givenAList_whenRemovingElementsWithStreamFilter_thenTheResultIsCorrect() { |
| 184 | + // given |
| 185 | + List<Integer> list = list(1, 1, 2, 3); |
| 186 | + int valueToRemove = 1; |
| 187 | + |
| 188 | + // when |
| 189 | + List<Integer> result = removeWithStreamFilter(list, valueToRemove); |
| 190 | + |
| 191 | + // then |
| 192 | + assertThat(result).isEqualTo(list(2, 3)); |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + public void givenAList_whenRemovingElementsWithCallingRemoveIf_thenTheResultIsCorrect() { |
| 197 | + // given |
| 198 | + List<Integer> list = list(1, 1, 2, 3); |
| 199 | + int valueToRemove = 1; |
| 200 | + |
| 201 | + // when |
| 202 | + removeWithRemoveIf(list, valueToRemove); |
| 203 | + |
| 204 | + // then |
| 205 | + assertThat(list).isEqualTo(list(2, 3)); |
| 206 | + } |
| 207 | + |
| 208 | +} |
0 commit comments