Search code examples
haskellquickcheck

Using Haskell QuickCheck to test a TCP port validator


I am trying to use QuickCheck (for the first time) to test a function that validates TCP port numbers:

validatePort :: Int -> Either String Int
validatePort port =
  if port > 0 && port <= 65535
    then Right port
    else Left "Port must be between 1 and 65535 inclusive"

I wrote an instance of Arbitrary like this:

instance Arbitrary Int where
  arbitrary = choose (1, 65535)

but I'm not sure how to write the test property.


Solution

  • As a starting point, first: import Test.QuickCheck which already defines Arbitrary instance for Int

    Then write a property:

    prop_validate_port port = 
      if   port > 0 && port <= 65535
      then validatePort port == Right port
      else validatePort port == Left "Port must be between 1 and 65535 inclusive"
    

    And run the test:

    >quickCheck prop_validate_port
    >+++ OK. passed 100 tests.