web3.js/test/utils.toBigNumber.js

46 lines
2.0 KiB
JavaScript
Raw Normal View History

2015-03-07 23:33:22 -08:00
var chai = require('chai');
2015-03-08 01:07:32 -08:00
var utils = require('../lib/utils');
2015-02-26 06:54:49 -08:00
var BigNumber = require('bignumber.js');
2015-03-07 23:33:22 -08:00
var assert = chai.assert;
var tests = [
{ value: 1, expected: '1' },
{ value: '1', expected: '1' },
{ value: '0x1', expected: '1'},
{ value: '0x01', expected: '1'},
{ value: 15, expected: '15'},
{ value: '15', expected: '15'},
{ value: '0xf', expected: '15'},
{ value: '0x0f', expected: '15'},
2015-03-08 01:07:32 -08:00
{ value: new BigNumber('f', 16), expected: '15'},
2015-03-07 23:33:22 -08:00
{ value: -1, expected: '-1'},
{ value: '-1', expected: '-1'},
{ value: '-0x1', expected: '-1'},
{ value: '-0x01', expected: '-1'},
{ value: -15, expected: '-15'},
{ value: '-15', expected: '-15'},
{ value: '-0xf', expected: '-15'},
{ value: '-0x0f', expected: '-15'},
{ value: '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '115792089237316195423570985008687907853269984665640564039457584007913129639935'},
{ value: '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '115792089237316195423570985008687907853269984665640564039457584007913129639933'},
{ value: '-0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '-115792089237316195423570985008687907853269984665640564039457584007913129639935'},
{ value: '-0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '-115792089237316195423570985008687907853269984665640564039457584007913129639933'},
{ value: 0, expected: '0'},
{ value: '0', expected: '0'},
{ value: '0x0', expected: '0'},
{ value: -0, expected: '0'},
{ value: '-0', expected: '0'},
2015-03-08 01:07:32 -08:00
{ value: '-0x0', expected: '0'},
{ value: new BigNumber(0), expected: '0'}
2015-03-07 23:33:22 -08:00
];
2015-02-26 06:54:49 -08:00
describe('utils', function () {
describe('toBigNumber', function () {
2015-03-07 23:33:22 -08:00
tests.forEach(function (test) {
it('should turn ' + test.value + ' to ' + test.expected, function () {
assert.equal(utils.toBigNumber(test.value).toString(10), test.expected);
});
2015-02-26 06:54:49 -08:00
});
});
2015-03-07 08:53:06 -08:00
});