KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tests > sosnoski > util > array > IntArrayTest


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.array;
24
25 import com.sosnoski.util.array.IntArray;
26
27 import junit.framework.*;
28
29 public class IntArrayTest extends TestCase
30 {
31     private static final int TEST_ITEMS = 80;
32
33     private IntArray m_array;
34
35     public IntArrayTest(String JavaDoc name) {
36         super(name);
37     }
38
39     public void setUp() {
40         m_array = new IntArray();
41     }
42
43     public void tearDown() {
44         m_array = null;
45     }
46
47     private int gen(int index) {
48         return index;
49     }
50
51     private boolean ifMatch(int a, int b) {
52         return a == b;
53     }
54
55     public void fillSequential(int count) {
56         for (int i = 0; i < count; i++) {
57             m_array.add(gen(i));
58         }
59     }
60
61     public void testAdd() {
62         fillSequential(TEST_ITEMS);
63         assert(m_array.size() == TEST_ITEMS);
64     }
65
66     public void testClear() {
67         fillSequential(TEST_ITEMS);
68         m_array.clear();
69         assert(m_array.size() == 0);
70     }
71
72     public void testRemove() {
73         fillSequential(TEST_ITEMS);
74         m_array.remove(TEST_ITEMS/2);
75         assert(m_array.size() == TEST_ITEMS-1);
76         for (int i = 0; i < TEST_ITEMS/2; i++) {
77             assert(ifMatch(m_array.get(i), i));
78         }
79         for (int i = TEST_ITEMS/2; i < TEST_ITEMS-1; i++) {
80             assert(ifMatch(m_array.get(i), i+1));
81         }
82         m_array.clear();
83         fillSequential(TEST_ITEMS);
84         int start = TEST_ITEMS/3;
85         int end = start + start + 1;
86         int change = end - start;
87         m_array.remove(start, end);
88         assert(m_array.size() == TEST_ITEMS-change);
89         for (int i = 0; i < start; i++) {
90             assert(ifMatch(m_array.get(i), i));
91         }
92         for (int i = start; i < TEST_ITEMS-change; i++) {
93             assert(ifMatch(m_array.get(i), i+change));
94         }
95     }
96
97     public void testInsert() {
98         fillSequential(TEST_ITEMS);
99         m_array.add(TEST_ITEMS/2, gen(-1));
100         assert(m_array.size() == TEST_ITEMS+1);
101         for (int i = 0; i < TEST_ITEMS/2; i++) {
102             assert(ifMatch(m_array.get(i), i));
103         }
104         assert(ifMatch(m_array.get(TEST_ITEMS/2), -1));
105         for (int i = TEST_ITEMS/2; i < TEST_ITEMS; i++) {
106             assert(ifMatch(m_array.get(i+1), i));
107         }
108     }
109
110     public void testSet() {
111         fillSequential(TEST_ITEMS);
112         for (int i = 0; i < TEST_ITEMS; i++) {
113             m_array.set(i, gen(TEST_ITEMS-i));
114         }
115         assert(m_array.size() == TEST_ITEMS);
116         for (int i = 0; i < TEST_ITEMS; i++) {
117             assert(ifMatch(m_array.get(i), TEST_ITEMS-i));
118         }
119     }
120
121     public void testSetSize() {
122         fillSequential(TEST_ITEMS);
123         m_array.setSize(TEST_ITEMS/2);
124         assert(m_array.size() == TEST_ITEMS/2);
125     }
126
127     public void testToArray() {
128         fillSequential(TEST_ITEMS);
129         int[] array = m_array.toArray();
130         assert(array.length == TEST_ITEMS);
131         for (int i = 0; i < TEST_ITEMS; i++) {
132             assert(ifMatch(array[i], i));
133         }
134         array = m_array.toArray(TEST_ITEMS/2-1, TEST_ITEMS/2);
135         assert(array.length == TEST_ITEMS/2);
136         for (int i = 0; i < TEST_ITEMS/2; i++) {
137             assert(ifMatch(array[i], TEST_ITEMS/2-1+i));
138         }
139     }
140
141     public void testClone() {
142         fillSequential(TEST_ITEMS);
143         IntArray clone = (IntArray)m_array.clone();
144         assert(clone.size() == TEST_ITEMS);
145         for (int i = 0; i < TEST_ITEMS; i++) {
146             assert(ifMatch(clone.get(i), i));
147         }
148     }
149
150     public static Test suite() {
151         return new TestSuite(IntArrayTest.class);
152     }
153
154     public static void main(String JavaDoc[] args) {
155         String JavaDoc[] names = { IntArrayTest.class.getName() };
156         junit.textui.TestRunner.main(names);
157     }
158 }
159
Popular Tags