big integers on abi.js output, tests

This commit is contained in:
Marek Kotewicz 2015-01-16 11:58:26 +01:00
parent f1295b506d
commit 2c36d5ff45
5 changed files with 78 additions and 13 deletions

13
dist/ethereum.js vendored
View File

@ -176,7 +176,16 @@ var setupOutputTypes = function () {
/// Formats input right-aligned input bytes to int /// Formats input right-aligned input bytes to int
/// @returns right-aligned input bytes formatted to int /// @returns right-aligned input bytes formatted to int
var formatInt = function (value) { var formatInt = function (value) {
return value.length <= 8 ? +parseInt(value, 16) : hexToDec(value); // check if it's negative number
// it it is, return two's complement
if (value.substr(0, 1).toLowerCase() === 'f') {
return new BigNumber(value, 16).minus(new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)).minus(1);
}
return new BigNumber(value, 16);
};
var formatUInt = function (value) {
return new BigNumber(value, 16);
}; };
/// @returns right-aligned input bytes formatted to hex /// @returns right-aligned input bytes formatted to hex
@ -200,7 +209,7 @@ var setupOutputTypes = function () {
}; };
return [ return [
{ type: prefixedType('uint'), format: formatInt }, { type: prefixedType('uint'), format: formatUInt },
{ type: prefixedType('int'), format: formatInt }, { type: prefixedType('int'), format: formatInt },
{ type: prefixedType('hash'), format: formatHash }, { type: prefixedType('hash'), format: formatHash },
{ type: prefixedType('string'), format: formatString }, { type: prefixedType('string'), format: formatString },

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -175,7 +175,16 @@ var setupOutputTypes = function () {
/// Formats input right-aligned input bytes to int /// Formats input right-aligned input bytes to int
/// @returns right-aligned input bytes formatted to int /// @returns right-aligned input bytes formatted to int
var formatInt = function (value) { var formatInt = function (value) {
return value.length <= 8 ? +parseInt(value, 16) : hexToDec(value); // check if it's negative number
// it it is, return two's complement
if (value.substr(0, 1).toLowerCase() === 'f') {
return new BigNumber(value, 16).minus(new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)).minus(1);
}
return new BigNumber(value, 16);
};
var formatUInt = function (value) {
return new BigNumber(value, 16);
}; };
/// @returns right-aligned input bytes formatted to hex /// @returns right-aligned input bytes formatted to hex
@ -199,7 +208,7 @@ var setupOutputTypes = function () {
}; };
return [ return [
{ type: prefixedType('uint'), format: formatInt }, { type: prefixedType('uint'), format: formatUInt },
{ type: prefixedType('int'), format: formatInt }, { type: prefixedType('int'), format: formatInt },
{ type: prefixedType('hash'), format: formatHash }, { type: prefixedType('hash'), format: formatHash },
{ type: prefixedType('string'), format: formatString }, { type: prefixedType('string'), format: formatString },

View File

@ -360,6 +360,14 @@ describe('abi', function() {
// then // then
assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1);
assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10);
assert.equal(
parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0].toString(10),
new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).toString(10)
);
assert.equal(
parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0].toString(10),
new BigNumber("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0", 16).toString(10)
);
}); });
it('should parse output uint256', function() { it('should parse output uint256', function() {
@ -377,6 +385,14 @@ describe('abi', function() {
// then // then
assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1);
assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10);
assert.equal(
parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0].toString(10),
new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).toString(10)
);
assert.equal(
parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0].toString(10),
new BigNumber("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0", 16).toString(10)
);
}); });
it('should parse output uint128', function() { it('should parse output uint128', function() {
@ -394,6 +410,14 @@ describe('abi', function() {
// then // then
assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1);
assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10);
assert.equal(
parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0].toString(10),
new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).toString(10)
);
assert.equal(
parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0].toString(10),
new BigNumber("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0", 16).toString(10)
);
}); });
it('should parse output int', function() { it('should parse output int', function() {
@ -411,6 +435,8 @@ describe('abi', function() {
// then // then
assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1);
assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10);
assert.equal(parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0], -1);
assert.equal(parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0], -16);
}); });
it('should parse output int256', function() { it('should parse output int256', function() {
@ -428,6 +454,8 @@ describe('abi', function() {
// then // then
assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1);
assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10);
assert.equal(parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0], -1);
assert.equal(parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0], -16);
}); });
it('should parse output int128', function() { it('should parse output int128', function() {
@ -445,6 +473,8 @@ describe('abi', function() {
// then // then
assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1);
assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10);
assert.equal(parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0], -1);
assert.equal(parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0], -16);
}); });
it('should parse output hash', function() { it('should parse output hash', function() {
@ -460,7 +490,10 @@ describe('abi', function() {
var parser = abi.outputParser(d); var parser = abi.outputParser(d);
// then // then
assert.equal(parser.test("0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0], "0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1") assert.equal(
parser.test("0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0],
"0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1"
);
}); });
it('should parse output hash256', function() { it('should parse output hash256', function() {
@ -476,7 +509,10 @@ describe('abi', function() {
var parser = abi.outputParser(d); var parser = abi.outputParser(d);
// then // then
assert.equal(parser.test("0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0], "0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1") assert.equal(
parser.test("0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0],
"0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1"
);
}); });
it('should parse output hash160', function() { it('should parse output hash160', function() {
@ -492,7 +528,10 @@ describe('abi', function() {
var parser = abi.outputParser(d); var parser = abi.outputParser(d);
// then // then
assert.equal(parser.test("0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0], "0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1") assert.equal(
parser.test("0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0],
"0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1"
);
// TODO shouldnt' the expected hash be shorter? // TODO shouldnt' the expected hash be shorter?
}); });
@ -509,7 +548,10 @@ describe('abi', function() {
var parser = abi.outputParser(d); var parser = abi.outputParser(d);
// then // then
assert.equal(parser.test("0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0], "0x407d73d8a49eeb85d32cf465507dd71d507100c1") assert.equal(
parser.test("0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0],
"0x407d73d8a49eeb85d32cf465507dd71d507100c1"
);
}); });
it('should parse output bool', function() { it('should parse output bool', function() {
@ -545,8 +587,13 @@ describe('abi', function() {
var parser = abi.outputParser(d); var parser = abi.outputParser(d);
// then // then
assert.equal(parser.test("0x68656c6c6f000000000000000000000000000000000000000000000000000000776f726c64000000000000000000000000000000000000000000000000000000")[0], 'hello'); assert.equal(
assert.equal(parser.test("0x68656c6c6f000000000000000000000000000000000000000000000000000000776f726c64000000000000000000000000000000000000000000000000000000")[1], 'world'); parser.test("0x68656c6c6f000000000000000000000000000000000000000000000000000000776f726c64000000000000000000000000000000000000000000000000000000")[0],
'hello'
);
assert.equal(
parser.test("0x68656c6c6f000000000000000000000000000000000000000000000000000000776f726c64000000000000000000000000000000000000000000000000000000")[1],
'world');
}); });