KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tests > sosnoski > util > queue > ObjectQueueTest


1 /*
2  * Copyright (c) 2000-2001 Sosnoski Software Solutions, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */

22
23 package tests.sosnoski.util.queue;
24
25 import com.sosnoski.util.queue.ObjectQueue;
26
27 import junit.framework.*;
28
29 public class ObjectQueueTest extends TestCase
30 {
31     private static final int TEST_ITEMS = 10;
32     
33     private ObjectQueue m_queue;
34     
35     public ObjectQueueTest(String JavaDoc name) {
36         super(name);
37     }
38     
39     public void setUp() {
40         m_queue = new ObjectQueue();
41     }
42     
43     public void tearDown() {
44         m_queue = null;
45     }
46     
47     private Object JavaDoc gen(int index) {
48         return new Integer JavaDoc(index);
49     }
50     
51     private boolean ifMatch(Object JavaDoc a, int b) {
52         return gen(b).equals(a);
53     }
54     
55     private void fillSequential(int count) {
56         for (int i = 0; i < count; i++) {
57             m_queue.add(gen(i));
58         }
59     }
60     
61     public void testAdd() {
62         fillSequential(TEST_ITEMS);
63         assert(m_queue.size() == TEST_ITEMS);
64         fillSequential(TEST_ITEMS);
65         assert(m_queue.size() == TEST_ITEMS*2);
66     }
67     
68     public void testClear() {
69         fillSequential(TEST_ITEMS);
70         m_queue.clear();
71         assert(m_queue.size() == 0);
72         assert(m_queue.isEmpty());
73     }
74     
75     public void testRemove() {
76         fillSequential(TEST_ITEMS);
77         assert(m_queue.size() == TEST_ITEMS);
78         for (int i = 0; i < TEST_ITEMS; i++) {
79             assert(ifMatch(m_queue.remove(), i));
80         }
81         assert(m_queue.size() == 0);
82         fillSequential(TEST_ITEMS);
83         for (int i = 0; i < TEST_ITEMS; i++) {
84             assert(ifMatch(m_queue.remove(), i));
85         }
86     }
87     
88     public void testDiscard() {
89         fillSequential(TEST_ITEMS);
90         m_queue.discard(TEST_ITEMS/2);
91         assert(m_queue.size() == TEST_ITEMS/2);
92         for (int i = TEST_ITEMS/2; i < TEST_ITEMS; i++) {
93             assert(ifMatch(m_queue.remove(), i));
94         }
95     }
96     
97     public void testToArray() {
98         fillSequential(TEST_ITEMS);
99         Object JavaDoc[] array = m_queue.toArray();
100         assert(array.length == TEST_ITEMS);
101         for (int i = 0; i < TEST_ITEMS; i++) {
102             assert(ifMatch(array[i], i));
103         }
104     }
105     
106     public void testToTyped() {
107         fillSequential(TEST_ITEMS);
108         Integer JavaDoc[] array = (Integer JavaDoc[])m_queue.toArray(Integer JavaDoc.class);
109         assert(array.length == TEST_ITEMS);
110         for (int i = 0; i < TEST_ITEMS; i++) {
111             assert(ifMatch(array[i], i));
112         }
113     }
114     
115     public void testClone() {
116         fillSequential(TEST_ITEMS);
117         ObjectQueue clone = (ObjectQueue)m_queue.clone();
118         assert(clone.size() == TEST_ITEMS);
119         for (int i = 0; i < TEST_ITEMS; i++) {
120             assert(ifMatch(m_queue.remove(), i));
121         }
122     }
123     
124     public static Test suite() {
125         return new TestSuite(ObjectQueueTest.class);
126     }
127     
128     public static void main(String JavaDoc[] args) {
129         String JavaDoc[] names = { ObjectQueueTest.class.getName() };
130         junit.textui.TestRunner.main(names);
131     }
132 }
133
Popular Tags