|
| 1 | +package com.thealgorithms.datastructures.lists; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +public class CircularDoublyLinkedListTest { |
| 10 | + |
| 11 | + private CircularDoublyLinkedList<Integer> list; |
| 12 | + |
| 13 | + @BeforeEach |
| 14 | + public void setUp() { |
| 15 | + list = new CircularDoublyLinkedList<>(); |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + public void testInitialSize() { |
| 20 | + assertEquals(0, list.getSize(), "Initial size should be 0."); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testAppendAndSize() { |
| 25 | + list.append(10); |
| 26 | + list.append(20); |
| 27 | + list.append(30); |
| 28 | + |
| 29 | + assertEquals(3, list.getSize(), "Size after appends should be 3."); |
| 30 | + assertEquals("[ 10, 20, 30 ]", list.toString(), "List content should match appended values."); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testRemove() { |
| 35 | + list.append(10); |
| 36 | + list.append(20); |
| 37 | + list.append(30); |
| 38 | + |
| 39 | + int removed = list.remove(1); |
| 40 | + assertEquals(20, removed, "Removed element at index 1 should be 20."); |
| 41 | + |
| 42 | + assertEquals("[ 10, 30 ]", list.toString(), "List content should reflect removal."); |
| 43 | + assertEquals(2, list.getSize(), "Size after removal should be 2."); |
| 44 | + |
| 45 | + removed = list.remove(0); |
| 46 | + assertEquals(10, removed, "Removed element at index 0 should be 10."); |
| 47 | + assertEquals("[ 30 ]", list.toString(), "List content should reflect second removal."); |
| 48 | + assertEquals(1, list.getSize(), "Size after second removal should be 1."); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testRemoveInvalidIndex() { |
| 53 | + list.append(10); |
| 54 | + list.append(20); |
| 55 | + |
| 56 | + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(2), "Removing at invalid index 2 should throw exception."); |
| 57 | + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1), "Removing at negative index should throw exception."); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testToStringEmpty() { |
| 62 | + assertEquals("[]", list.toString(), "Empty list should display as []."); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testSingleElement() { |
| 67 | + list.append(10); |
| 68 | + |
| 69 | + assertEquals(1, list.getSize(), "Size after adding single element should be 1."); |
| 70 | + assertEquals("[ 10 ]", list.toString(), "Single element list string should be formatted correctly."); |
| 71 | + int removed = list.remove(0); |
| 72 | + assertEquals(10, removed, "Removed element should be the one appended."); |
| 73 | + assertEquals("[]", list.toString(), "List should be empty after removing last element."); |
| 74 | + assertEquals(0, list.getSize(), "Size after removing last element should be 0."); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testNullAppend() { |
| 79 | + assertThrows(NullPointerException.class, () -> list.append(null), "Appending null should throw NullPointerException."); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testRemoveLastPosition() { |
| 84 | + list.append(10); |
| 85 | + list.append(20); |
| 86 | + list.append(30); |
| 87 | + int removed = list.remove(list.getSize() - 1); |
| 88 | + assertEquals(30, removed, "Last element removed should be 30."); |
| 89 | + assertEquals(2, list.getSize(), "Size should decrease after removing last element."); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testRemoveFromEmptyThrows() { |
| 94 | + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(0), "Remove from empty list should throw."); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testRepeatedAppendAndRemove() { |
| 99 | + for (int i = 0; i < 100; i++) { |
| 100 | + list.append(i); |
| 101 | + } |
| 102 | + assertEquals(100, list.getSize()); |
| 103 | + |
| 104 | + for (int i = 99; i >= 0; i--) { |
| 105 | + int removed = list.remove(i); |
| 106 | + assertEquals(i, removed, "Removed element should match appended value."); |
| 107 | + } |
| 108 | + assertEquals(0, list.getSize(), "List should be empty after all removes."); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testToStringAfterMultipleRemoves() { |
| 113 | + list.append(1); |
| 114 | + list.append(2); |
| 115 | + list.append(3); |
| 116 | + list.remove(2); |
| 117 | + list.remove(0); |
| 118 | + assertEquals("[ 2 ]", list.toString(), "ToString should correctly represent remaining elements."); |
| 119 | + } |
| 120 | +} |
0 commit comments