|
| 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.Test; |
| 7 | + |
| 8 | +public class CircleLinkedListTest { |
| 9 | + |
| 10 | + @Test |
| 11 | + public void testAppendAndSize() { |
| 12 | + CircleLinkedList<Integer> list = new CircleLinkedList<>(); |
| 13 | + list.append(1); |
| 14 | + list.append(2); |
| 15 | + list.append(3); |
| 16 | + |
| 17 | + assertEquals(3, list.getSize()); |
| 18 | + assertEquals("[ 1, 2, 3 ]", list.toString()); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + public void testRemove() { |
| 23 | + CircleLinkedList<Integer> list = new CircleLinkedList<>(); |
| 24 | + list.append(1); |
| 25 | + list.append(2); |
| 26 | + list.append(3); |
| 27 | + list.append(4); |
| 28 | + |
| 29 | + assertEquals(2, list.remove(1)); |
| 30 | + assertEquals(3, list.remove(1)); |
| 31 | + assertEquals("[ 1, 4 ]", list.toString()); |
| 32 | + assertEquals(2, list.getSize()); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testRemoveInvalidIndex() { |
| 37 | + CircleLinkedList<Integer> list = new CircleLinkedList<>(); |
| 38 | + list.append(1); |
| 39 | + list.append(2); |
| 40 | + |
| 41 | + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(2)); |
| 42 | + assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testToStringEmpty() { |
| 47 | + CircleLinkedList<Integer> list = new CircleLinkedList<>(); |
| 48 | + assertEquals("[]", list.toString()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testToStringAfterRemoval() { |
| 53 | + CircleLinkedList<Integer> list = new CircleLinkedList<>(); |
| 54 | + list.append(1); |
| 55 | + list.append(2); |
| 56 | + list.append(3); |
| 57 | + list.remove(1); |
| 58 | + |
| 59 | + assertEquals("[ 1, 3 ]", list.toString()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testSingleElement() { |
| 64 | + CircleLinkedList<Integer> list = new CircleLinkedList<>(); |
| 65 | + list.append(1); |
| 66 | + |
| 67 | + assertEquals(1, list.getSize()); |
| 68 | + assertEquals("[ 1 ]", list.toString()); |
| 69 | + assertEquals(1, list.remove(0)); |
| 70 | + assertEquals("[]", list.toString()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testNullElement() { |
| 75 | + CircleLinkedList<String> list = new CircleLinkedList<>(); |
| 76 | + assertThrows(NullPointerException.class, () -> list.append(null)); |
| 77 | + } |
| 78 | +} |
0 commit comments