private bool EachOutputContainsAnyOf(SyllableProvider p, params string[] validSubstrings) { bool outputAlwaysAppears = true; for (int i = 0; i < 1000; i++) { var s = p.NextSyllable(); if (!s.ContainsAny(validSubstrings)) { outputAlwaysAppears = false; break; } } return(outputAlwaysAppears); }
public void Provider_WithNoVowelsDefined_ProviderThrowsInvalidOperationException() { // Define everything except vowel and vowel sequences var provider = new SyllableProvider() .WithLeadingConsonants("b") .WithLeadingConsonantSequences("bb", "bbb") .WithTrailingConsonants("b") .WithTrailingConsonantSequences("bbbb", "bbbbb"); // Syllables always require a vowel so syllable generation should fail for (int i = 0; i < 1000; i++) { Assert.ThrowsException <InvalidOperationException>(() => provider.NextStartingSyllable()); Assert.ThrowsException <InvalidOperationException>(() => provider.NextSyllable()); Assert.ThrowsException <InvalidOperationException>(() => provider.NextEndingSyllable()); } }
private bool AllOutputContainsAtLeastOnce(SyllableProvider p, params string[] validSubstrings) { bool[] substringAppeared = new bool[validSubstrings.Length]; for (int i = 0; i < 1000; i++) { var s = p.NextSyllable(); for (int j = 0; j < validSubstrings.Length; j++) { if (s.Contains(validSubstrings[j])) { substringAppeared[j] = true; } } } return(substringAppeared.All(x => x)); }
public void Provider_WhenCustomVowelsDefined_OnlyCustomVowelsAppearInProviderOutput() { // Define one and only one vowel then check name output var provider = new SyllableProvider() .WithVowels("a") .WithLeadingConsonants("bcdfg") .WithTrailingConsonants("bcdfg"); for (int i = 0; i < 1000; i++) { var startingSyllable = provider.NextStartingSyllable(); var syllable = provider.NextSyllable(); var endingSyllable = provider.NextEndingSyllable(); Assert.IsTrue(startingSyllable.Contains("a")); Assert.IsTrue(syllable.Contains("a")); Assert.IsTrue(endingSyllable.Contains("a")); Assert.IsFalse(Regex.IsMatch(startingSyllable, "[e|i|o|u]")); Assert.IsFalse(Regex.IsMatch(syllable, "[e|i|o|u]")); Assert.IsFalse(Regex.IsMatch(endingSyllable, "[e|i|o|u]")); } // Change the one and only vowel and check name output again provider = new SyllableProvider() .WithVowels("y") .WithLeadingConsonants("bcdfg") .WithTrailingConsonants("bcdfg"); for (int i = 0; i < 1000; i++) { var startingSyllable = provider.NextStartingSyllable(); var syllable = provider.NextSyllable(); var endingSyllable = provider.NextEndingSyllable(); Assert.IsTrue(startingSyllable.Contains("y")); Assert.IsTrue(syllable.Contains("y")); Assert.IsTrue(endingSyllable.Contains("y")); Assert.IsFalse(Regex.IsMatch(startingSyllable, "[a|e|i|o|u]")); Assert.IsFalse(Regex.IsMatch(syllable, "[a|e|i|o|u]")); Assert.IsFalse(Regex.IsMatch(endingSyllable, "[a|e|i|o|u]")); } }
public void Provider_WithCustomComponents_AllComponentsAppearInProviderOutput() { // In this test we define one instance of a vowel, vowel sequence // leading consonant, leading consonant sequence, trailing consonant // and trailing consonant sequence, then check that each instance occurs // at least once in the provider's syllable generation. var provider = new SyllableProvider() .WithVowels("a") .WithVowelSequences("ee") .WithLeadingConsonants("b") .WithLeadingConsonantSequences("cc") .WithTrailingConsonants("d") .WithTrailingConsonantSequences("ff") .WithProbability( vowelIsSequence: 0.5, vowelBeginsStartingSyllable: 0.25, consonantBeginsSyllable: 0.50, consonantEndsSyllable: 0.50); /* * .WithProbability(y => y * .OfVowelSequences(0.5) * .OfStartingSyllableLeadingVowels(0.25) * .OfLeadingConsonantSequences(0.50) * .OfTrailingConsonants(0.50)); */ bool foundVowel = false; bool foundVowelSequence = false; bool foundStartingConsonant = false; bool foundStartingConsonantSequence = false; bool foundEndingConsonant = false; bool foundEndingConsonantSequence = false; // Starting syllables only for (int i = 0; i < 500; i++) { var syllable = provider.NextStartingSyllable(); if (syllable.Contains("a")) { foundVowel = true; } if (syllable.Contains("ee")) { foundVowelSequence = true; } if (syllable.Contains("b")) { foundStartingConsonant = true; } if (syllable.Contains("cc")) { foundStartingConsonantSequence = true; } if (syllable.Contains("d")) { foundEndingConsonant = true; } if (syllable.Contains("ff")) { foundEndingConsonantSequence = true; } } Assert.IsTrue(foundVowel); Assert.IsTrue(foundVowelSequence); Assert.IsTrue(foundStartingConsonant); Assert.IsTrue(foundStartingConsonantSequence); Assert.IsTrue(foundEndingConsonant); Assert.IsTrue(foundEndingConsonantSequence); // Reset foundVowel = false; foundVowelSequence = false; foundStartingConsonant = false; foundStartingConsonantSequence = false; foundEndingConsonant = false; foundEndingConsonantSequence = false; // All syllables for (int i = 0; i < 500; i++) { var syllable = provider.NextSyllable(); if (syllable.Contains("a")) { foundVowel = true; } if (syllable.Contains("ee")) { foundVowelSequence = true; } if (syllable.Contains("b")) { foundStartingConsonant = true; } if (syllable.Contains("cc")) { foundStartingConsonantSequence = true; } if (syllable.Contains("d")) { foundEndingConsonant = true; } if (syllable.Contains("ff")) { foundEndingConsonantSequence = true; } } Assert.IsTrue(foundVowel); Assert.IsTrue(foundVowelSequence); Assert.IsTrue(foundStartingConsonant); Assert.IsTrue(foundStartingConsonantSequence); Assert.IsTrue(foundEndingConsonant); Assert.IsTrue(foundEndingConsonantSequence); // Reset foundVowel = false; foundVowelSequence = false; foundStartingConsonant = false; foundStartingConsonantSequence = false; foundEndingConsonant = false; foundEndingConsonantSequence = false; // Ending syllables only for (int i = 0; i < 500; i++) { var syllable = provider.NextEndingSyllable(); if (syllable.Contains("a")) { foundVowel = true; } if (syllable.Contains("ee")) { foundVowelSequence = true; } if (syllable.Contains("b")) { foundStartingConsonant = true; } if (syllable.Contains("cc")) { foundStartingConsonantSequence = true; } if (syllable.Contains("d")) { foundEndingConsonant = true; } if (syllable.Contains("ff")) { foundEndingConsonantSequence = true; } } Assert.IsTrue(foundVowel); Assert.IsTrue(foundVowelSequence); Assert.IsTrue(foundStartingConsonant); Assert.IsTrue(foundStartingConsonantSequence); Assert.IsTrue(foundEndingConsonant); Assert.IsTrue(foundEndingConsonantSequence); }