Add parameters to testing program
[packages/utf8-string.git] / tests / BenchBytestring.hs
1 import qualified Data.ByteString as S
2 import qualified Data.ByteString.Lazy as L
3 import qualified Data.String.UTF8 as UTF8
4 import qualified Codec.Binary.UTF8.String as List
5
6 import System.Environment(getArgs)
7 import System.IO
8 import Data.Word
9
10 main = mapM_ run_test =<< getArgs
11
12 run_test x = case reads x of
13 [(n,"")] | n < test_num -> tests !! n
14 _ -> hPutStrLn stderr ("Invalid test: " ++ x)
15
16 tests = [ main0, main1, main2, main3, main4 ]
17 test_num = length tests
18
19
20 main0 = do putStrLn "Correctness: Data.ByteString"
21 putStrLn ("Errors: " ++ show encodeDecodeTest)
22
23 main1 = do putStrLn "Speed: Data.ByteString"
24 txt <- S.readFile "test"
25 print (UTF8.length $ UTF8.fromRep txt)
26
27 main2 = do putStrLn "Speed: Data.ByteString.Lazy"
28 txt <- L.readFile "test"
29 print (UTF8.length $ UTF8.fromRep txt)
30
31 main3 = do putStrLn "Speed: [Word8]"
32 txt <- hGetContents =<< openBinaryFile "test" ReadMode
33 let bytes :: [Word8]
34 bytes = map (fromIntegral . fromEnum) txt
35 print (UTF8.length $ UTF8.fromRep bytes)
36
37 main4 = do putStrLn "Speed: [Word8] (direct)"
38 txt <- hGetContents =<< openBinaryFile "test" ReadMode
39 let bytes :: [Word8]
40 bytes = map (fromIntegral . fromEnum) txt
41 print (length $ List.decode bytes)
42
43 encodeDecodeTest :: String
44 encodeDecodeTest =
45 filter (\x -> enc x /= [x]) legal_codepoints
46 ++ filter (\x -> enc x /= [UTF8.replacement_char]) illegal_codepoints
47 where
48 legal_codepoints = ['\0'..'\xd7ff'] ++ ['\xe000'..'\xfffd']
49 ++ ['\x10000'..'\x10ffff']
50 illegal_codepoints = '\xffff' : '\xfffe' : ['\xd800'..'\xdfff']
51
52 {-# INLINE enc #-}
53 enc x = UTF8.toString (UTF8.fromString [x] :: UTF8.UTF8 S.ByteString)
54
55