1
/* Test case for bug 317216
3
* Uses nsIConverterInputStream to decode UTF-16 text with valid surrogate
4
* pairs and lone surrogate characters
6
* Sample text is: "A" in Mathematical Bold Capitals (U+1D400)
8
* The test uses buffers of 4 different lengths to test end of buffer in mid-
9
* UTF16 character and mid-surrogate pair
13
// 0: Valid surrogate pair
14
["%00%2D%00%2D%D8%35%DC%20%00%2D%00%2D",
15
// expected: surrogate pair
17
// 1: Lone high surrogate
18
["%00%2D%00%2D%D8%35%00%2D%00%2D",
19
// expected: one replacement char
21
// 2: Lone low surrogate
22
["%00%2D%00%2D%DC%20%00%2D%00%2D",
23
// expected: one replacement char
25
// 3: Two high surrogates
26
["%00%2D%00%2D%D8%35%D8%35%00%2D%00%2D",
27
// expected: two replacement chars
29
// 4: Two low surrogates
30
["%00%2D%00%2D%DC%20%DC%20%00%2D%00%2D",
31
// expected: two replacement chars
33
// 5: Low surrogate followed by high surrogate
34
["%00%2D%00%2D%DC%20%D8%35%00%2D%00%2D",
35
// expected: two replacement chars
37
// 6: Lone high surrogate followed by valid surrogate pair
38
["%00%2D%00%2D%D8%35%D8%35%DC%20%00%2D%00%2D",
39
// expected: replacement char followed by surrogate pair
40
"--\uFFFD\uD835\uDC20--"],
41
// 7: Lone low surrogate followed by valid surrogate pair
42
["%00%2D%00%2D%DC%20%D8%35%DC%20%00%2D%00%2D",
43
// expected: replacement char followed by surrogate pair
44
"--\uFFFD\uD835\uDC20--"],
45
// 8: Valid surrogate pair followed by lone high surrogate
46
["%00%2D%00%2D%D8%35%DC%20%D8%35%00%2D%00%2D",
47
// expected: surrogate pair followed by replacement char
48
"--\uD835\uDC20\uFFFD--"],
49
// 9: Valid surrogate pair followed by lone low surrogate
50
["%00%2D%00%2D%D8%35%DC%20%DC%20%00%2D%00%2D",
51
// expected: surrogate pair followed by replacement char
52
"--\uD835\uDC20\uFFFD--"],
53
// 10: Lone high surrogate at the end of the input
54
["%00%2D%00%2D%00%2D%00%2D%D8%35%",
57
// 11: Half code unit at the end of the input
58
["%00%2D%00%2D%00%2D%00%2D%D8",
62
const IOService = Components.Constructor("@mozilla.org/network/io-service;1",
64
const ConverterInputStream =
65
Components.Constructor("@mozilla.org/intl/converter-input-stream;1",
66
"nsIConverterInputStream",
68
const ios = new IOService();
70
function testCase(testText, expectedText, bufferLength, charset)
72
var dataURI = "data:text/plain;charset=" + charset + "," + testText;
74
var channel = ios.newChannel(dataURI, "", null);
75
var testInputStream = channel.open();
76
var testConverter = new ConverterInputStream(testInputStream,
81
if (!(testConverter instanceof
82
Components.interfaces.nsIUnicharLineInputStream))
83
throw "not line input stream";
88
// read the line and check for eof
90
more = testConverter.readLine(line);
94
// escape the strings before comparing for better readability
95
do_check_eq(escape(outStr), escape(expectedText));
98
// Byte-swap %-encoded utf-16
99
function flip(str) { return str.replace(/(%..)(%..)/g, "$2$1"); }
103
for (var i = 0; i < 12; ++i) {
104
for (var bufferLength = 4; bufferLength < 8; ++ bufferLength) {
105
testCase(test[i][0], test[i][1], bufferLength, "UTF-16BE");
106
testCase(flip(test[i][0]), test[i][1], bufferLength, "UTF-16LE");