luhn (empty) → 0.1
raw patch · 5 files changed
+147/−0 lines, 5 filesdep +QuickCheckdep +basebuild-type:Customsetup-changed
Dependencies added: QuickCheck, base
Files
- LICENSE +11/−0
- Setup.lhs +7/−0
- luhn.cabal +19/−0
- src/Luhn.hs +98/−0
- src/Tests.hs +12/−0
+ LICENSE view
@@ -0,0 +1,11 @@+Copyright (c) 2008, N-Sim Ltd.+All rights reserved.++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.+ * Neither the name of the N-Sim Ltd. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.+
+ Setup.lhs view
@@ -0,0 +1,7 @@+#! /usr/bin/env runhaskell++> import Distribution.Simple+> import System.Cmd+> tests _ _ _ _ = system "runhaskell src/Tests.hs" >> return ()+> main = defaultMainWithHooks (simpleUserHooks {runTests = tests})+
+ luhn.cabal view
@@ -0,0 +1,19 @@+Build-Type: Custom+Name: luhn+Category: Data+Version: 0.1+Cabal-Version: >= 1.2+Synopsis: An implementation of Luhn's check digit algorithm.+Description: An implementation of Luhn's check digit algorithm.+License: BSD3+License-File: LICENSE+Copyright: (c) 2008 N-Sim Ltd.+Author: Henry Bucklow+Maintainer: jhb@n-sim.com+Tested-With: GHC==6.10+Build-Depends: base, QuickCheck+Exposed-Modules: Luhn+Hs-Source-Dirs: src+Extra-Source-Files: src/Tests.hs+GHC-Options: -Wall+
+ src/Luhn.hs view
@@ -0,0 +1,98 @@+{- |+Module : Luhn+Description : An implementation of Luhn's check digit algorithm.+Copyright : (c) N-Sim Ltd. 2008+License : BSD3++Maintainer : jhb@n-sim.com+Stability : provisional+Portability : portable++An implementation of Luhn's check digit algorithm.+-}+module Luhn(+ -- * Creating a check digit+ addLuhnDigit,+ -- * Validating a check digit+ checkLuhnDigit,+ -- * QuickCheck tests+ prop_checkLuhn,+ prop_checkSingleError+) where++import Test.QuickCheck++-- | Like Python's enumerate function - returns a tuple where the first+-- element is the index from 0 of the second element in the input list.+enumerate :: Integral n => [a] -> [(n, a)]+enumerate xs = enumerate' 0 xs+ where+ enumerate' _ [] = []+ enumerate' counter (a:as) =+ (counter, a) : enumerate' (counter + 1) as++-- | Returns the digits of a number as a list, in reverse order.+digitsRev :: Integral n => n -> [n]+digitsRev i = case i of+ 0 -> []+ _ -> lastDigit : digitsRev rest+ where (rest, lastDigit) = quotRem i 10++-- | Returns the digits of a number as a list.+digits :: Integral n => n -> [n]+digits = reverse . digitsRev++-- | Takes a list of digits, and converts them back into a number.+unDigits :: Integral n => [n] -> n+unDigits = foldl (\ a b -> a * 10 + b) 0++-- | Appends a Luhn check digit to the end of a number.+addLuhnDigit :: Integral n+ => n -- ^ Number to which a Luhn check digit will be appended.+ -> n -- ^ Number with the appended Luhn check digit.+addLuhnDigit num = num * 10 + checkDigit+ where+ checkDigit = (10 - total `mod` 10) `mod` 10+ total = sum $ concat $ map doubleEven (enumerate $ digitsRev num)+ doubleEven :: Integral n => (Int, n) -> [n]+ doubleEven (i, n) = if odd i+ then [n]+ else digitsRev (2 * n)++-- | Validates that the Luhn check digit (assumed to be the last/least-+-- significant digit in the number) is correct.+checkLuhnDigit :: Integral n+ => n -- ^ Number with a Luhn check digit as its last digit.+ -> Bool -- ^ Whether or not the check digit is consistent.+checkLuhnDigit num = total `mod` 10 == 0+ where+ total = sum $ concat $ map doubleOdd (enumerate $ digitsRev num)+ doubleOdd :: Integral n => (Int, n) -> [n]+ doubleOdd (i, n) = if odd i+ then digitsRev (2 * n)+ else [n]++-- | Validates that a generated check digit validates.+prop_checkLuhn+ :: Integer -- ^ Number to validate a Luhn check digit for.+ -> Property+prop_checkLuhn i = i > 0 ==> (checkLuhnDigit . addLuhnDigit) i++-- | Any single number transcription error should result in a failure in+-- the validation of a Luhn check digit. This property validates this.+prop_checkSingleError+ :: Integer -- ^ The number to transcribe.+ -> Integer -- ^ The position to introduce a transcription error.+ -> Integer -- ^ The number to transcribe in place of the original.+ -> Property+prop_checkSingleError i modDigit replace = i > 0 ==>+ let checkNum = addLuhnDigit i+ checkDigits = digits checkNum+ modDigit' = modDigit `mod` fromIntegral (length checkDigits - 1)+ start = take (fromIntegral modDigit') checkDigits+ rest = drop (fromIntegral (modDigit' + 1)) checkDigits+ newDigits = start ++ [replace `mod` 10] ++ rest+ newNum = unDigits newDigits+ in+ (newNum == checkNum) || (not $ checkLuhnDigit newNum)+
+ src/Tests.hs view
@@ -0,0 +1,12 @@+module Main where++import Luhn+import Text.Printf+import Test.QuickCheck++tests = [+ ("checkLuhn", quickCheck prop_checkLuhn),+ ("checkSingleError", quickCheck prop_checkSingleError)]++main = mapM_ (\(s,a) -> printf "%-25s: " s >> a) tests+