001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.lang;
032:
033: import junit.framework.TestCase;
034:
035: /**
036: * @author Taras Puchko
037: */
038: public class _CharacterTestCase extends TestCase {
039:
040: public void testCharCount() throws Exception {
041: assertEquals(1, Character.charCount('a'));
042: assertEquals(2, Character.charCount(0x1D11E));
043: }
044:
045: public void testCodePointAt() throws Exception {
046: assertEquals('b', Character.codePointAt("abc", 1));
047: assertEquals(0x20001, Character
048: .codePointAt("a\uD840\uDC01c", 1));
049: assertEquals(0xD840, Character.codePointAt("a\uD840", 1));
050: assertEquals(0x1D11E, Character.codePointAt("\uD834\uDD1E"
051: .toCharArray(), 0));
052: assertEquals(0xD834, Character.codePointAt("\uD834\uDD1Ex"
053: .toCharArray(), 0, 1));
054: assertEquals(0xDD1E, Character.codePointAt("x\uDD1E"
055: .toCharArray(), 1));
056: try {
057: Character.codePointAt("abc", -1);
058: fail();
059: } catch (IndexOutOfBoundsException e) {
060: //ok
061: }
062: try {
063: Character.codePointAt("abc".toCharArray(), -1);
064: fail();
065: } catch (IndexOutOfBoundsException e) {
066: //ok
067: }
068: try {
069: Character.codePointAt("abc", 10);
070: fail();
071: } catch (IndexOutOfBoundsException e) {
072: //ok
073: }
074: try {
075: Character.codePointAt("abc".toCharArray(), 10);
076: fail();
077: } catch (IndexOutOfBoundsException e) {
078: //ok
079: }
080: try {
081: Character.codePointAt("abc".toCharArray(), 2, 1);
082: fail();
083: } catch (IndexOutOfBoundsException e) {
084: //ok
085: }
086: try {
087: Character.codePointAt("abc".toCharArray(), 0, -1);
088: fail();
089: } catch (IndexOutOfBoundsException e) {
090: //ok
091: }
092: try {
093: Character.codePointAt("abc".toCharArray(), 0, 10);
094: fail();
095: } catch (IndexOutOfBoundsException e) {
096: //ok
097: }
098: }
099:
100: public void testCodePointBefore() throws Exception {
101: assertEquals('a', Character.codePointBefore("abc", 1));
102: assertEquals('c', Character.codePointBefore("abc", 3));
103: assertEquals(0x20001, Character.codePointBefore(
104: "a\uD840\uDC01c", 3));
105: assertEquals(0xDC01, Character.codePointBefore("a\uDC01", 2));
106: assertEquals(0x1D11E, Character.codePointBefore("\uD834\uDD1E"
107: .toCharArray(), 2));
108: assertEquals(0xDD1E, Character.codePointBefore("\uD834\uDD1Ex"
109: .toCharArray(), 2, 1));
110: assertEquals(0xDD1E, Character.codePointBefore("x\uDD1E", 2));
111: try {
112: Character.codePointBefore("abc", 0);
113: fail();
114: } catch (IndexOutOfBoundsException e) {
115: //ok
116: }
117: try {
118: Character.codePointBefore("abc".toCharArray(), 0);
119: fail();
120: } catch (IndexOutOfBoundsException e) {
121: //ok
122: }
123: try {
124: Character.codePointBefore("abc", 4);
125: fail();
126: } catch (IndexOutOfBoundsException e) {
127: //ok
128: }
129: try {
130: Character.codePointBefore("abc".toCharArray(), 4);
131: fail();
132: } catch (IndexOutOfBoundsException e) {
133: //ok
134: }
135: try {
136: Character.codePointBefore("abc".toCharArray(), 1, 2);
137: fail();
138: } catch (IndexOutOfBoundsException e) {
139: //ok
140: }
141: try {
142: Character.codePointBefore("abc".toCharArray(), 0, -1);
143: fail();
144: } catch (IndexOutOfBoundsException e) {
145: //ok
146: }
147: try {
148: Character.codePointBefore("abc".toCharArray(), 0, 10);
149: fail();
150: } catch (IndexOutOfBoundsException e) {
151: //ok
152: }
153: }
154:
155: public void testCodePointCount() throws Exception {
156: assertEquals(4, Character.codePointCount("abcdef", 1, 5));
157: assertEquals(3, Character.codePointCount("ab\uD834\uDD1Eef", 1,
158: 5));
159: assertEquals(5, Character.codePointCount("ab\uD834x\uDD1Eef",
160: 1, 6));
161: assertEquals(4, Character.codePointCount(
162: "abcdef".toCharArray(), 1, 4));
163: assertEquals(3, Character.codePointCount("ab\uD834\uDD1Eef"
164: .toCharArray(), 1, 4));
165: assertEquals(5, Character.codePointCount("ab\uD834x\uDD1Eef"
166: .toCharArray(), 1, 5));
167: try {
168: Character.codePointCount("abc", -1, 2);
169: fail();
170: } catch (IndexOutOfBoundsException e) {
171: //ok
172: }
173: try {
174: Character.codePointCount("abc", 1, 5);
175: fail();
176: } catch (IndexOutOfBoundsException e) {
177: //ok
178: }
179: try {
180: Character.codePointCount("abc", 2, 1);
181: fail();
182: } catch (IndexOutOfBoundsException e) {
183: //ok
184: }
185: try {
186: Character.codePointCount("abc".toCharArray(), -1, 2);
187: fail();
188: } catch (IndexOutOfBoundsException e) {
189: //ok
190: }
191: try {
192: Character.codePointCount("abc".toCharArray(), 1, 3);
193: fail();
194: } catch (IndexOutOfBoundsException e) {
195: //ok
196: }
197: try {
198: Character.codePointCount("abc".toCharArray(), 2, -1);
199: fail();
200: } catch (IndexOutOfBoundsException e) {
201: //ok
202: }
203: }
204:
205: public void testDigit() throws Exception {
206: assertEquals(5, Character.digit((int) '5', 10));
207: assertEquals(-1, Character.digit((int) 'z', 10));
208: }
209:
210: public void testGetNumericValue() throws Exception {
211: assertEquals(5, Character.getNumericValue((int) '5'));
212: }
213:
214: public void testGetType() throws Exception {
215: assertEquals(Character.DECIMAL_DIGIT_NUMBER, Character
216: .getType((int) '5'));
217: }
218:
219: public void testIsDefined() throws Exception {
220: assertTrue(Character.isDefined((int) 'a'));
221: assertFalse(Character.isDefined(0x50000));
222: }
223:
224: public void testIsDigit() throws Exception {
225: assertTrue(Character.isDigit((int) '5'));
226: assertFalse(Character.isDigit((int) 'a'));
227: }
228:
229: public void testIsHighSurrogate() throws Exception {
230: assertTrue(Character.isHighSurrogate('\uD834'));
231: assertFalse(Character.isHighSurrogate('a'));
232: }
233:
234: public void testIsIdentifierIgnorable() throws Exception {
235: assertTrue(Character.isIdentifierIgnorable(0x0085));
236: assertFalse(Character.isIdentifierIgnorable((int) 'a'));
237: }
238:
239: public void testIsISOControl() throws Exception {
240: assertTrue(Character.isISOControl(9));
241: assertFalse(Character.isISOControl((int) 'a'));
242: }
243:
244: public void testIsJavaIdentifierPart() throws Exception {
245: assertTrue(Character.isJavaIdentifierPart((int) 'a'));
246: assertFalse(Character.isJavaIdentifierPart((int) '+'));
247: }
248:
249: public void testIsJavaIdentifierStart() throws Exception {
250: assertTrue(Character.isJavaIdentifierStart((int) 'a'));
251: assertFalse(Character.isJavaIdentifierStart((int) '1'));
252: }
253:
254: public void testIsLetter() throws Exception {
255: assertTrue(Character.isLetter((int) 'a'));
256: assertFalse(Character.isLetter((int) '1'));
257: }
258:
259: public void testIsLetterOrDigit() throws Exception {
260: assertTrue(Character.isLetterOrDigit((int) 'a'));
261: assertTrue(Character.isLetterOrDigit((int) '5'));
262: assertFalse(Character.isLetterOrDigit((int) '.'));
263: }
264:
265: public void testIsLowerCase() throws Exception {
266: assertTrue(Character.isLowerCase((int) 'a'));
267: assertFalse(Character.isLowerCase((int) 'A'));
268: }
269:
270: public void testIsLowSurrogate() throws Exception {
271: assertTrue(Character.isLowSurrogate('\uDD1E'));
272: assertFalse(Character.isLowSurrogate('\uD834'));
273: assertFalse(Character.isLowSurrogate('a'));
274: }
275:
276: public void testIsSpaceChar() throws Exception {
277: assertTrue(Character.isSpaceChar((int) ' '));
278: assertFalse(Character.isSpaceChar((int) 'x'));
279: }
280:
281: public void testIsSupplementaryCodePoint() throws Exception {
282: assertTrue(Character.isSupplementaryCodePoint(0x1D11E));
283: assertFalse(Character.isSupplementaryCodePoint((int) 'a'));
284: assertFalse(Character.isSupplementaryCodePoint(-100));
285: }
286:
287: public void testIsSurrogatePair() throws Exception {
288: assertTrue(Character.isSurrogatePair('\uD834', '\uDD1E'));
289: assertFalse(Character.isSurrogatePair('\uDD1E', '\uD834'));
290: assertFalse(Character.isSurrogatePair('a', 'b'));
291: }
292:
293: public void testIsTitleCase() throws Exception {
294: assertTrue(Character.isTitleCase(453));
295: assertFalse(Character.isTitleCase((int) 'A'));
296: }
297:
298: public void testIsUnicodeIdentifierPart() throws Exception {
299: assertTrue(Character.isUnicodeIdentifierPart((int) '_'));
300: assertFalse(Character.isUnicodeIdentifierPart((int) '.'));
301: }
302:
303: public void testIsUnicodeIdentifierStart() throws Exception {
304: assertTrue(Character.isUnicodeIdentifierStart((int) 'a'));
305: assertFalse(Character.isUnicodeIdentifierStart((int) '.'));
306: }
307:
308: public void testIsUpperCase() throws Exception {
309: assertTrue(Character.isUpperCase((int) 'A'));
310: assertFalse(Character.isUpperCase((int) 'a'));
311: }
312:
313: public void testIsValidCodePoint() throws Exception {
314: assertTrue(Character.isValidCodePoint((int) 'A'));
315: assertFalse(Character.isValidCodePoint(0x1000000));
316: assertFalse(Character.isValidCodePoint(-1));
317: }
318:
319: public void testIsWhitespace() throws Exception {
320: assertTrue(Character.isWhitespace(9));
321: assertFalse(Character.isWhitespace((int) 'x'));
322: }
323:
324: public void testOffsetByCodePoints() throws Exception {
325: assertEquals(5, Character.offsetByCodePoints("abcdef", 2, 3));
326: assertEquals(6, Character.offsetByCodePoints(
327: "ab\uD834\uDD1Eef", 2, 3));
328: assertEquals(2, Character.offsetByCodePoints(
329: "ab\uD834\uDD1Eef", 4, -1));
330: assertEquals(0, Character.offsetByCodePoints("\uDD1Eef", 3, -3));
331: assertEquals(3, Character.offsetByCodePoints("ab\uD834", 1, 2));
332: assertEquals(3, Character.offsetByCodePoints("abcdef"
333: .toCharArray(), 1, 3, 2, 1));
334: assertEquals(1, Character.offsetByCodePoints("abc"
335: .toCharArray(), 1, 1, 2, -1));
336: assertEquals(1, Character.offsetByCodePoints("ab\uD834\uDD1Eef"
337: .toCharArray(), 1, 4, 5, -3));
338: try {
339: Character.offsetByCodePoints("abc", -1, 3);
340: fail();
341: } catch (IndexOutOfBoundsException e) {
342: //ok
343: }
344: try {
345: Character.offsetByCodePoints("abc", 5, 0);
346: fail();
347: } catch (IndexOutOfBoundsException e) {
348: //ok
349: }
350: try {
351: Character.offsetByCodePoints("abc", 1, 3);
352: fail();
353: } catch (IndexOutOfBoundsException e) {
354: //ok
355: }
356: try {
357: Character.offsetByCodePoints("abc", 1, -2);
358: fail();
359: } catch (IndexOutOfBoundsException e) {
360: //ok
361: }
362: try {
363: Character.offsetByCodePoints("abc".toCharArray(), -1, 0, 1,
364: 0);
365: fail();
366: } catch (IndexOutOfBoundsException e) {
367: //ok
368: }
369: try {
370: Character.offsetByCodePoints("abc".toCharArray(), 1, -1, 1,
371: 0);
372: fail();
373: } catch (IndexOutOfBoundsException e) {
374: //ok
375: }
376: try {
377: Character.offsetByCodePoints("abc".toCharArray(), 1, 3, 1,
378: 0);
379: fail();
380: } catch (IndexOutOfBoundsException e) {
381: //ok
382: }
383: try {
384: Character.offsetByCodePoints("abc".toCharArray(), 1, 2, 0,
385: 0);
386: fail();
387: } catch (IndexOutOfBoundsException e) {
388: //ok
389: }
390: try {
391: Character.offsetByCodePoints("abc".toCharArray(), 1, 1, 1,
392: 2);
393: fail();
394: } catch (IndexOutOfBoundsException e) {
395: //ok
396: }
397: try {
398: Character.offsetByCodePoints("abc".toCharArray(), 1, 1, 3,
399: 0);
400: fail();
401: } catch (IndexOutOfBoundsException e) {
402: //ok
403: }
404: try {
405: Character.offsetByCodePoints("abc".toCharArray(), 1, 1, 2,
406: -2);
407: fail();
408: } catch (IndexOutOfBoundsException e) {
409: //ok
410: }
411: }
412:
413: public void testReverseBytes() throws Exception {
414: assertEquals(0x3412, Character.reverseBytes((char) 0x1234));
415: }
416:
417: public void testToChars() throws Exception {
418: assertEquals("a", new String(Character.toChars('a')));
419: assertEquals("\uD840\uDC01", new String(Character
420: .toChars(0x20001)));
421: char[] chars = new char[10];
422: assertEquals(1, Character.toChars('a', chars, 5));
423: assertEquals('a', chars[5]);
424: assertEquals(2, Character.toChars(0x20001, chars, 7));
425: assertEquals('\uD840', chars[7]);
426: assertEquals('\uDC01', chars[8]);
427: try {
428: Character.toChars(-100);
429: fail();
430: } catch (IllegalArgumentException e) {
431: //ok
432: }
433: try {
434: Character.toChars(0x1000000, chars, 0);
435: fail();
436: } catch (IllegalArgumentException e) {
437: //ok
438: }
439: }
440:
441: public void testToCodePoint() throws Exception {
442: assertEquals(0x1D11E, Character.toCodePoint('\uD834', '\uDD1E'));
443: assertEquals(0x20001, Character.toCodePoint('\uD840', '\uDC01'));
444: }
445:
446: public void testToLowerCase() throws Exception {
447: assertEquals('a', Character.toLowerCase((int) 'A'));
448: assertEquals('5', Character.toLowerCase((int) '5'));
449: }
450:
451: public void testToTitleCase() throws Exception {
452: assertEquals('A', Character.toTitleCase((int) 'a'));
453: assertEquals('Z', Character.toTitleCase((int) 'Z'));
454: assertEquals('5', Character.toTitleCase((int) '5'));
455: assertEquals(453, Character.toTitleCase(454));
456: }
457:
458: public void testToUpperCase() throws Exception {
459: assertEquals('A', Character.toUpperCase((int) 'a'));
460: assertEquals('Z', Character.toUpperCase((int) 'Z'));
461: assertEquals(452, Character.toUpperCase(453));
462: assertEquals(452, Character.toUpperCase(454));
463: }
464:
465: public void testValueOf() throws Exception {
466: Character character = 'a';
467: assertEquals("a", String.valueOf(character));
468: }
469:
470: }
|