OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / tests / api / java / nio / charset / Charset_TestGenerator.java
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package tests.api.java.nio.charset;
17
18 import dalvik.annotation.TestLevel;
19 import dalvik.annotation.TestTargetClass;
20 import dalvik.annotation.TestTargetNew;
21
22 import junit.framework.TestCase;
23
24 import java.nio.ByteBuffer;
25 import java.nio.CharBuffer;
26 import java.nio.charset.CharacterCodingException;
27 import java.nio.charset.Charset;
28 import java.nio.charset.CharsetDecoder;
29 import java.nio.charset.CharsetEncoder;
30 import java.nio.charset.CodingErrorAction;
31
32 /**
33  * Super class for concrete charset test suites.
34  */
35 public class Charset_TestGenerator {
36
37     Charset charset;
38     CharsetDecoder decoder;
39     CharsetEncoder encoder;
40
41
42     static final int[] codes = {
43         9, 12, 28, 31, 48, 51, 54, 57, 65, 68, 71, 74, 77, 80, 83, 86,
44         89, 97, 100, 103, 106, 109, 112, 115, 118, 121, 170, 181, 186, 192, 195, 198,
45         201, 204, 207, 210, 213, 216, 219, 222, 225, 228, 231, 234, 237, 240, 243, 246,
46         249, 252, 255, 258, 261, 264, 267, 270, 273, 276, 279, 282, 285, 288, 291, 294,
47         297, 300, 303, 306, 309, 312, 315, 318, 321, 324, 327, 330, 333, 336, 339, 342,
48         345, 348, 351, 354, 357, 360, 363, 366, 369, 372, 375, 378, 381, 384, 387, 390,
49         393, 396, 399, 402, 405, 408, 411, 414, 417, 420, 423, 426, 429, 432, 435, 438,
50         441, 444, 447, 450, 453, 456, 459, 462, 465, 468, 471, 474, 477, 480, 483, 486,
51         489, 492, 495, 498, 501, 504, 507, 510, 513, 516, 519, 522, 525, 528, 531, 534,
52         537, 540, 543, 546, 549, 552, 555, 558, 561, 564, 592, 595, 598, 601, 604, 607,
53         610, 613, 616, 619, 622, 625, 628, 631, 634, 637, 640, 643, 646, 649, 652, 655,
54         658, 661, 664, 667, 670, 673, 676, 679, 682, 685, 688, 691, 694, 697, 700, 703,
55         710, 713, 716, 719, 736, 739, 750, 890, 902, 905, 908, 911, 914, 917, 920, 923,
56         926, 929, 932, 935, 938, 941, 944, 947, 950, 953, 956, 959, 962, 965, 968, 971,
57         974, 977, 980, 983, 986, 989, 992, 995, 998, 1001, 1004, 1007, 1010, 1013, 1016, 1019,
58         1024, 1027, 1060, 1093, 1126, 1162, 1195, 1228, 1261, 1294, 1329, 1362, 1395, 1488, 1521, 1569,
59         1602, 1635, 1668, 1701, 1734, 1774, 1808, 1869, 1920, 1953, 2308, 2341, 2384, 2437, 2470, 2524,
60         2565, 2598, 2649, 2693, 2726, 2768, 2821, 2854, 2908, 2947, 2980, 3047, 3080, 3114, 3168, 3205,
61         3238, 3294, 3333, 3366, 3424, 3461, 3494, 3585, 3618, 3651, 3713, 3746, 3779, 3840, 3873, 3906,
62         3939, 3976, 4096, 4129, 4162, 4256, 4289, 4322, 4355, 4388, 4421, 4454, 4487, 4520, 4553, 4586,
63         4619, 4652, 4685, 4718, 4752, 4786, 4819, 4852, 4885, 4918, 4951, 5024, 5057, 5090, 5123, 5156,
64         5189, 5222, 5255, 5288, 5321, 5354, 5387, 5420, 5453, 5486, 5519, 5552, 5585, 5618, 5651, 5684,
65         5717, 5750, 5783, 5816, 5849, 5888, 5921, 5954, 5987, 6020, 6053, 6103, 6158, 6191, 6224, 6257,
66         6290, 6400, 6470, 6503, 7424, 7457, 7490, 7523, 7680, 7713, 7746, 7779, 7812, 7845, 7878, 7911,
67         7944, 7977, 8010, 8043, 8076, 8109, 8144, 8178, 8232, 8287, 8450, 8484, 8517, 12288, 12337, 12370,
68         12403, 12436, 12469, 12502, 12535, 12568, 12601, 12634, 12667, 12704, 12784, 13312, 13345, 13378, 13411, 13444,
69         13477, 13510, 13543, 13576, 13609, 13642, 13675, 13708, 13741, 13774, 13807, 13840, 13873, 13906, 13939, 13972,
70         14005, 14038, 14071, 14104, 14137, 14170, 14203, 14236, 14269, 14302, 14335, 14368, 14401, 14434, 14467, 14500,
71         14533, 14566, 14599, 14632, 14665, 14698, 14731, 14764, 14797, 14830, 14863, 14896, 14929, 14962, 14995, 15028,
72         15061, 15094, 15127, 15160, 15193, 15226, 15259, 15292, 15325, 15358, 15391, 15424, 15457, 15490, 15523, 15556,
73         15589, 15622, 15655, 15688, 15721, 15754, 15787, 15820, 15853, 15886, 15919, 15952, 15985, 16018, 16051, 16084,
74         16117, 16150, 16183, 16216, 16249, 16282, 16315, 16348, 16381, 16414, 16447, 16480, 16513, 16546, 16579, 16612,
75         16645, 16678, 16711, 16744, 16777, 16810, 16843, 16876, 16909, 16942, 16975, 17008, 17041, 17074, 17107, 17140,
76         17173, 17206, 17239, 17272, 17305, 17338, 17371, 17404, 17437, 17470, 17503, 17536, 17569, 17602, 17635, 17668,
77         17701, 17734, 17767, 17800, 17833, 17866, 17899, 17932, 17965, 17998, 18031, 18064, 18097, 18130, 18163, 18196,
78         18229, 18262, 18295, 18328, 18361, 18394, 18427, 18460, 18493, 18526, 18559, 18592, 18625, 18658, 18691, 18724,
79         18757, 18790, 18823, 18856, 18889, 18922, 18955, 18988, 19021, 19054, 19087, 19120, 19153, 19186, 19219, 19252,
80         19285, 19318, 19351, 19384, 19417, 19450, 19483, 19516, 19549, 19582, 19615, 19648, 19681, 19714, 19747, 19780,
81         19813, 19846, 19879, 19968, 20001, 20034, 20067, 20100, 20133, 20166, 20199, 20232, 20265, 20298, 20331, 20364,
82         20397, 20430, 20463, 20496, 20529, 20562, 20595, 20628, 20661, 20694, 20727, 20760, 20793, 20826, 20859, 20892,
83         20925, 20958, 20991, 21024, 21057, 21090, 21123, 21156, 21189, 21222, 21255, 21288, 21321, 21354, 21387, 21420,
84         21453, 21486, 21519, 21552, 21585, 21618, 21651, 21684, 21717, 21750, 21783, 21816, 21849, 21882, 21915, 21948,
85         21981, 22014, 22047, 22080, 22113, 22146, 22179, 22212, 22245, 22278, 22311, 22344, 22377, 22410, 22443, 22476,
86         22509, 22542, 22575, 22608, 22641, 22674, 22707, 22740, 22773, 22806, 22839, 22872, 22905, 22938, 22971, 23004,
87         23037, 23070, 23103, 23136, 23169, 23202, 23235, 23268, 23301, 23334, 23367, 23400, 23433, 23466, 23499, 23532,
88         23565, 23598, 23631, 23664, 23697, 23730, 23763, 23796, 23829, 23862, 23895, 23928, 23961, 23994, 24027, 24060,
89         24093, 24126, 24159, 24192, 24225, 24258, 24291, 24324, 24357, 24390, 24423, 24456, 24489, 24522, 24555, 24588,
90         24621, 24654, 24687, 24720, 24753, 24786, 24819, 24852, 24885, 24918, 24951, 24984, 25017, 25050, 25083, 25116,
91         25149, 25182, 25215, 25248, 25281, 25314, 25347, 25380, 25413, 25446, 25479, 25512, 25545, 25578, 25611, 25644,
92         25677, 25710, 25743, 25776, 25809, 25842, 25875, 25908, 25941, 25974, 26007, 26040, 26073, 26106, 26139, 26172,
93         26205, 26238, 26271, 26304, 26337, 26370, 26403, 26436, 26469, 26502, 26535, 26568, 26601, 26634, 26667, 26700,
94         26733, 26766, 26799, 26832, 26865, 26898, 26931, 26964, 26997, 27030, 27063, 27096, 27129, 27162, 27195, 27228,
95         27261, 27294, 27327, 27360, 27393, 27426, 27459, 27492, 27525, 27558, 27591, 27624, 27657, 27690, 27723, 27756,
96         27789, 27822, 27855, 27888, 27921, 27954, 27987, 28020, 28053, 28086, 28119, 28152, 28185, 28218, 28251, 28284,
97         28317, 28350, 28383, 28416, 28449, 28482, 28515, 28548, 28581, 28614, 28647, 28680, 28713, 28746, 28779, 28812,
98         28845, 28878, 28911, 28944, 28977, 29010, 29043, 29076, 29109, 29142, 29175, 29208, 29241, 29274, 29307, 29340,
99         29373, 29406, 29439, 29472, 29505, 29538, 29571, 29604, 29637, 29670, 29703, 29736, 29769, 29802, 29835, 29868,
100         29901, 29934, 29967, 30000, 30033, 30066, 30099, 30132, 30165, 30198, 30231, 30264, 30297, 30330, 30363, 30396,
101         30429, 30462, 30495, 30528, 30561, 30594, 30627, 30660, 30693, 30726, 30759, 30792, 30825, 30858, 30891, 30924,
102         30957, 30990, 31023, 31056, 31089, 31122, 31155, 31188, 31221, 31254, 31287, 31320, 31353, 31386, 31419, 31452,
103         31485, 31518, 31551, 31584, 31617, 31650, 31683, 31716, 31749, 31782, 31815, 31848, 31881, 31914, 31947, 31980,
104         32013, 32046, 32079, 32112, 32145, 32178, 32211, 32244, 32277, 32310, 32343, 32376, 32409, 32442, 32475, 32508,
105         32541, 32574, 32607, 32640, 32673, 32706, 32739
106     };
107     static final char[] chars = Charset_AbstractTest.theseChars(codes);
108
109     static abstract class CodesGenerator {
110         int row = 0, col = 0;
111         abstract void consume (int code);
112
113         boolean isAccepted (int code)
114         {
115             return Character.isLetterOrDigit(code);
116         }
117     }
118
119     static class CodesGenerator1 extends CodesGenerator {
120         @Override
121         void consume (int code) {
122             System.out.print(code);
123             System.out.print(", ");
124             col++;
125             if (col == 16) {
126                 System.out.println();
127                 row++;
128                 col = 0;
129             }
130         }
131
132         @Override
133         boolean isAccepted (int code)
134         {
135             return Character.isLetterOrDigit(code) || Character.isWhitespace(code);
136         }
137     }
138
139     static class CodesGenerator2 extends CodesGenerator {
140         @Override
141         void consume (int code) {
142 //            System.out.print(code);
143 //            System.out.print(", ");
144             System.out.print((char) code);
145             col++;
146             if (col == 80) {
147                 System.out.println();
148                 row++;
149                 col = 0;
150             }
151         }
152     }
153
154     static class CodesGenerator3 extends CodesGenerator {
155         char[] buf = new char[8];
156
157         @Override
158         void consume (int code) {
159             buf[col] = (char) code;
160             System.out.print(code);
161             System.out.print(", ");
162             col++;
163             if (col == 8) {
164                 System.out.print(" // ");
165                 System.out.println(buf);
166                 row++;
167                 col = 0;
168             }
169         }
170
171         @Override
172         boolean isAccepted (int code)
173         {
174             return Character.isLetterOrDigit(code);
175 //                    || Character.isWhitespace(code);
176         }
177     }
178
179     static void genCodes () {
180         CodesGenerator gen = new CodesGenerator1();
181         int code = 0;
182         while (code < (1 << 10)) {
183             while (!gen.isAccepted(code)) code ++;
184             gen.consume(code);
185             code += 3;
186         }
187         while (code < (1 << 15)) {
188             while (!gen.isAccepted(code)) code ++;
189             gen.consume(code);
190             code += 33;
191         }
192     }
193
194
195     static abstract class Dumper {
196         int row = 0, col = 0;
197         abstract void consume (int code);
198     }
199
200     static class Dumper1 extends Dumper {
201         int colMax;
202
203         Dumper1 () {
204             colMax = 16;
205         }
206
207         Dumper1 (int colums) {
208             colMax = colums;
209         }
210
211         @Override
212         void consume (int code) {
213             System.out.print(code);
214             System.out.print(", ");
215             col++;
216             if (col == colMax) {
217                 System.out.println();
218                 row++;
219                 col = 0;
220             }
221         }
222     }
223
224     static void genEncoded (Charset charset, CharBuffer cb) {
225         System.out.println(charset.name());
226         Dumper out = new Dumper1();
227         CharsetEncoder encoder = charset.newEncoder();
228         encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
229         try {
230             ByteBuffer bb = encoder.encode(cb);
231 //            bb.rewind();
232             while (bb.hasRemaining()) {
233                 out.consume(bb.get());
234             }
235         } catch (CharacterCodingException e) {
236             System.out.println(e);
237 //                e.printStackTrace();
238         }
239     }
240
241     static void genDecoded (Charset charset, ByteBuffer bb) {
242         CharsetDecoder decoder = charset.newDecoder();
243 //        System.out.println(code);
244 //        bytes[0] = (byte) code;
245 //        System.out.println(bytes[0]);
246 //        ByteBuffer inputBB = ByteBuffer.wrap(bytes);
247 //        CharBuffer outputCB;
248 //        try {
249 //            outputCB = decoder.decode(inputBB);
250 //            outputCB.rewind();
251 //            System.out.println(outputCB);
252 //        } catch (CharacterCodingException e) {
253 //            System.out.println(e);
254 ////                e.printStackTrace();
255 //        }
256     }
257
258
259     public static void main(String[] args) {
260 //        charset = Charset.defaultCharset();
261 //        decoder = charset.newDecoder();
262 //        System.out.println(charset.name());
263         genEncoded(Charset.forName("MacRoman"), CharBuffer.wrap(chars));
264 //        genEncoded(Charset.forName(charsetNames[37]), CharBuffer.wrap(chars));
265     }
266
267 }