source: branches/0.15.x/abcl/test/src/org/armedbear/lisp/FastStringBufferTest.java

Last change on this file was 11527, checked in by Mark Evenson, 17 years ago

(Phil Hudson) Make FastStringBuffer? an adapter to java-1.5's StringBuilder?.

JUnit tests integrated into 'build.xml', run via 'ant abcl.test'.

Further integration with other build systems (Lisp and Netbeans) has not been done.

File size: 6.6 KB
Line 
1package org.armedbear.lisp;
2
3import static java.lang.Math.abs;
4import java.util.Random;
5
6import org.junit.After;
7import static org.junit.Assert.*;
8import org.junit.Before;
9import org.junit.Test;
10import org.junit.runner.JUnitCore;
11import org.junit.Assert;
12import java.util.Date;
13
14/**
15 * Unit tests for {@link FastStringBuffer}.
16 */
17public class FastStringBufferTest
18{
19    /** Class under test. */
20    private static final Class CLASS = FastStringBuffer.class;
21    private static final Random random = new Random();
22    private static final String CTOR_ARG = "abcde";
23
24    public static void main(final String args[]) {
25        JUnitCore.main("org.armedbear.lisp.FastStringBufferTest");
26    }
27
28    /** Test instance. */
29    private FastStringBuffer buffer = null;
30
31    @Before
32    public void setUp()
33    {
34        buffer = new FastStringBuffer(CTOR_ARG);
35    }
36
37    @After
38    public void tearDown()
39    {
40        buffer = null;
41    }
42
43    @Test
44    public void defaultConstructor()
45    {
46        assertNotNull("Default constructor failed", new FastStringBuffer());
47    }
48
49    @Test
50    public void constructorString()
51    {
52        assertNotNull("String constructor failed",
53                      new FastStringBuffer(CTOR_ARG));
54    }
55
56    @Test
57    public void constructorchar()
58    {
59        assertNotNull("char constructor failed", new FastStringBuffer('c'));
60    }
61
62    @Test
63    public void constructorint()
64    {
65        assertNotNull("int constructor failed", new FastStringBuffer(12));
66    }
67
68    @Test(expected=OutOfMemoryError.class)
69    public void constructorMaxint()
70    {
71        new FastStringBuffer(Integer.MAX_VALUE);
72    }
73
74    @Test(expected=NegativeArraySizeException.class)
75    public void constructorMinint()
76    {
77        new FastStringBuffer(Integer.MIN_VALUE);
78    }
79
80    @Test
81    public void lengthAfterConstructorint()
82    {
83        final FastStringBuffer foo = new FastStringBuffer(234);
84        assertEquals("Length from int constructor not 0", 0, foo.length());
85    }
86
87    @Test
88    public void lengthAfterDefaultConstructor()
89    {
90        assertEquals("Length from default constructor not 0", 0,
91                     new FastStringBuffer().length());
92    }
93
94    @Test
95    public void lengthAfterConstructorString()
96    {
97        final int len = CTOR_ARG.length();
98        assertEquals("Length from String constructor not " + len, len,
99                     new FastStringBuffer(CTOR_ARG).length());
100    }
101
102    @Test
103    public void lengthAfterConstructorchar()
104    {
105        final char w = 'w';
106        final FastStringBuffer newBuffer = new FastStringBuffer(w);
107        final int len = newBuffer.length();
108        assertEquals("Length from char constructor: " + len, 1, len);
109    }
110
111    // Target method to be made private during refactoring
112    // @Test(expect=NoSuchMethodException.class)
113    @Test
114    public void capacity() throws NoSuchMethodException
115    {
116        CLASS.getMethod("capacity", (Class[]) null);
117    }
118
119    @Test
120    public void charAt()
121    {
122        assertEquals("Indexed char unexpected", 'c', buffer.charAt(2));
123    }
124
125    @Test
126    public void getChars()
127    {
128        final char[] dst = {0, 0};
129        final char[] cmp = {'b', 'c'};
130        buffer.getChars(1, 3, dst, 0);
131        assertArrayEquals("Subarray unexpected; cmp: " + new String(cmp) +
132                          ", dst: " + new String(dst), cmp, dst);
133    }
134
135    @Test(expected=StringIndexOutOfBoundsException.class)
136    public void getCharsBadStartIndex()
137    {
138        buffer.getChars(-1, -1, null, 0);
139    }
140
141    @Test(expected=StringIndexOutOfBoundsException.class)
142    public void getCharsInvertedStartEnd()
143    {
144        buffer.getChars(3, 1, null, 0);
145    }
146
147    @Test(expected=StringIndexOutOfBoundsException.class)
148    public void getCharsExcessiveEnd()
149    {
150        buffer.getChars(1, 7, null, 0);
151    }
152
153    @Test
154    public void setCharAt()
155    {
156        buffer.setCharAt(2, 'z');
157        assertEquals("Incorrect setCharAt", "abzde", buffer.toString());
158    }
159
160    @Test(expected=IndexOutOfBoundsException.class)
161    public void setCharAtExcess()
162    {
163        buffer.setCharAt(8, 'x');
164    }
165
166    @Test(expected=StringIndexOutOfBoundsException.class)
167    public void setCharAtNeg()
168    {
169        buffer.setCharAt(-2, 'x');
170    }
171
172    @Test
173    public void ensureCapacity()
174    {
175        buffer.ensureCapacity(200);
176        assertTrue("Unexpected capacity", buffer.capacity() >= 200);
177    }
178
179    @Test(expected=NoSuchMethodException.class)
180    public void setText() throws NoSuchMethodException
181    {
182        FastStringBuffer.class.getMethod("setText");
183    }
184
185    @Test
186    public void append()
187    {
188        buffer.append("fgh");
189        assertEquals("abcdefgh", buffer.toString());
190    }
191
192    @Test
193    public void appendNullString()
194    {
195        buffer.append((String)null);
196        assertEquals("abcdenull", buffer.toString());
197    }
198
199    @Test
200    public void appendcharArray()
201    {
202        buffer.append(new char[]{'x', 'y', 'z'});
203        assertEquals("abcdexyz", buffer.toString());
204    }
205
206    @Test(expected=NullPointerException.class)
207    public void appendNullcharArray()
208    {
209        buffer.append((char []) null);
210    }
211
212    @Test(expected=IndexOutOfBoundsException.class)
213    public void appendWithin()
214    {
215        buffer.append(new char[]{'x', 'y', 'z'}, 1, 3);
216        assertEquals("abcdexyz", buffer.toString());
217    }
218
219    @Test
220    public void appendObject()
221    {
222        buffer.append(new Date());
223        assertTrue(buffer.length() > 5);
224    }
225
226    @Test
227    public void appendchar()
228    {
229        buffer.append('f');
230        assertEquals("abcdef", buffer.toString());
231    }
232
233    @Test
234    public void appendint()
235    {
236        buffer.append(1);
237        assertEquals("abcde1", buffer.toString());
238    }
239
240    @Test
241    public void appendlong()
242    {
243        buffer.append(1L);
244        assertEquals("abcde1", buffer.toString());
245    }
246
247    @Test
248    public void setLength()
249    {
250        buffer.setLength(3);
251        assertEquals("abc", buffer.toString());
252    }
253
254    @Test(expected=IndexOutOfBoundsException.class)
255    public void setLengthNeg()
256    {
257        buffer.setLength(-1);
258    }
259
260    @Test
261    public void setLengthExcess()
262    {
263        buffer.setLength(12);
264        assertEquals(12, buffer.length());
265        // This just seems weird to me
266        assertFalse(CTOR_ARG.equals(buffer.toString()));
267    }
268
269    @Test
270    public void reverse()
271    {
272        buffer.reverse();
273        assertEquals("edcba", buffer.toString());
274    }
275
276    @Test
277    public void testToString()
278    {
279        assertEquals(CTOR_ARG, buffer.toString());
280    }
281
282    @Test
283    public void toCharArray()
284    {
285        assertArrayEquals(new char[] {'a', 'b', 'c', 'd', 'e'},
286                          buffer.toCharArray());
287    }
288
289}
Note: See TracBrowser for help on using the repository browser.