From 9c23256a165049516cb07dd948874b28c1d0c7ee Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Thu, 10 Jul 2014 18:45:09 -0700 Subject: [PATCH] add tests to make sure bignum interface works correctly --- test/test.Bignum.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/test.Bignum.js b/test/test.Bignum.js index be88258..0910cc1 100644 --- a/test/test.Bignum.js +++ b/test/test.Bignum.js @@ -62,4 +62,47 @@ describe('Bignum', function() { }); + describe('#fromBuffer', function() { + + it('should work with big endian', function() { + var bn = Bignum.fromBuffer(new Buffer('0001', 'hex'), {endian: 'big'}); + bn.toString().should.equal('1'); + }); + + it('should work with big endian 256', function() { + var bn = Bignum.fromBuffer(new Buffer('0100', 'hex'), {endian: 'big'}); + bn.toString().should.equal('256'); + }); + + it('should work with little endian if we specify the size', function() { + var bn = Bignum.fromBuffer(new Buffer('0100', 'hex'), {size: 2, endian: 'little'}); + bn.toString().should.equal('1'); + }); + + }); + + describe('#toBuffer', function() { + + it('should create a 4 byte buffer', function() { + var bn = new Bignum(1); + bn.toBuffer({size: 4}).toString('hex').should.equal('00000001'); + }); + + it('should create a 4 byte buffer in little endian', function() { + var bn = new Bignum(1); + bn.toBuffer({size: 4, endian: 'little'}).toString('hex').should.equal('01000000'); + }); + + it('should create a 2 byte buffer even if you ask for a 1 byte', function() { + var bn = new Bignum('ff00', 16); + bn.toBuffer({size: 1}).toString('hex').should.equal('ff00'); + }); + + it('should create a 4 byte buffer even if you ask for a 1 byte', function() { + var bn = new Bignum('ffffff00', 16); + bn.toBuffer({size: 4}).toString('hex').should.equal('ffffff00'); + }); + + }); + });