on 0bd3d4e help sign in
101 lines 3.3 kB history raw
2#include "MachDeps.h"
4-- Coverage build takes too long with default number of tests
5maxTestCount :: Int
6#ifdef DEVBUILD
7maxTestCount = 100
8#else
9maxTestCount = 10
10#endif
12maxArrLen :: Int
13maxArrLen = defaultChunkSize * 8
15genericTestFrom ::
16 (Int -> Stream IO Int -> IO (Array Int))
17 -> Property
18genericTestFrom arrFold =
19 forAll (choose (0, maxArrLen)) $ \len ->
20 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
21 monadicIO $ do
22 arr <- run $ arrFold len $ S.fromList list
23 assert (A.length arr == len)
25testLength :: Property
26testLength = genericTestFrom (S.fold . A.writeN)
28testLengthFromStreamN :: Property
29testLengthFromStreamN = genericTestFrom A.fromStreamN
31genericTestFromTo ::
32 (Int -> Stream IO Int -> IO (Array Int))
33 -> (Array Int -> Stream IO Int)
34 -> ([Int] -> [Int] -> Bool)
35 -> Property
36genericTestFromTo arrFold arrUnfold listEq =
37 forAll (choose (0, maxArrLen)) $ \len ->
38 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
39 monadicIO $ do
40 arr <- run $ arrFold len $ S.fromList list
41 xs <- run $ S.fold Fold.toList $ arrUnfold arr
42 assert (listEq xs list)
45testFoldNUnfold :: Property
46testFoldNUnfold =
47 genericTestFromTo (S.fold . A.writeN) (S.unfold A.reader) (==)
49testFoldNToStream :: Property
50testFoldNToStream =
51 genericTestFromTo (S.fold . A.writeN) A.read (==)
53testFoldNToStreamRev :: Property
54testFoldNToStreamRev =
55 genericTestFromTo
56 (S.fold . A.writeN)
57 A.readRev
58 (\xs list -> xs == reverse list)
60testFromStreamNUnfold :: Property
61testFromStreamNUnfold =
62 genericTestFromTo A.fromStreamN (S.unfold A.reader) (==)
64testFromStreamNToStream :: Property
65testFromStreamNToStream = genericTestFromTo A.fromStreamN A.read (==)
67testFromListN :: Property
68testFromListN =
69 forAll (choose (0, maxArrLen)) $ \len ->
70 forAll (choose (0, len)) $ \n ->
71 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
72 monadicIO $ do
73 let arr = A.fromListN n list
74 xs <- run $ S.fold Fold.toList $ S.unfold A.reader arr
75 listEquals (==) xs (take n list)
77foldManyWith :: (Int -> Fold IO Int (Array Int)) -> Property
78foldManyWith f =
79 forAll (choose (0, maxArrLen)) $ \len ->
80 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
81 monadicIO $ do
82 xs <- run
83 $ S.fold Fold.toList
84 $ S.unfoldMany A.reader
85 $ S.foldMany (f 240)
86 $ S.fromList list
87 assert (xs == list)
89commonMain :: SpecWith ()
90commonMain = do
91 describe "Construction" $ do
92 prop "length . writeN n === n" testLength
93 prop "length . fromStreamN n === n" testLengthFromStreamN
94 prop "read . writeN === id " testFoldNUnfold
95 prop "toStream . writeN === id" testFoldNToStream
96 prop "toStreamRev . writeN === reverse" testFoldNToStreamRev
97 prop "read . fromStreamN === id" testFromStreamNUnfold
98 prop "toStream . fromStreamN === id" testFromStreamNToStream
99 prop "fromListN" testFromListN
100 prop "foldMany with writeN concats to original"
101 (foldManyWith A.writeN)