Guest User

Untitled

a guest
Apr 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. import Test.QuickCheck
  2. import Nnhp
  3.  
  4. {- For most of the problems, randomly choosen parameters
  5. - are not a good test, as some obvious combinations are
  6. - not likely to be generated, not matter how interresting
  7. - they are to the test case at hand.
  8. -}
  9.  
  10. -- P01
  11. prop_myLastHasAtMostOneElement list =
  12. (length . myLast) list < 2
  13.  
  14. -- P02
  15. prop_myButLastHasAtMostTwoElements list =
  16. (length . myButLast) list < 3
  17.  
  18. -- P03
  19. prop_elementAtCorrectForPositiveIndices list i =
  20. (i < 1 || i >= length list ) || (elementAt list i == list !! i)
  21.  
  22. -- P04
  23. prop_numberOfElementsEquivalentToLength list =
  24. numberOfElements list == length list
  25.  
  26. prop_numberOfElementsIterEquivalentToLength list =
  27. numberOfElements list == length list
  28.  
  29. -- P05
  30. prop_myReverseSquaredEqualsIdentity list =
  31. (myReverse . myReverse) list == list
  32.  
  33. -- P06
  34. -- this is a stupid reimplementation of the function :-)
  35. -- The type specification is important here, as quickCheck will
  36. -- use nil-filled lists (always generating obvious
  37. -- palindromes) if omitted. Restricting to numbers is better, but
  38. -- still unsatisfactory, as the odds to generate palindromes is
  39. -- rather low. Computing the odds is left as an exercice to the
  40. -- reader :-)
  41.  
  42. -- TODO: read the documentation of QuickCheck, to learn how to
  43. -- generate better test data
  44. prop_palindromeListsAreEqualToTheirReverse :: Num a => [a] -> Bool
  45. prop_palindromeListsAreEqualToTheirReverse list =
  46. isPalindrome list == (reverse list == list)
  47.  
  48. -- P08
  49. prop_compressShouldBeIdempotent :: Num a => [a] -> Bool
  50. prop_compressShouldBeIdempotent list =
  51. (compress . compress) list == compress list
  52.  
  53. -- P09
  54. -- suggestions welcomed to test `pack' :-)
  55. prop_maybePackIsCorrect :: Bool
  56. prop_maybePackIsCorrect =
  57. pack [1,1,1,2,5,3,7,7,7,8,8,8,9,1,5,5] == [[1,1,1],[2],[5],[3],[7,7,7],[8,8,8],[9],[1],[5,5]]
  58.  
  59. -- P10 & P12
  60. prop_decodeRlIsTheInverseOfEncode :: Num a => [a] -> Bool
  61. prop_decodeRlIsTheInverseOfEncode list =
  62. (decodeRl . encode) list == list
Add Comment
Please, Sign In to add comment