KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tests > sosnoski > util > CharBufferTest


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;
24
25 import com.sosnoski.util.CharBuffer;
26 import com.sosnoski.util.array.CharArray;
27
28 import junit.framework.*;
29
30 public class CharBufferTest extends TestCase
31 {
32     private static final int TEST_ITEMS = 10;
33     private static final String JavaDoc TEST_STRING = "abcdefghijklmnopqrstuvwxyz0123";
34     private static final char[] TEST_CHARS = TEST_STRING.toCharArray();
35     private static final CharArray TEST_ARRAY = new CharBuffer(TEST_STRING);
36     
37     private CharBuffer m_array;
38     
39     public CharBufferTest(String JavaDoc name) {
40         super(name);
41     }
42     
43     public void setUp() {
44         m_array = new CharBuffer();
45     }
46     
47     public void tearDown() {
48         m_array = null;
49     }
50     
51     private String JavaDoc swapCase(String JavaDoc base) {
52         StringBuffer JavaDoc buff = new StringBuffer JavaDoc(base);
53         for (int i = 0; i < buff.length(); i++) {
54             char chr = buff.charAt(i);
55             if (Character.isUpperCase(chr)) {
56                 chr = Character.toLowerCase(chr);
57             } else {
58                 chr = Character.toUpperCase(chr);
59             }
60             buff.setCharAt(i, chr);
61         }
62         return buff.toString();
63     }
64     
65     public void testAppend() {
66         m_array.append(TEST_STRING);
67         assert(m_array.size() == TEST_STRING.length());
68         assert(m_array.toString().equals(TEST_STRING));
69         m_array.clear();
70         char[] chars = TEST_STRING.toCharArray();
71         m_array.append(chars);
72         assert(m_array.size() == chars.length);
73         assert(m_array.toString().equals(TEST_STRING));
74         m_array.clear();
75         int start = chars.length / 3;
76         int length = start + 1;
77         m_array.append(chars, start, length);
78         assert(m_array.size() == length);
79         assert(m_array.toString().equals(TEST_STRING.substring
80             (start, start+length)));
81         m_array.clear();
82         CharArray array = new CharBuffer(TEST_STRING);
83         m_array.append(array);
84         assert(m_array.size() == TEST_STRING.length());
85         assert(m_array.toString().equals(TEST_STRING));
86         m_array.append(TEST_STRING);
87         assert(m_array.size() == TEST_STRING.length()*2);
88     }
89     
90     public void testEquals() {
91         m_array.append(TEST_STRING);
92         char[] chars = TEST_STRING.toCharArray();
93         CharArray array = new CharBuffer(TEST_STRING);
94         assert(m_array.equals(TEST_STRING));
95         assert(m_array.equals(chars));
96         assert(m_array.equals(array));
97         m_array.clear();
98         int start = chars.length / 3;
99         int length = start + 1;
100         m_array.append(chars, start, length);
101         assert(!m_array.equals(TEST_STRING));
102         assert(!m_array.equals(chars));
103         assert(!m_array.equals(array));
104         assert(m_array.equals(chars, start, length));
105         assert(CharBuffer.equals(chars, TEST_STRING.toCharArray()));
106         assert(CharBuffer.equals(chars, TEST_STRING));
107         chars[start+length-1]++;
108         assert(!m_array.equals(chars, start, length));
109         assert(!CharBuffer.equals(chars, TEST_STRING.toCharArray()));
110         assert(!CharBuffer.equals(chars, TEST_STRING));
111     }
112     
113     public void testEqualsNoCase() {
114         m_array.append(TEST_STRING);
115         String JavaDoc cased = swapCase(TEST_STRING);
116         char[] chars = cased.toCharArray();
117         CharArray array = new CharBuffer(cased);
118         assert(!m_array.equals(cased));
119         assert(!m_array.equals(chars));
120         assert(!m_array.equals(array));
121         assert(m_array.equalsNoCase(cased));
122         assert(m_array.equalsNoCase(chars));
123         assert(m_array.equalsNoCase(array));
124         m_array.clear();
125         char[] base = TEST_STRING.toCharArray();
126         int start = base.length / 3;
127         int length = start + 1;
128         m_array.append(base, start, length);
129         assert(!m_array.equals(chars, start, length));
130         assert(m_array.equalsNoCase(chars, start, length));
131         assert(!CharBuffer.equals(chars, TEST_STRING.toCharArray()));
132         assert(!CharBuffer.equals(chars, TEST_STRING));
133         assert(CharBuffer.equalsNoCase(chars, TEST_STRING.toCharArray()));
134         assert(CharBuffer.equalsNoCase(chars, TEST_STRING));
135         chars[start+length-1]++;
136         assert(!m_array.equalsNoCase(chars, start, length));
137         assert(!CharBuffer.equalsNoCase(chars, TEST_STRING.toCharArray()));
138         assert(!CharBuffer.equalsNoCase(chars, TEST_STRING));
139     }
140     
141     public void testInsert() {
142         m_array.append(TEST_STRING);
143         int offset = TEST_STRING.length() / 3;
144         m_array.insert(offset, TEST_STRING);
145         assert(m_array.size() == TEST_STRING.length()*2);
146         assert(m_array.equals(TEST_STRING.substring(0, offset) + TEST_STRING +
147             TEST_STRING.substring(offset)));
148         m_array.clear();
149         char[] chars = TEST_STRING.toCharArray();
150         m_array.append(chars);
151         m_array.insert(offset, chars);
152         assert(m_array.size() == chars.length*2);
153         assert(m_array.equals(TEST_STRING.substring(0, offset) + TEST_STRING +
154             TEST_STRING.substring(offset)));
155     }
156     
157     public void testReplace() {
158         m_array.append(TEST_STRING);
159         int start = TEST_STRING.length() / 3;
160         int end = start + start;
161         m_array.replace(start, end, TEST_STRING);
162         assert(m_array.size() == TEST_STRING.length()*2-start);
163         assert(m_array.equals(TEST_STRING.substring(0, start) + TEST_STRING +
164             TEST_STRING.substring(end)));
165         m_array.clear();
166         char[] chars = TEST_STRING.toCharArray();
167         m_array.append(chars);
168         m_array.replace(start, end, chars);
169         assert(m_array.size() == chars.length*2-start);
170         assert(m_array.equals(TEST_STRING.substring(0, start) + TEST_STRING +
171             TEST_STRING.substring(end)));
172     }
173     
174     public void testHash() {
175         m_array.append(TEST_STRING);
176         assert(m_array.hashCode() == CharBuffer.hashCode(TEST_STRING));
177         assert(m_array.hashCode() ==
178             CharBuffer.hashCode(TEST_STRING.toCharArray()));
179     }
180     
181     public void testClone() {
182         m_array.append(TEST_STRING);
183         CharBuffer clone = (CharBuffer)m_array.clone();
184         assert(clone.size() == TEST_STRING.length());
185         assert(clone.toString().equals(TEST_STRING));
186     }
187     
188     public void testToString() {
189         m_array.append(TEST_STRING);
190         assert(m_array.toString().equals(TEST_STRING));
191         int start = TEST_STRING.length() / 3;
192         int length = start + 1;
193         assert(m_array.toString(start, length).equals
194             (TEST_STRING.substring(start, start+length)));
195     }
196     
197     public static Test suite() {
198         return new TestSuite(CharBufferTest.class);
199     }
200     
201     public static void main(String JavaDoc[] args) {
202         String JavaDoc[] names = { CharBufferTest.class.getName() };
203         junit.textui.TestRunner.main(names);
204     }
205 }
206
Popular Tags