1 |
/* |
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 |
#include <assert.h> |
21 |
#include <string.h> |
22 |
|
23 |
#include "encoding.h" |
24 |
#include "stream.h" |
25 |
|
26 |
int main(void) { |
27 |
jschar * characters; |
28 |
size_t num_characters; |
29 |
int result; |
30 |
|
31 |
/* e, e grave, e acute, e circumflex */ |
32 |
uint8_t utf8[] = { |
33 |
'e', |
34 |
0xc3, |
35 |
0xa8, |
36 |
0xc3, |
37 |
0xa9, |
38 |
0xc3, |
39 |
0xaa, |
40 |
}; |
41 |
|
42 |
result = jscoverage_bytes_to_characters("UTF-8", utf8, 7, &characters, &num_characters); |
43 |
|
44 |
assert(result == 0); |
45 |
assert(num_characters == 4); |
46 |
assert(characters[0] == 'e'); |
47 |
assert(characters[1] == 0xe8); |
48 |
assert(characters[2] == 0xe9); |
49 |
assert(characters[3] == 0xea); |
50 |
|
51 |
free(characters); |
52 |
|
53 |
/* |
54 |
BOM is 0xfeff |
55 |
= 1111 1110 1111 1111 |
56 |
UTF: 1110---- 10------ 10------ |
57 |
= 11101111 10111011 10111111 |
58 |
= EF BB BF |
59 |
*/ |
60 |
uint8_t utf8_with_bom[] = { |
61 |
0xef, |
62 |
0xbb, |
63 |
0xbf, |
64 |
'e', |
65 |
0xc3, |
66 |
0xa8, |
67 |
0xc3, |
68 |
0xa9, |
69 |
0xc3, |
70 |
0xaa, |
71 |
}; |
72 |
|
73 |
result = jscoverage_bytes_to_characters("UTF-8", utf8_with_bom, 10, &characters, &num_characters); |
74 |
|
75 |
assert(result == 0); |
76 |
assert(num_characters == 4); |
77 |
assert(characters[0] == 'e'); |
78 |
assert(characters[1] == 0xe8); |
79 |
assert(characters[2] == 0xe9); |
80 |
assert(characters[3] == 0xea); |
81 |
|
82 |
free(characters); |
83 |
|
84 |
#ifndef _WIN32 |
85 |
uint8_t utf16be[] = { |
86 |
0, 'e', |
87 |
0, 0xe8, |
88 |
0, 0xe9, |
89 |
0, 0xea, |
90 |
}; |
91 |
|
92 |
result = jscoverage_bytes_to_characters("UTF-16BE", utf16be, 8, &characters, &num_characters); |
93 |
|
94 |
assert(result == 0); |
95 |
assert(num_characters == 4); |
96 |
assert(characters[0] == 'e'); |
97 |
assert(characters[1] == 0xe8); |
98 |
assert(characters[2] == 0xe9); |
99 |
assert(characters[3] == 0xea); |
100 |
|
101 |
free(characters); |
102 |
|
103 |
uint8_t utf16be_with_bom[] = { |
104 |
0xfe, 0xff, |
105 |
0, 'e', |
106 |
0, 0xe8, |
107 |
0, 0xe9, |
108 |
0, 0xea, |
109 |
}; |
110 |
|
111 |
result = jscoverage_bytes_to_characters("UTF-16BE", utf16be_with_bom, 10, &characters, &num_characters); |
112 |
|
113 |
assert(result == 0); |
114 |
assert(num_characters == 4); |
115 |
assert(characters[0] == 'e'); |
116 |
assert(characters[1] == 0xe8); |
117 |
assert(characters[2] == 0xe9); |
118 |
assert(characters[3] == 0xea); |
119 |
|
120 |
free(characters); |
121 |
|
122 |
uint8_t utf16le[] = { |
123 |
'e', 0, |
124 |
0xe8, 0, |
125 |
0xe9, 0, |
126 |
0xea, 0, |
127 |
}; |
128 |
|
129 |
result = jscoverage_bytes_to_characters("UTF-16LE", utf16le, 8, &characters, &num_characters); |
130 |
|
131 |
assert(result == 0); |
132 |
assert(num_characters == 4); |
133 |
assert(characters[0] == 'e'); |
134 |
assert(characters[1] == 0xe8); |
135 |
assert(characters[2] == 0xe9); |
136 |
assert(characters[3] == 0xea); |
137 |
|
138 |
free(characters); |
139 |
|
140 |
uint8_t utf16le_with_bom[] = { |
141 |
0xff, 0xfe, |
142 |
'e', 0, |
143 |
0xe8, 0, |
144 |
0xe9, 0, |
145 |
0xea, 0, |
146 |
}; |
147 |
|
148 |
result = jscoverage_bytes_to_characters("UTF-16LE", utf16le_with_bom, 10, &characters, &num_characters); |
149 |
|
150 |
assert(result == 0); |
151 |
assert(num_characters == 4); |
152 |
assert(characters[0] == 'e'); |
153 |
assert(characters[1] == 0xe8); |
154 |
assert(characters[2] == 0xe9); |
155 |
assert(characters[3] == 0xea); |
156 |
|
157 |
free(characters); |
158 |
#endif |
159 |
|
160 |
/* bogus encoding */ |
161 |
uint8_t bogus[] = {'b', 'o', 'g', 'u', 's'}; |
162 |
|
163 |
result = jscoverage_bytes_to_characters("BOGUS", bogus, 5, &characters, &num_characters); |
164 |
|
165 |
assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED); |
166 |
|
167 |
#ifndef _WIN32 |
168 |
/* malformed US-ASCII */ |
169 |
/* NOTE: Windows simply discards the high bit */ |
170 |
uint8_t malformed_ascii[] = { |
171 |
'e', |
172 |
0xe8, |
173 |
0xe9, |
174 |
0xea, |
175 |
}; |
176 |
|
177 |
result = jscoverage_bytes_to_characters("US-ASCII", malformed_ascii, 4, &characters, &num_characters); |
178 |
|
179 |
assert(result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE); |
180 |
|
181 |
/* malformed UTF-8 */ |
182 |
/* NOTE: Windows simply decodes as many bytes as it can, then it stops */ |
183 |
uint8_t malformed_utf8[] = { |
184 |
'e', |
185 |
0xe8, |
186 |
0xe9, |
187 |
0xea, |
188 |
}; |
189 |
|
190 |
result = jscoverage_bytes_to_characters("UTF-8", malformed_utf8, 4, &characters, &num_characters); |
191 |
|
192 |
assert(result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE); |
193 |
#endif |
194 |
|
195 |
return 0; |
196 |
} |