react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / buffer / test / write.js
80724 viewsvar B = require('../').Buffer1var test = require('tape')2var isnan = require('is-nan')3if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false45test('buffer.write string should get parsed as number', function (t) {6var b = new B(64)7b.writeUInt16LE('1003', 0)8t.equal(b.readUInt16LE(0), 1003)9t.end()10})1112test('buffer.writeUInt8 a fractional number will get Math.floored', function (t) {13// Some extra work is necessary to make this test pass with the Object implementation1415var b = new B(1)16b.writeInt8(5.5, 0)17t.equal(b[0], 5)18t.end()19})2021test('writeUint8 with a negative number throws', function (t) {22var buf = new B(1)2324t.throws(function () {25buf.writeUInt8(-3, 0)26})2728t.end()29})3031test('hex of write{Uint,Int}{8,16,32}{LE,BE}', function (t) {32t.plan(2 * (2 * 2 * 2 + 2))33var hex = [34'03', '0300', '0003', '03000000', '00000003',35'fd', 'fdff', 'fffd', 'fdffffff', 'fffffffd'36]37var reads = [ 3, 3, 3, 3, 3, -3, -3, -3, -3, -3 ]38var xs = ['UInt','Int']39var ys = [8,16,32]40for (var i = 0; i < xs.length; i++) {41var x = xs[i]42for (var j = 0; j < ys.length; j++) {43var y = ys[j]44var endianesses = (y === 8) ? [''] : ['LE','BE']45for (var k = 0; k < endianesses.length; k++) {46var z = endianesses[k]4748var v1 = new B(y / 8)49var writefn = 'write' + x + y + z50var val = (x === 'Int') ? -3 : 351v1[writefn](val, 0)52t.equal(53v1.toString('hex'),54hex.shift()55)56var readfn = 'read' + x + y + z57t.equal(58v1[readfn](0),59reads.shift()60)61}62}63}64t.end()65})6667test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) {68if (!B.TYPED_ARRAY_SUPPORT) {69t.pass('object impl: skipping overflow test')70t.end()71return72}7374t.plan(3 * (2 * 2 * 2 + 2))75var hex = [76'', '03', '00', '030000', '000000',77'', 'fd', 'ff', 'fdffff', 'ffffff'78]79var reads = [80undefined, 3, 0, NaN, 0,81undefined, 253, -256, 16777213, -25682]83var xs = ['UInt','Int']84var ys = [8,16,32]85for (var i = 0; i < xs.length; i++) {86var x = xs[i]87for (var j = 0; j < ys.length; j++) {88var y = ys[j]89var endianesses = (y === 8) ? [''] : ['LE','BE']90for (var k = 0; k < endianesses.length; k++) {91var z = endianesses[k]9293var v1 = new B(y / 8 - 1)94var next = new B(4)95next.writeUInt32BE(0, 0)96var writefn = 'write' + x + y + z97var val = (x === 'Int') ? -3 : 398v1[writefn](val, 0, true)99t.equal(100v1.toString('hex'),101hex.shift()102)103// check that nothing leaked to next buffer.104t.equal(next.readUInt32BE(0), 0)105// check that no bytes are read from next buffer.106next.writeInt32BE(~0, 0)107var readfn = 'read' + x + y + z108var r = reads.shift()109if (isnan(r))110t.pass('equal')111else112t.equal(113v1[readfn](0, true),114r115)116}117}118}119t.end()120})121122123