且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

单元测试函数返回随机结果

更新时间:2023-02-20 20:47:31

在除了测试该函数返回的日期在所需范围内,要确保结果是均匀分布。您所描述的测试将传递一个简单的返回你发送的日期功能!

In addition to testing that the function returns a date in the desired range, you want to ensure that the result is well-distributed. The test you describe would pass a function that simply returned the date you sent in!

因此​​,除了调用该函数多次和测试结果保持在所需范围内,我也试图评估的分布,也许是通过将结果在水桶和检查的水桶有结果的人数大致持平在完成后。您可能需要超过100个电话来获得稳定的结果,但这并不听起来像一个昂贵的(运行时明智的)功能,让您可以轻松地运行了几K的迭代。

So in addition to calling the function multiple times and testing that the result stays in the desired range, I would also try to assess the distribution, perhaps by putting the results in buckets and checking that the buckets have roughly equal numbers of results after you are done. You may need more than 100 calls to get stable results, but this doesn't sound like an expensive (run-time wise) function, so you can easily run it for a few K iterations.

我以前有一个问题,非均匀随机功能..他们可以是一个真正的痛苦,这是值得的测试早期。

I've had a problem before with non-uniform "random" functions.. they can be a real pain, it's worth testing for early.