コード例 #1
0
        public void Provider_WithCustomVowelProbability_AffectsNameGenerationSuccess()
        {
            // Defining at least one vowel sequence, but no individual vowels also shouldn't work if
            // the probability of vowel sequences is not set to 100%
            var provider  = new SyllableProvider().WithVowelSequences("aa");
            var generator = new NameGenerator().UsingProvider(provider);

            Assert.ThrowsException <InvalidOperationException>(() => // Some will succeed, but expecting some to fail
            {
                for (int i = 0; i < 1000; i++)
                {
                    generator.Next();
                }
            });

            // Defining at least one vowel sequence, set to 100% probability
            // without any possibility of vowels starting name shoudl work
            // provider.WithVowelSequenceProbability(1.0).WithStartingSyllableLeadingVowelSequenceProbability(1.0);
            //provider.WithProbability(x => x.OfVowelSequences(1.0).OfStartingSyllableLeadingVowelSequence(1.0));
            provider.WithProbability(vowelIsSequence: 1.0, vowelBeginsStartingSyllableAndIsSequence: 1.0);

            for (int i = 0; i < 1000; i++)
            {
                Assert.IsNotNull(generator.Next());
            }

            // The normal scenario where at least one individual vowel is defined
            //provider.WithVowels("a").WithProbability(x => x.OfVowelSequences(0));
            provider.WithVowels("a").WithProbability(vowelIsSequence: 0);

            for (int i = 0; i < 1000; i++)
            {
                Assert.IsNotNull(generator.Next());
            }
        }
コード例 #2
0
        public void Provider_CustomConsonantProbabilityDefined_AffectsSyllableGenerationCorrectly()
        {
            var provider = new SyllableProvider()
                           .WithVowels("a")
                           .WithVowelSequences("ee")
                           .WithLeadingConsonants("b")
                           .WithLeadingConsonantSequences("cc")
                           .WithTrailingConsonants("d")
                           .WithTrailingConsonantSequences("ff");

            // Disable all leading and trailing consonants
            Assert.IsTrue(EachOutputNeverContainsAnyOf(
                              provider.WithProbability(
                                  consonantBeginsSyllable: 0.0,
                                  consonantBeginsSyllableAndIsSequence: 0.0,
                                  consonantEndsSyllable: 0.0,
                                  consonantEndsSyllableAndIsSequence: 0.0),
                              "b", "cc", "d", "ff"));

            // Consonant sequence probability doesn't matter
            // if the consonant probability is zero
            Assert.IsTrue(EachOutputNeverContainsAnyOf(
                              provider.WithProbability(
                                  consonantBeginsSyllable: 0.0,
                                  consonantBeginsSyllableAndIsSequence: 1.0,
                                  consonantEndsSyllable: 0.0,
                                  consonantEndsSyllableAndIsSequence: 1.0),
                              "b", "cc", "d", "ff"));

            // Consonant sequence probability only matters
            // if the consonant probability is not zero

            provider.WithProbability(
                consonantBeginsSyllable: 1.0,
                consonantBeginsSyllableAndIsSequence: 0.0,
                consonantEndsSyllable: 1.0,
                consonantEndsSyllableAndIsSequence: 0.0);

            // There should be no consonant sequences
            Assert.IsTrue(EachOutputContainsAnyOf(provider, "b", "d"));
            Assert.IsTrue(EachOutputNeverContainsAnyOf(provider, "cc", "ff"));

            provider.WithProbability(
                consonantBeginsSyllable: 1.0,
                consonantBeginsSyllableAndIsSequence: 1.0,
                consonantEndsSyllable: 1.0,
                consonantEndsSyllableAndIsSequence: 1.0);

            // There should always be consonant sequences
            Assert.IsTrue(EachOutputNeverContainsAnyOf(provider, "b", "d"));
            Assert.IsTrue(EachOutputContainsAnyOf(provider, "cc", "ff"));

            // Test whether a value between 0 and 1 ouputs both consonants and consonant sequences
            provider.WithProbability(
                consonantBeginsSyllable: 1.0,
                consonantBeginsSyllableAndIsSequence: 0.5,
                consonantEndsSyllable: 1.0,
                consonantEndsSyllableAndIsSequence: 0.5);

            Assert.IsTrue(AllOutputContainsAtLeastOnce(provider, "b", "d", "cc", "ff"));

            // Not all output will have a consonant
            provider.WithProbability(
                consonantBeginsSyllable: 0.5,
                consonantBeginsSyllableAndIsSequence: 0.5,
                consonantEndsSyllable: 0.5,
                consonantEndsSyllableAndIsSequence: 0.5);

            Assert.IsTrue(AllOutputContainsAtLeastOnce(provider, "b", "d", "cc", "ff"));
        }