Unit testing IO Int and similar in Haskell

妖精的绣舞 提交于 2020-08-19 04:17:08

问题


From Ninety-Nine Haskell Problems:

Question 23: Extract a given number of randomly selected elements from a list.

This is a partial solution. For simplicity, this code just selects one element from a list.

import System.Random (randomRIO)

randItem :: [a] -> IO a
randItem xs = do
    i <- randomRIO (0,length xs - 1)
    return $ xs !! i

so randItem [1..10] would return an IO Int that corresponds to (but does not equal) some Int from 1 through 10.

So far, so good. But what kind of tests can I write for my randItem function? What relationship--if any--can I confirm between the input and output?

I can use the same logic as the above function to produce m Bool, but I cannot figure out how to test for m Bool. Thank you.


回答1:


There's a couple of things you can do. If you're using QuickCheck, you could write the following properties:

  • The length of the returned list should be equal to the input length.
  • All elements in the returned list should be elements of the list of candidates.

Apart from that, the beauty of Haskell's Random library is that (as most other Haskell code) it's deterministic. If, instead of basing your implementation on randomRIO, you could base it on randomR or randomRs. This would enable you to pass some known RandomGen values to some deterministic unit test cases (not QuickCheck). These could serve as regression tests.


I've now published an article about the above approach, complete with source code.



来源:https://stackoverflow.com/questions/46791466/unit-testing-io-int-and-similar-in-haskell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!