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