xpcom/tests/unit/test_streams.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/unit/test_streams.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,152 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const Ci = Components.interfaces;
     1.9 +const Cr = Components.results;
    1.10 +const CC = Components.Constructor;
    1.11 +
    1.12 +var Pipe = CC("@mozilla.org/pipe;1",
    1.13 +              "nsIPipe",
    1.14 +              "init");
    1.15 +var BinaryOutput = CC("@mozilla.org/binaryoutputstream;1",
    1.16 +                      "nsIBinaryOutputStream",
    1.17 +                      "setOutputStream");
    1.18 +var BinaryInput = CC("@mozilla.org/binaryinputstream;1",
    1.19 +                     "nsIBinaryInputStream",
    1.20 +                     "setInputStream");
    1.21 +
    1.22 +/**
    1.23 + * Binary stream tests.
    1.24 + */
    1.25 +function test_binary_streams() {
    1.26 +  var p, is, os;
    1.27 +
    1.28 +  p = new Pipe(false, false, 1024, 1, null);
    1.29 +  is = new BinaryInput(p.inputStream);
    1.30 +  os = new BinaryOutput(p.outputStream);
    1.31 +
    1.32 +  const LargeNum = Math.pow(2, 18) + Math.pow(2, 12) + 1;
    1.33 +  const HugeNum = Math.pow(2, 62);
    1.34 +  const HelloStr = "Hello World";
    1.35 +  const HelloArray = Array.map(HelloStr, function(c) {return c.charCodeAt(0)});
    1.36 +  var countObj = {};
    1.37 +  var msg = {};
    1.38 +
    1.39 +  // Test reading immediately after writing.
    1.40 +  os.writeBoolean(true);
    1.41 +  do_check_eq(is.readBoolean(), true);
    1.42 +  os.write8(4);
    1.43 +  do_check_eq(is.read8(), 4);
    1.44 +  os.write16(4);
    1.45 +  do_check_eq(is.read16(), 4);
    1.46 +  os.write16(1024);
    1.47 +  do_check_eq(is.read16(), 1024);
    1.48 +  os.write32(7);
    1.49 +  do_check_eq(is.read32(), 7);
    1.50 +  os.write32(LargeNum);
    1.51 +  do_check_eq(is.read32(), LargeNum);
    1.52 +  os.write64(LargeNum);
    1.53 +  do_check_eq(is.read64(), LargeNum);
    1.54 +  os.write64(1024);
    1.55 +  do_check_eq(is.read64(), 1024);
    1.56 +  os.write64(HugeNum);
    1.57 +  do_check_eq(is.read64(), HugeNum);
    1.58 +  os.writeFloat(2.5);
    1.59 +  do_check_eq(is.readFloat(), 2.5);
    1.60 +//  os.writeDouble(Math.SQRT2);
    1.61 +//  do_check_eq(is.readDouble(), Math.SQRT2);
    1.62 +  os.writeStringZ("Mozilla");
    1.63 +  do_check_eq(is.readCString(), "Mozilla");
    1.64 +  os.writeWStringZ("Gecko");
    1.65 +  do_check_eq(is.readString(), "Gecko");
    1.66 +  os.writeBytes(HelloStr, HelloStr.length);
    1.67 +  do_check_eq(is.available(), HelloStr.length);
    1.68 +  msg = is.readBytes(HelloStr.length);
    1.69 +  do_check_eq(msg, HelloStr);
    1.70 +  msg = null;
    1.71 +  countObj.value = -1;
    1.72 +  os.writeByteArray(HelloArray, HelloArray.length);
    1.73 +  do_check_eq(is.available(), HelloStr.length);
    1.74 +  msg = is.readByteArray(HelloStr.length)
    1.75 +  do_check_eq(typeof msg, typeof HelloArray);
    1.76 +  do_check_eq(msg.toSource(), HelloArray.toSource());
    1.77 +  do_check_eq(is.available(), 0);
    1.78 +
    1.79 +  // Test writing in one big chunk.
    1.80 +  os.writeBoolean(true);
    1.81 +  os.write8(4);
    1.82 +  os.write16(4);
    1.83 +  os.write16(1024);
    1.84 +  os.write32(7);
    1.85 +  os.write32(LargeNum);
    1.86 +  os.write64(LargeNum);
    1.87 +  os.write64(1024);
    1.88 +  os.write64(HugeNum);
    1.89 +  os.writeFloat(2.5);
    1.90 +//  os.writeDouble(Math.SQRT2);
    1.91 +  os.writeStringZ("Mozilla");
    1.92 +  os.writeWStringZ("Gecko");
    1.93 +  os.writeBytes(HelloStr, HelloStr.length);
    1.94 +  os.writeByteArray(HelloArray, HelloArray.length);
    1.95 +  // Available should not be zero after a long write like this.
    1.96 +  do_check_neq(is.available(), 0);
    1.97 +
    1.98 +  // Test reading in one big chunk.
    1.99 +  do_check_eq(is.readBoolean(), true);
   1.100 +  do_check_eq(is.read8(), 4);
   1.101 +  do_check_eq(is.read16(), 4);
   1.102 +  do_check_eq(is.read16(), 1024);
   1.103 +  do_check_eq(is.read32(), 7);
   1.104 +  do_check_eq(is.read32(), LargeNum);
   1.105 +  do_check_eq(is.read64(), LargeNum);
   1.106 +  do_check_eq(is.read64(), 1024);
   1.107 +  do_check_eq(is.read64(), HugeNum);
   1.108 +  do_check_eq(is.readFloat(), 2.5);
   1.109 +//  do_check_eq(is.readDouble(), Math.SQRT2);
   1.110 +  do_check_eq(is.readCString(), "Mozilla");
   1.111 +  do_check_eq(is.readString(), "Gecko");
   1.112 +  // Remember, we wrote HelloStr twice - once as a string, and then as an array.
   1.113 +  do_check_eq(is.available(), HelloStr.length * 2);
   1.114 +  msg = is.readBytes(HelloStr.length);
   1.115 +  do_check_eq(msg, HelloStr);
   1.116 +  msg = null;
   1.117 +  countObj.value = -1;
   1.118 +  do_check_eq(is.available(), HelloStr.length);
   1.119 +  msg = is.readByteArray(HelloStr.length)
   1.120 +  do_check_eq(typeof msg, typeof HelloArray);
   1.121 +  do_check_eq(msg.toSource(), HelloArray.toSource());
   1.122 +  do_check_eq(is.available(), 0);
   1.123 +  
   1.124 +  // Test for invalid actions.
   1.125 +  os.close();
   1.126 +  is.close();
   1.127 +
   1.128 +  try {
   1.129 +    os.writeBoolean(false);
   1.130 +    do_throw("Not reached!");
   1.131 +  } catch (e if (e instanceof Ci.nsIException &&
   1.132 +                 e.result == Cr.NS_BASE_STREAM_CLOSED)) {
   1.133 +    // do nothing
   1.134 +  }
   1.135 +
   1.136 +  try {
   1.137 +    is.available();
   1.138 +    do_throw("Not reached!");
   1.139 +  } catch (e if (e instanceof Ci.nsIException &&
   1.140 +                 e.result == Cr.NS_BASE_STREAM_CLOSED)) {
   1.141 +    // do nothing
   1.142 +  }
   1.143 +
   1.144 +  try {
   1.145 +    is.readBoolean();
   1.146 +    do_throw("Not reached!");
   1.147 +  } catch (e if (e instanceof Ci.nsIException &&
   1.148 +                 e.result == Cr.NS_ERROR_FAILURE)) {
   1.149 +    // do nothing
   1.150 +  }
   1.151 +}
   1.152 +
   1.153 +function run_test() {
   1.154 +  test_binary_streams();
   1.155 +}

mercurial