7/23/2023 0 Comments Cabal code zero downloadTesting the code produces the following output: $ ghc -make cond.hs Prop_emptyList list = length list = 0 => headExists list = False Prop_headExists list = length list > 0 => headExists list = True These two conditions can be written as follows: import Data. Also, when the list is empty, the headExists function must return ∟alse. In our example, if the condition that the list is non-empty is True, then the property that the headExists function for the list must return is True. The notation syntax is condition => property. The property that the headExists function will return True only for non-empty lists can be defined as a constraint. We can also re-write the above code based on conditional properties. Prop_headExists list = headExists list = not (lengthZero list)Įxecuting the code produces the required output: $ ghc -make head.hs The complete code is shown below: import Data.List We can then use this function to assert that for any Integer list, if headExists returns ∟alse then the lengthZero function must return True. We can write a helper function lengthZero for the above, as follows: lengthZero :: -> Bool We know that if the list is empty, then its length is zero. The way we defined the property is incorrect. Clearly, the headExists computation will return True and not ∟alse. The QuickCheck library generated test cases for different types and it returned a failure after the third test, for which the input was. Testing the code produces the following output: $ ghc -make head.hs Prop_headExists emptyList = headExists emptyList = False Lets assume that, by mistake, you wrote an incorrect property-based test where the headExists function will always return ∟alse, ignoring the otherwise case. You can load the above code in GHCi and test it out, as follows: ghci> headExists You can write a headExists function to check if the head exists for a list of integers, as shown below: headExists :: -> Bool ![]() ![]() The head function in Haskell expects to receive a non-empty list. You can also dump the input that was generated for the various test cases using the verboseCheck function, as shown below: main :: IO ()Įxecuting the above code with the updated main function will yield 100 input test cases that were generated in runtime. You can also invoke the quickCheck function in a main function, as shown below: import Test.QuickCheckĬompiling and executing the above code produces the following output: $ ghc -make sum.hs You can test the code directly in the GHCi prompt, using the following command: $ ghci sum.hs You must first define the invariant in a function as shown below: prop_mySum a b = mySum a b = mySum b a You can ascertain the property of the function that a + b is the same as b + a by using the QuickCheck library.
0 Comments
Leave a Reply. |