1 package org.apache.jcs.utils.struct;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
38 SingleLinkedList list = new SingleLinkedList();
39
40
41 Object result = list.takeFirst();
42
43
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
53 SingleLinkedList list = new SingleLinkedList();
54
55
56 int numToPut = 100;
57 for ( int i = 0; i < numToPut; i++ )
58 {
59 list.addLast( new Integer( i ) );
60 }
61
62
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
72 Object result = list.takeFirst();
73
74
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
84 SingleLinkedList list = new SingleLinkedList();
85
86
87 int numToPut = 100;
88 for ( int i = 0; i < numToPut; i++ )
89 {
90 list.addLast( new Integer( i ) );
91 }
92
93
94 assertEquals( "Wrong nubmer in list.", numToPut, list.size() );
95
96
97 list.clear();
98 Object result = list.takeFirst();
99
100
101 assertEquals( "Wrong nubmer in list.", 0, list.size() );
102 assertNull( "Shounldn't have anything left.", result );
103 }
104 }