Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple generators seed question #32

Open
VinCal opened this issue Dec 18, 2024 · 2 comments
Open

Multiple generators seed question #32

VinCal opened this issue Dec 18, 2024 · 2 comments

Comments

@VinCal
Copy link

VinCal commented Dec 18, 2024

Hi, I'm very new to property based testing and to your library, thanks for creating it! I have a question about the seeds when using multiple Generators. I have made this small example which highlights my issue:

public class Dummy
{
    private readonly int[] m_Single;

    public Dummy(int range)
    {
        m_Single = Gen.Int[0, 10].Array[range].Single();
    }

    public void DoSomething()
    {
        if (m_Single.Sum() % 2 == 0)
        {
            Assert.IsTrue(false);
        }
    }
}

public static class GenHelper
{
    public static Gen<Dummy> GenDummy => Gen.Select(Gen.Int[1, 512], x => new Dummy(x));
}

[TestMethod]
public void seed_test()
{
    GenHelper.GenDummy.Sample(d => d.DoSomething(), null, null, -1, -1, -1);
}

When I run this test, it fails and gives me a seed, but I'm unsure if it 'belongs' to the Sample or Single method. For example, I got this seed: 00007TA2XpFw, when I provide it to both Sample and Single, the test succeeds again. When I provide it only to Sample and then run the test again it sometimes succeeds and sometimes fails with a different hash. If I provide the last hash to Single it fails every time as expected.

m_Single = Gen.Int[0, 10].Array[range].Single(x => true, "a6C8duJJ7EAJ");
...
GenHelper.GenDummy.Sample(d => d.DoSomething(), null, "00007TA2XpFw", 1, -1, 1);

My real use-case has a lot more generators, so trying these different seeds for different Generators is not that feasible. Perhaps I am misunderstanding how to use the seed? Thanks!

@AnthonyLloyd
Copy link
Owner

AnthonyLloyd commented Dec 22, 2024

Hi,
You can't use Sample and Single together. Single is meant to be just a quick way of creating a random value when it doesn't matter what the value is so it doesn't work with the Sample seed but it's own. I'll make this more clear on the Single method.
In the example above instead of using Single you can create a Gen and Use Gen.SelectMany instead of Gen.Select.

@VinCal
Copy link
Author

VinCal commented Dec 23, 2024

Hey, thank you very much! SelectMany indeed solved my issue. Thanks again for the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants