2014-04-02 15:28:08 +00:00
|
|
|
var count = (function random(Decimal) {
|
|
|
|
var start = +new Date(),
|
2014-06-04 22:59:07 +00:00
|
|
|
dp, error, i, j, k, log, m, r,
|
2014-04-02 15:28:08 +00:00
|
|
|
passed = 0,
|
|
|
|
total = 0;
|
|
|
|
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
log = console.log;
|
|
|
|
error = console.error;
|
|
|
|
} else {
|
|
|
|
log = function (str) { document.body.innerHTML += str.replace('\n', '<br>') };
|
|
|
|
error = function (str) { document.body.innerHTML += '<div style="color: red">' +
|
|
|
|
str.replace('\n', '<br>') + '</div>' };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Decimal && typeof require === 'function') {
|
2014-06-04 22:59:07 +00:00
|
|
|
Decimal = require('../decimal.js');
|
2014-04-02 15:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function assertException(func, message) {
|
|
|
|
var actual;
|
|
|
|
total++;
|
|
|
|
try {
|
|
|
|
func();
|
|
|
|
} catch (e) {
|
|
|
|
actual = e;
|
|
|
|
}
|
|
|
|
if (actual && actual.name == 'Decimal Error') {
|
|
|
|
passed++;
|
|
|
|
//log('\n Expected and actual: ' + actual);
|
|
|
|
} else {
|
|
|
|
error('\n Test number: ' + total + ' failed');
|
|
|
|
error('\n Expected: ' + message + ' to raise a Decimal Error.');
|
|
|
|
error(' Actual: ' + (actual || 'no exception'));
|
|
|
|
//process.exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log('\n Testing random...');
|
|
|
|
|
2014-06-04 22:59:07 +00:00
|
|
|
Decimal.config({ errors: true, crypto: false });
|
2014-04-02 15:28:08 +00:00
|
|
|
|
2014-06-04 22:59:07 +00:00
|
|
|
for ( i = 0; i < 9996; i++ ) {
|
2014-04-02 15:28:08 +00:00
|
|
|
|
2014-06-04 22:59:07 +00:00
|
|
|
//Decimal.crypto = false;
|
|
|
|
//Decimal.crypto = true;
|
|
|
|
// 50% chance that Decimal.crypto is true.
|
|
|
|
//Decimal.crypto = Math.random() > 0.5;
|
2014-04-02 15:28:08 +00:00
|
|
|
|
2014-06-04 22:59:07 +00:00
|
|
|
if ( Math.random() > 0.5 ) {
|
|
|
|
dp = Decimal.precision = Math.random() * 10 + 1 | 0;
|
|
|
|
r = Decimal.random();
|
|
|
|
} else {
|
|
|
|
dp = Math.random() * 10 | 0;
|
|
|
|
r = Decimal.random(dp);
|
|
|
|
}
|
2014-04-02 15:28:08 +00:00
|
|
|
|
2014-06-04 22:59:07 +00:00
|
|
|
//log(r.toString());
|
2014-04-02 15:28:08 +00:00
|
|
|
|
2014-06-04 22:59:07 +00:00
|
|
|
if ( r.c[0] ) {
|
|
|
|
j = r.c.length;
|
|
|
|
k = r.c[j - 1];
|
|
|
|
j *= 7;
|
2014-04-02 15:28:08 +00:00
|
|
|
|
2014-06-04 22:59:07 +00:00
|
|
|
// Decrement for trailing zeros in last element of r.c.
|
|
|
|
for ( ; k % 10 === 0; k /= 10, j-- );
|
|
|
|
} else {
|
|
|
|
j = 0;
|
|
|
|
}
|
2014-04-02 15:28:08 +00:00
|
|
|
|
2014-06-04 22:59:07 +00:00
|
|
|
// Check number of decimal places (j is actual dp).
|
|
|
|
if ( j > dp ) {
|
|
|
|
m = ' r.c.length - r.e - 1 > dp';
|
2014-04-02 15:28:08 +00:00
|
|
|
|
2014-06-04 22:59:07 +00:00
|
|
|
// Check 0 <= r < 1
|
|
|
|
} else if ( r.lt(0) || r.gte(1) ) {
|
|
|
|
m = ' r.lt(0) || r.gte(1)';
|
2014-04-02 15:28:08 +00:00
|
|
|
|
2014-06-04 22:59:07 +00:00
|
|
|
// Check that the attributes of r are formed correctly.
|
|
|
|
} else if ( !r.eq( new Decimal(r) ) || !r.eq( new Decimal( r.toString() ) ) ) {
|
|
|
|
m = ' !r.eq( new Decimal(r) ) || !r.eq( new Decimal( r.toString() ) )';
|
|
|
|
}
|
2014-04-02 15:28:08 +00:00
|
|
|
|
2014-06-04 22:59:07 +00:00
|
|
|
total++;
|
2014-04-02 15:28:08 +00:00
|
|
|
|
2014-06-04 22:59:07 +00:00
|
|
|
if (m) {
|
|
|
|
error('\n Test number: ' + total + ' failed');
|
|
|
|
error(m);
|
|
|
|
error(' r: ' + r);
|
|
|
|
error(' r.c: ' + r.c);
|
|
|
|
error(' r.e: ' + r.e);
|
|
|
|
error(' r.s: ' + r.s);
|
|
|
|
error(' dp: ' + dp);
|
|
|
|
m = null;
|
|
|
|
} else {
|
|
|
|
passed++;
|
2014-04-02 15:28:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertException(function () { Decimal.random(Infinity) }, 'Infinity');
|
|
|
|
assertException(function () { Decimal.random('-Infinity') }, "'-Infinity'");
|
|
|
|
assertException(function () { Decimal.random(NaN) }, 'NaN');
|
|
|
|
assertException(function () { Decimal.random('nanny') }, "'nanny'");
|
|
|
|
|
|
|
|
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
|
|
|
|
return [passed, total];
|
|
|
|
})(this.Decimal);
|
|
|
|
if (typeof module !== 'undefined' && module.exports) module.exports = count;
|