View Javadoc
1   package org.apache.commons.jcs3.utils.struct;
2   
3   
4   
5   /*
6    * Licensed to the Apache Software Foundation (ASF) under one
7    * or more contributor license agreements.  See the NOTICE file
8    * distributed with this work for additional information
9    * regarding copyright ownership.  The ASF licenses this file
10   * to you under the Apache License, Version 2.0 (the
11   * "License"); you may not use this file except in compliance
12   * with the License.  You may obtain a copy of the License at
13   *
14   *   http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing,
17   * software distributed under the License is distributed on an
18   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   * KIND, either express or implied.  See the License for the
20   * specific language governing permissions and limitations
21   * under the License.
22   */
23  
24  import junit.framework.TestCase;
25  
26  /** Unit tests for the double linked list. */
27  public class DoubleLinkedListUnitTest
28      extends TestCase
29  {
30      /** verify that the last is added when the list is empty. */
31      public void testAddLast_Empty()
32      {
33          // SETUP
34          final DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<>();
35  
36          final String payload1 = "payload1";
37          final DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<>( payload1 );
38  
39          // WO WORK
40          list.addLast( node1 );
41  
42          // VERIFY
43          assertEquals( "Wrong last", node1, list.getLast() );
44      }
45  
46      /** verify that the last is added when the list is empty. */
47      public void testAddLast_NotEmpty()
48      {
49          // SETUP
50          final DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<>();
51  
52          final String payload1 = "payload1";
53          final DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<>( payload1 );
54  
55          final String payload2 = "payload2";
56          final DoubleLinkedListNode<String> node2 = new DoubleLinkedListNode<>( payload2 );
57  
58          // WO WORK
59          list.addLast( node1 );
60          list.addLast( node2 );
61  
62          // VERIFY
63          assertEquals( "Wrong last", node2, list.getLast() );
64      }
65  
66      /** verify that it's added last. */
67      public void testMakeLast_wasFirst()
68      {
69          // SETUP
70          final DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<>();
71  
72          final String payload1 = "payload1";
73          final DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<>( payload1 );
74  
75          final String payload2 = "payload2";
76          final DoubleLinkedListNode<String> node2 = new DoubleLinkedListNode<>( payload2 );
77  
78          list.addFirst( node2 );
79          list.addFirst(  node1 );
80  
81          // DO WORK
82          list.makeLast( node1 );
83  
84          // VERIFY
85          assertEquals( "Wrong size", 2, list.size() );
86          assertEquals( "Wrong last", node1, list.getLast() );
87          assertEquals( "Wrong first", node2, list.getFirst() );
88      }
89  
90      /** verify that it's added last. */
91      public void testMakeLast_wasLast()
92      {
93          // SETUP
94          final DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<>();
95  
96          final String payload1 = "payload1";
97          final DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<>( payload1 );
98  
99          final String payload2 = "payload2";
100         final DoubleLinkedListNode<String> node2 = new DoubleLinkedListNode<>( payload2 );
101 
102         list.addFirst( node1 );
103         list.addFirst(  node2 );
104 
105         // DO WORK
106         list.makeLast( node1 );
107 
108         // VERIFY
109         assertEquals( "Wrong size", 2, list.size() );
110         assertEquals( "Wrong last", node1, list.getLast() );
111         assertEquals( "Wrong first", node2, list.getFirst() );
112     }
113 
114     /** verify that it's added last. */
115     public void testMakeLast_wasAlone()
116     {
117         // SETUP
118         final DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<>();
119 
120         final String payload1 = "payload1";
121         final DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<>( payload1 );
122 
123         list.addFirst( node1 );
124 
125         // DO WORK
126         list.makeLast( node1 );
127 
128         // VERIFY
129         assertEquals( "Wrong size", 1, list.size() );
130         assertEquals( "Wrong last", node1, list.getLast() );
131         assertEquals( "Wrong first", node1, list.getFirst() );
132     }
133 
134     /** verify that it's added last. */
135     public void testMakeLast_wasInMiddle()
136     {
137         // SETUP
138         final DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<>();
139 
140         final String payload1 = "payload1";
141         final DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<>( payload1 );
142 
143         final String payload2 = "payload2";
144         final DoubleLinkedListNode<String> node2 = new DoubleLinkedListNode<>( payload2 );
145 
146         final String payload3 = "payload3";
147         final DoubleLinkedListNode<String> node3 = new DoubleLinkedListNode<>( payload3 );
148 
149         list.addFirst( node2 );
150         list.addFirst(  node1 );
151         list.addFirst(  node3 );
152 
153         // DO WORK
154         list.makeLast( node1 );
155 
156         // VERIFY
157         assertEquals( "Wrong size", 3, list.size() );
158         assertEquals( "Wrong last", node1, list.getLast() );
159         assertEquals( "Wrong first", node3, list.getFirst() );
160     }
161 }