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 |
uint8_t utf16be[] = { |
85 |
0, 'e', |
86 |
0, 0xe8, |
87 |
0, 0xe9, |
88 |
0, 0xea, |
89 |
}; |
90 |
|
91 |
result = jscoverage_bytes_to_characters("UTF-16BE", utf16be, 8, &characters, &num_characters); |
92 |
|
93 |
assert(result == 0); |
94 |
assert(num_characters == 4); |
95 |
assert(characters[0] == 'e'); |
96 |
assert(characters[1] == 0xe8); |
97 |
assert(characters[2] == 0xe9); |
98 |
assert(characters[3] == 0xea); |
99 |
|
100 |
free(characters); |
101 |
|
102 |
uint8_t utf16be_with_bom[] = { |
103 |
0xfe, 0xff, |
104 |
0, 'e', |
105 |
0, 0xe8, |
106 |
0, 0xe9, |
107 |
0, 0xea, |
108 |
}; |
109 |
|
110 |
result = jscoverage_bytes_to_characters("UTF-16BE", utf16be_with_bom, 10, &characters, &num_characters); |
111 |
|
112 |
assert(result == 0); |
113 |
assert(num_characters == 4); |
114 |
assert(characters[0] == 'e'); |
115 |
assert(characters[1] == 0xe8); |
116 |
assert(characters[2] == 0xe9); |
117 |
assert(characters[3] == 0xea); |
118 |
|
119 |
free(characters); |
120 |
|
121 |
uint8_t utf16le[] = { |
122 |
'e', 0, |
123 |
0xe8, 0, |
124 |
0xe9, 0, |
125 |
0xea, 0, |
126 |
}; |
127 |
|
128 |
result = jscoverage_bytes_to_characters("UTF-16LE", utf16le, 8, &characters, &num_characters); |
129 |
|
130 |
assert(result == 0); |
131 |
assert(num_characters == 4); |
132 |
assert(characters[0] == 'e'); |
133 |
assert(characters[1] == 0xe8); |
134 |
assert(characters[2] == 0xe9); |
135 |
assert(characters[3] == 0xea); |
136 |
|
137 |
free(characters); |
138 |
|
139 |
uint8_t utf16le_with_bom[] = { |
140 |
0xff, 0xfe, |
141 |
'e', 0, |
142 |
0xe8, 0, |
143 |
0xe9, 0, |
144 |
0xea, 0, |
145 |
}; |
146 |
|
147 |
result = jscoverage_bytes_to_characters("UTF-16LE", utf16le_with_bom, 10, &characters, &num_characters); |
148 |
|
149 |
assert(result == 0); |
150 |
assert(num_characters == 4); |
151 |
assert(characters[0] == 'e'); |
152 |
assert(characters[1] == 0xe8); |
153 |
assert(characters[2] == 0xe9); |
154 |
assert(characters[3] == 0xea); |
155 |
|
156 |
free(characters); |
157 |
|
158 |
/* bogus encoding */ |
159 |
uint8_t bogus[] = {'b', 'o', 'g', 'u', 's'}; |
160 |
|
161 |
result = jscoverage_bytes_to_characters("BOGUS", bogus, 5, &characters, &num_characters); |
162 |
|
163 |
assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED); |
164 |
|
165 |
/* malformed */ |
166 |
uint8_t malformed_ascii[] = { |
167 |
'e', |
168 |
0xe8, |
169 |
0xe9, |
170 |
0xea, |
171 |
}; |
172 |
|
173 |
result = jscoverage_bytes_to_characters("US-ASCII", malformed_ascii, 4, &characters, &num_characters); |
174 |
|
175 |
assert(result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE); |
176 |
|
177 |
return 0; |
178 |
} |