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      public AbstractLinkedListTest(final String testName) {
32          super(testName);
33      }
34  
35      protected void checkNodes() {
36          final AbstractLinkedList<E> list = getCollection();
37          for (int i = 0; i < list.size; i++) {
38              assertEquals(list.getNode(i, false).next, list.getNode(i + 1, true));
39              if (i < list.size - 1) {
40                  assertEquals(list.getNode(i + 1, false).previous,
41                      list.getNode(i, false));
42              }
43          }
44      }
45  
46      /**
47       * {@inheritDoc}
48       */
49      @Override
50      public AbstractLinkedList<E> getCollection() {
51          return (AbstractLinkedList<E>) super.getCollection();
52      }
53  
54      @Test
55      @SuppressWarnings("unchecked")
56      public void testAddNodeAfter() {
57          resetEmpty();
58          final AbstractLinkedList<E> list = getCollection();
59          if (!isAddSupported()) {
60              try {
61                  list.addFirst(null);
62              } catch (final UnsupportedOperationException ex) {}
63          }
64  
65          list.addFirst((E) "value1");
66          list.addNodeAfter(list.getNode(0, false), (E) "value2");
67          assertEquals("value1", list.getFirst());
68          assertEquals("value2", list.getLast());
69          list.removeFirst();
70          checkNodes();
71          list.addNodeAfter(list.getNode(0, false), (E) "value3");
72          checkNodes();
73          assertEquals("value2", list.getFirst());
74          assertEquals("value3", list.getLast());
75          list.addNodeAfter(list.getNode(0, false), (E) "value4");
76          checkNodes();
77          assertEquals("value2", list.getFirst());
78          assertEquals("value3", list.getLast());
79          assertEquals("value4", list.get(1));
80          list.addNodeAfter(list.getNode(2, false), (E) "value5");
81          checkNodes();
82          assertEquals("value2", list.getFirst());
83          assertEquals("value4", list.get(1));
84          assertEquals("value3", list.get(2));
85          assertEquals("value5", list.getLast());
86      }
87  
88      @Test
89      @SuppressWarnings("unchecked")
90      public void testGetNode() {
91          resetEmpty();
92          final AbstractLinkedList<E> list = getCollection();
93          // get marker
94          assertEquals(list.getNode(0, true).previous, list.getNode(0, true).next);
95          assertThrows(IndexOutOfBoundsException.class, () -> list.getNode(0, false),
96                  "Expecting IndexOutOfBoundsException.");
97          list.addAll( Arrays.asList((E[]) new String[]{"value1", "value2"}));
98          checkNodes();
99          list.addFirst((E) "value0");
100         checkNodes();
101         list.removeNode(list.getNode(1, false));
102         checkNodes();
103         assertThrows(IndexOutOfBoundsException.class, () -> list.getNode(2, false),
104                 "Expecting IndexOutOfBoundsException.");
105         assertThrows(IndexOutOfBoundsException.class, () -> list.getNode(-1, false),
106                 "Expecting IndexOutOfBoundsException.");
107         assertThrows(IndexOutOfBoundsException.class, () -> list.getNode(3, true),
108                 "Expecting IndexOutOfBoundsException.");
109     }
110 
111     @Test
112     @SuppressWarnings("unchecked")
113     public void testRemoveFirst() {
114         resetEmpty();
115         final AbstractLinkedList<E> list = getCollection();
116         if (!isRemoveSupported()) {
117             try {
118                 list.removeFirst();
119             } catch (final UnsupportedOperationException ex) {}
120         }
121 
122         list.addAll(Arrays.asList((E[]) new String[] { "value1", "value2" }));
123         assertEquals("value1", list.removeFirst());
124         checkNodes();
125         list.addLast((E) "value3");
126         checkNodes();
127         assertEquals("value2", list.removeFirst());
128         assertEquals("value3", list.removeFirst());
129         checkNodes();
130         list.addLast((E) "value4");
131         checkNodes();
132         assertEquals("value4", list.removeFirst());
133         checkNodes();
134     }
135 
136     @Test
137     @SuppressWarnings("unchecked")
138     public void testRemoveLast() {
139         resetEmpty();
140         final AbstractLinkedList<E> list = getCollection();
141         if (!isRemoveSupported()) {
142             try {
143                 list.removeLast();
144             } catch (final UnsupportedOperationException ex) {}
145         }
146 
147         list.addAll(Arrays.asList((E[]) new String[] { "value1", "value2" }));
148         assertEquals("value2", list.removeLast());
149         list.addFirst((E) "value3");
150         checkNodes();
151         assertEquals("value1", list.removeLast());
152         assertEquals("value3", list.removeLast());
153         list.addFirst((E) "value4");
154         checkNodes();
155         assertEquals("value4", list.removeFirst());
156     }
157 
158     @Test
159     @SuppressWarnings("unchecked")
160     public void testRemoveNode() {
161         resetEmpty();
162         if (!isAddSupported() || !isRemoveSupported()) {
163             return;
164         }
165         final AbstractLinkedList<E> list = getCollection();
166 
167         list.addAll(Arrays.asList((E[]) new String[] { "value1", "value2" }));
168         list.removeNode(list.getNode(0, false));
169         checkNodes();
170         assertEquals("value2", list.getFirst());
171         assertEquals("value2", list.getLast());
172         list.addFirst((E) "value1");
173         list.addFirst((E) "value0");
174         checkNodes();
175         list.removeNode(list.getNode(1, false));
176         assertEquals("value0", list.getFirst());
177         assertEquals("value2", list.getLast());
178         checkNodes();
179         list.removeNode(list.getNode(1, false));
180         assertEquals("value0", list.getFirst());
181         assertEquals("value0", list.getLast());
182         checkNodes();
183     }
184 
185 }