fix runs test

This commit is contained in:
Manuel Araoz 2014-04-23 15:01:04 -03:00
parent 532564461e
commit 34ac69616a
1 changed files with 20 additions and 17 deletions

View File

@ -9,7 +9,7 @@ var should = chai.should();
var Key = bitcore.Key; var Key = bitcore.Key;
describe('Key randomness tests', function() { describe('Key randomness tests', function() {
var RUNS = 100; var RUNS = 128;
it('should pass Frequency (Monobits) Test', function() { it('should pass Frequency (Monobits) Test', function() {
/* /*
Description: The focus of the test is the proportion Description: The focus of the test is the proportion
@ -65,14 +65,16 @@ describe('Key randomness tests', function() {
return (b >> index) & 1; return (b >> index) & 1;
}; };
var getBitInKey = function(key, index) { var getBitInKey = function(key, index) {
var bindex = parseInt(index / key.length); var bindex = parseInt(index / 8);
return getBitInByte(key[bindex], index - bindex * key.length); bindex.should.be.below(key.length);
return getBitInByte(key[bindex], index - bindex * 8);
}; };
var getBitInKeys = function(keys, index) { var getBitInKeys = function(keys, index) {
var kindex = parseInt(index / keys.length); var kindex = parseInt(index / (keys[0].length*8));
return getBitInKey(keys[kindex], index - keys.length * kindex); kindex.should.be.below(keys.length);
return getBitInKey(keys[kindex], index - (keys[0].length*8) * kindex);
}; };
it.skip('should pass Runs Test', function() { it('should pass Runs Test', function() {
var keys = []; var keys = [];
for (var i = 0; i < RUNS; i++) { for (var i = 0; i < RUNS; i++) {
keys.push(Key.generateSync().private); keys.push(Key.generateSync().private);
@ -90,18 +92,19 @@ describe('Key randomness tests', function() {
} }
count += 1; count += 1;
} }
console.log(h); var ratio = 0;
var max; count = 0;
for(i = 1; i < 32; i++) { for(i = 1; i < 8; i++) {
var f = h[i]; var next = h[i+1];
if (i === 1) max = f; var current = h[i];
if (typeof current === 'undefined' || current === 0) continue;
var fNorm = f * 80 / max; if (typeof next === 'undefined') continue;
var s = ''; var r = next / current;
for (var c = 0; c<fNorm; c++) { ratio += r * current;
s += '*'; count += 1 * current;
}
} }
var p = ratio / count;
(p-0.5).should.be.below(0.01);
}); });
it('should pass Test For The Longest Run Of Ones In A Block', function() { it('should pass Test For The Longest Run Of Ones In A Block', function() {