asiftasleem / nbuilder

Automatically exported from code.google.com/p/nbuilder
0 stars 0 forks source link

GetRandom.Phrase sometimes returns an empty phrase #80

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. trying and create 1000's of phrases, with a max length of .. say 10.
2. throw an exception when result is null or empty (it will be string.Empty)

What is the expected output? What do you see instead?
It shouldn't return an empty string.

What version of the product are you using? On what operating system?
Latest on NuGet.

Please provide any additional information below.

The error occurs when the first word returned in the Phrase code is a long 
latin word.

eg. 

Here's some code i made up which is BASED On your phrase code.
I did this so i can reproduce the error, in your code.

public static string PhraseRepo(int length)
{
    Random rnd = new Random(System.Guid.NewGuid().GetHashCode());
    var count = latinWords.Length;
    var result = string.Empty;
    var done = false;
    while (!done)
    {
        var index = rnd.Next(0, count - 1);
        var word = latinWords[index];

        // Ok - FIRST iteration ...
        //      word returned from LatinWords is: "incididunt" 

        if (result.Length + word.Length + 1 > length)
        {
            // We're here because..
            // 1. result.Length == 0 (nothing set, yet).
            // 2. word.Length == 10. +1 == 11.
            // 3. length == 10 .. so 0 + 11 IS greater than 10..
            //    so .. we're in here :(
            done = true;
        }
        else
        {
            result += word + " ";
        }
    }

    return result.Trim();
}

so it needs to be smart enough to try and either cut down the word they have .. 

maybe ..

if (result.Length + word.Length + 1 > length)
        {
            if (string.IsNullOrEmpty(result.Trim())
            {
                result = word.Substring(0, length);
            }
            done = true;
        }

and we need to make sure length argument value passed in, is greater than 1.

eg.

if (length <= 0) throw new ArgumentOutOfRangeException("length")

Hope this helps :)

Original issue reported on code.google.com by jus...@adler.com.au on 24 Nov 2011 at 4:50