48c669b735d74c36ca2faebb98b58649911abad7
[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
5 import System.IO
6 import Data.Word
7
8 main = main4
9
10 main1 = do putStrLn "Speed: Data.ByteString"
11 txt <- S.readFile "test"
12 print (UTF8.length $ UTF8.fromRep txt)
13
14 main2 = do putStrLn "Speed: Data.ByteString.Lazy"
15 txt <- L.readFile "test"
16 print (UTF8.length $ UTF8.fromRep txt)
17
18 main3 = do putStrLn "Speed: [Word8]"
19 txt <- hGetContents =<< openBinaryFile "test" ReadMode
20 let bytes :: [Word8]
21 bytes = map (fromIntegral . fromEnum) txt
22 print (UTF8.length $ UTF8.fromRep bytes)
23
24 main4 = do putStrLn "Correctness: Data.ByteString"
25 print encodeDecodeTest
26
27 encodeDecodeTest :: String
28 encodeDecodeTest =
29 filter (\x -> enc x /= [x]) legal_codepoints
30 ++ filter (\x -> enc x /= [UTF8.replacement_char]) illegal_codepoints
31 where
32 legal_codepoints = ['\0'..'\xd7ff'] ++ ['\xe000'..'\xfffd']
33 ++ ['\x10000'..'\x10ffff']
34 illegal_codepoints = '\xffff' : '\xfffe' : ['\xd800'..'\xdfff']
35
36 {-# INLINE enc #-}
37 enc x = UTF8.toString (UTF8.fromString [x] :: UTF8.UTF8 S.ByteString)
38
39