on 8ecf96a help sign in
streamly/test/String.hs
111 lines 3.1 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE FlexibleContexts #-}
4module Main (main) where
6import Data.Char (ord, chr)
7import Data.Word (Word8)
8import Test.Hspec.QuickCheck
9import Test.QuickCheck
10 (Property, forAll, Gen, listOf, arbitraryUnicodeChar, arbitrary)
11import Test.QuickCheck.Monadic (run, monadicIO, assert)
13import Test.Hspec as H
15import qualified Streamly.Mem.Array as A
16import qualified Streamly.Prelude as S
17import qualified Streamly.String as SS
19-- Coverage build takes too long with default number of tests
20{-
21maxTestCount :: Int
22#ifdef DEVBUILD
23maxTestCount = 100
24#else
25maxTestCount = 10
26#endif
27-}
29-- Use quickcheck-unicode instead?
30genUnicode :: Gen String
31genUnicode = listOf arbitraryUnicodeChar
33genWord8 :: Gen [Word8]
34genWord8 = listOf arbitrary
36propDecodeEncodeId :: Property
37propDecodeEncodeId =
38 forAll genUnicode $ \list ->
39 monadicIO $ do
40 let wrds = SS.encodeUtf8 $ S.fromList list
41 chrs <- S.toList $ SS.decodeUtf8 wrds
42 assert (chrs == list)
44testLines :: Property
45testLines =
46 forAll genUnicode $ \list ->
47 monadicIO $ do
48 xs <- S.toList
49 $ S.map A.toList
50 $ SS.lines
51 $ S.fromList list
52 assert (xs == lines list)
54testLinesArray :: Property
55testLinesArray =
56 forAll genWord8 $ \list ->
57 monadicIO $ do
58 xs <- S.toList
59 $ S.map A.toList
60 $ A.splitArraysOn 10
61 $ S.yield (A.fromList list)
62 assert (xs == map (map (fromIntegral . ord))
63 (lines (map (chr . fromIntegral) list)))
65testWords :: Property
66testWords =
67 forAll genUnicode $ \list ->
68 monadicIO $ do
69 xs <- S.toList
70 $ S.map A.toList
71 $ SS.words
72 $ S.fromList list
73 assert (xs == words list)
75testUnlines :: Property
76testUnlines =
77 forAll genUnicode $ \list ->
78 monadicIO $ do
79 xs <- S.toList
80 $ SS.unlines
81 $ SS.lines
82 $ S.fromList list
83 assert (xs == unlines (lines list))
85testUnwords :: Property
86testUnwords =
87 forAll genUnicode $ \list ->
88 monadicIO $ do
89 xs <- run
90 $ S.toList
91 $ SS.unwords
92 $ SS.words
93 $ S.fromList list
94 assert (xs == unwords (words list))
96main :: IO ()
97main = hspec
98 $ H.parallel
99 $ modifyMaxSuccess (const 1000)
100 $ do
101 describe "UTF8 - Encoding / Decoding" $ do
102 prop "decodeUtf8 . encodeUtf8 == id" $ propDecodeEncodeId
103 prop "Streamly.String.lines == Prelude.lines" $ testLines
104 prop "Arrays Streamly.String.lines == Prelude.lines" $ testLinesArray
105 prop "Streamly.String.words == Prelude.words" $ testWords
106 prop
107 "Streamly.String.unlines . Streamly.String.lines == unlines . lines"
108 $ testUnlines
109 prop
110 "Streamly.String.unwords . Streamly.String.words == unwords . words"
111 $ testUnwords