packages feed

crc16-0.1.1: Tests.hs

module Tests where

import Test.HUnit.Base
import Test.HUnit.Text
import Data.Digest.CRC16
import qualified Data.ByteString as B
import Data.Word


crc16 :: Word16 -> Bool -> Word16 -> [Word8] -> Word16
crc16 poly inverse initial =
    B.foldl (crc16Update poly inverse) initial . B.pack


-- | Test functions
--
-- Run with: 
-- 
-- > import Test.HUnit.Text
-- > runTestTT tests
--
-- Checkout http://zorc.breitbandkatze.de/crc.html
tests :: Test
tests = "Basic" ~: [
             0x29B1 ~=? crc16 0x1021 False 0xffff simple_string
            ,0x89f6 ~=? crc16 0x1021 True 0xffff simple_string
            ,0x31C3 ~=? crc16 0x1021 False 0x0000 simple_string
        ]
        where 
            simple_string = [fromIntegral (fromEnum x) :: Word8 | x <- "123456789"]