intl/uconv/tests/unit/test_bug317216.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/uconv/tests/unit/test_bug317216.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,134 @@
     1.4 +/* Test case for bug 317216
     1.5 + *
     1.6 + * Uses nsIConverterInputStream to decode UTF-16 text with valid surrogate
     1.7 + * pairs and lone surrogate characters
     1.8 + *
     1.9 + * Sample text is: "A" in Mathematical Bold Capitals (U+1D400)
    1.10 + *
    1.11 + * The test uses buffers of 4 different lengths to test end of buffer in mid-
    1.12 + * UTF16 character and mid-surrogate pair
    1.13 + */
    1.14 +
    1.15 +const test = [
    1.16 +// 0: Valid surrogate pair
    1.17 +              ["%D8%35%DC%20%00%2D%00%2D",
    1.18 +//    expected: surrogate pair
    1.19 +               "\uD835\uDC20--"],
    1.20 +// 1: Lone high surrogate
    1.21 +              ["%D8%35%00%2D%00%2D",
    1.22 +//    expected: one replacement char
    1.23 +               "\uFFFD--"],
    1.24 +// 2: Lone low surrogate
    1.25 +              ["%DC%20%00%2D%00%2D",
    1.26 +//    expected: one replacement char
    1.27 +               "\uFFFD--"],
    1.28 +// 3: Two high surrogates
    1.29 +              ["%D8%35%D8%35%00%2D%00%2D",
    1.30 +//    expected: two replacement chars
    1.31 +               "\uFFFD\uFFFD--"],
    1.32 +// 4: Two low surrogates
    1.33 +              ["%DC%20%DC%20%00%2D%00%2D",
    1.34 +//    expected: two replacement chars
    1.35 +	       "\uFFFD\uFFFD--"],
    1.36 +// 5: Low surrogate followed by high surrogate
    1.37 +              ["%DC%20%D8%35%00%2D%00%2D",
    1.38 +//    expected: two replacement chars
    1.39 +               "\uFFFD\uFFFD--"],
    1.40 +// 6: Lone high surrogate followed by valid surrogate pair
    1.41 +              ["%D8%35%D8%35%DC%20%00%2D%00%2D",
    1.42 +//    expected: replacement char followed by surrogate pair
    1.43 +               "\uFFFD\uD835\uDC20--"],
    1.44 +// 7: Lone low surrogate followed by valid surrogate pair
    1.45 +              ["%DC%20%D8%35%DC%20%00%2D%00%2D",
    1.46 +//    expected: replacement char followed by surrogate pair
    1.47 +               "\uFFFD\uD835\uDC20--"],
    1.48 +// 8: Valid surrogate pair followed by lone high surrogate
    1.49 +              ["%D8%35%DC%20%D8%35%00%2D%00%2D",
    1.50 +//    expected: surrogate pair followed by replacement char
    1.51 +               "\uD835\uDC20\uFFFD--"],
    1.52 +// 9: Valid surrogate pair followed by lone low surrogate
    1.53 +              ["%D8%35%DC%20%DC%20%00%2D%00%2D",
    1.54 +//    expected: surrogate pair followed by replacement char
    1.55 +               "\uD835\uDC20\uFFFD--"],
    1.56 +// 10: Lone high surrogate at the end of the input
    1.57 +              ["%D8%35%",
    1.58 +//    expected: nothing
    1.59 +               ""],
    1.60 +// 11: Half code unit at the end of the input
    1.61 +              ["%D8",
    1.62 +//    expected: nothing
    1.63 +              ""]];
    1.64 +
    1.65 +const IOService = Components.Constructor("@mozilla.org/network/io-service;1",
    1.66 +                                         "nsIIOService");
    1.67 +const ConverterInputStream =
    1.68 +      Components.Constructor("@mozilla.org/intl/converter-input-stream;1",
    1.69 +                             "nsIConverterInputStream",
    1.70 +                             "init");
    1.71 +const ios = new IOService();
    1.72 +
    1.73 +function testCase(testText, expectedText, bufferLength, charset)
    1.74 +{
    1.75 +  var dataURI = "data:text/plain;charset=" + charset + "," + testText;
    1.76 +
    1.77 +  var channel = ios.newChannel(dataURI, "", null);
    1.78 +  var testInputStream = channel.open();
    1.79 +  var testConverter = new ConverterInputStream(testInputStream,
    1.80 +                                               charset,
    1.81 +                                               bufferLength,
    1.82 +                                               0xFFFD);
    1.83 +
    1.84 +  if (!(testConverter instanceof
    1.85 +        Components.interfaces.nsIUnicharLineInputStream))
    1.86 +    throw "not line input stream";
    1.87 +
    1.88 +  var outStr = "";
    1.89 +  var more;
    1.90 +  do {
    1.91 +    // read the line and check for eof
    1.92 +    var line = {};
    1.93 +    more = testConverter.readLine(line);
    1.94 +    outStr += line.value;
    1.95 +  } while (more);
    1.96 +
    1.97 +  // escape the strings before comparing for better readability
    1.98 +  do_check_eq(escape(outStr), escape(expectedText));
    1.99 +}
   1.100 +
   1.101 +// Add 32 dummy characters to the test text to work around the minimum buffer
   1.102 +// size of an ns*Buffer
   1.103 +const MINIMUM_BUFFER_SIZE=32;
   1.104 +function padBytes(str)
   1.105 +{
   1.106 +  var padding = "";
   1.107 +  for (var i = 0; i < MINIMUM_BUFFER_SIZE; ++i) {
   1.108 +    padding += "%00%2D";
   1.109 +  }
   1.110 +  return padding + str;
   1.111 +}
   1.112 +
   1.113 +function padUnichars(str)
   1.114 +{
   1.115 +  var padding = "";
   1.116 +  for (var i = 0; i < MINIMUM_BUFFER_SIZE; ++i) {
   1.117 +    padding += "-";
   1.118 +  }
   1.119 +  return padding + str;
   1.120 +}
   1.121 +
   1.122 +// Byte-swap %-encoded utf-16
   1.123 +function flip(str) { return str.replace(/(%..)(%..)/g, "$2$1"); }
   1.124 +
   1.125 +function run_test()
   1.126 +{
   1.127 +  for (var i = 0; i < 12; ++i) {
   1.128 +    for (var bufferLength = MINIMUM_BUFFER_SIZE;
   1.129 +	 bufferLength < MINIMUM_BUFFER_SIZE + 4;
   1.130 +	 ++ bufferLength) {
   1.131 +      var testText = padBytes(test[i][0]);
   1.132 +      var expectedText = padUnichars(test[i][1]);
   1.133 +      testCase(testText, expectedText, bufferLength, "UTF-16BE");
   1.134 +      testCase(flip(testText), expectedText, bufferLength, "UTF-16LE");
   1.135 +    }
   1.136 +  }
   1.137 +}

mercurial