Skip to content

Commit

Permalink
Add a regression test for #2927 (caused in #2158)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgonggrijp committed Dec 16, 2021
1 parent aec10f9 commit 354337a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,28 @@
var partialSample = _.sample(_.range(1000), 10);
var partialSampleSorted = partialSample.sort();
assert.notDeepEqual(partialSampleSorted, _.range(10), 'samples from the whole array, not just the beginning');
// The next few lines (up to END) are a regression test for #2927.
var alphabet = 'abcdefghijklmnopqrstuvwxyz';
var prefixLength = 5;
var prefix = _.toArray(alphabet.slice(0, prefixLength));
// We're going to take three random samples from the alphabet and count how
// many of them are exact prefixes of the alphabet ('abcde').
var verbatimPrefixes = 0;
_.times(3, function() {
var sample = _.toArray(_.sample(alphabet, prefixLength));
if (_.isEqual(sample, prefix)) ++verbatimPrefixes;
});
// The probability of a sample of length N being a prefix is 1/(A!/(A-N)!),
// with A being the length of the alphabet. That amounts to roughly 1 in
// 7.9e6 when N=5 and A=26. Most of the time, therefore, we should find that
// verbatimPrefixes=0. We will however accept the occasional hit. Only when
// it happens twice, does it start to look really suspicious; the
// probability of this happening is roughly 1 in 21e12. If you are lucky
// enough to witness this, you should be fine when you run the test again.
// However, if you can reliably make the test fail again, you can be sure
// that the code is not working as intended.
assert.ok(verbatimPrefixes < 2, 'sampling a string should not just return a prefix');
// END of regression test for #2927.
});

QUnit.test('toArray', function(assert) {
Expand Down

0 comments on commit 354337a

Please # to comment.