-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest.js
205 lines (171 loc) · 5.98 KB
/
test.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var format,
time = process.hrtime();
passed = 0,
total = 0,
Big = require('./toFormat')(require('big')),
Decimal = require('./toFormat')(require('decimal'));
function T(expected, value, dp){
if (isFinite(value)) {
++total;
if (expected === new Big(value).toFormat(dp)) {
++passed;
} else {
console.error('\n Test number: ' + total + ' failed (Big)');
console.error(' Expected: ' + expected);
console.error(' Actual: ' + actual);
}
}
++total;
if (expected === new Decimal(value).toFormat(dp)) {
++passed
} else {
console.error('\n Test number: ' + total + ' failed (Decimal)');
console.error(' Expected: ' + expected);
console.error(' Actual: ' + actual);
}
}
console.log('\n Testing toFormat...');
format = {
decimalSeparator: '.',
groupSeparator: ',',
groupSize: 3,
secondaryGroupSize: 0,
fractionGroupSeparator: ' ',
fractionGroupSize: 0
};
Big.format = Decimal.format = format;
T('0', 0);
T('1', 1);
T('-1', -1);
T('123.456', 123.456);
T('NaN', NaN);
T('Infinity', 1/0);
T('-Infinity', -1/0);
T('123.456', 123.456, 3);
T('NaN', NaN, 0);
T('Infinity', 1/0, 3);
T('-Infinity', -1/0, 0);
T('0.0', 0, 1);
T('1.00', 1, 2);
T('-1.000', -1, 3);
T('123.4560', 123.456, 4);
T('NaN', NaN, 5);
T('Infinity', 1/0, 6);
T('-Infinity', -1/0, 7);
T('9,876.54321', 9876.54321);
T('4,018,736,400,000,000,000,000', '4.0187364e+21');
T('999,999,999,999,999', 999999999999999);
T('99,999,999,999,999', 99999999999999);
T('9,999,999,999,999', 9999999999999);
T('999,999,999,999', 999999999999);
T('99,999,999,999', 99999999999);
T('9,999,999,999', 9999999999);
T('999,999,999', 999999999);
T('99,999,999', 99999999);
T('9,999,999', 9999999);
T('999,999', 999999);
T('99,999', 99999);
T('9,999', 9999);
T('999', 999);
T('99', 99);
T('9', 9);
T('76,852.342091', '7.6852342091e+4');
format.groupSeparator = ' ';
T('76 852.34', '7.6852342091e+4', 2);
T('76 852.342091', '7.6852342091e+4');
T('76 852.3420910871', '7.6852342091087145832640897e+4', 10);
format.fractionGroupSize = 5;
T('4 018 736 400 000 000 000 000', '4.0187364e+21');
T('76 852.34209 10871 45832 64089', '7.685234209108714583264089e+4', 20);
T('76 852.34209 10871 45832 64089 7', '7.6852342091087145832640897e+4', 21);
T('76 852.34209 10871 45832 64089 70000', '7.6852342091087145832640897e+4', 25);
T('999 999 999 999 999', 999999999999999, 0);
T('99 999 999 999 999.0', 99999999999999, 1);
T('9 999 999 999 999.00', 9999999999999, 2);
T('999 999 999 999.000', 999999999999, 3);
T('99 999 999 999.0000', 99999999999, 4);
T('9 999 999 999.00000', 9999999999, 5);
T('999 999 999.00000 0', 999999999, 6);
T('99 999 999.00000 00', 99999999, 7);
T('9 999 999.00000 000', 9999999, 8);
T('999 999.00000 0000', 999999, 9);
T('99 999.00000 00000', 99999, 10);
T('9 999.00000 00000 0', 9999, 11);
T('999.00000 00000 00', 999, 12);
T('99.00000 00000 000', 99, 13);
T('9.00000 00000 0000', 9, 14);
T('1.00000 00000 00000', 1, 15);
T('1.00000 00000 0000', 1, 14);
T('1.00000 00000 000', 1, 13);
T('1.00000 00000 00', 1, 12);
T('1.00000 00000 0', 1, 11);
T('1.00000 00000', 1, 10);
T('1.00000 0000', 1, 9);
format.fractionGroupSize = 0;
T('4 018 736 400 000 000 000 000', '4.0187364e+21');
T('76 852.34209108714583264089', '7.685234209108714583264089e+4', 20);
T('76 852.342091087145832640897', '7.6852342091087145832640897e+4', 21);
T('76 852.3420910871458326408970000', '7.6852342091087145832640897e+4', 25);
T('999 999 999 999 999', 999999999999999, 0);
T('99 999 999 999 999.0', 99999999999999, 1);
T('9 999 999 999 999.00', 9999999999999, 2);
T('999 999 999 999.000', 999999999999, 3);
T('99 999 999 999.0000', 99999999999, 4);
T('9 999 999 999.00000', 9999999999, 5);
T('999 999 999.000000', 999999999, 6);
T('99 999 999.0000000', 99999999, 7);
T('9 999 999.00000000', 9999999, 8);
T('999 999.000000000', 999999, 9);
T('99 999.0000000000', 99999, 10);
T('9 999.00000000000', 9999, 11);
T('999.000000000000', 999, 12);
T('99.0000000000000', 99, 13);
T('9.00000000000000', 9, 14);
T('1.000000000000000', 1, 15);
T('1.00000000000000', 1, 14);
T('1.0000000000000', 1, 13);
T('1.000000000000', 1, 12);
T('1.00000000000', 1, 11);
T('1.0000000000', 1, 10);
T('1.000000000', 1, 9);
format = {
decimalSeparator: '.',
groupSeparator: ',',
groupSize: 3,
secondaryGroupSize: 2
};
Big.format = Decimal.format = format;
T('9,876.54321', 9876.54321);
T('10,00,037.123', '1000037.123456789', 3);
T('4,01,87,36,40,00,00,00,00,00,000', '4.0187364e+21');
T('99,99,99,99,99,99,999', 999999999999999);
T('9,99,99,99,99,99,999', 99999999999999);
T('99,99,99,99,99,999', 9999999999999);
T('9,99,99,99,99,999', 999999999999);
T('99,99,99,99,999', 99999999999);
T('9,99,99,99,999', 9999999999);
T('99,99,99,999', 999999999);
T('9,99,99,999', 99999999);
T('99,99,999', 9999999);
T('9,99,999', 999999);
T('99,999', 99999);
T('9,999', 9999);
T('999', 999);
T('99', 99);
T('9', 9);
format.decimalSeparator = ',';
format.groupSeparator = '.';
T('1.23.45.60.000,000000000008', '1.23456000000000000000789e+9', 12);
format.groupSeparator = '';
T('10000000000123456789000000,0000000001', '10000000000123456789000000.000000000100000001', 10);
format.groupSeparator = ' ';
format.groupSize = 1;
format.secondaryGroupSize = 4;
T('4658 0734 6509 8347 6580 3645 0,6', '4658073465098347658036450.59764985763489569875659876459', 1);
format.fractionGroupSize = 2;
format.fractionGroupSeparator = ':';
format.secondaryGroupSize = null;
T('4 6 5 8 0 7 3 4 6 5 0 9 8 3 4 7 6 5 8 0 3 6 4 5 0,59:76:49:85:76:34:89:56:98:75:65:98:76:45:9', '4658073465098347658036450.59764985763489569875659876459' );
time = process.hrtime(time);
time = time[0] * 1e3 + (time[1] / 1e6 | 0);
console.log('\n ' + passed + ' of ' + total + ' tests passed in ' + time + ' ms \n');