-
Notifications
You must be signed in to change notification settings - Fork 4
/
Generate.hs
55 lines (49 loc) · 1.09 KB
/
Generate.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{-# LANGUAGE ScopedTypeVariables #-}
-- |
import Data.Char
import Data.Ix
import Data.List
import Data.Monoid
import Test.QuickCheck
fieldCount = 10
rowCount = 1000
newtype S = S String
instance Arbitrary S where
arbitrary = do
s <- arbitrary
pure (S (map clean s))
where
clean c =
if inRange ('a', 'z') c ||
inRange ('0', '9') c || inRange ('A', 'Z') c || elem c "\r\n,\" \t"
then c
else 'a'
main :: IO ()
main = do
rows <-
generate
(vectorOf
1000
((,,,) <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary))
putStr
(unlines
(map
(intercalate "," .
map printField .
(\(S v, S x, S y, S z) -> take fieldCount (cycle [v, x, y, z])))
rows))
printField :: String -> String
printField cs =
if any invalid cs
then escape cs
else cs
where
invalid c = c == ',' || c == '\r' || c == '\n' || c == '"'
escape =
("\"" <>) .
(<> "\"") .
concatMap
(\c ->
if c == '"'
then "\"\""
else [c])