{-# LANGUAGE BangPatterns #-} {-# LANGUAGE MagicHash #-} module Main where import Data.Word import Data.List (findIndex) import GHC.Exts -- Having a wrapper gives us two things: -- * it's easier to test everything (no need for code using raw primops) -- * we test the deriving mechanism for Word8# data TestWord8 = T8 Word8# deriving (Eq, Ord) mkT8 :: Word -> TestWord8 mkT8 (W# a) = T8 (wordToWord8# a) main :: IO () main = do let input = [ (a, b) | a <- allWord8, b <- allWord8 ] -- -- (==) -- let expected = [ a == b | (a, b) <- input ] actual = [ mkT8 a == mkT8 b | (a, b) <- input ] checkResults "(==)" input expected actual -- -- (/=) -- let expected = [ a /= b | (a, b) <- input ] actual = [ mkT8 a /= mkT8 b | (a, b) <- input ] checkResults "(/=)" input expected actual -- -- (<) -- let expected = [ a < b | (a, b) <- input ] actual = [ mkT8 a < mkT8 b | (a, b) <- input ] checkResults "(<)" input expected actual -- -- (>) -- let expected = [ a > b | (a, b) <- input ] actual = [ mkT8 a > mkT8 b | (a, b) <- input ] checkResults "(>)" input expected actual -- -- (<=) -- let expected = [ a <= b | (a, b) <- input ] actual = [ mkT8 a <= mkT8 b | (a, b) <- input ] checkResults "(<=)" input expected actual -- -- (>=) -- let expected = [ a >= b | (a, b) <- input ] actual = [ mkT8 a >= mkT8 b | (a, b) <- input ] checkResults "(>=)" input expected actual checkResults :: (Eq a, Eq b, Show a, Show b) => String -> [a] -> [b] -> [b] -> IO () checkResults test inputs expected actual = case findIndex (\(e, a) -> e /= a) (zip expected actual) of Nothing -> putStrLn $ "Pass: " ++ test Just i -> error $ "FAILED: " ++ test ++ " for input: " ++ show (inputs !! i) ++ " expected: " ++ show (expected !! i) ++ " but got: " ++ show (actual !! i) allWord8 :: [Word] allWord8 = [ minWord8 .. maxWord8 ] minWord8 :: Word minWord8 = fromIntegral (minBound :: Word8) maxWord8 :: Word maxWord8 = fromIntegral (maxBound :: Word8)