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

Testing distribution::rand() #49

Closed
Beakerboy opened this issue Aug 4, 2016 · 4 comments
Closed

Testing distribution::rand() #49

Beakerboy opened this issue Aug 4, 2016 · 4 comments
Labels

Comments

@Beakerboy
Copy link
Contributor

I don't know unit testing enough to mock it up correctly. I was thinking the test directory could have something like this, but it looks fishy the way it is. Any advice?

class TestDistribution extends Continuous
{
    public static function rand($x)
    {
        // Have the parent call the method below which returns a float that is specified by the test.
        // If you assume that the PHP random number generator works, we will test if the method works
        // given a specific value.  Then again, if we have already tested the inverse function, in this case we
        // pretty much know it works.
        return parent::rand();
    }

    static function randomFloat()
    {
        return self::x;
    }
}
@markrogoyski
Copy link
Owner

You can't mock static functions in PHPUnit; only object methods.

If static method a uses static method b, you'd want to have unit tests on method b on its own. Then the tests on static method a are testing just a, since you know your tests on b test that that works properly.

Now, if method b is a random number generator, then it is going to be impossible to test specific output since the output will depend on the rng. The best you could do is test that it is a number, that it is within the range of possible values based on the range of the rng, etc.

This doesn't necessarily sound like an issue of not being able to mock static methods, but that a rng is being used. Even if there was just one static method and it used a PHP rand function, your output is dependent on that, and you can't know exactly what the output will be.

@markrogoyski
Copy link
Owner

Thinking about this a little more, a possibly elegant solution is to make your static method a higher-order function that accepts as an additional parameter a function (PHP callable closure) that will act as the random number generator. Then for unit tests, you can pass in your testRNG function that returns non-random numbers so you can test the expected output of your method knowing what the inputs will be because you seeded the RNG with your own values.

Then, from a user interface perspective, you might want to wrap that higher-order function with a convenience wrapper method that will supply the standard RNG, but still give the user the option to directly call it with their own RNG if they want.

Just an idea.

@Beakerboy
Copy link
Contributor Author

I think I have a better solution. If I add the Dirac Delta as a distribution, I could add it's inverse function to the DiracDetla class, which would override the parent inverse function. The inverse of the Dirac Delta is Zero for all values of p.

@Beakerboy
Copy link
Contributor Author

This is in a pull request.

markrogoyski pushed a commit that referenced this issue Aug 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants