• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1if (typeof T === 'undefined') require('../setup');
2
3T('Decimal', function () {
4
5  Decimal.config({
6    precision: 40,
7    rounding: 4,
8    toExpNeg: -9e15,
9    toExpPos: 9e15,
10    maxE: 9e15,
11    minE: -9e15,
12    crypto: false,
13    modulo: 1
14  });
15
16  var t = function (coefficient, exponent, sign, n) {
17    T.assertEqualProps(coefficient, exponent, sign, new Decimal(n));
18  }
19
20  t([0], 0, 1, 0);
21  t([0], 0, -1, -0);
22  t([1], 0, -1, -1);
23  t([10], 1, -1, -10);
24
25  t([1], 0, 1, 1);
26  t([10], 1, 1, 10);
27  t([100], 2, 1, 100);
28  t([1000], 3, 1, 1000);
29  t([10000], 4, 1, 10000);
30  t([100000], 5, 1, 100000);
31  t([1000000], 6, 1, 1000000);
32
33  t([1], 7, 1, 10000000);
34  t([10], 8, 1, 100000000);
35  t([100], 9, 1, 1000000000);
36  t([1000], 10, 1, 10000000000);
37  t([10000], 11, 1, 100000000000);
38  t([100000], 12, 1, 1000000000000);
39  t([1000000], 13, 1, 10000000000000);
40
41  t([1], 14, -1, -100000000000000);
42  t([10], 15, -1, -1000000000000000);
43  t([100], 16, -1, -10000000000000000);
44  t([1000], 17, -1, -100000000000000000);
45  t([10000], 18, -1, -1000000000000000000);
46  t([100000], 19, -1, -10000000000000000000);
47  t([1000000], 20, -1, -100000000000000000000);
48
49  t([1000000], -1, 1, 1e-1);
50  t([100000], -2, -1, -1e-2);
51  t([10000], -3, 1, 1e-3);
52  t([1000], -4, -1, -1e-4);
53  t([100], -5, 1, 1e-5);
54  t([10], -6, -1, -1e-6);
55  t([1], -7, 1, 1e-7);
56
57  t([1000000], -8, 1, 1e-8);
58  t([100000], -9, -1, -1e-9);
59  t([10000], -10, 1, 1e-10);
60  t([1000], -11, -1, -1e-11);
61  t([100], -12, 1, 1e-12);
62  t([10], -13, -1, -1e-13);
63  t([1], -14, 1, 1e-14);
64
65  t([1000000], -15, 1, 1e-15);
66  t([100000], -16, -1, -1e-16);
67  t([10000], -17, 1, 1e-17);
68  t([1000], -18, -1, -1e-18);
69  t([100], -19, 1, 1e-19);
70  t([10], -20, -1, -1e-20);
71  t([1], -21, 1, 1e-21);
72
73  t([9], 0, 1, '9');
74  t([99], 1, -1, '-99');
75  t([999], 2, 1, '999');
76  t([9999], 3, -1, '-9999');
77  t([99999], 4, 1, '99999');
78  t([999999], 5, -1, '-999999');
79  t([9999999], 6, 1, '9999999');
80
81  t([9, 9999999], 7, -1, '-99999999');
82  t([99, 9999999], 8, 1, '999999999');
83  t([999, 9999999], 9, -1, '-9999999999');
84  t([9999, 9999999], 10, 1, '99999999999');
85  t([99999, 9999999], 11, -1, '-999999999999');
86  t([999999, 9999999], 12, 1, '9999999999999');
87  t([9999999, 9999999], 13, -1, '-99999999999999');
88
89  t([9, 9999999, 9999999], 14, 1, '999999999999999');
90  t([99, 9999999, 9999999], 15, -1, '-9999999999999999');
91  t([999, 9999999, 9999999], 16, 1, '99999999999999999');
92  t([9999, 9999999, 9999999], 17, -1, '-999999999999999999');
93  t([99999, 9999999, 9999999], 18, 1, '9999999999999999999');
94  t([999999, 9999999, 9999999], 19, -1, '-99999999999999999999');
95  t([9999999, 9999999, 9999999], 20, 1, '999999999999999999999');
96
97  // Test base conversion.
98
99  t = function (expected, n) {
100    T.assertEqual(expected, new Decimal(n).valueOf());
101  }
102
103  function randInt() {
104    return Math.floor(Math.random() * 0x20000000000000 / Math.pow(10, Math.random() * 16 | 0));
105  }
106
107  // Test random integers against Number.prototype.toString(base).
108  for (var k, i = 0; i < 127; i++) {
109    k = randInt();
110    t(k.toString(), '0b' + k.toString(2));
111    k = randInt();
112    t(k.toString(), '0B' + k.toString(2));
113    k = randInt();
114    t(k.toString(), '0o' + k.toString(8));
115    k = randInt();
116    t(k.toString(), '0O' + k.toString(8));
117    k = randInt();
118    t(k.toString(), '0x' + k.toString(16));
119    k = randInt();
120    t(k.toString(), '0X' + k.toString(16));
121  }
122
123  // Binary.
124  t('0', '0b0');
125  t('0', '0B0');
126  t('-5', '-0b101');
127  t('5', '+0b101');
128  t('1.5', '0b1.1');
129  t('-1.5', '-0b1.1');
130
131  t('18181', '0b100011100000101.00');
132  t('-12.5', '-0b1100.10');
133  t('343872.5', '0b1010011111101000000.10');
134  t('-328.28125', '-0b101001000.010010');
135  t('-341919.144535064697265625', '-0b1010011011110011111.0010010100000000010');
136  t('97.10482025146484375', '0b1100001.000110101101010110000');
137  t('-120914.40625', '-0b11101100001010010.01101');
138  t('8080777260861123367657', '0b1101101100000111101001111111010001111010111011001010100101001001011101001');
139
140  // Octal.
141  t('8', '0o10');
142  t('-8.5', '-0O010.4');
143  t('8.5', '+0O010.4');
144  t('-262144.000000059604644775390625', '-0o1000000.00000001');
145  t('572315667420.390625', '0o10250053005734.31');
146
147  // Hex.
148  t('1', '0x00001');
149  t('255', '0xff');
150  t('-15.5', '-0Xf.8');
151  t('15.5', '+0Xf.8');
152  t('-16777216.00000000023283064365386962890625', '-0x1000000.00000001');
153  t('325927753012307620476767402981591827744994693483231017778102969592507', '0xc16de7aa5bf90c3755ef4dea45e982b351b6e00cd25a82dcfe0646abb');
154
155  // Test parsing.
156
157  var tx = function (fn, msg) {
158    T.assertException(fn, msg);
159  }
160
161  t('NaN', NaN);
162  t('NaN', -NaN);
163  t('NaN', 'NaN');
164  t('NaN', '-NaN');
165  t('NaN', '+NaN');
166
167  tx(function () {new Decimal(' NaN')}, "' NaN'");
168  tx(function () {new Decimal('NaN ')}, "'NaN '");
169  tx(function () {new Decimal(' NaN ')}, "' NaN '");
170  tx(function () {new Decimal(' -NaN')}, "' -NaN'");
171  tx(function () {new Decimal(' +NaN')}, "' +NaN'");
172  tx(function () {new Decimal('-NaN ')}, "'-NaN '");
173  tx(function () {new Decimal('+NaN ')}, "'+NaN '");
174  tx(function () {new Decimal('.NaN')}, "'.NaN'");
175  tx(function () {new Decimal('NaN.')}, "'NaN.'");
176
177  t('Infinity', Infinity);
178  t('-Infinity', -Infinity);
179  t('Infinity', 'Infinity');
180  t('-Infinity', '-Infinity');
181  t('Infinity', '+Infinity');
182
183  tx(function () {new Decimal(' Infinity')}, "' Infinity '");
184  tx(function () {new Decimal('Infinity ')}, "'Infinity '");
185  tx(function () {new Decimal(' Infinity ')}, "' Infinity '");
186  tx(function () {new Decimal(' -Infinity')}, "' -Infinity'");
187  tx(function () {new Decimal(' +Infinity')}, "' +Infinity'");
188  tx(function () {new Decimal('.Infinity')}, "'.Infinity'");
189  tx(function () {new Decimal('Infinity.')}, "'Infinity.'");
190
191  t('0', 0);
192  t('-0', -0);
193  t('0', '0');
194  t('-0', '-0');
195  t('0', '0.');
196  t('-0', '-0.');
197  t('0', '0.0');
198  t('-0', '-0.0');
199  t('0', '0.00000000');
200  t('-0', '-0.0000000000000000000000');
201
202  tx(function () {new Decimal(' 0')}, "' 0'");
203  tx(function () {new Decimal('0 ')}, "'0 '");
204  tx(function () {new Decimal(' 0 ')}, "' 0 '");
205  tx(function () {new Decimal('0-')}, "'0-'");
206  tx(function () {new Decimal(' -0')}, "' -0'");
207  tx(function () {new Decimal('-0 ')}, "'-0 '");
208  tx(function () {new Decimal('+0 ')}, "'+0 '");
209  tx(function () {new Decimal(' +0')}, "' +0'");
210  tx(function () {new Decimal(' .0')}, "' .0'");
211  tx(function () {new Decimal('0. ')}, "'0. '");
212  tx(function () {new Decimal('+-0')}, "'+-0'");
213  tx(function () {new Decimal('-+0')}, "'-+0'");
214  tx(function () {new Decimal('--0')}, "'--0'");
215  tx(function () {new Decimal('++0')}, "'++0'");
216  tx(function () {new Decimal('.-0')}, "'.-0'");
217  tx(function () {new Decimal('.+0')}, "'.+0'");
218  tx(function () {new Decimal('0 .')}, "'0 .'");
219  tx(function () {new Decimal('. 0')}, "'. 0'");
220  tx(function () {new Decimal('..0')}, "'..0'");
221  tx(function () {new Decimal('+.-0')}, "'+.-0'");
222  tx(function () {new Decimal('-.+0')}, "'-.+0'");
223  tx(function () {new Decimal('+. 0')}, "'+. 0'");
224  tx(function () {new Decimal('.0.')}, "'.0.'");
225
226  t('1', 1);
227  t('-1', -1);
228  t('1', '1');
229  t('-1', '-1');
230  t('0.1', '.1');
231  t('0.1', '.1');
232  t('-0.1', '-.1');
233  t('0.1', '+.1');
234  t('1', '1.');
235  t('1', '1.0');
236  t('-1', '-1.');
237  t('1', '+1.');
238  t('-1', '-1.0000');
239  t('1', '1.0000');
240  t('1', '1.00000000');
241  t('-1', '-1.000000000000000000000000');
242  t('1', '+1.000000000000000000000000');
243
244  tx(function () {new Decimal(' 1')}, "' 1'");
245  tx(function () {new Decimal('1 ')}, "'1 '");
246  tx(function () {new Decimal(' 1 ')}, "' 1 '");
247  tx(function () {new Decimal('1-')}, "'1-'");
248  tx(function () {new Decimal(' -1')}, "' -1'");
249  tx(function () {new Decimal('-1 ')}, "'-1 '");
250  tx(function () {new Decimal(' +1')}, "' +1'");
251  tx(function () {new Decimal('+1 ')}, "'+1'");
252  tx(function () {new Decimal('.1.')}, "'.1.'");
253  tx(function () {new Decimal('+-1')}, "'+-1'");
254  tx(function () {new Decimal('-+1')}, "'-+1'");
255  tx(function () {new Decimal('--1')}, "'--1'");
256  tx(function () {new Decimal('++1')}, "'++1'");
257  tx(function () {new Decimal('.-1')}, "'.-1'");
258  tx(function () {new Decimal('.+1')}, "'.+1'");
259  tx(function () {new Decimal('1 .')}, "'1 .'");
260  tx(function () {new Decimal('. 1')}, "'. 1'");
261  tx(function () {new Decimal('..1')}, "'..1'");
262  tx(function () {new Decimal('+.-1')}, "'+.-1'");
263  tx(function () {new Decimal('-.+1')}, "'-.+1'");
264  tx(function () {new Decimal('+. 1')}, "'+. 1'");
265  tx(function () {new Decimal('-. 1')}, "'-. 1'");
266  tx(function () {new Decimal('1..')}, "'1..'");
267  tx(function () {new Decimal('+1..')}, "'+1..'");
268  tx(function () {new Decimal('-1..')}, "'-1..'");
269  tx(function () {new Decimal('-.1.')}, "'-.1.'");
270  tx(function () {new Decimal('+.1.')}, "'+.1.'");
271  tx(function () {new Decimal('.-10.')}, "'.-10.'");
272  tx(function () {new Decimal('.+10.')}, "'.+10.'");
273  tx(function () {new Decimal('. 10.')}, "'. 10.'");
274
275  t('123.456789', 123.456789);
276  t('-123.456789', -123.456789);
277  t('-123.456789', '-123.456789');
278  t('123.456789', '123.456789');
279  t('123.456789', '+123.456789');
280
281  tx(function () {new Decimal(void 0)}, "void 0");
282  tx(function () {new Decimal('undefined')}, "'undefined'");
283  tx(function () {new Decimal(null)}, "null");
284  tx(function () {new Decimal('null')}, "'null'");
285  tx(function () {new Decimal({})}, "{}");
286  tx(function () {new Decimal([])}, "[]");
287  tx(function () {new Decimal(function () {})}, "function () {}");
288  tx(function () {new Decimal(new Date)}, "new Date");
289  tx(function () {new Decimal(new RegExp)}, "new RegExp");
290  tx(function () {new Decimal('')}, "''");
291  tx(function () {new Decimal(' ')}, "' '");
292  tx(function () {new Decimal('nan')}, "'nan'");
293  tx(function () {new Decimal('23e')}, "'23e'");
294  tx(function () {new Decimal('e4')}, "'e4'");
295  tx(function () {new Decimal('ff')}, "'ff'");
296  tx(function () {new Decimal('0xg')}, "'oxg'");
297  tx(function () {new Decimal('0Xfi')}, "'0Xfi'");
298  tx(function () {new Decimal('++45')}, "'++45'");
299  tx(function () {new Decimal('--45')}, "'--45'");
300  tx(function () {new Decimal('9.99--')}, "'9.99--'");
301  tx(function () {new Decimal('9.99++')}, "'9.99++'");
302  tx(function () {new Decimal('0 0')}, "'0 0'");
303});
304