JasonBock / SpackleNet

Spackle is a project that contains a number of helper methods I've used to supplement the core classes in .NET.
MIT License
14 stars 1 forks source link

There seems to be a bug in overload for generate that included values to exclude #4

Closed JasonBock closed 8 years ago

JasonBock commented 10 years ago
/// <summary>
/// Generates a value for the specified type
/// that is not in a specified list of values.
/// </summary>
/// <typeparam name="T">The type to generate a value for.</typeparam>
/// <param name="excludedValues">A set of values that should not be generated.</param>
/// <returns>Returns a random value of type <typeparamref name="T" />.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="excludedValues"/> is <c>null</c>.</exception>
public T Generate<T>(SortedSet<T> excludedValues)
{
    if(excludedValues == null)
    {
        throw new ArgumentNullException("excludedValues");
    }

    var value = (T)this.Generate(typeof(T));

    while(excludedValues.Contains(value))
    {
        value = (T)this.Generate(typeof(T));
    }

    return value;
}

The logic in the while seems to be broken. I created a local extention method that fixes the problem which you can lift if you want to update the library or maybe come up with something better than what I threw together. Also as a feture request could an email address generator be added as well as a way to make string values that do not just consist of a single char.

while (excludedValues.Any(s => value.ToString().Contains(s)))
JasonBock commented 8 years ago

I can't figure out why this issue is an "issue" any more. Closing it.