1   package org.apache.jcs.utils.struct;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  
24  /***
25   * Tests for the simple linked list.
26   * <p>
27   * @author Aaron Smuts
28   */
29  public class SingleLinkedListUnitTest
30      extends TestCase
31  {
32      /***
33       * Verify that we get a null and that there are no exceptions.
34       */
35      public void testTakeFromEmptyList()
36      {
37          // SETUP
38          SingleLinkedList list = new SingleLinkedList();
39  
40          // DO WORK
41          Object result = list.takeFirst();
42  
43          // VERIFY
44          assertNull( "Shounldn't have anything.", result );
45      }
46  
47      /***
48       * Verify FIFO behavior. Verifies that all items are removed.
49       */
50      public void testAddABunchAndTakeFromList()
51      {
52          // SETUP
53          SingleLinkedList list = new SingleLinkedList();
54  
55          // DO WORK
56          int numToPut = 100;
57          for ( int i = 0; i < numToPut; i++ )
58          {
59              list.addLast( new Integer( i ) );
60          }
61  
62          // VERIFY
63          assertEquals( "Wrong nubmer in list.", numToPut, list.size() );
64  
65          for ( int i = 0; i < numToPut; i++ )
66          {
67              Object result = list.takeFirst();
68              assertEquals( "Wrong value returned.", new Integer( i ), result );
69          }
70  
71          // DO WORK
72          Object result = list.takeFirst();
73  
74          // VERIFY
75          assertNull( "Shounldn't have anything left.", result );
76      }
77  
78      /***
79       * Verify that after calling clear all items are removed adn the size is 0.
80       */
81      public void testAddABunchAndClear()
82      {
83          // SETUP
84          SingleLinkedList list = new SingleLinkedList();
85  
86          // DO WORK
87          int numToPut = 100;
88          for ( int i = 0; i < numToPut; i++ )
89          {
90              list.addLast( new Integer( i ) );
91          }
92  
93          // VERIFY
94          assertEquals( "Wrong nubmer in list.", numToPut, list.size() );
95  
96          // DO WORK
97          list.clear();
98          Object result = list.takeFirst();
99  
100         // VERIFY
101         assertEquals( "Wrong nubmer in list.", 0, list.size() );
102         assertNull( "Shounldn't have anything left.", result );
103     }
104 }