View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4.list;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import java.util.Arrays;
23  
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Test case for {@link AbstractLinkedList}.
28   */
29  public abstract class AbstractLinkedListTest<E> extends AbstractListTest<E> {
30  
31      protected void checkNodes() {
32          final AbstractLinkedList<E> list = getCollection();
33          for (int i = 0; i < list.size; i++) {
34              assertEquals(list.getNode(i, false).next, list.getNode(i + 1, true));
35              if (i < list.size - 1) {
36                  assertEquals(list.getNode(i + 1, false).previous, list.getNode(i, false));
37              }
38          }
39      }
40  
41      /**
42       * {@inheritDoc}
43       */
44      @Override
45      public AbstractLinkedList<E> getCollection() {
46          return (AbstractLinkedList<E>) super.getCollection();
47      }
48  
49      @Test
50      @SuppressWarnings("unchecked")
51      public void testAddNodeAfter() {
52          resetEmpty();
53          final AbstractLinkedList<E> list = getCollection();
54          if (!isAddSupported()) {
55              assertThrows(UnsupportedOperationException.class, () -> list.addFirst(null));
56          }
57          list.addFirst((E) "value1");
58          list.addNodeAfter(list.getNode(0, false), (E) "value2");
59          assertEquals("value1", list.getFirst());
60          assertEquals("value2", list.getLast());
61          list.removeFirst();
62          checkNodes();
63          list.addNodeAfter(list.getNode(0, false), (E) "value3");
64          checkNodes();
65          assertEquals("value2", list.getFirst());
66          assertEquals("value3", list.getLast());
67          list.addNodeAfter(list.getNode(0, false), (E) "value4");
68          checkNodes();
69          assertEquals("value2", list.getFirst());
70          assertEquals("value3", list.getLast());
71          assertEquals("value4", list.get(1));
72          list.addNodeAfter(list.getNode(2, false), (E) "value5");
73          checkNodes();
74          assertEquals("value2", list.getFirst());
75          assertEquals("value4", list.get(1));
76          assertEquals("value3", list.get(2));
77          assertEquals("value5", list.getLast());
78      }
79  
80      @Test
81      @SuppressWarnings("unchecked")
82      public void testGetNode() {
83          resetEmpty();
84          final AbstractLinkedList<E> list = getCollection();
85          // get marker
86          assertEquals(list.getNode(0, true).previous, list.getNode(0, true).next);
87          assertThrows(IndexOutOfBoundsException.class, () -> list.getNode(0, false), "Expecting IndexOutOfBoundsException.");
88          list.addAll(Arrays.asList((E[]) new String[] { "value1", "value2" }));
89          checkNodes();
90          list.addFirst((E) "value0");
91          checkNodes();
92          list.removeNode(list.getNode(1, false));
93          checkNodes();
94          assertThrows(IndexOutOfBoundsException.class, () -> list.getNode(2, false), "Expecting IndexOutOfBoundsException.");
95          assertThrows(IndexOutOfBoundsException.class, () -> list.getNode(-1, false), "Expecting IndexOutOfBoundsException.");
96          assertThrows(IndexOutOfBoundsException.class, () -> list.getNode(3, true), "Expecting IndexOutOfBoundsException.");
97      }
98  
99      @Test
100     @SuppressWarnings("unchecked")
101     public void testRemoveFirst() {
102         resetEmpty();
103         final AbstractLinkedList<E> list = getCollection();
104         if (!isRemoveSupported()) {
105             assertThrows(UnsupportedOperationException.class, list::removeFirst);
106         }
107 
108         list.addAll(Arrays.asList((E[]) new String[] { "value1", "value2" }));
109         assertEquals("value1", list.removeFirst());
110         checkNodes();
111         list.addLast((E) "value3");
112         checkNodes();
113         assertEquals("value2", list.removeFirst());
114         assertEquals("value3", list.removeFirst());
115         checkNodes();
116         list.addLast((E) "value4");
117         checkNodes();
118         assertEquals("value4", list.removeFirst());
119         checkNodes();
120     }
121 
122     @Test
123     @SuppressWarnings("unchecked")
124     public void testRemoveLast() {
125         resetEmpty();
126         final AbstractLinkedList<E> list = getCollection();
127         if (!isRemoveSupported()) {
128             assertThrows(UnsupportedOperationException.class, list::removeLast);
129         }
130 
131         list.addAll(Arrays.asList((E[]) new String[] { "value1", "value2" }));
132         assertEquals("value2", list.removeLast());
133         list.addFirst((E) "value3");
134         checkNodes();
135         assertEquals("value1", list.removeLast());
136         assertEquals("value3", list.removeLast());
137         list.addFirst((E) "value4");
138         checkNodes();
139         assertEquals("value4", list.removeFirst());
140     }
141 
142     @Test
143     @SuppressWarnings("unchecked")
144     public void testRemoveNode() {
145         resetEmpty();
146         if (!isAddSupported() || !isRemoveSupported()) {
147             return;
148         }
149         final AbstractLinkedList<E> list = getCollection();
150 
151         list.addAll(Arrays.asList((E[]) new String[] { "value1", "value2" }));
152         list.removeNode(list.getNode(0, false));
153         checkNodes();
154         assertEquals("value2", list.getFirst());
155         assertEquals("value2", list.getLast());
156         list.addFirst((E) "value1");
157         list.addFirst((E) "value0");
158         checkNodes();
159         list.removeNode(list.getNode(1, false));
160         assertEquals("value0", list.getFirst());
161         assertEquals("value2", list.getLast());
162         checkNodes();
163         list.removeNode(list.getNode(1, false));
164         assertEquals("value0", list.getFirst());
165         assertEquals("value0", list.getLast());
166         checkNodes();
167     }
168 
169 }