|
| 1 | +package com.thealgorithms.datastructures.lists; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +public class CircularDoublyLinkedListTest { |
| 9 | + |
| 10 | + private CircularDoublyLinkedList<Integer> list; |
| 11 | + |
| 12 | + @BeforeEach |
| 13 | + public void setUp() { |
| 14 | + list = new CircularDoublyLinkedList<>(); |
| 15 | + } |
| 16 | + |
| 17 | + @Test |
| 18 | + public void testInitialSize() { |
| 19 | + assertEquals(0, list.getSize(), "Initial size should be 0."); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testAppendAndSize() { |
| 24 | + list.append(10); |
| 25 | + list.append(20); |
| 26 | + list.append(30); |
| 27 | + |
| 28 | + assertEquals(3, list.getSize(), "Size after appends should be 3."); |
| 29 | + assertEquals("[ 10, 20, 30 ]", list.toString(), "List content should match appended values."); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testRemove() { |
| 34 | + list.append(10); |
| 35 | + list.append(20); |
| 36 | + list.append(30); |
| 37 | + |
| 38 | + int removed = list.remove(1); |
| 39 | + assertEquals(20, removed, "Removed element at index 1 should be 20."); |
| 40 | + |
| 41 | + assertEquals("[ 10, 30 ]", list.toString(), "List content should reflect removal."); |
| 42 | + assertEquals(2, list.getSize(), "Size after removal should be 2."); |
| 43 | + |
| 44 | + removed = list.remove(0); |
| 45 | + assertEquals(10, removed, "Removed element at index 0 should be 10."); |
| 46 | + assertEquals("[ 30 ]", list.toString(), "List content should reflect second removal."); |
| 47 | + assertEquals(1, list.getSize(), "Size after second removal should be 1."); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testRemoveInvalidIndex() { |
| 52 | + list.append(10); |
| 53 | + list.append(20); |
| 54 | + |
| 55 | + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(2), |
| 56 | + "Removing at invalid index 2 should throw exception."); |
| 57 | + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1), |
| 58 | + "Removing at negative index should throw exception."); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testToStringEmpty() { |
| 63 | + assertEquals("[]", list.toString(), "Empty list should display as []."); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testSingleElement() { |
| 68 | + list.append(10); |
| 69 | + |
| 70 | + assertEquals(1, list.getSize(), "Size after adding single element should be 1."); |
| 71 | + assertEquals("[ 10 ]", list.toString(), "Single element list string should be formatted correctly."); |
| 72 | + int removed = list.remove(0); |
| 73 | + assertEquals(10, removed, "Removed element should be the one appended."); |
| 74 | + assertEquals("[]", list.toString(), "List should be empty after removing last element."); |
| 75 | + assertEquals(0, list.getSize(), "Size after removing last element should be 0."); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testNullAppend() { |
| 80 | + assertThrows(NullPointerException.class, () -> list.append(null), |
| 81 | + "Appending null should throw NullPointerException."); |
| 82 | + } |
| 83 | +} |
0 commit comments