diff --git a/benchmark/get_byte.js b/benchmark/get_byte.js new file mode 100644 index 0000000..f7bcc17 --- /dev/null +++ b/benchmark/get_byte.js @@ -0,0 +1,76 @@ +'use strict'; + +const Benchmark = require('benchmark'); +const benchmarks = require('beautify-benchmark'); +const ByteBuffer = require('../'); + +class OldByteBuffer extends ByteBuffer { + get(index, length) { + if (index instanceof Buffer || Array.isArray(index)) { // get (byte[] dst, int offset, int length) + var dst = index; + var offset = length || 0; + length = dst.length; + + this.checkArraySize(dst.length, offset, length); + this.checkForUnderflow(length); + + if (dst instanceof Buffer) { + this._bytes.copy(dst, offset, this._offset, (this._offset + length)); + } else { + for (var i = offset; i < (offset + length); i++) { + dst[i] = this._bytes[i]; + } + } + this._offset += length; + return this; + } else if (typeof index !== 'number') { + index = this._offset++; + } else if (typeof length === 'number') { + // offset, length => Buffer + return this._copy(index, index + length); + } + // return byte + return this._bytes[index]; + } +} + +const suite = new Benchmark.Suite(); + +const buf = new Buffer([2, 1, 255, 255, 255, 254, 1]); + +suite + .add('old get()', function() { + const bytes = new OldByteBuffer({ array: buf }); + bytes.get(); + }) + .add('new get()', function() { + const bytes = new ByteBuffer({ array: buf }); + bytes.get(); + }) + .add('old get(index)', function() { + const bytes = new OldByteBuffer({ array: buf }); + bytes.get(0); + }) + .add('new get(index)', function() { + const bytes = new ByteBuffer({ array: buf }); + bytes.get(0); + }) + .on('cycle', function(event) { + benchmarks.add(event.target); + }) + .on('start', function(event) { + console.log('\n node version: %s, date: %s\n Starting...', process.version, Date()); + }) + .on('complete', function done() { + benchmarks.log(); + }) + .run({ 'async': false }); + +// node version: v7.10.0, date: Thu May 11 2017 02:58:46 GMT+0800 (CST) +// Starting... +// 4 tests completed. + +// old get() x 2,234,273 ops/sec ±1.45% (90 runs sampled) +// new get() x 24,872,342 ops/sec ±2.13% (89 runs sampled) +// old get(index) x 2,339,452 ops/sec ±0.81% (93 runs sampled) +// new get(index) x 25,533,534 ops/sec ±0.84% (100 runs sampled) diff --git a/lib/byte.js b/lib/byte.js index f7d97e9..9bc6bfc 100644 --- a/lib/byte.js +++ b/lib/byte.js @@ -105,31 +105,33 @@ ByteBuffer.prototype.put = function (src, offset, length) { }; ByteBuffer.prototype.get = function (index, length) { - if (index instanceof Buffer || Array.isArray(index)) { // get (byte[] dst, int offset, int length) - var dst = index; - var offset = length || 0; - length = dst.length; + if (index == null && length == null) { + return this._bytes[this._offset++]; + } + if (typeof index === 'number' && length == null) { + return this._bytes[index]; + } + if (typeof index === 'number' && typeof length === 'number') { + // offset, length => Buffer + return this._copy(index, index + length); + } - this.checkArraySize(dst.length, offset, length); - this.checkForUnderflow(length); + var dst = index; + var offset = length || 0; + length = dst.length; - if (dst instanceof Buffer) { - this._bytes.copy(dst, offset, this._offset, (this._offset + length)); - } else { - for (var i = offset; i < (offset + length); i++) { - dst[i] = this._bytes[i]; - } + this.checkArraySize(dst.length, offset, length); + this.checkForUnderflow(length); + + if (Buffer.isBuffer(dst)) { + this._bytes.copy(dst, offset, this._offset, (this._offset + length)); + } else { + for (var i = offset; i < (offset + length); i++) { + dst[i] = this._bytes[i]; } - this._offset += length; - return this; - } else if (typeof index !== 'number') { - index = this._offset++; - } else if (typeof length === 'number') { - // offset, length => Buffer - return this._copy(index, index + length); } - // return byte - return this._bytes[index]; + this._offset += length; + return this; }; ByteBuffer.prototype.read = function (size) { diff --git a/package.json b/package.json index 9cc727f..8dfd4c1 100644 --- a/package.json +++ b/package.json @@ -11,15 +11,15 @@ "test-cov": "node node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- -t 5000 test/*.test.js", "test-travis": "node node_modules/.bin/istanbul cover node_modules/.bin/_mocha --report lcovonly -- -t 5000 test/*.test.js", "jshint": "jshint .", - "autod": "autod -w --prefix '~' -e benchmark && npm run cnpm", + "autod": "autod -w --prefix '^' -e benchmark && npm run cnpm", "cnpm": "npm install --registry=https://registry.npm.taobao.org", "contributors": "contributors -f plain -o AUTHORS", "optimized": "node --allow-natives-syntax --trace_opt --trace_deopt test/optimized.js" }, "dependencies": { - "debug": "~2.2.0", - "long": "~2.2.5", - "utility": "~1.4.0" + "debug": "^2.6.6", + "long": "^3.2.0", + "utility": "^1.12.0" }, "devDependencies": { "autod": "*", @@ -29,8 +29,7 @@ "istanbul": "*", "jshint": "*", "mocha": "*", - "optimized": "1", - "should": "7" + "optimized": "^1.2.0" }, "homepage": "https://github.com/node-modules/byte", "repository": { diff --git a/test/byte.test.js b/test/byte.test.js index cd25293..e63572b 100644 --- a/test/byte.test.js +++ b/test/byte.test.js @@ -11,20 +11,16 @@ "use strict"; -/** - * Module dependencies. - */ - -var should = require('should'); var Long = require('long'); +var assert = require('assert'); var ByteBuffer = require('../'); describe('byte.test.js', function () { describe('new ByteBuffer()', function () { it('should create ByteBuffer', function () { var bytes = new ByteBuffer(); - bytes.array().should.length(0); - bytes.position().should.equal(0); + assert(bytes.array().length === 0); + assert(bytes.position() === 0); }); }); @@ -38,46 +34,48 @@ describe('byte.test.js', function () { bytes.putString(0, ''); bytes.position(0); - bytes.getString().should.equal(''); - bytes.getString().should.equal(''); - bytes.getString().should.equal(''); - bytes.getString().should.equal(''); + assert(bytes.getString() === ''); + assert(bytes.getString() === ''); + assert(bytes.getString() === ''); + assert(bytes.getString() === ''); }); it('should put strings', function () { var bytes = new ByteBuffer({size: 10}); bytes.putString('foo, 中文'); - bytes.getString(0).should.equal('foo, 中文'); + assert(bytes.getString(0) === 'foo, 中文'); bytes.putString(0, 'foo, 中国'); - bytes.getString(0).should.equal('foo, 中国'); + assert(bytes.getString(0) === 'foo, 中国'); bytes.putString(0, new Buffer('foo, 中国')); - bytes.getString(0).should.equal('foo, 中国'); + assert(bytes.getString(0) === 'foo, 中国'); bytes.putString('foo2'); - bytes.getString(new Buffer('foo, 中国').length + 4).should.equal('foo2'); + assert(bytes.getString(new Buffer('foo, 中国').length + 4) === 'foo2'); bytes.position(0); - bytes.getString().should.equal('foo, 中国'); + assert(bytes.getString() === 'foo, 中国'); bytes = new ByteBuffer({size: 1}); bytes.putCString(0, ''); - bytes._size.should.equal(4 * 2); + assert(bytes._size === 4 * 2); bytes = new ByteBuffer({size: 10}); bytes.putCString('foo, \u0000中文\u0000'); - bytes.getCString(0).should.equal('foo, \u0000中文\u0000'); + assert(bytes.getCString(0) === 'foo, \u0000中文\u0000'); bytes.putCString(0, 'bar123123, \u0000中文\u0000'); - bytes.getCString(0).should.equal('bar123123, \u0000中文\u0000'); + assert(bytes.getCString(0) === 'bar123123, \u0000中文\u0000'); var lbadstr = 'bar123123123123123123123123123123123123, \u0000中文\u0000'; bytes.putCString(0, lbadstr); - bytes.getCString(0).should.equal(lbadstr); + assert(bytes.getCString(0) === lbadstr); bytes.position(0); bytes.putCString('bar123123, \u0000中文\u0000'); bytes.putCString('foo2foo, \u0000中文\u0000'); - bytes.getCString(new Buffer('bar123123, \u0000中文\u0000').length + 4 + 1).should.equal('foo2foo, \u0000中文\u0000'); + assert( + bytes.getCString(new Buffer('bar123123, \u0000中文\u0000').length + 4 + 1) === 'foo2foo, \u0000中文\u0000' + ); bytes.position(0); - bytes.getCString().should.equal('bar123123, \u0000中文\u0000'); + assert(bytes.getCString() === 'bar123123, \u0000中文\u0000'); }); }); @@ -85,29 +83,29 @@ describe('byte.test.js', function () { it('should put a char', function () { var bytes = ByteBuffer.allocate(2); bytes.putChar('a'); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.putChar('A'); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.putChar(255); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.putChar(2, 'a'); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.putChar('a'); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.putChar('a'); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.putChar('a'); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.position(0); - bytes.getChar().should.equal('a'); - bytes.getChar().should.equal('A'); - bytes.getChar().should.equal('a'); - bytes.getChar().should.equal('a'); - bytes.getChar().should.equal('a'); - bytes.getChar().should.equal('a'); - bytes.getChar(1).should.equal('A'); - bytes.position().should.equal(6); + assert(bytes.getChar() === 'a'); + assert(bytes.getChar() === 'A'); + assert(bytes.getChar() === 'a'); + assert(bytes.getChar() === 'a'); + assert(bytes.getChar() === 'a'); + assert(bytes.getChar() === 'a'); + assert(bytes.getChar(1) === 'A'); + assert(bytes.position() === 6); }); }); @@ -129,19 +127,19 @@ describe('byte.test.js', function () { ]; var bytes = ByteBuffer.allocate(4); bytes.putFloat(0); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.position(0); - bytes.getFloat().should.equal(0); + assert(bytes.getFloat() === 0); cases.forEach(function (item) { bytes.order(ByteBuffer.BIG_ENDIAN); bytes.putFloat(0, item[0]); - bytes.toString().should.equal(item[1]); - String(bytes.getFloat(0)).should.containEql(item[0]); + assert(bytes.toString() === item[1]); + assert(String(bytes.getFloat(0)).indexOf(item[0]) >= 0); bytes.order(ByteBuffer.LITTLE_ENDIAN); bytes.putFloat(0, item[0]); - bytes.toString().should.equal(item[2]); - String(bytes.getFloat(0)).should.containEql(item[0]); + assert(bytes.toString() === item[2]); + assert(String(bytes.getFloat(0)).indexOf(item[0]) >= 0); }); }); }); @@ -168,19 +166,19 @@ describe('byte.test.js', function () { ]; var bytes = ByteBuffer.allocate(1); bytes.putInt(0); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.position(0); - bytes.getInt().should.equal(0); + assert(bytes.getInt() === 0); cases.forEach(function (item) { bytes.order(ByteBuffer.BIG_ENDIAN); bytes.putInt(0, item[0]); - bytes.toString().should.equal(item[1]); - bytes.getInt(0).should.equal(item[0]); + assert(bytes.toString() === item[1]); + assert(bytes.getInt(0) === item[0]); bytes.order(ByteBuffer.LITTLE_ENDIAN); bytes.putInt(0, item[0]); - bytes.toString().should.equal(item[2]); - bytes.getInt(0).should.equal(item[0]); + assert(bytes.toString() === item[2]); + assert(bytes.getInt(0) === item[0]); }); }); }); @@ -189,29 +187,29 @@ describe('byte.test.js', function () { it('should put and get 8 bits int', function () { var bytes = ByteBuffer.allocate(1); bytes.putInt8(-128); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.putInt8(0, -128); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.position(0); - bytes.getInt8().should.equal(-128); + assert(bytes.getInt8() === -128); bytes.putInt8(0, 0); - bytes.toString().should.equal(''); - bytes.getInt8(0).should.equal(0); + assert(bytes.toString() === ''); + assert(bytes.getInt8(0) === 0); bytes.putInt8(0, -128); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.putInt8(0, 127); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.position(0); bytes.putUInt8(255); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.position(0); - bytes.getUInt8().should.equal(255); + assert(bytes.getUInt8() === 255); - bytes.putUInt8(0, 0).toString().should.equal(''); - bytes.putUInt8(0, 0).getUInt8(0).should.equal(0); + assert(bytes.putUInt8(0, 0).toString() === ''); + assert(bytes.putUInt8(0, 0).getUInt8(0) === 0); }); }); @@ -238,19 +236,19 @@ describe('byte.test.js', function () { ]; var bytes = ByteBuffer.allocate(1); bytes.putUInt(0); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.position(0); - bytes.getUInt().should.equal(0); + assert(bytes.getUInt() === 0); cases.forEach(function (item) { bytes.order(ByteBuffer.BIG_ENDIAN); bytes.putUInt(0, item[0]); - bytes.toString().should.equal(item[1]); - bytes.getUInt(0).should.equal(item[0]); + assert(bytes.toString() === item[1]); + assert(bytes.getUInt(0) === item[0]); bytes.order(ByteBuffer.LITTLE_ENDIAN); bytes.putUInt(0, item[0]); - bytes.toString().should.equal(item[2]); - bytes.getUInt(0).should.equal(item[0]); + assert(bytes.toString() === item[2]); + assert(bytes.getUInt(0) === item[0]); }); }); }); @@ -277,20 +275,20 @@ describe('byte.test.js', function () { ]; var bytes = ByteBuffer.allocate(1); bytes.putShort(0); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.position(0); - bytes.getShort().should.equal(0); + assert(bytes.getShort() === 0); cases.forEach(function (item) { bytes.order(ByteBuffer.BIG_ENDIAN); bytes.putShort(0, item[0]); - bytes.toString().should.equal(item[1]); - bytes.getShort(0).should.equal(item[0]); + assert(bytes.toString() === item[1]); + assert(bytes.getShort(0) === item[0]); bytes.order(ByteBuffer.LITTLE_ENDIAN); bytes.putShort(0, item[0]); - bytes.toString().should.equal(item[2]); - bytes.getShort(0).should.equal(item[0]); + assert(bytes.toString() === item[2]); + assert(bytes.getShort(0) === item[0]); }); }); }); @@ -316,20 +314,20 @@ describe('byte.test.js', function () { ]; var bytes = ByteBuffer.allocate(1); bytes.putUInt16(0); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.position(0); - bytes.getUInt16().should.equal(0); + assert(bytes.getUInt16() === 0); cases.forEach(function (item) { bytes.order(ByteBuffer.BIG_ENDIAN); bytes.putUInt16(0, item[0]); - bytes.toString().should.equal(item[1]); - bytes.getUInt16(0).should.equal(item[0]); + assert(bytes.toString() === item[1]); + assert(bytes.getUInt16(0) === item[0]); bytes.order(ByteBuffer.LITTLE_ENDIAN); bytes.putUInt16(0, item[0]); - bytes.toString().should.equal(item[2]); - bytes.getUInt16(0).should.equal(item[0]); + assert(bytes.toString() === item[2]); + assert(bytes.getUInt16(0) === item[0]); }); }); }); @@ -369,19 +367,19 @@ describe('byte.test.js', function () { ]; var bytes = ByteBuffer.allocate(1); bytes.putLong(0); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.position(0); - bytes.getLong().toString().should.equal('0'); + assert(bytes.getLong().toString() === '0'); cases.forEach(function (item) { bytes.order(ByteBuffer.BIG_ENDIAN); bytes.putLong(0, item[0]); - bytes.toString().should.equal(item[1]); - bytes.getLong(0).toString().should.equal(String(item[0])); + assert(bytes.toString() === item[1]); + assert(bytes.getLong(0).toString() === String(item[0])); bytes.order(ByteBuffer.LITTLE_ENDIAN); bytes.putLong(0, item[0]); - bytes.toString().should.equal(item[2]); - bytes.getLong(0).toString().should.equal(String(item[0])); + assert(bytes.toString() === item[2]); + assert(bytes.getLong(0).toString() === String(item[0])); }); }); }); @@ -390,48 +388,50 @@ describe('byte.test.js', function () { it('should put a double', function () { var bytes = ByteBuffer.allocate(2); bytes.putDouble(1); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); bytes.position(0); - bytes.getDouble().should.equal(1); + assert(bytes.getDouble() === 1); bytes.putDouble(0, 0); - bytes.toString().should.equal(''); - bytes.getDouble(0).should.equal(0); + assert(bytes.toString() === ''); + assert(bytes.getDouble(0) === 0); bytes.putDouble(0, 2); - bytes.toString().should.equal(''); - bytes.getDouble(0).should.equal(2); + assert(bytes.toString() === ''); + assert(bytes.getDouble(0) === 2); bytes.putDouble(0, 1024); - bytes.toString().should.equal(''); - bytes.getDouble(0).should.equal(1024); + assert(bytes.toString() === ''); + assert(bytes.getDouble(0) === 1024); bytes.order(ByteBuffer.LITTLE_ENDIAN); bytes.putDouble(0, 1); - bytes.toString().should.equal(''); - bytes.getDouble(0).should.equal(1); + assert(bytes.toString() === ''); + assert(bytes.getDouble(0) === 1); bytes.putDouble(0, 1024); - bytes.toString().should.equal(''); - bytes.getDouble(0).should.equal(1024); + assert(bytes.toString() === ''); + assert(bytes.getDouble(0) === 1024); bytes.order(ByteBuffer.BIG_ENDIAN); bytes.putDouble(0, 1123123.123123); - bytes.toString().should.equal(''); - bytes.getDouble(0).should.equal(1123123.123123); + assert(bytes.toString() === ''); + assert(bytes.getDouble(0) === 1123123.123123); bytes.order(ByteBuffer.LITTLE_ENDIAN); bytes.putDouble(0, 1123123.123123); - bytes.toString().should.equal(''); - bytes.getDouble(0).should.equal(1123123.123123); + assert(bytes.toString() === ''); + assert(bytes.getDouble(0) === 1123123.123123); bytes.order(ByteBuffer.BIG_ENDIAN); bytes.putDouble(1123123.123123); - bytes.toString().should.equal(''); - bytes.getDouble(8).should.equal(1123123.123123); + assert( + bytes.toString() === '' + ); + assert(bytes.getDouble(8) === 1123123.123123); - ByteBuffer.wrap(bytes.array()).getDouble(8).should.equal(1123123.123123); - ByteBuffer.wrap(bytes.array(), 8, 8).getDouble(0).should.equal(1123123.123123); + assert(ByteBuffer.wrap(bytes.array()).getDouble(8) === 1123123.123123); + assert(ByteBuffer.wrap(bytes.array(), 8, 8).getDouble(0) === 1123123.123123); }); }); @@ -439,33 +439,33 @@ describe('byte.test.js', function () { it('should put byte', function () { var bytes = ByteBuffer.allocate(1); bytes.put(1); - bytes.toString().should.equal(''); - bytes.get(0).should.equal(1); + assert(bytes.toString() === ''); + assert(bytes.get(0) === 1); bytes.put(0, 2); - bytes.toString().should.equal(''); - bytes.get(0).should.equal(2); + assert(bytes.toString() === ''); + assert(bytes.get(0) === 2); bytes.put(1); - bytes.toString().should.equal(''); - bytes.get(1).should.equal(1); + assert(bytes.toString() === ''); + assert(bytes.get(1) === 1); bytes.put(new Buffer([255, 255, 255])); - bytes.toString().should.equal(''); - bytes.get(2, 3).should.eql(new Buffer([255, 255, 255])); + assert(bytes.toString() === ''); + assert.deepEqual(bytes.get(2, 3), new Buffer([255, 255, 255])); bytes.put(new Buffer([255, 254, 1]), 1, 2); - bytes.toString().should.equal(''); - bytes.get(5, 2).should.eql(new Buffer([254, 1])); + assert(bytes.toString() === ''); + assert.deepEqual(bytes.get(5, 2), new Buffer([254, 1])); bytes.position(0); - bytes.read(7).should.eql(new Buffer([2, 1, 255, 255, 255, 254, 1])); - bytes.position().should.equal(7); + assert.deepEqual(bytes.read(7), new Buffer([2, 1, 255, 255, 255, 254, 1])); + assert(bytes.position() === 7); bytes.position(0); bytes.skip(5); - bytes.read(2).should.eql(new Buffer([254, 1])); - bytes.position().should.equal(7); + assert.deepEqual(bytes.read(2), new Buffer([254, 1])); + assert(bytes.position() === 7); }); }); @@ -474,24 +474,24 @@ describe('byte.test.js', function () { var bytes = ByteBuffer.allocate(1); bytes.putRawString('hello'); bytes.putRawString(' world'); - bytes.toString().should.equal(''); - bytes.getRawString(0, 11).should.equal('hello world'); + assert(bytes.toString() === ''); + assert(bytes.getRawString(0, 11) === 'hello world'); bytes.position(0); - bytes.getRawString().should.equal('h'); + assert(bytes.getRawString() === 'h'); bytes = ByteBuffer.allocate(1); bytes.putRawString('你好'); - bytes.toString().should.equal(''); - bytes.position(0).readRawString(6).should.equal('你好'); + assert(bytes.toString() === ''); + assert(bytes.position(0).readRawString(6) === '你好'); bytes.putRawString(0, '我们'); - bytes.toString().should.equal(''); - bytes.getRawString(0, 6).should.equal('我们'); + assert(bytes.toString() === ''); + assert(bytes.getRawString(0, 6) === '我们'); - bytes.readRawString(0, 6).should.equal('我们'); + assert(bytes.readRawString(0, 6) === '我们'); bytes = ByteBuffer.allocate(1); bytes.putRawString(''); - bytes.toString().should.equal(''); + assert(bytes.toString() === ''); }); it('should put emoji', function () { @@ -499,28 +499,28 @@ describe('byte.test.js', function () { var bytes = ByteBuffer.allocate(1); var str = 'hello\u9983\u5c32'; bytes.putRawString(str); - bytes.toString().should.eql(''); - bytes.getRawString(0, 11).should.eql(str); + assert(bytes.toString() === ''); + assert.deepEqual(bytes.getRawString(0, 11), str); // gbk var bytes = ByteBuffer.allocate(1); var str = 'hello\ud83c\udf3c'; bytes.putRawString(str); - bytes.toString().should.eql(''); - bytes.getRawString(0, 11).should.eql(str); + assert(bytes.toString() === ''); + assert.deepEqual(bytes.getRawString(0, 11), str); var bytes = ByteBuffer.allocate(1); // java encode bytes: [-19, -96, -67, -19, -72, -128, 87, 119, 119, -23, -126, -93] var str = '\ud83d\ude00Www那'; bytes.putRawString(str); - bytes.toString().should.eql(''); - bytes.getRawString(0, 12).should.eql(str); + assert(bytes.toString() === ''); + assert.deepEqual(bytes.getRawString(0, 12), str); // Construction of a special test case which triggers the bug // of allocating insufficient space via _checkSize var bytes = ByteBuffer.allocate(4); var str = '\ud83d\ude00'; bytes.putRawString(str); - bytes.toString().should.eql(''); + assert(bytes.toString() === ''); }); }); @@ -529,17 +529,17 @@ describe('byte.test.js', function () { var bytes = ByteBuffer.allocate(8); bytes.putInt(0); bytes.putInt(1); - bytes.copy(4).should.eql(new Buffer([0, 0, 0, 1])); + assert.deepEqual(bytes.copy(4), new Buffer([0, 0, 0, 1])); }); it('should copy(start, end)', function () { var bytes = ByteBuffer.allocate(9); bytes.putInt(0); bytes.putInt(1); - bytes.copy(0, 8).should.eql(new Buffer([0, 0, 0, 0, 0, 0, 0, 1])); - bytes.copy(0, 9).should.eql(new Buffer([0, 0, 0, 0, 0, 0, 0, 1])); - bytes.copy(0, 4).should.eql(new Buffer([0, 0, 0, 0])); - bytes.copy(4, 8).should.eql(new Buffer([0, 0, 0, 1])); + assert.deepEqual(bytes.copy(0, 8), new Buffer([0, 0, 0, 0, 0, 0, 0, 1])); + assert.deepEqual(bytes.copy(0, 9), new Buffer([0, 0, 0, 0, 0, 0, 0, 1])); + assert.deepEqual(bytes.copy(0, 4), new Buffer([0, 0, 0, 0])); + assert.deepEqual(bytes.copy(4, 8), new Buffer([0, 0, 0, 1])); }); }); @@ -548,7 +548,7 @@ describe('byte.test.js', function () { var bytes = ByteBuffer.allocate(4096); bytes.putRawString('hello'); bytes.putRawString('world'); - bytes._copy(0, 5).toString().should.equal('hello'); + assert(bytes._copy(0, 5).toString() === 'hello'); }); it('should splice > 2048 ok', function () { @@ -556,7 +556,7 @@ describe('byte.test.js', function () { for (var i = 0; i < 800; i++) { bytes.putRawString('hello'); } - bytes._copy(1000, 4000).toString().should.have.length(3000); + assert(bytes._copy(1000, 4000).toString().length === 3000); }); }); @@ -564,46 +564,46 @@ describe('byte.test.js', function () { it('should capacity()', function () { var bytes = ByteBuffer.allocate(4); bytes.putInt(0); - bytes.capacity().should.eql(4); + assert(bytes.capacity() === 4); bytes.putInt(1); - bytes.capacity().should.eql(16); + assert(bytes.capacity() === 16); bytes.flip(); - bytes.capacity().should.eql(16); + assert(bytes.capacity() === 16); }); }); describe('remaining(), hasRemaining()', function () { it('should remaining(), hasRemaining()', function () { var bytes = ByteBuffer.allocate(8); - bytes.remaining().should.eql(8); - bytes.hasRemaining().should.true; + assert(bytes.remaining() === 8); + assert(bytes.hasRemaining()); bytes.put(1); - bytes.remaining().should.eql(7); - bytes.hasRemaining().should.true; + assert(bytes.remaining() === 7); + assert(bytes.hasRemaining()); bytes.putShort(2); - bytes.remaining().should.eql(5); - bytes.hasRemaining().should.true; + assert(bytes.remaining() === 5); + assert(bytes.hasRemaining()); bytes.put(1); bytes.putInt(1); - bytes.remaining().should.eql(0); - bytes.hasRemaining().should.false; + assert(bytes.remaining() === 0); + assert(!bytes.hasRemaining()); }); }); describe('flip(), limit()', function () { it('should flip(), limit()', function () { var bytes = ByteBuffer.allocate(8); - bytes.limit().should.eql(8); + assert(bytes.limit() === 8); bytes.putInt(1); - bytes.limit().should.eql(8); + assert(bytes.limit() === 8); bytes.flip(); - bytes.limit().should.eql(4); + assert(bytes.limit() === 4); }); it('should limit(newLimit)', function () { var bytes = ByteBuffer.allocate(8); - bytes.limit().should.eql(8); - bytes.limit(4).limit().should.eql(4); + assert(bytes.limit() === 8); + assert(bytes.limit(4).limit() === 4); }); }); @@ -614,12 +614,12 @@ describe('byte.test.js', function () { bytes.flip(); // switch to read mode var buf = new Buffer(4); bytes.get(buf); - buf.should.eql(new Buffer([0, 0, 0, 1])); + assert.deepEqual(buf, new Buffer([0, 0, 0, 1])); var buf2 = new Buffer(1); - (function() { + assert.throws(function() { bytes.get(buf2); - }).should.throw('BufferOverflowException'); + }, 'BufferOverflowException'); }); it('should get(dst, offset, length) again', function () { @@ -629,9 +629,9 @@ describe('byte.test.js', function () { bytes.flip(); // switch to read mode var buf = new Buffer(4); bytes.get(buf); - buf.should.eql(new Buffer([0, 0, 0, 1])); + assert.deepEqual(buf, new Buffer([0, 0, 0, 1])); bytes.get(buf); - buf.should.eql(new Buffer([0, 0, 0, 5])); + assert.deepEqual(buf, new Buffer([0, 0, 0, 5])); }); }); }); diff --git a/test/optimized.js b/test/optimized.js index 964590a..b5ffdfb 100644 --- a/test/optimized.js +++ b/test/optimized.js @@ -14,8 +14,8 @@ * Module dependencies. */ +var assert = require('assert'); var optimized = require('optimized'); -var should = require('should'); var ByteBuffer = require('..'); var putBytes = ByteBuffer.allocate(2); @@ -60,36 +60,34 @@ putIntBytes.putInt._name = 'putInt'; putFloatBytes.putFloat._name = 'putFloat'; putDoubleBytes.putDouble._name = 'putDouble'; -// optimized(ByteBuffer.allocate, [1024], ByteBuffer).should.ok; +assert(optimized(putBytes._checkSize, [0], putBytes)); +assert(optimized(putBytes._checkSize, [100], putBytes)); -optimized(putBytes._checkSize, [0], putBytes).should.ok; -optimized(putBytes._checkSize, [100], putBytes).should.ok; +assert(optimized(putBytes.put, [0, 1], putBytes)); +assert(optimized(putBytes.put, [1], putBytes)); +assert(optimized(putBytes.put, [new Buffer([1, 2]), 1, 2], putBytes)); -optimized(putBytes.put, [0, 1], putBytes).should.ok; -optimized(putBytes.put, [1], putBytes).should.ok; -optimized(putBytes.put, [new Buffer([1, 2]), 1, 2], putBytes).should.ok; +assert(optimized(putBytes.get, [0, 1], putBytes)); +assert(optimized(putBytes.get, [1], putBytes)); -optimized(putBytes.get, [0, 1], putBytes).should.ok; -optimized(putBytes.get, [1], putBytes).should.ok; +assert(optimized(putBytes.read, [1], putBytes)); -optimized(putBytes.read, [1], putBytes).should.ok; +assert(optimized(putCharBytes.putChar, ['a'], putCharBytes)); +assert(optimized(putCharBytes.putChar, [0, 'a'], putCharBytes)); +assert(optimized(putCharBytes.putChar, [97], putCharBytes)); +assert(optimized(putCharBytes.putChar, [0, 97], putCharBytes)); -optimized(putCharBytes.putChar, ['a'], putCharBytes).should.ok; -optimized(putCharBytes.putChar, [0, 'a'], putCharBytes).should.ok; -optimized(putCharBytes.putChar, [97], putCharBytes).should.ok; -optimized(putCharBytes.putChar, [0, 97], putCharBytes).should.ok; +assert(optimized(putCharBytes.getChar, [], putCharBytes)); +assert(optimized(putCharBytes.getChar, [0], putCharBytes)); -optimized(putCharBytes.getChar, [], putCharBytes).should.ok; -optimized(putCharBytes.getChar, [0], putCharBytes).should.ok; +assert(optimized(putShortBytes.putShort, [0, 1], putShortBytes)); +assert(optimized(putShortBytes.putShort, [1], putShortBytes)); -optimized(putShortBytes.putShort, [0, 1], putShortBytes).should.ok; -optimized(putShortBytes.putShort, [1], putShortBytes).should.ok; +assert(optimized(putIntBytes.putInt, [0, 1], putIntBytes)); +assert(optimized(putIntBytes.putInt, [1], putIntBytes)); -optimized(putIntBytes.putInt, [0, 1], putIntBytes).should.ok; -optimized(putIntBytes.putInt, [1], putIntBytes).should.ok; +assert(optimized(putFloatBytes.putFloat, [0, 1.1], putFloatBytes)); +assert(optimized(putFloatBytes.putFloat, [1.1], putFloatBytes)); -optimized(putFloatBytes.putFloat, [0, 1.1], putFloatBytes).should.ok; -optimized(putFloatBytes.putFloat, [1.1], putFloatBytes).should.ok; - -optimized(putDoubleBytes.putDouble, [0, 1.1], putDoubleBytes).should.ok; -optimized(putDoubleBytes.putDouble, [1.1], putDoubleBytes).should.ok; +assert(optimized(putDoubleBytes.putDouble, [0, 1.1], putDoubleBytes)); +assert(optimized(putDoubleBytes.putDouble, [1.1], putDoubleBytes));