From 4b6c1c6e05c3b0357b6cf64143fb23bd707569e3 Mon Sep 17 00:00:00 2001 From: Wynn Teo <49014791+wynnteo@users.noreply.github.com> Date: Sat, 23 Dec 2023 10:27:39 +0800 Subject: [PATCH] First version of Iterator vs For Loop unit testing (#15365) * First version of Iterator vs For Loop unit testing * Add a new line after package * Update the method name --- .../IteratorForLoopUnitTest.java | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 core-java-modules/core-java-collections-5/src/test/java/com/baeldung/iteratorvsforloop/IteratorForLoopUnitTest.java diff --git a/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/iteratorvsforloop/IteratorForLoopUnitTest.java b/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/iteratorvsforloop/IteratorForLoopUnitTest.java new file mode 100644 index 0000000000..a7655c68eb --- /dev/null +++ b/core-java-modules/core-java-collections-5/src/test/java/com/baeldung/iteratorvsforloop/IteratorForLoopUnitTest.java @@ -0,0 +1,129 @@ +package com.baeldung.iteratorvsforloop; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.ConcurrentModificationException; +import java.util.List; +import java.util.Iterator; +import java.util.Collections; +import java.util.ListIterator; + +public class IteratorForLoopUnitTest { + + @Test + public void givenEmptyCollection_whenUsingForLoop_thenNoElementsAreIterated() { + List names = Collections.emptyList(); + StringBuilder stringBuilder = new StringBuilder(); + + for (int i = 0; i < names.size(); i++) { + stringBuilder.append(names.get(i)); + } + + assertEquals("", stringBuilder.toString()); + } + + @Test + public void givenEmptyCollection_whenUsingIterator_thenNoElementsAreIterated() { + List names = Collections.emptyList(); + StringBuilder stringBuilder = new StringBuilder(); + + Iterator iterator = names.iterator(); + while (iterator.hasNext()) { + stringBuilder.append(iterator.next()); + } + + assertEquals("", stringBuilder.toString()); + } + + + @Test + public void givenCollectionWithElements_whenUsingForLoop_thenAllElementsAreIterated() { + List names = Arrays.asList("Alice", "Bob", "Charlie"); + StringBuilder stringBuilder = new StringBuilder(); + + for (int i = 0; i < names.size(); i++) { + stringBuilder.append(names.get(i)); + } + + assertEquals("AliceBobCharlie", stringBuilder.toString()); + } + + @Test + public void givenCollectionWithElements_whenUsingIterator_thenAllElementsAreIterated() { + List names = Arrays.asList("Alice", "Bob", "Charlie"); + StringBuilder stringBuilder = new StringBuilder(); + + Iterator iterator = names.iterator(); + while (iterator.hasNext()) { + stringBuilder.append(iterator.next()); + } + + assertEquals("AliceBobCharlie", stringBuilder.toString()); + } + + @Test + public void givenCollectionWithElements_whenUsingForLoop_thenAllElementsAreIteratedReverseOrder() { + List names = Arrays.asList("Alice", "Bob", "Charlie"); + StringBuilder stringBuilder = new StringBuilder(); + + for (int i = names.size() - 1; i >= 0; i--) { + stringBuilder.append(names.get(i)); + } + + assertEquals("CharlieBobAlice", stringBuilder.toString()); + } + + @Test + public void givenCollectionWithElements_whenUsingListIterator_thenAllElementsAreIteratedInReverseOrder() { + List names = Arrays.asList("Alice", "Bob", "Charlie"); + StringBuilder stringBuilder = new StringBuilder(); + + ListIterator listIterator = names.listIterator(names.size()); + while (listIterator.hasPrevious()) { + stringBuilder.append(listIterator.previous()); + } + + assertEquals("CharlieBobAlice", stringBuilder.toString()); + } + + @Test + public void givenCollectionWithElements_whenRemovingElementDuringForLoopIteration_thenConcurrentModificationExceptionIsThrown() { + List names = new ArrayList<>(List.of("Alice", "Bob", "Charlie")); + + assertThrows(ConcurrentModificationException.class, () -> { + for (String name : names) { + names.remove("Bob"); + } + }); + } + + @Test + public void givenCollectionWithElements_whenRemovingElementUsingIterator_thenElementIsRemovedSafely() { + List names = new ArrayList<>(Arrays.asList("Alice", "Bob", "Charlie")); + Iterator iterator = names.iterator(); + + while (iterator.hasNext()) { + String name = iterator.next(); + if (name.equals("Bob")) { + iterator.remove(); + } + } + List expected = Arrays.asList("Alice", "Charlie"); + assertIterableEquals(expected, names); + } + + @Test + public void givenCollectionWithElements_whenModifyingElementToLowerCaseDuringForLoopIteration_thenElementsAreModifiedToLowerCase() { + List names = new ArrayList<>(List.of("Alice", "Bob", "Charlie")); + + for (int i = 0; i < names.size(); i++) { + names.set(i, names.get(i).toLowerCase()); + } + + List expected = Arrays.asList("alice","bob", "charlie"); + assertIterableEquals(expected, names); + } +}