public void GivenRandomStringGeneratorWhenRandomStringsGeneratedThenAllCharactersAreUsed() { ISet <char> alphaChars = new HashSet <char> { '0', '1', '2', '3', '4', '5' }; ISet <char> foundChars = new HashSet <char>(); string alpha = string.Join("", alphaChars); int maxIterations = 100; int rndStringLength = 4; IRandomStringGenerator generator = NewGenerator(); for (int index = 0; index < maxIterations; index++) { string rnd = generator.RandomString(rndStringLength, alpha); char[] rndChars = rnd.ToCharArray(); foreach (char c in rndChars) { foundChars.Add(c); } if (alphaChars.Count == foundChars.Count) { Assert.Pass("All chars found at index: " + index); } } Assert.Fail(string.Format("Not all characters found in random strings. Found: '{0}'", string.Join(", ", alphaChars))); }
public void GivenRandomStringGeneratorWhenRandomStringsGeneratedThenAllCharactersHaveEvenDistributionWithin10Percent() { ISet <char> alphaChars = new HashSet <char> { '0', '1', '2', '3', '4', '5' }; IDictionary <char, int> charCount = new Dictionary <char, int>(); string alpha = string.Join("", alphaChars); int maxIterations = 10000; int rndStringLength = 1; IRandomStringGenerator generator = NewGenerator(); for (int index = 0; index < maxIterations; index++) { string rnd = generator.RandomString(rndStringLength, alpha); char[] rndChars = rnd.ToCharArray(); foreach (char c in rndChars) { if (charCount.ContainsKey(c)) { charCount[c]++; } else { charCount.Add(c, 1); } } } double expectedCharCount = (maxIterations * rndStringLength) / alphaChars.Count; double delta = expectedCharCount * 0.1; foreach (var pair in charCount) { Assert.AreEqual(expectedCharCount, pair.Value, delta, "Character distribution for char '{0}' not even", pair.Key); } }