Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80559 views
1
var assert = require('assert');
2
var BN = require('../').BN;
3
var fixtures = require('./fixtures');
4
5
describe('BN.js/Constructor', function() {
6
describe('with Smi input', function() {
7
it('should accept one limb number', function() {
8
assert.equal(new BN(12345).toString(16), '3039');
9
});
10
11
it('should accept two-limb number', function() {
12
assert.equal(new BN(0x4123456).toString(16), '4123456');
13
});
14
15
it('should accept 52 bits of precision', function() {
16
var num = Math.pow(2, 52);
17
assert.equal(new BN(num, 10).toString(10), num.toString(10));
18
});
19
20
it('should accept max safe integer', function() {
21
var num = Math.pow(2, 53) - 1;
22
assert.equal(new BN(num, 10).toString(10), num.toString(10));
23
});
24
25
it('should not accept an unsafe integer', function() {
26
var num = Math.pow(2, 53);
27
assert.throws(function() { new BN(num, 10); });
28
});
29
30
});
31
32
describe('with String input', function() {
33
it('should accept base-16', function() {
34
assert.equal(new BN('1A6B765D8CDF', 16).toString(16), '1a6b765d8cdf');
35
assert.equal(new BN('1A6B765D8CDF', 16).toString(), '29048849665247');
36
});
37
38
it('should accept base-hex', function() {
39
assert.equal(new BN('FF', 'hex').toString(), '255');
40
});
41
42
it('should accept base-16 with spaces', function() {
43
var num = 'a89c e5af8724 c0a23e0e 0ff77500';
44
assert.equal(new BN(num, 16).toString(16), num.replace(/ /g, ''));
45
});
46
47
it('should accept long base-16', function() {
48
var num = '123456789abcdef123456789abcdef123456789abcdef';
49
assert.equal(new BN(num, 16).toString(16), num);
50
});
51
52
it('should accept positive base-10', function() {
53
assert.equal(new BN('10654321').toString(), '10654321');
54
assert.equal(new BN('29048849665247').toString(16), '1a6b765d8cdf');
55
});
56
57
it('should accept negative base-10', function() {
58
assert.equal(new BN('-29048849665247').toString(16), '-1a6b765d8cdf');
59
});
60
61
it('should accept long base-10', function() {
62
var num = '10000000000000000';
63
assert.equal(new BN(num).toString(10), num);
64
});
65
66
it('should accept base-2', function() {
67
var base2 = '11111111111111111111111111111111111111111111111111111';
68
assert.equal(new BN(base2, 2).toString(2), base2);
69
});
70
71
it('should accept base-36', function() {
72
var base36 = 'zzZzzzZzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz';
73
assert.equal(new BN(base36, 36).toString(36), base36.toLowerCase());
74
});
75
76
it('should not overflow limbs during base-10', function() {
77
var num = '65820182292848241686198767302293' +
78
'20890292528855852623664389292032';
79
assert(new BN(num).words[0] < 0x4000000);
80
});
81
});
82
83
describe('with Array input', function() {
84
it('should not fail on empty array', function() {
85
assert.equal(new BN([ ]).toString(16), '0');
86
});
87
88
it('should import/export big endian', function() {
89
assert.equal(new BN([ 1, 2, 3 ]).toString(16), '10203');
90
assert.equal(new BN([ 1, 2, 3, 4 ]).toString(16), '1020304');
91
assert.equal(new BN([ 1, 2, 3, 4, 5 ]).toString(16), '102030405');
92
assert.equal(new BN([ 1, 2, 3, 4, 5, 6, 7, 8 ]).toString(16),
93
'102030405060708');
94
assert.equal(new BN([ 1, 2, 3, 4 ]).toArray().join(','), '1,2,3,4');
95
assert.equal(new BN([ 1, 2, 3, 4, 5, 6, 7, 8 ]).toArray().join(','),
96
'1,2,3,4,5,6,7,8');
97
});
98
99
it('should import little endian', function() {
100
assert.equal(new BN([ 1, 2, 3 ], 10, 'le').toString(16), '30201');
101
assert.equal(new BN([ 1, 2, 3, 4 ], 10, 'le').toString(16), '4030201');
102
assert.equal(new BN([ 1, 2, 3, 4, 5 ], 10, 'le').toString(16),
103
'504030201');
104
assert.equal(new BN([ 1, 2, 3, 4, 5, 6, 7, 8 ], 'le').toString(16),
105
'807060504030201');
106
});
107
108
it('should import big endian with implicit base', function() {
109
assert.equal(new BN([ 1, 2, 3, 4, 5 ], 'le').toString(16), '504030201');
110
});
111
});
112
113
describe('with BN input', function() {
114
it('should clone BN', function() {
115
var num = new BN(12345);
116
assert.equal(new BN(num).toString(10), '12345');
117
});
118
});
119
});
120
121