Skip to content

Commit

Permalink
Merge pull request #62 from conradz/rand-tests
Browse files Browse the repository at this point in the history
Make all random tests deterministic
  • Loading branch information
millermedeiros committed Mar 5, 2013
2 parents c7dda76 + 672f876 commit dd82368
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 287 deletions.
27 changes: 27 additions & 0 deletions tests/spec/random/helper-mockRandom.js
@@ -0,0 +1,27 @@
define(['mout/random/random'], function(random) {
var original;

// Values to return from the mocked generator
// The values should be equally split with no bias.
var values = [0.1, 0.7, 0.3, 0.45, 0.55, 0.9, 0.2, 0.35, 0.8, 0.65];

function mockRandom() {
original = random.get;

var i = 0;
random.get = function() {
return values[i++ % values.length];
};
}

mockRandom.end = function() {
if (!original) {
throw new Error('Invalid mockRandom.end()');
}

random.get = original;
original = null;
};

return mockRandom;
});
65 changes: 16 additions & 49 deletions tests/spec/random/spec-choice.js
@@ -1,70 +1,37 @@
define(['mout/random/choice'], function (choice) {
define(['mout/random/choice', './helper-mockRandom'], function (choice, mockRandom) {

describe('random/choice()', function () {

beforeEach(function(){
this.addMatchers({
toDiffAny : function(vals){
var n = arguments.length;
while(n--){
if(this.actual !== arguments[n]) return true;
}
return false;
}
});
mockRandom();
});

afterEach(function() {
mockRandom.end();
});

it('should pick a random argument', function(){
var choices = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var a = choice.apply(null, choices),
b = choice.apply(null, choices);

var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

var q = choice.apply(null, arr);
var w = choice.apply(null, arr);
var e = choice.apply(null, arr);
var r = choice.apply(null, arr);
var t = choice.apply(null, arr);
var y = choice(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
var u = choice(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
var i = choice(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
var o = choice(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

expect( arr ).toContain( q );
expect( arr ).toContain( w );
expect( arr ).toContain( e );
expect( arr ).toContain( r );
expect( arr ).toContain( t );
expect( arr ).toContain( y );

expect( q ).toDiffAny(w, e, r, t, y, u, i, o);
expect( choices ).toContain( a );
expect( choices ).toContain( b );
expect( a ).not.toEqual( b );
});

it('should work with array', function(){

var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

var q = choice(arr);
var w = choice(arr);
var e = choice(arr);
var r = choice(arr);
var t = choice(arr);
var y = choice(arr);
var u = choice(arr);
var i = choice(arr);
var o = choice(arr);
var a = choice(arr),
b = choice(arr);

expect( arr ).toContain( q );
expect( arr ).toContain( w );
expect( arr ).toContain( e );
expect( arr ).toContain( r );
expect( arr ).toContain( t );
expect( arr ).toContain( y );

expect( q ).toDiffAny(w, e, r, t, y, u, i, o);
expect( arr ).toContain( a );
expect( arr ).toContain( b );
expect( a ).not.toEqual( b );
});

});


});

29 changes: 10 additions & 19 deletions tests/spec/random/spec-guid.js
@@ -1,31 +1,22 @@
define(['mout/random/guid'], function (guid) {
define(['mout/random/guid', './helper-mockRandom'], function (guid, mockRandom) {

describe('random/guid()', function(){

beforeEach(function(){
this.addMatchers({
toDiffAny : function(vals){
var n = arguments.length;
while(n--){
if(this.actual !== arguments[n]) return true;
}
return false;
}
});
mockRandom();
});

afterEach(function(){
mockRandom.end();
});

it('returns a random guid each call', function(){
var q = guid();
var w = guid();
var e = guid();
var r = guid();
var t = guid();
var y = guid();
var a = guid();
var b = guid();

// match guid v4 format e.g. 3f2504e0-2f89-41d3-9a0c-0305e82c3301
expect( q ).toMatch(/[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[ab89][a-f0-9]{3}-[a-f0-9]{12}/);
expect( q ).not.toBeUndefined();
expect( q ).toDiffAny(w, e, r, t, y);
expect( a ).toMatch(/[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[ab89][a-f0-9]{3}-[a-f0-9]{12}/);
expect( a ).not.toEqual( b );
});

});
Expand Down
61 changes: 19 additions & 42 deletions tests/spec/random/spec-rand.js
@@ -1,56 +1,33 @@
define(['mout/random/rand'], function (rand) {
define(['mout/random/rand', './helper-mockRandom'], function (rand, mockRandom) {

describe('random/rand()', function(){

beforeEach(function(){
this.addMatchers({
toSnap : function(min, max){
return this.actual === min || this.actual === max;
},
toDiffAny : function(vals){
var n = arguments.length;
while(n--){
if(this.actual !== arguments[n]) return true;
}
return false;
}
});
mockRandom();
});

afterEach(function() {
mockRandom.end();
});

it('returns a rand number at each call', function(){
var q = rand();
var w = rand();
var e = rand();
var r = rand();
var t = rand();
var y = rand();
expect( q ).not.toBeUndefined();
expect( q ).not.toEqual( Infinity );
expect( q ).toDiffAny(w, e, r, t, y);
var a = rand(),
b = rand();
expect( a ).not.toBeUndefined();
expect( a ).not.toEqual( Infinity );
expect( a ).not.toEqual( b );
});

it('returns a rand number inside range', function(){
var q = rand(0, 9999);
var w = rand(0, 9999);
var e = rand(0, 9999);
var r = rand(0, 9999);
var t = rand(0, 9999);
var y = rand(0, 9999);
expect( q ).toBeLessThan(9999.01);
expect( q ).toBeGreaterThan(-0.01);
expect( w ).toBeLessThan(9999.01);
expect( w ).toBeGreaterThan(-0.01);
expect( e ).toBeLessThan(9999.01);
expect( e ).toBeGreaterThan(-0.01);
expect( r ).toBeLessThan(9999.01);
expect( r ).toBeGreaterThan(-0.01);
expect( t ).toBeLessThan(9999.01);
expect( t ).toBeGreaterThan(-0.01);
expect( y ).toBeLessThan(9999.01);
expect( y ).toBeGreaterThan(-0.01);

expect( q ).toDiffAny(w, e, r, t, y);
var a = rand(0, 9999),
b = rand(0, 9999);
expect( a ).toBeLessThan(9999.01);
expect( a ).toBeGreaterThan(-0.01);
expect( b ).toBeLessThan(9999.01);
expect( b ).toBeGreaterThan(-0.01);
expect( a ).not.toEqual( b );
});

});

});
46 changes: 15 additions & 31 deletions tests/spec/random/spec-randBit.js
@@ -1,41 +1,28 @@
define(['mout/random/randBit'], function (randBit) {
define(['mout/random/randBit', './helper-mockRandom'], function (randBit, mockRandom) {

describe('random/randBit()', function(){

beforeEach(function(){
this.addMatchers({
toDiffAny : function(vals){
var n = arguments.length;
while(n--){
if(this.actual !== arguments[n]) return true;
}
return false;
}
});
mockRandom();
});

afterEach(function(){
mockRandom.end();
});

it('returns a random number at each call', function(){
var q = randBit();
var w = randBit();
var e = randBit();
var r = randBit();
var t = randBit();
var y = randBit();
var a = randBit();
var s = randBit();
var d = randBit();
var f = randBit();
expect( q ).not.toBeUndefined();
expect( q ).not.toEqual( Infinity );
expect( q ).not.toEqual( NaN );
expect( q ).toDiffAny(w, e, r, t, y, a, s, d, f);
var a = randBit(),
b = randBit();
expect( a ).not.toBeUndefined();
expect( a ).not.toEqual( Infinity );
expect( a ).not.toEqual( NaN );
expect( a ).not.toEqual( b );
});

it('shouldn\t be biased', function () {

var c1 = 0,
c0 = 0,
n = 1000,
n = 10,
rnd;

while (n--) {
Expand All @@ -49,11 +36,8 @@ define(['mout/random/randBit'], function (randBit) {
}
}

expect( c0 ).toBeLessThan( 560 );
expect( c0 ).toBeGreaterThan( 440 );
expect( c1 ).toBeLessThan( 560 );
expect( c1 ).toBeGreaterThan( 440 );

expect( c0 ).toEqual(5);
expect( c1 ).toEqual(5);
});

});
Expand Down
24 changes: 8 additions & 16 deletions tests/spec/random/spec-randHex.js
@@ -1,27 +1,19 @@
define(['mout/random/randHex'], function (randHex) {
define(['mout/random/randHex', './helper-mockRandom'], function (randHex, mockRandom) {

describe('random/randHex()', function () {

beforeEach(function(){
this.addMatchers({
toDiffAny : function(vals){
var n = vals.length;
while(n--){
if(this.actual !== vals[n]) return true;
}
return false;
}
});
mockRandom();
});

afterEach(function() {
mockRandom.end();
});

it('should return a random hexadecimal value', function () {
var results = [];
var n = 16;
while (n--){
results[n] = randHex();
}
expect( randHex() ).toDiffAny( results );
var a = randHex(),
b = randHex();
expect( a ).not.toEqual( b );
});

it('should return a 6 char length hex value by default', function () {
Expand Down

0 comments on commit dd82368

Please sign in to comment.