fixed from decimal;

This commit is contained in:
Marek Kotewicz 2015-03-08 08:48:45 +01:00
parent 50fc61155e
commit 9fe105e794
7 changed files with 88 additions and 26 deletions

7
dist/ethereum.js vendored
View File

@ -1976,7 +1976,12 @@ var fromDecimal = function (value) {
* @return {String}
*/
var toHex = function (value) {
return '0x' + toBigNumber(value).toString(16);
var number = toBigNumber(value);
var result = number.toString(16);
if (number.lessThan(0)) {
return '-0x' + result.substr(1);
}
return '0x' + result;
};
/**

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -155,7 +155,12 @@ var fromDecimal = function (value) {
* @return {String}
*/
var toHex = function (value) {
return '0x' + toBigNumber(value).toString(16);
var number = toBigNumber(value);
var result = number.toString(16);
if (number.lessThan(0)) {
return '-0x' + result.substr(1);
}
return '0x' + result;
};
/**

View File

@ -1,12 +1,43 @@
var assert = require('assert');
var chai = require('chai');
var utils = require('../lib/utils.js');
var assert = chai.assert;
var tests = [
{ value: 1, expected: '0x1' },
{ value: '1', expected: '0x1' },
{ value: '0x1', expected: '0x1'},
{ value: '0x01', expected: '0x1'},
{ value: 15, expected: '0xf'},
{ value: '15', expected: '0xf'},
{ value: '0xf', expected: '0xf'},
{ value: '0x0f', expected: '0xf'},
{ value: -1, expected: '-0x1'},
{ value: '-1', expected: '-0x1'},
{ value: '-0x1', expected: '-0x1'},
{ value: '-0x01', expected: '-0x1'},
{ value: -15, expected: '-0xf'},
{ value: '-15', expected: '-0xf'},
{ value: '-0xf', expected: '-0xf'},
{ value: '-0x0f', expected: '-0xf'},
{ value: '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'},
{ value: '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd'},
{ value: '-0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '-0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'},
{ value: '-0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '-0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd'},
{ value: 0, expected: '0x0'},
{ value: '0', expected: '0x0'},
{ value: '0x0', expected: '0x0'},
{ value: -0, expected: '0x0'},
{ value: '-0', expected: '0x0'},
{ value: '-0x0', expected: '0x0'}
];
describe('utils', function () {
describe('fromDecimal', function () {
it('should return the correct value', function () {
assert.equal(utils.fromDecimal(1000), "0x3e8");
assert.equal(utils.fromDecimal('1000'), "0x3e8");
tests.forEach(function (test) {
it('should turn ' + test.value + ' to ' + test.expected, function () {
assert.equal(utils.fromDecimal(test.value), test.expected);
});
});
});
});
});

View File

@ -34,12 +34,6 @@ var tests = [
describe('utils', function () {
describe('toBigNumber', function () {
it('should return the correct value', function () {
assert.equal(utils.isBigNumber(utils.toBigNumber(100000)), true);
assert.equal(utils.toBigNumber(100000).toString(10), '100000');
});
tests.forEach(function (test) {
it('should turn ' + test.value + ' to ' + test.expected, function () {
assert.equal(utils.toBigNumber(test.value).toString(10), test.expected);

View File

@ -1,16 +1,43 @@
var assert = require('assert');
var chai = require('chai');
var utils = require('../lib/utils.js');
var assert = chai.assert;
var tests = [
{ value: 1, expected: '0x1' },
{ value: '1', expected: '0x1' },
{ value: '0x1', expected: '0x1'},
{ value: '0x01', expected: '0x1'},
{ value: 15, expected: '0xf'},
{ value: '15', expected: '0xf'},
{ value: '0xf', expected: '0xf'},
{ value: '0x0f', expected: '0xf'},
{ value: -1, expected: '-0x1'},
{ value: '-1', expected: '-0x1'},
{ value: '-0x1', expected: '-0x1'},
{ value: '-0x01', expected: '-0x1'},
{ value: -15, expected: '-0xf'},
{ value: '-15', expected: '-0xf'},
{ value: '-0xf', expected: '-0xf'},
{ value: '-0x0f', expected: '-0xf'},
{ value: '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'},
{ value: '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd'},
{ value: '-0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '-0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'},
{ value: '-0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '-0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd'},
{ value: 0, expected: '0x0'},
{ value: '0', expected: '0x0'},
{ value: '0x0', expected: '0x0'},
{ value: -0, expected: '0x0'},
{ value: '-0', expected: '0x0'},
{ value: '-0x0', expected: '0x0'}
];
describe('utils', function () {
describe('toHex', function () {
it('should return the correct value', function () {
assert.equal(utils.toHex(1000), "0x3e8");
assert.equal(utils.toHex('1000'), "0x3e8");
//assert.equal(utils.toHex('hello'), "0x68656c6c6f");
//assert.equal(utils.toHex({test: 'test'}), "0x7b2274657374223a2274657374227d");
//assert.equal(utils.toHex([1,2,3,{hallo:'test'}]), '0x5b312c322c332c7b2268616c6c6f223a2274657374227d5d');
tests.forEach(function (test) {
it('should turn ' + test.value + ' to ' + test.expected, function () {
assert.equal(utils.toHex(test.value), test.expected);
});
});
});
});