1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/*
* test_TextEncoder.js
* bug 764234 tests
*/
function runTextEncoderTests()
{
test(testEncoderEncode, "testEncoderEncode");
test(testEncoderGetEncoding, "testEncoderGetEncoding");
test(testInvalidSequence, "testInvalidSequence");
test(testInputString, "testInputString");
test(testStreamingOptions, "testStreamingOptions");
}
function testEncoderEncode()
{
var data = "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07\u0e08\u0e09"
+ "\u0e0a\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f\u0e10\u0e11\u0e12\u0e13\u0e14"
+ "\u0e15\u0e16\u0e17\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d\u0e1e\u0e1f"
+ "\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25\u0e26\u0e27\u0e28\u0e29\u0e2a"
+ "\u0e2b\u0e2c\u0e2d\u0e2e\u0e2f\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35"
+ "\u0e36\u0e37\u0e38\u0e39\u0e3a\u0e3f\u0e40\u0e41\u0e42\u0e43\u0e44"
+ "\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b\u0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 0xB4, 0xE0, 0xB8, 0xB5, 0xE0,
0xB8, 0xB6, 0xE0, 0xB8, 0xB7, 0xE0, 0xB8, 0xB8, 0xE0,
0xB8, 0xB9, 0xE0, 0xB8, 0xBA, 0xE0, 0xB8, 0xBF, 0xE0,
0xB9, 0x80, 0xE0, 0xB9, 0x81, 0xE0, 0xB9, 0x82, 0xE0,
0xB9, 0x83, 0xE0, 0xB9, 0x84, 0xE0, 0xB9, 0x85, 0xE0,
0xB9, 0x86, 0xE0, 0xB9, 0x87, 0xE0, 0xB9, 0x88, 0xE0,
0xB9, 0x89, 0xE0, 0xB9, 0x8A, 0xE0, 0xB9, 0x8B, 0xE0,
0xB9, 0x8C, 0xE0, 0xB9, 0x8D, 0xE0, 0xB9, 0x8E, 0xE0,
0xB9, 0x8F, 0xE0, 0xB9, 0x90, 0xE0, 0xB9, 0x91, 0xE0,
0xB9, 0x92, 0xE0, 0xB9, 0x93, 0xE0, 0xB9, 0x94, 0xE0,
0xB9, 0x95, 0xE0, 0xB9, 0x96, 0xE0, 0xB9, 0x97, 0xE0,
0xB9, 0x98, 0xE0, 0xB9, 0x99, 0xE0, 0xB9, 0x9A, 0xE0,
0xB9, 0x9B];
// valid encoding passed
testSingleString({input: data, expected: expectedString,
msg: "testing encoding with valid utf-8 encoding."});
}
function testInvalidSequence()
{
var data = "\u0e43\u0e44\ufffd\u0e45";
var expectedString = [0xE0, 0xB9, 0x83, 0xE0, 0xB9, 0x84, 0xEF, 0xBF, 0xBD,
0xE0, 0xB9, 0x85];
//Test null input string
testSingleString({input: data, expected: expectedString,
msg: "encoder with replacement character test."});
}
function testInputString()
{
//Test null input string
testSingleString({input: "", expected: [],
msg: "encoder null input string test."});
//Test spaces as input string
testSingleString({input: " ", expected: [32, 32],
msg: "spaces as input string."});
}
function testSingleString(test)
{
var outText;
try {
var stream = test.stream ? {stream: true} : null;
outText = (new TextEncoder()).encode(test.input, stream);
} catch (e) {
assert_equals(e.name, test.error, test.msg + " error thrown from the constructor.");
if (test.errorMessage) {
assert_equals(e.message, test.errorMessage, test.msg + " error thrown from the constructor.");
}
return;
}
assert_true(!test.error, test.msg);
if (outText.length !== test.expected.length) {
assert_equals(outText.length, test.expected.length, test.msg + " length mismatch");
return;
}
for (var i = 0; i < outText.length; i++) {
if (outText[i] != test.expected[i]) {
assert_equals(escape(stringFromArray(outText.buffer)), escape(stringFromArray(test.expected)),
test.msg + " Bytes do not match expected bytes.");
return;
}
}
}
function stringFromArray(a) {
return Array.map.call(a, function(v){return String.fromCharCode(v)}).join('');
}
function testStreamingOptions()
{
var data = [
"\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07\u0e08\u0e09\u0e0a"
+ "\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f\u0e10\u0e11\u0e12\u0e13\u0e14"
+ "\u0e15\u0e16\u0e17\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d",
"\u0e1e\u0e1f\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25\u0e26\u0e27\u0e28"
+ "\u0e29\u0e2a\u0e2b\u0e2c\u0e2d\u0e2e\u0e2f\u0e30\u0e31\u0e32"
+ "\u0e33\u0e34\u0e35\u0e36\u0e37\u0e38\u0e39\u0e3a\u0e3f\u0e40"
+ "\u0e41\u0e42",
"\u0e43\u0e44\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b\u0e4c\u0e4d"
+ "\u0e4e\u0e4f\u0e50\u0e51\u0e52\u0e53\u0e54\u0e55\u0e56\u0e57"
+ "\u0e58\u0e59\u0e5a\u0e5b"
];
var expected = [[0xC2, 0xA0, 0xE0, 0xB8, 0x81, 0xE0, 0xB8, 0x82, 0xE0,
0xB8, 0x83, 0xE0, 0xB8, 0x84, 0xE0, 0xB8, 0x85, 0xE0,
0xB8, 0x86, 0xE0, 0xB8, 0x87, 0xE0, 0xB8, 0x88, 0xE0,
0xB8, 0x89, 0xE0, 0xB8, 0x8A, 0xE0, 0xB8, 0x8B, 0xE0,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0, 0xB9, 0x8A, 0xE0, 0xB9, 0x8B,
0xE0, 0xB9, 0x8C, 0xE0, 0xB9, 0x8D, 0xE0, 0xB9, 0x8E,
0xE0, 0xB9, 0x8F, 0xE0, 0xB9, 0x90, 0xE0, 0xB9, 0x91,
0xE0, 0xB9, 0x92, 0xE0, 0xB9, 0x93, 0xE0, 0xB9, 0x94,
0xE0, 0xB9, 0x95, 0xE0, 0xB9, 0x96, 0xE0, 0xB9, 0x97,
0xE0, 0xB9, 0x98, 0xE0, 0xB9, 0x99, 0xE0, 0xB9, 0x9A,
0xE0, 0xB9, 0x9B]];
// STREAMING TEST ONE: test streaming three valid strings with stream option
// set to true for all three.
testArrayOfStrings({array: [
{input: data[0], stream: true, expected: expected[0]},
{input: data[1], stream: true, expected: expected[1]},
{input: data[2], stream: true, expected: expected[2]},
], msg: "streaming test one."});
// STREAMING TEST TWO: test streaming valid strings with stream option
// streaming option: false from constructor, string 1 stream: true,
// string 2 stream: false, string 3 stream: false
testArrayOfStrings({array: [
{input: data[0], stream: true, expected: expected[0]},
{input: data[1], expected: expected[1]},
{input: data[2], expected: expected[2]},
], msg: "streaming test two."});
}
function arrayFromString(s) {
return s.split('').map(function(c){return String.charCodeAt(c)});
}
function testArrayOfStrings(test)
{
var encoder;
try {
encoder = new TextEncoder();
} catch (e) {
assert_equals(e.name, test.error, test.msg);
return;
}
assert_true(!test.error, test.msg);
var array = test.array;
for (var i = 0; i < array.length; i += 1) {
var stream = array[i].stream ? {stream: true} : null;
var view = encoder.encode(array[i].input, stream);
var stringLen = view.length;
var expected = array[i].expected;
for (var j = 0; j < stringLen; j++) {
if (view[j] !== expected[j]) {
assert_equals(view[j], expected[j], msg + " Bytes do not match expected bytes.");
return;
}
}
}
}
function testEncoderGetEncoding()
{
var encoder = new TextEncoder();
assert_equals(encoder.encoding, "utf-8", "TextEncoder encoding test.");
}
|