react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / buffer / test / methods.js
80724 viewsvar B = require('../').Buffer1var test = require('tape')2if (process.env.OBJECT_IMPL) B.TYPED_ARRAY_SUPPORT = false345test('buffer.toJSON', function (t) {6var data = [1, 2, 3, 4]7t.deepEqual(8new B(data).toJSON(),9{ type: 'Buffer', data: [1,2,3,4] }10)11t.end()12})1314test('buffer.copy', function (t) {15// copied from nodejs.org example16var buf1 = new B(26)17var buf2 = new B(26)1819for (var i = 0 ; i < 26 ; i++) {20buf1[i] = i + 97; // 97 is ASCII a21buf2[i] = 33; // ASCII !22}2324buf1.copy(buf2, 8, 16, 20)2526t.equal(27buf2.toString('ascii', 0, 25),28'!!!!!!!!qrst!!!!!!!!!!!!!'29)30t.end()31})3233test('test offset returns are correct', function (t) {34var b = new B(16)35t.equal(4, b.writeUInt32LE(0, 0))36t.equal(6, b.writeUInt16LE(0, 4))37t.equal(7, b.writeUInt8(0, 6))38t.equal(8, b.writeInt8(0, 7))39t.equal(16, b.writeDoubleLE(0, 8))40t.end()41})4243test('concat() a varying number of buffers', function (t) {44var zero = []45var one = [ new B('asdf') ]46var long = []47for (var i = 0; i < 10; i++) {48long.push(new B('asdf'))49}5051var flatZero = B.concat(zero)52var flatOne = B.concat(one)53var flatLong = B.concat(long)54var flatLongLen = B.concat(long, 40)5556t.equal(flatZero.length, 0)57t.equal(flatOne.toString(), 'asdf')58t.equal(flatOne, one[0])59t.equal(flatLong.toString(), (new Array(10+1).join('asdf')))60t.equal(flatLongLen.toString(), (new Array(10+1).join('asdf')))61t.end()62})6364test('fill', function (t) {65var b = new B(10)66b.fill(2)67t.equal(b.toString('hex'), '02020202020202020202')68t.end()69})7071test('fill (string)', function (t) {72var b = new B(10)73b.fill('abc')74t.equal(b.toString(), 'abcabcabca')75b.fill('է')76t.equal(b.toString(), 'էէէէէ')77t.end()78})7980test('copy() empty buffer with sourceEnd=0', function (t) {81var source = new B([42])82var destination = new B([43])83source.copy(destination, 0, 0, 0)84t.equal(destination.readUInt8(0), 43)85t.end()86})8788test('copy() after slice()', function (t) {89var source = new B(200)90var dest = new B(200)91var expected = new B(200)92for (var i = 0; i < 200; i++) {93source[i] = i94dest[i] = 095}9697source.slice(2).copy(dest)98source.copy(expected, 0, 2)99t.deepEqual(dest, expected)100t.end()101})102103test('buffer.slice sets indexes', function (t) {104t.equal((new B('hallo')).slice(0, 5).toString(), 'hallo')105t.end()106})107108test('buffer.slice out of range', function (t) {109t.plan(2)110t.equal((new B('hallo')).slice(0, 10).toString(), 'hallo')111t.equal((new B('hallo')).slice(10, 2).toString(), '')112t.end()113})114115116