Skip to content

Instantly share code, notes, and snippets.

@erickedji
Created October 25, 2009 15:37
Show Gist options
  • Save erickedji/218123 to your computer and use it in GitHub Desktop.
Save erickedji/218123 to your computer and use it in GitHub Desktop.
[Tests] Ninety-nine haskell problems
import Test.QuickCheck
import Nnhp
{- For most of the problems, randomly choosen parameters
- are not a good test, as some obvious combinations are
- not likely to be generated, not matter how interresting
- they are to the test case at hand.
-}
-- P01
prop_myLastHasAtMostOneElement list =
(length . myLast) list < 2
-- P02
prop_myButLastHasAtMostTwoElements list =
(length . myButLast) list < 3
-- P03
prop_elementAtCorrectForPositiveIndices list i =
(i < 1 || i >= length list ) || (elementAt list i == list !! i)
-- P04
prop_numberOfElementsEquivalentToLength list =
numberOfElements list == length list
prop_numberOfElementsIterEquivalentToLength list =
numberOfElements list == length list
-- P05
prop_myReverseSquaredEqualsIdentity list =
(myReverse . myReverse) list == list
-- P06
-- this is a stupid reimplementation of the function :-)
-- The type specification is important here, as quickCheck will
-- use nil-filled lists (always generating obvious
-- palindromes) if omitted. Restricting to numbers is better, but
-- still unsatisfactory, as the odds to generate palindromes is
-- rather low. Computing the odds is left as an exercice to the
-- reader :-)
-- TODO: read the documentation of QuickCheck, to learn how to
-- generate better test data
prop_palindromeListsAreEqualToTheirReverse :: Num a => [a] -> Bool
prop_palindromeListsAreEqualToTheirReverse list =
isPalindrome list == (reverse list == list)
-- P08
prop_compressShouldBeIdempotent :: Num a => [a] -> Bool
prop_compressShouldBeIdempotent list =
(compress . compress) list == compress list
-- P09
-- suggestions welcomed to test `pack' :-)
prop_maybePackIsCorrect :: Bool
prop_maybePackIsCorrect =
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]]
-- P10 & P12
prop_decodeRlIsTheInverseOfEncode :: Num a => [a] -> Bool
prop_decodeRlIsTheInverseOfEncode list =
(decodeRl . encode) list == list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment