Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from conradz/rand-tests
Make all random tests deterministic
- Loading branch information
Showing
9 changed files
with
150 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ); | ||
}); | ||
|
||
}); | ||
|
||
|
||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.