KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > java > lang > _CharacterTestCase


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.runtime.java.lang;
33
34 import junit.framework.TestCase;
35
36 /**
37  * @author Taras Puchko
38  */

39 public class _CharacterTestCase extends TestCase {
40
41     public void testCharCount() throws Exception JavaDoc {
42         assertEquals(1, Character.charCount('a'));
43         assertEquals(2, Character.charCount(0x1D11E));
44     }
45
46     public void testCodePointAt() throws Exception JavaDoc {
47         assertEquals('b', Character.codePointAt("abc", 1));
48         assertEquals(0x20001, Character.codePointAt("a\uD840\uDC01c", 1));
49         assertEquals(0xD840, Character.codePointAt("a\uD840", 1));
50         assertEquals(0x1D11E, Character.codePointAt("\uD834\uDD1E".toCharArray(), 0));
51         assertEquals(0xD834, Character.codePointAt("\uD834\uDD1Ex".toCharArray(), 0, 1));
52         assertEquals(0xDD1E, Character.codePointAt("x\uDD1E".toCharArray(), 1));
53         try {
54             Character.codePointAt("abc", -1);
55             fail();
56         } catch (IndexOutOfBoundsException JavaDoc e) {
57             //ok
58
}
59         try {
60             Character.codePointAt("abc".toCharArray(), -1);
61             fail();
62         } catch (IndexOutOfBoundsException JavaDoc e) {
63             //ok
64
}
65         try {
66             Character.codePointAt("abc", 10);
67             fail();
68         } catch (IndexOutOfBoundsException JavaDoc e) {
69             //ok
70
}
71         try {
72             Character.codePointAt("abc".toCharArray(), 10);
73             fail();
74         } catch (IndexOutOfBoundsException JavaDoc e) {
75             //ok
76
}
77         try {
78             Character.codePointAt("abc".toCharArray(), 2, 1);
79             fail();
80         } catch (IndexOutOfBoundsException JavaDoc e) {
81             //ok
82
}
83         try {
84             Character.codePointAt("abc".toCharArray(), 0, -1);
85             fail();
86         } catch (IndexOutOfBoundsException JavaDoc e) {
87             //ok
88
}
89         try {
90             Character.codePointAt("abc".toCharArray(), 0, 10);
91             fail();
92         } catch (IndexOutOfBoundsException JavaDoc e) {
93             //ok
94
}
95     }
96
97     public void testCodePointBefore() throws Exception JavaDoc {
98         assertEquals('a', Character.codePointBefore("abc", 1));
99         assertEquals('c', Character.codePointBefore("abc", 3));
100         assertEquals(0x20001, Character.codePointBefore("a\uD840\uDC01c", 3));
101         assertEquals(0xDC01, Character.codePointBefore("a\uDC01", 2));
102         assertEquals(0x1D11E, Character.codePointBefore("\uD834\uDD1E".toCharArray(), 2));
103         assertEquals(0xDD1E, Character.codePointBefore("\uD834\uDD1Ex".toCharArray(), 2, 1));
104         assertEquals(0xDD1E, Character.codePointBefore("x\uDD1E", 2));
105         try {
106             Character.codePointBefore("abc", 0);
107             fail();
108         } catch (IndexOutOfBoundsException JavaDoc e) {
109             //ok
110
}
111         try {
112             Character.codePointBefore("abc".toCharArray(), 0);
113             fail();
114         } catch (IndexOutOfBoundsException JavaDoc e) {
115             //ok
116
}
117         try {
118             Character.codePointBefore("abc", 4);
119             fail();
120         } catch (IndexOutOfBoundsException JavaDoc e) {
121             //ok
122
}
123         try {
124             Character.codePointBefore("abc".toCharArray(), 4);
125             fail();
126         } catch (IndexOutOfBoundsException JavaDoc e) {
127             //ok
128
}
129         try {
130             Character.codePointBefore("abc".toCharArray(), 1, 2);
131             fail();
132         } catch (IndexOutOfBoundsException JavaDoc e) {
133             //ok
134
}
135         try {
136             Character.codePointBefore("abc".toCharArray(), 0, -1);
137             fail();
138         } catch (IndexOutOfBoundsException JavaDoc e) {
139             //ok
140
}
141         try {
142             Character.codePointBefore("abc".toCharArray(), 0, 10);
143             fail();
144         } catch (IndexOutOfBoundsException JavaDoc e) {
145             //ok
146
}
147     }
148
149     public void testCodePointCount() throws Exception JavaDoc {
150         assertEquals(4, Character.codePointCount("abcdef", 1, 5));
151         assertEquals(3, Character.codePointCount("ab\uD834\uDD1Eef", 1, 5));
152         assertEquals(5, Character.codePointCount("ab\uD834x\uDD1Eef", 1, 6));
153         assertEquals(4, Character.codePointCount("abcdef".toCharArray(), 1, 4));
154         assertEquals(3, Character.codePointCount("ab\uD834\uDD1Eef".toCharArray(), 1, 4));
155         assertEquals(5, Character.codePointCount("ab\uD834x\uDD1Eef".toCharArray(), 1, 5));
156         try {
157             Character.codePointCount("abc", -1, 2);
158             fail();
159         } catch (IndexOutOfBoundsException JavaDoc e) {
160             //ok
161
}
162         try {
163             Character.codePointCount("abc", 1, 5);
164             fail();
165         } catch (IndexOutOfBoundsException JavaDoc e) {
166             //ok
167
}
168         try {
169             Character.codePointCount("abc", 2, 1);
170             fail();
171         } catch (IndexOutOfBoundsException JavaDoc e) {
172             //ok
173
}
174         try {
175             Character.codePointCount("abc".toCharArray(), -1, 2);
176             fail();
177         } catch (IndexOutOfBoundsException JavaDoc e) {
178             //ok
179
}
180         try {
181             Character.codePointCount("abc".toCharArray(), 1, 3);
182             fail();
183         } catch (IndexOutOfBoundsException JavaDoc e) {
184             //ok
185
}
186         try {
187             Character.codePointCount("abc".toCharArray(), 2, -1);
188             fail();
189         } catch (IndexOutOfBoundsException JavaDoc e) {
190             //ok
191
}
192     }
193
194     public void testDigit() throws Exception JavaDoc {
195         assertEquals(5, Character.digit((int) '5', 10));
196         assertEquals(-1, Character.digit((int) 'z', 10));
197     }
198
199     public void testGetDirectionality() throws Exception JavaDoc {
200         assertEquals(Character.DIRECTIONALITY_EUROPEAN_NUMBER, Character.getDirectionality((int) '5'));
201     }
202
203     public void testGetNumericValue() throws Exception JavaDoc {
204         assertEquals(5, Character.getNumericValue((int) '5'));
205     }
206
207     public void testGetType() throws Exception JavaDoc {
208         assertEquals(Character.DECIMAL_DIGIT_NUMBER, Character.getType((int) '5'));
209     }
210
211     public void testIsDefined() throws Exception JavaDoc {
212         assertTrue(Character.isDefined((int) 'a'));
213         assertFalse(Character.isDefined(0x50000));
214     }
215
216     public void testIsDigit() throws Exception JavaDoc {
217         assertTrue(Character.isDigit((int) '5'));
218         assertFalse(Character.isDigit((int) 'a'));
219     }
220
221     public void testIsHighSurrogate() throws Exception JavaDoc {
222         assertTrue(Character.isHighSurrogate('\uD834'));
223         assertFalse(Character.isHighSurrogate('a'));
224     }
225
226     public void testIsIdentifierIgnorable() throws Exception JavaDoc {
227         assertTrue(Character.isIdentifierIgnorable(0x0085));
228         assertFalse(Character.isIdentifierIgnorable((int) 'a'));
229     }
230
231     public void testIsISOControl() throws Exception JavaDoc {
232         assertTrue(Character.isISOControl(9));
233         assertFalse(Character.isISOControl((int) 'a'));
234     }
235
236     public void testIsJavaIdentifierPart() throws Exception JavaDoc {
237         assertTrue(Character.isJavaIdentifierPart((int) 'a'));
238         assertFalse(Character.isJavaIdentifierPart((int) '+'));
239     }
240
241     public void testIsJavaIdentifierStart() throws Exception JavaDoc {
242         assertTrue(Character.isJavaIdentifierStart((int) 'a'));
243         assertFalse(Character.isJavaIdentifierStart((int) '1'));
244     }
245
246     public void testIsLetter() throws Exception JavaDoc {
247         assertTrue(Character.isLetter((int) 'a'));
248         assertFalse(Character.isLetter((int) '1'));
249     }
250
251     public void testIsLetterOrDigit() throws Exception JavaDoc {
252         assertTrue(Character.isLetterOrDigit((int) 'a'));
253         assertTrue(Character.isLetterOrDigit((int) '5'));
254         assertFalse(Character.isLetterOrDigit((int) '.'));
255     }
256
257     public void testIsLowerCase() throws Exception JavaDoc {
258         assertTrue(Character.isLowerCase((int) 'a'));
259         assertFalse(Character.isLowerCase((int) 'A'));
260     }
261
262     public void testIsLowSurrogate() throws Exception JavaDoc {
263         assertTrue(Character.isLowSurrogate('\uDD1E'));
264         assertFalse(Character.isLowSurrogate('\uD834'));
265         assertFalse(Character.isLowSurrogate('a'));
266     }
267
268     public void testIsMirrored() throws Exception JavaDoc {
269         assertTrue(Character.isMirrored((int) '{'));
270         assertFalse(Character.isMirrored((int) 'z'));
271     }
272
273     public void testIsSpaceChar() throws Exception JavaDoc {
274         assertTrue(Character.isSpaceChar((int) ' '));
275         assertFalse(Character.isSpaceChar((int) 'x'));
276     }
277
278     public void testIsSupplementaryCodePoint() throws Exception JavaDoc {
279         assertTrue(Character.isSupplementaryCodePoint(0x1D11E));
280         assertFalse(Character.isSupplementaryCodePoint((int) 'a'));
281         assertFalse(Character.isSupplementaryCodePoint(-100));
282     }
283
284     public void testIsSurrogatePair() throws Exception JavaDoc {
285         assertTrue(Character.isSurrogatePair('\uD834', '\uDD1E'));
286         assertFalse(Character.isSurrogatePair('\uDD1E', '\uD834'));
287         assertFalse(Character.isSurrogatePair('a', 'b'));
288     }
289
290     public void testIsTitleCase() throws Exception JavaDoc {
291         assertTrue(Character.isTitleCase(453));
292         assertFalse(Character.isTitleCase((int) 'A'));
293     }
294
295     public void testIsUnicodeIdentifierPart() throws Exception JavaDoc {
296         assertTrue(Character.isUnicodeIdentifierPart((int) '_'));
297         assertFalse(Character.isUnicodeIdentifierPart((int) '.'));
298     }
299
300     public void testIsUnicodeIdentifierStart() throws Exception JavaDoc {
301         assertTrue(Character.isUnicodeIdentifierStart((int) 'a'));
302         assertFalse(Character.isUnicodeIdentifierStart((int) '.'));
303     }
304
305     public void testIsUpperCase() throws Exception JavaDoc {
306         assertTrue(Character.isUpperCase((int) 'A'));
307         assertFalse(Character.isUpperCase((int) 'a'));
308     }
309
310     public void testIsValidCodePoint() throws Exception JavaDoc {
311         assertTrue(Character.isValidCodePoint((int) 'A'));
312         assertFalse(Character.isValidCodePoint(0x1000000));
313         assertFalse(Character.isValidCodePoint(-1));
314     }
315
316     public void testIsWhitespace() throws Exception JavaDoc {
317         assertTrue(Character.isWhitespace(9));
318         assertFalse(Character.isWhitespace((int) 'x'));
319     }
320
321     public void testOffsetByCodePoints() throws Exception JavaDoc {
322         assertEquals(5, Character.offsetByCodePoints("abcdef", 2, 3));
323         assertEquals(6, Character.offsetByCodePoints("ab\uD834\uDD1Eef", 2, 3));
324         assertEquals(2, Character.offsetByCodePoints("ab\uD834\uDD1Eef", 4, -1));
325         assertEquals(0, Character.offsetByCodePoints("\uDD1Eef", 3, -3));
326         assertEquals(3, Character.offsetByCodePoints("ab\uD834", 1, 2));
327         assertEquals(3, Character.offsetByCodePoints("abcdef".toCharArray(), 1, 3, 2, 1));
328         assertEquals(1, Character.offsetByCodePoints("abc".toCharArray(), 1, 1, 2, -1));
329         assertEquals(1, Character.offsetByCodePoints("ab\uD834\uDD1Eef".toCharArray(), 1, 4, 5, -3));
330         try {
331             Character.offsetByCodePoints("abc", -1, 3);
332             fail();
333         } catch (IndexOutOfBoundsException JavaDoc e) {
334             //ok
335
}
336         try {
337             Character.offsetByCodePoints("abc", 5, 0);
338             fail();
339         } catch (IndexOutOfBoundsException JavaDoc e) {
340             //ok
341
}
342         try {
343             Character.offsetByCodePoints("abc", 1, 3);
344             fail();
345         } catch (IndexOutOfBoundsException JavaDoc e) {
346             //ok
347
}
348         try {
349             Character.offsetByCodePoints("abc", 1, -2);
350             fail();
351         } catch (IndexOutOfBoundsException JavaDoc e) {
352             //ok
353
}
354         try {
355             Character.offsetByCodePoints("abc".toCharArray(), -1, 0, 1, 0);
356             fail();
357         } catch (IndexOutOfBoundsException JavaDoc e) {
358             //ok
359
}
360         try {
361             Character.offsetByCodePoints("abc".toCharArray(), 1, -1, 1, 0);
362             fail();
363         } catch (IndexOutOfBoundsException JavaDoc e) {
364             //ok
365
}
366         try {
367             Character.offsetByCodePoints("abc".toCharArray(), 1, 3, 1, 0);
368             fail();
369         } catch (IndexOutOfBoundsException JavaDoc e) {
370             //ok
371
}
372         try {
373             Character.offsetByCodePoints("abc".toCharArray(), 1, 2, 0, 0);
374             fail();
375         } catch (IndexOutOfBoundsException JavaDoc e) {
376             //ok
377
}
378         try {
379             Character.offsetByCodePoints("abc".toCharArray(), 1, 1, 1, 2);
380             fail();
381         } catch (IndexOutOfBoundsException JavaDoc e) {
382             //ok
383
}
384         try {
385             Character.offsetByCodePoints("abc".toCharArray(), 1, 1, 3, 0);
386             fail();
387         } catch (IndexOutOfBoundsException JavaDoc e) {
388             //ok
389
}
390         try {
391             Character.offsetByCodePoints("abc".toCharArray(), 1, 1, 2, -2);
392             fail();
393         } catch (IndexOutOfBoundsException JavaDoc e) {
394             //ok
395
}
396     }
397
398     public void testReverseBytes() throws Exception JavaDoc {
399         assertEquals(0x3412, Character.reverseBytes((char) 0x1234));
400     }
401
402     public void testToChars() throws Exception JavaDoc {
403         assertEquals("a", new String JavaDoc(Character.toChars('a')));
404         assertEquals("\uD840\uDC01", new String JavaDoc(Character.toChars(0x20001)));
405         char[] chars = new char[10];
406         assertEquals(1, Character.toChars('a', chars, 5));
407         assertEquals('a', chars[5]);
408         assertEquals(2, Character.toChars(0x20001, chars, 7));
409         assertEquals('\uD840', chars[7]);
410         assertEquals('\uDC01', chars[8]);
411         try {
412             Character.toChars(-100);
413             fail();
414         } catch (IllegalArgumentException JavaDoc e) {
415             //ok
416
}
417         try {
418             Character.toChars(0x1000000, chars, 0);
419             fail();
420         } catch (IllegalArgumentException JavaDoc e) {
421             //ok
422
}
423     }
424
425     public void testToCodePoint() throws Exception JavaDoc {
426         assertEquals(0x1D11E, Character.toCodePoint('\uD834', '\uDD1E'));
427         assertEquals(0x20001, Character.toCodePoint('\uD840', '\uDC01'));
428     }
429
430     public void testToLowerCase() throws Exception JavaDoc {
431         assertEquals('a', Character.toLowerCase((int) 'A'));
432         assertEquals('5', Character.toLowerCase((int) '5'));
433     }
434
435     public void testToTitleCase() throws Exception JavaDoc {
436         assertEquals('A', Character.toTitleCase((int) 'a'));
437         assertEquals('Z', Character.toTitleCase((int) 'Z'));
438         assertEquals('5', Character.toTitleCase((int) '5'));
439         assertEquals(453, Character.toTitleCase(454));
440     }
441
442     public void testToUpperCase() throws Exception JavaDoc {
443         assertEquals('A', Character.toUpperCase((int) 'a'));
444         assertEquals('Z', Character.toUpperCase((int) 'Z'));
445         assertEquals(452, Character.toUpperCase(453));
446         assertEquals(452, Character.toUpperCase(454));
447     }
448
449     public void testValueOf() throws Exception JavaDoc {
450         Character JavaDoc character = 'a';
451         assertEquals("a", String.valueOf(character));
452     }
453
454 }
Popular Tags