forked from web3/web3.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.toTwosComplement.js
41 lines (38 loc) · 1.86 KB
/
utils.toTwosComplement.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Created by danielbruce on 2017-09-25.
*/
/**
* Created by danielbruce on 2017-09-25.
*/
var chai = require('chai');
var utils = require('../lib/utils/utils');
var BigNumber = require('bignumber.js');
var assert = chai.assert;
var tests = [
{ value: 1, expected: new BigNumber(1).round() },
{ value: '1', expected: new BigNumber(1) },
{ value: '0x1', expected: new BigNumber(1) },
{ value: '15', expected: new BigNumber(15)},
{ value: '0xf', expected: new BigNumber(15)},
{ value: -1, expected: new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(new BigNumber(-1)).plus(1)},
{ value: '-1', expected: new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(new BigNumber(-1)).plus(1)},
{ value: '-0x1', expected: new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(new BigNumber(-1)).plus(1)},
{ value: '-15', expected: new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(new BigNumber(-15)).plus(1)},
{ value: '-0xf', expected: new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(new BigNumber(-15)).plus(1)},
{ value: 0, expected: new BigNumber(0)},
{ value: '0', expected: new BigNumber(0)},
{ value: '0x0', expected: new BigNumber(0)},
{ value: -0, expected: new BigNumber(0)},
{ value: '-0', expected: new BigNumber(0)},
{ value: '-0x0', expected: new BigNumber(0)},
{ value: new BigNumber(15), expected: new BigNumber(15)}
];
describe('lib/utils/utils', function () {
describe('toTwosComplement', function () {
tests.forEach(function (test) {
it('printing ' + test.value, function () {
assert(test.expected.equals(utils.toTwosComplement(test.value)));
});
});
});
});