Skip to content

Instantly share code, notes, and snippets.

@7shi
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 7shi/559091b71ca0bc521599 to your computer and use it in GitHub Desktop.
Save 7shi/559091b71ca0bc521599 to your computer and use it in GitHub Desktop.
[Haskell]バイナリ操作の取っ掛かり
{-# LANGUAGE CPP, TemplateHaskell #-}
-----------------------------------------------------------------------------
--
-- Module : Main
-- Copyright :
-- License : Public Domain
--
-- Maintainer :
-- Stability :
-- Portability :
--
-- |
--
-----------------------------------------------------------------------------
module Main (
main
) where
import Control.Monad (unless)
import Data.List (stripPrefix)
import System.Exit (exitFailure)
import Test.QuickCheck.All (quickCheckAll)
import qualified Data.ByteString as B
import Data.Char
import Text.Printf
import Data.Bits
hex1 n
| 0 <= n && n <= 9 = chr ((ord '0') + n)
| 10 <= n && n <= 15 = chr ((ord 'a') + (n - 0xa))
hex2 n = [hex1 (shiftR n 4)] ++ [hex1 (n .&. 0xf)]
prop_hex1 n = [hex1 m] == printf "%x" m where m = n .&. 0xf
prop_hex2 n = hex2 m == printf "%02x" m where m = n .&. 0xff
-- Hello World
exeMain = do
bin <- B.readFile "test"
putStrLn $ "len " ++ (show $ B.length bin)
-- Entry point for unit tests.
testMain = do
allPass <- $quickCheckAll -- Run QuickCheck on all prop_ functions
unless allPass exitFailure
-- This is a clunky, but portable, way to use the same Main module file
-- for both an application and for unit tests.
-- MAIN_FUNCTION is preprocessor macro set to exeMain or testMain.
-- That way we can use the same file for both an application and for tests.
#ifndef MAIN_FUNCTION
#define MAIN_FUNCTION exeMain
#endif
main = MAIN_FUNCTION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment