michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const Ci = Components.interfaces; michael@0: const Cr = Components.results; michael@0: const CC = Components.Constructor; michael@0: michael@0: var Pipe = CC("@mozilla.org/pipe;1", michael@0: "nsIPipe", michael@0: "init"); michael@0: var BinaryOutput = CC("@mozilla.org/binaryoutputstream;1", michael@0: "nsIBinaryOutputStream", michael@0: "setOutputStream"); michael@0: var BinaryInput = CC("@mozilla.org/binaryinputstream;1", michael@0: "nsIBinaryInputStream", michael@0: "setInputStream"); michael@0: michael@0: /** michael@0: * Binary stream tests. michael@0: */ michael@0: function test_binary_streams() { michael@0: var p, is, os; michael@0: michael@0: p = new Pipe(false, false, 1024, 1, null); michael@0: is = new BinaryInput(p.inputStream); michael@0: os = new BinaryOutput(p.outputStream); michael@0: michael@0: const LargeNum = Math.pow(2, 18) + Math.pow(2, 12) + 1; michael@0: const HugeNum = Math.pow(2, 62); michael@0: const HelloStr = "Hello World"; michael@0: const HelloArray = Array.map(HelloStr, function(c) {return c.charCodeAt(0)}); michael@0: var countObj = {}; michael@0: var msg = {}; michael@0: michael@0: // Test reading immediately after writing. michael@0: os.writeBoolean(true); michael@0: do_check_eq(is.readBoolean(), true); michael@0: os.write8(4); michael@0: do_check_eq(is.read8(), 4); michael@0: os.write16(4); michael@0: do_check_eq(is.read16(), 4); michael@0: os.write16(1024); michael@0: do_check_eq(is.read16(), 1024); michael@0: os.write32(7); michael@0: do_check_eq(is.read32(), 7); michael@0: os.write32(LargeNum); michael@0: do_check_eq(is.read32(), LargeNum); michael@0: os.write64(LargeNum); michael@0: do_check_eq(is.read64(), LargeNum); michael@0: os.write64(1024); michael@0: do_check_eq(is.read64(), 1024); michael@0: os.write64(HugeNum); michael@0: do_check_eq(is.read64(), HugeNum); michael@0: os.writeFloat(2.5); michael@0: do_check_eq(is.readFloat(), 2.5); michael@0: // os.writeDouble(Math.SQRT2); michael@0: // do_check_eq(is.readDouble(), Math.SQRT2); michael@0: os.writeStringZ("Mozilla"); michael@0: do_check_eq(is.readCString(), "Mozilla"); michael@0: os.writeWStringZ("Gecko"); michael@0: do_check_eq(is.readString(), "Gecko"); michael@0: os.writeBytes(HelloStr, HelloStr.length); michael@0: do_check_eq(is.available(), HelloStr.length); michael@0: msg = is.readBytes(HelloStr.length); michael@0: do_check_eq(msg, HelloStr); michael@0: msg = null; michael@0: countObj.value = -1; michael@0: os.writeByteArray(HelloArray, HelloArray.length); michael@0: do_check_eq(is.available(), HelloStr.length); michael@0: msg = is.readByteArray(HelloStr.length) michael@0: do_check_eq(typeof msg, typeof HelloArray); michael@0: do_check_eq(msg.toSource(), HelloArray.toSource()); michael@0: do_check_eq(is.available(), 0); michael@0: michael@0: // Test writing in one big chunk. michael@0: os.writeBoolean(true); michael@0: os.write8(4); michael@0: os.write16(4); michael@0: os.write16(1024); michael@0: os.write32(7); michael@0: os.write32(LargeNum); michael@0: os.write64(LargeNum); michael@0: os.write64(1024); michael@0: os.write64(HugeNum); michael@0: os.writeFloat(2.5); michael@0: // os.writeDouble(Math.SQRT2); michael@0: os.writeStringZ("Mozilla"); michael@0: os.writeWStringZ("Gecko"); michael@0: os.writeBytes(HelloStr, HelloStr.length); michael@0: os.writeByteArray(HelloArray, HelloArray.length); michael@0: // Available should not be zero after a long write like this. michael@0: do_check_neq(is.available(), 0); michael@0: michael@0: // Test reading in one big chunk. michael@0: do_check_eq(is.readBoolean(), true); michael@0: do_check_eq(is.read8(), 4); michael@0: do_check_eq(is.read16(), 4); michael@0: do_check_eq(is.read16(), 1024); michael@0: do_check_eq(is.read32(), 7); michael@0: do_check_eq(is.read32(), LargeNum); michael@0: do_check_eq(is.read64(), LargeNum); michael@0: do_check_eq(is.read64(), 1024); michael@0: do_check_eq(is.read64(), HugeNum); michael@0: do_check_eq(is.readFloat(), 2.5); michael@0: // do_check_eq(is.readDouble(), Math.SQRT2); michael@0: do_check_eq(is.readCString(), "Mozilla"); michael@0: do_check_eq(is.readString(), "Gecko"); michael@0: // Remember, we wrote HelloStr twice - once as a string, and then as an array. michael@0: do_check_eq(is.available(), HelloStr.length * 2); michael@0: msg = is.readBytes(HelloStr.length); michael@0: do_check_eq(msg, HelloStr); michael@0: msg = null; michael@0: countObj.value = -1; michael@0: do_check_eq(is.available(), HelloStr.length); michael@0: msg = is.readByteArray(HelloStr.length) michael@0: do_check_eq(typeof msg, typeof HelloArray); michael@0: do_check_eq(msg.toSource(), HelloArray.toSource()); michael@0: do_check_eq(is.available(), 0); michael@0: michael@0: // Test for invalid actions. michael@0: os.close(); michael@0: is.close(); michael@0: michael@0: try { michael@0: os.writeBoolean(false); michael@0: do_throw("Not reached!"); michael@0: } catch (e if (e instanceof Ci.nsIException && michael@0: e.result == Cr.NS_BASE_STREAM_CLOSED)) { michael@0: // do nothing michael@0: } michael@0: michael@0: try { michael@0: is.available(); michael@0: do_throw("Not reached!"); michael@0: } catch (e if (e instanceof Ci.nsIException && michael@0: e.result == Cr.NS_BASE_STREAM_CLOSED)) { michael@0: // do nothing michael@0: } michael@0: michael@0: try { michael@0: is.readBoolean(); michael@0: do_throw("Not reached!"); michael@0: } catch (e if (e instanceof Ci.nsIException && michael@0: e.result == Cr.NS_ERROR_FAILURE)) { michael@0: // do nothing michael@0: } michael@0: } michael@0: michael@0: function run_test() { michael@0: test_binary_streams(); michael@0: }