Skip to content

Commit

Permalink
Add the NoDuplication support for PickOneFrom() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpierrain committed Jan 25, 2021
1 parent d9bf9fe commit 5822caa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
32 changes: 31 additions & 1 deletion Diverse.Tests/FuzzerWithoutDuplicationShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ public void Be_able_to_pick_always_different_values_from_a_list_of_int()
});
}

[Test]
public void Be_able_to_pick_always_different_values_from_a_medium_size_list_of_int()
{
var fuzzer = new Fuzzer(noDuplication: true);

var candidates = Enumerable.Range(-10000, 10000).ToArray();

CheckThatNoDuplicationIsMadeWhileGenerating(fuzzer, candidates.LongLength, () =>
{
return fuzzer.PickOneFrom(candidates);
});
}

[Test]
public void Be_able_to_pick_always_different_values_from_a_list_of_enum()
{
Expand All @@ -196,6 +209,21 @@ public void Be_able_to_pick_always_different_values_from_a_list_of_enum()
});
}

[Test]
public void Be_able_to_pick_different_values_from_a_list_of_string()
{
var fuzzer = new Fuzzer(884871408);
var rateCodes = new[] { "PRBA", "LRBAMC", "AVG1", "PRBB", "LRBA", "BBSP", "PRBA2", "LRBA2MC", "PRPH", "PBCITE_tes", "PRPH2", "PRP2N2", "PR3NS1", "PBHS", "PBSNWH", "PBTHE", "PBVPOM", "PBZOO", "PHBO", "PHBPP", "PAB01", "PHB01", "PH3P", "LH3PMC", "CAMI", "FRCAMIF", "GENERICRATECODE", "PBGGG", "PBPPBRAZIL", "PBSENIOR" };

var noDupFuzzer = fuzzer.GenerateNoDuplicationFuzzer();

CheckThatNoDuplicationIsMadeWhileGenerating(noDupFuzzer, rateCodes.Length, () =>
{
return noDupFuzzer.PickOneFrom(rateCodes);
});

}

#endregion

#region Persons
Expand Down Expand Up @@ -320,6 +348,8 @@ public void Be_able_to_provide_always_different_values_of_DateTime()
}

#endregion



[Test]
public void Throw_DuplicationException_with_fix_explanation_when_number_of_attempts_is_too_low()
Expand All @@ -345,7 +375,7 @@ public void Throw_DuplicationException_with_fix_explanation_when_number_of_attem
- Increase the value of the {nameof(Fuzzer.MaxFailingAttemptsForNoDuplication)} property for your {nameof(IFuzz)} instance.");
}

private static void CheckThatNoDuplicationIsMadeWhileGenerating<T>(Fuzzer fuzzer, long maxNumberOfElements, Func<T> fuzzingFunction)
private static void CheckThatNoDuplicationIsMadeWhileGenerating<T>(IFuzz fuzzer, long maxNumberOfElements, Func<T> fuzzingFunction)
{
var returnedElements = new HashSet<T>(); //T
for (var i = 0; i < maxNumberOfElements; i++)
Expand Down
20 changes: 19 additions & 1 deletion Diverse/Fuzzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -673,12 +673,30 @@ public T PickOneFrom<T>(IList<T> candidates)
{
return GenerateWithoutDuplication<T>(CaptureCurrentMethod(), HashArguments(candidates),
MaxFailingAttemptsForNoDuplication,
standardGenerationFunction: (safeFuzzer) => safeFuzzer.PickOneFrom(candidates));
standardGenerationFunction: (safeFuzzer) => safeFuzzer.PickOneFrom(candidates),
lastChanceGenerationFunction: alreadyProvidedSortedSet => LastChanceToFindNotAlreadyPickedValue(alreadyProvidedSortedSet, candidates, this));
}

return _collectionFuzzer.PickOneFrom(candidates);
}

private static Maybe<T> LastChanceToFindNotAlreadyPickedValue<T>(SortedSet<object> alreadyProvidedValues, IList<T> candidates, IFuzz fuzzer)
{
var allPossibleValues = candidates.ToArray();

var remainingCandidates = allPossibleValues.Except<T>(alreadyProvidedValues.Cast<T>()).ToArray();

if (remainingCandidates.Any())
{
var index = fuzzer.GenerateInteger(0, remainingCandidates.Length - 1);
var pickOneFrom = remainingCandidates[index];

return new Maybe<T>(pickOneFrom);
}

return new Maybe<T>();
}

#endregion

#region DateTimeFuzzer
Expand Down

0 comments on commit 5822caa

Please sign in to comment.