on 666eb07 help sign in
110 lines 3.7 kB history raw
2-- Coverage build takes too long with default number of tests
3maxTestCount :: Int
4#ifdef DEVBUILD
5maxTestCount = 100
6#else
7maxTestCount = 10
8#endif
10allocOverhead :: Int
11allocOverhead = 2 * sizeOf (undefined :: Int)
13-- XXX this should be in sync with the defaultChunkSize in Array code, or we
14-- should expose that and use that. For fast testing we could reduce the
15-- defaultChunkSize under CPP conditionals.
16--
17defaultChunkSize :: Int
18defaultChunkSize = 32 * k - allocOverhead
19 where k = 1024
21maxArrLen :: Int
22maxArrLen = defaultChunkSize * 8
24genericTestFrom ::
25 (Int -> Stream IO Int -> IO (Array Int))
26 -> Property
27genericTestFrom arrFold =
28 forAll (choose (0, maxArrLen)) $ \len ->
29 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
30 monadicIO $ do
31 arr <- run $ arrFold len $ S.fromList list
32 assert (A.length arr == len)
34testLength :: Property
35testLength = genericTestFrom (S.fold . A.writeN)
37testLengthFromStreamN :: Property
38testLengthFromStreamN = genericTestFrom A.fromStreamN
40genericTestFromTo ::
41 (Int -> Stream IO Int -> IO (Array Int))
42 -> (Array Int -> Stream IO Int)
43 -> ([Int] -> [Int] -> Bool)
44 -> Property
45genericTestFromTo arrFold arrUnfold listEq =
46 forAll (choose (0, maxArrLen)) $ \len ->
47 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
48 monadicIO $ do
49 arr <- run $ arrFold len $ S.fromList list
50 xs <- run $ S.fold Fold.toList $ arrUnfold arr
51 assert (listEq xs list)
54testFoldNUnfold :: Property
55testFoldNUnfold =
56 genericTestFromTo (S.fold . A.writeN) (S.unfold A.reader) (==)
58testFoldNToStream :: Property
59testFoldNToStream =
60 genericTestFromTo (S.fold . A.writeN) A.read (==)
62testFoldNToStreamRev :: Property
63testFoldNToStreamRev =
64 genericTestFromTo
65 (S.fold . A.writeN)
66 A.readRev
67 (\xs list -> xs == reverse list)
69testFromStreamNUnfold :: Property
70testFromStreamNUnfold =
71 genericTestFromTo A.fromStreamN (S.unfold A.reader) (==)
73testFromStreamNToStream :: Property
74testFromStreamNToStream = genericTestFromTo A.fromStreamN A.read (==)
76testFromListN :: Property
77testFromListN =
78 forAll (choose (0, maxArrLen)) $ \len ->
79 forAll (choose (0, len)) $ \n ->
80 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
81 monadicIO $ do
82 let arr = A.fromListN n list
83 xs <- run $ S.fold Fold.toList $ S.unfold A.reader arr
84 listEquals (==) xs (take n list)
86foldManyWith :: (Int -> Fold IO Int (Array Int)) -> Property
87foldManyWith f =
88 forAll (choose (0, maxArrLen)) $ \len ->
89 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
90 monadicIO $ do
91 xs <- run
92 $ S.fold Fold.toList
93 $ S.unfoldMany A.reader
94 $ S.foldMany (f 240)
95 $ S.fromList list
96 assert (xs == list)
98commonMain :: SpecWith ()
99commonMain = do
100 describe "Construction" $ do
101 prop "length . writeN n === n" testLength
102 prop "length . fromStreamN n === n" testLengthFromStreamN
103 prop "read . writeN === id " testFoldNUnfold
104 prop "toStream . writeN === id" testFoldNToStream
105 prop "toStreamRev . writeN === reverse" testFoldNToStreamRev
106 prop "read . fromStreamN === id" testFromStreamNUnfold
107 prop "toStream . fromStreamN === id" testFromStreamNToStream
108 prop "fromListN" testFromListN
109 prop "foldMany with writeN concats to original"
110 (foldManyWith A.writeN)