1 // Tags: JDK1.6 |
|
2 |
|
3 // Copyright (C) 2009 Red Hat, Inc. |
|
4 |
|
5 // This file is part of Mauve. |
|
6 |
|
7 // Mauve is free software; you can redistribute it and/or modify |
|
8 // it under the terms of the GNU General Public License as published by |
|
9 // the Free Software Foundation; either version 2, or (at your option) |
|
10 // any later version. |
|
11 |
|
12 // Mauve is distributed in the hope that it will be useful, |
|
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 // GNU General Public License for more details. |
|
16 |
|
17 // You should have received a copy of the GNU General Public License |
|
18 // along with Mauve; see the file COPYING. If not, write to |
|
19 // the Free Software Foundation, 59 Temple Place - Suite 330, |
|
20 // Boston, MA 02111-1307, USA. */ |
|
21 |
|
22 package gnu.testlet.java.lang.String; |
|
23 |
|
24 import gnu.testlet.Testlet; |
|
25 import gnu.testlet.TestHarness; |
|
26 |
|
27 import java.nio.charset.Charset; |
|
28 import java.nio.charset.UnsupportedCharsetException; |
|
29 |
|
30 public class ConsCharset |
|
31 implements Testlet |
|
32 { |
|
33 public void test (TestHarness h) |
|
34 { |
|
35 try |
|
36 { |
|
37 byte[] cp437Bytes = asByteArray(new int[] { 224, 226, 227, 228, 156 }); |
|
38 checkString(h, new String(cp437Bytes, Charset.forName("CP437")), |
|
39 "\u03b1\u0393\u03c0\u03a3\u00a3"); |
|
40 } |
|
41 catch (UnsupportedCharsetException e) |
|
42 { |
|
43 // Skip tests as CP437 is not required by the spec. |
|
44 } |
|
45 |
|
46 byte[] utf8Bytes = asByteArray(new int[] { 0xC3,0x9F,0xE2,0x85,0x93,0xE2,0x82,0xAF,0xF0,0x90,0x85,0x80 }); |
|
47 checkString(h, new String(utf8Bytes, Charset.forName("UTF8")), |
|
48 "\u00DF\u2153\u20AF\uD800\uDD40"); |
|
49 |
|
50 byte[] isoBytes = asByteArray(new int[] {0x48,0x65,0x6C,0x6C,0x6F,0x20,0x57,0x6F,0x72,0x6C,0x64,0x21}); |
|
51 checkString(h, new String(isoBytes, Charset.forName("ISO-8859-1")), |
|
52 "Hello World!"); |
|
53 } |
|
54 |
|
55 private void checkString(TestHarness h, String result, String expected) |
|
56 { |
|
57 for (int a = 0; a < result.length(); ++a) |
|
58 { |
|
59 h.check(result.charAt(a), expected.charAt(a)); |
|
60 } |
|
61 h.check(result, expected); |
|
62 } |
|
63 |
|
64 private byte[] asByteArray(int[] ints) |
|
65 { |
|
66 byte[] bytes = new byte[ints.length]; |
|
67 for (int a = 0; a < ints.length; ++a) |
|
68 bytes[a] = Integer.valueOf(ints[a]).byteValue(); |
|
69 return bytes; |
|
70 } |
|
71 |
|
72 } |
|