summaryrefslogtreecommitdiffstats
path: root/intl/uconv/tests/unit/test_input_stream.js
blob: b33fb98356fc45c1d774b07b2be95bc38d9176f9 (plain)
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
var CC = Components.Constructor;
var converter = Cc[
  "@mozilla.org/intl/scriptableunicodeconverter"
].createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";

var SIS = CC(
  "@mozilla.org/scriptableinputstream;1",
  "nsIScriptableInputStream",
  "init"
);

function test_char(code) {
  dump("test_char(0x" + code.toString(16) + ")\n");
  var original = String.fromCharCode(code);
  var nativeStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
    Ci.nsIStringInputStream
  );
  nativeStream.setUTF8Data(original);
  var stream = new SIS(nativeStream);
  var utf8Result = stream.read(stream.available());
  stream.close();
  var result = converter.ConvertToUnicode(utf8Result);
  Assert.equal(escape(original), escape(result));
}

function run_test() {
  // This is not a very comprehensive test.
  for (var i = 0x007f - 2; i <= 0x007f; i++) {
    test_char(i);
  }
  for (i = 0x07ff - 2; i <= 0x07ff; i++) {
    test_char(i);
  }
  for (i = 0x1000 - 2; i <= 0x1000 + 2; i++) {
    test_char(i);
  }
  for (i = 0xe000; i <= 0xe000 + 2; i++) {
    test_char(i);
  }
}