/[jscoverage]/trunk/tests/encodings.c
ViewVC logotype

Annotation of /trunk/tests/encodings.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 310 - (hide annotations)
Mon Oct 13 07:19:42 2008 UTC (11 years, 8 months ago) by siliconforks
File MIME type: text/plain
File size: 4524 byte(s)
Test HAVE_ICONV instead of platform.
1 siliconforks 174 /*
2     encodings.c - test handling different character encodings
3     Copyright (C) 2008 siliconforks.com
4    
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9    
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     GNU General Public License for more details.
14    
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18     */
19    
20 siliconforks 310 #include <config.h>
21    
22 siliconforks 174 #include <assert.h>
23     #include <string.h>
24    
25     #include "encoding.h"
26     #include "stream.h"
27    
28     int main(void) {
29     jschar * characters;
30     size_t num_characters;
31     int result;
32    
33     /* e, e grave, e acute, e circumflex */
34     uint8_t utf8[] = {
35     'e',
36     0xc3,
37     0xa8,
38     0xc3,
39     0xa9,
40     0xc3,
41     0xaa,
42     };
43    
44     result = jscoverage_bytes_to_characters("UTF-8", utf8, 7, &characters, &num_characters);
45    
46     assert(result == 0);
47     assert(num_characters == 4);
48     assert(characters[0] == 'e');
49     assert(characters[1] == 0xe8);
50     assert(characters[2] == 0xe9);
51     assert(characters[3] == 0xea);
52    
53     free(characters);
54    
55     /*
56     BOM is 0xfeff
57     = 1111 1110 1111 1111
58 siliconforks 205 UTF: 1110---- 10------ 10------
59 siliconforks 174 = 11101111 10111011 10111111
60     = EF BB BF
61     */
62     uint8_t utf8_with_bom[] = {
63     0xef,
64     0xbb,
65     0xbf,
66     'e',
67     0xc3,
68     0xa8,
69     0xc3,
70     0xa9,
71     0xc3,
72     0xaa,
73     };
74    
75     result = jscoverage_bytes_to_characters("UTF-8", utf8_with_bom, 10, &characters, &num_characters);
76    
77     assert(result == 0);
78     assert(num_characters == 4);
79     assert(characters[0] == 'e');
80     assert(characters[1] == 0xe8);
81     assert(characters[2] == 0xe9);
82     assert(characters[3] == 0xea);
83    
84     free(characters);
85    
86 siliconforks 310 #ifdef HAVE_ICONV
87 siliconforks 221 uint8_t utf16be[] = {
88     0, 'e',
89     0, 0xe8,
90     0, 0xe9,
91     0, 0xea,
92     };
93    
94     result = jscoverage_bytes_to_characters("UTF-16BE", utf16be, 8, &characters, &num_characters);
95    
96     assert(result == 0);
97     assert(num_characters == 4);
98     assert(characters[0] == 'e');
99     assert(characters[1] == 0xe8);
100     assert(characters[2] == 0xe9);
101     assert(characters[3] == 0xea);
102    
103     free(characters);
104    
105     uint8_t utf16be_with_bom[] = {
106     0xfe, 0xff,
107     0, 'e',
108     0, 0xe8,
109     0, 0xe9,
110     0, 0xea,
111     };
112    
113     result = jscoverage_bytes_to_characters("UTF-16BE", utf16be_with_bom, 10, &characters, &num_characters);
114    
115     assert(result == 0);
116     assert(num_characters == 4);
117     assert(characters[0] == 'e');
118     assert(characters[1] == 0xe8);
119     assert(characters[2] == 0xe9);
120     assert(characters[3] == 0xea);
121    
122     free(characters);
123    
124     uint8_t utf16le[] = {
125     'e', 0,
126     0xe8, 0,
127     0xe9, 0,
128     0xea, 0,
129     };
130    
131     result = jscoverage_bytes_to_characters("UTF-16LE", utf16le, 8, &characters, &num_characters);
132    
133     assert(result == 0);
134     assert(num_characters == 4);
135     assert(characters[0] == 'e');
136     assert(characters[1] == 0xe8);
137     assert(characters[2] == 0xe9);
138     assert(characters[3] == 0xea);
139    
140     free(characters);
141    
142     uint8_t utf16le_with_bom[] = {
143     0xff, 0xfe,
144     'e', 0,
145     0xe8, 0,
146     0xe9, 0,
147     0xea, 0,
148     };
149    
150     result = jscoverage_bytes_to_characters("UTF-16LE", utf16le_with_bom, 10, &characters, &num_characters);
151    
152     assert(result == 0);
153     assert(num_characters == 4);
154     assert(characters[0] == 'e');
155     assert(characters[1] == 0xe8);
156     assert(characters[2] == 0xe9);
157     assert(characters[3] == 0xea);
158    
159     free(characters);
160 siliconforks 302 #endif
161 siliconforks 221
162     /* bogus encoding */
163     uint8_t bogus[] = {'b', 'o', 'g', 'u', 's'};
164    
165     result = jscoverage_bytes_to_characters("BOGUS", bogus, 5, &characters, &num_characters);
166    
167     assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);
168    
169 siliconforks 310 #ifdef HAVE_ICONV
170 siliconforks 301 /* malformed US-ASCII */
171 siliconforks 302 /* NOTE: Windows simply discards the high bit */
172 siliconforks 221 uint8_t malformed_ascii[] = {
173     'e',
174     0xe8,
175     0xe9,
176     0xea,
177     };
178    
179     result = jscoverage_bytes_to_characters("US-ASCII", malformed_ascii, 4, &characters, &num_characters);
180    
181     assert(result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE);
182 siliconforks 309 #endif
183 siliconforks 221
184 siliconforks 301 /* malformed UTF-8 */
185     uint8_t malformed_utf8[] = {
186     'e',
187     0xe8,
188     0xe9,
189     0xea,
190     };
191    
192     result = jscoverage_bytes_to_characters("UTF-8", malformed_utf8, 4, &characters, &num_characters);
193    
194     assert(result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE);
195    
196 siliconforks 174 return 0;
197     }

  ViewVC Help
Powered by ViewVC 1.1.24