1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-09-28 22:40:48 +00:00
MikeMcl_decimal.js/test/modules/random.js

30 lines
719 B
JavaScript
Raw Normal View History

2016-01-25 00:11:32 +00:00
if (typeof T === 'undefined') require('../setup');
2016-02-06 17:51:29 +00:00
T('random', function () {
var i, sd, maxDigits;
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
function tx(fn, msg) {
T.assertException(fn, msg);
}
2016-01-25 00:11:32 +00:00
2016-02-06 17:51:29 +00:00
maxDigits = 100;
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
for (i = 0; i < 996; i++) {
sd = Math.random() * maxDigits + 1 | 0;
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
if (Math.random() > 0.5) {
Decimal.precision = sd;
r = Decimal.random();
} else {
r = Decimal.random(sd);
2016-01-25 00:11:32 +00:00
}
2016-02-04 23:52:10 +00:00
T.assert(r.sd() <= sd && r.gte(0) && r.lt(1) && r.eq(r) && r.eq(r.valueOf()));
}
tx(function () { Decimal.random(Infinity) }, 'Infinity');
tx(function () { Decimal.random('-Infinity') }, "'-Infinity'");
tx(function () { Decimal.random(NaN) }, 'NaN');
tx(function () { Decimal.random(null) }, 'null');
2016-02-06 17:51:29 +00:00
});