@@ -0,0 +1,109 @@
1+
2+-- Coverage build takes too long with default number of tests
3+maxTestCount :: Int
4+#ifdef DEVBUILD
5+maxTestCount = 100
6+#else
7+maxTestCount = 10
8+#endif
9+
10+allocOverhead :: Int
11+allocOverhead = 2 * sizeOf (undefined :: Int)
12+
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+--
17+defaultChunkSize :: Int
18+defaultChunkSize = 32 * k - allocOverhead
19+ where k = 1024
20+
21+maxArrLen :: Int
22+maxArrLen = defaultChunkSize * 8
23+
24+genericTestFrom ::
25+ (Int -> SerialT IO Int -> IO (Array Int))
26+ -> Property
27+genericTestFrom 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)
33+
34+testLength :: Property
35+testLength = genericTestFrom (\n -> S.fold (A.writeN n))
36+
37+testLengthFromStreamN :: Property
38+testLengthFromStreamN = genericTestFrom A.fromStreamN
39+
40+genericTestFromTo ::
41+ (Int -> SerialT IO Int -> IO (Array Int))
42+ -> (Array Int -> SerialT IO Int)
43+ -> ([Int] -> [Int] -> Bool)
44+ -> Property
45+genericTestFromTo 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.toList $ arrUnfold arr
51+ assert (listEq xs list)
52+
53+
54+testFoldNUnfold :: Property
55+testFoldNUnfold =
56+ genericTestFromTo (\n -> S.fold (A.writeN n)) (S.unfold A.read) (==)
57+
58+testFoldNToStream :: Property
59+testFoldNToStream =
60+ genericTestFromTo (\n -> S.fold (A.writeN n)) A.toStream (==)
61+
62+testFoldNToStreamRev :: Property
63+testFoldNToStreamRev =
64+ genericTestFromTo
65+ (\n -> S.fold (A.writeN n))
66+ A.toStreamRev
67+ (\xs list -> xs == reverse list)
68+
69+testFromStreamNUnfold :: Property
70+testFromStreamNUnfold = genericTestFromTo A.fromStreamN (S.unfold A.read) (==)
71+
72+testFromStreamNToStream :: Property
73+testFromStreamNToStream = genericTestFromTo A.fromStreamN A.toStream (==)
74+
75+testFromListN :: Property
76+testFromListN =
77+ forAll (choose (0, maxArrLen)) $ \len ->
78+ forAll (choose (0, len)) $ \n ->
79+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
80+ monadicIO $ do
81+ let arr = A.fromListN n list
82+ xs <- run $ S.toList $ (S.unfold A.read) arr
83+ listEquals (==) xs (take n list)
84+
85+foldManyWith :: (Int -> Fold IO Int (Array Int)) -> Property
86+foldManyWith f =
87+ forAll (choose (0, maxArrLen)) $ \len ->
88+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
89+ monadicIO $ do
90+ xs <- run
91+ $ S.toList
92+ $ S.unfoldMany A.read
93+ $ S.foldMany (f 240)
94+ $ S.fromList list
95+ assert (xs == list)
96+
97+commonMain :: SpecWith ()
98+commonMain = do
99+ describe "Construction" $ do
100+ prop "length . writeN n === n" testLength
101+ prop "length . fromStreamN n === n" testLengthFromStreamN
102+ prop "read . writeN === id " testFoldNUnfold
103+ prop "toStream . writeN === id" testFoldNToStream
104+ prop "toStreamRev . writeN === reverse" testFoldNToStreamRev
105+ prop "read . fromStreamN === id" testFromStreamNUnfold
106+ prop "toStream . fromStreamN === id" testFromStreamNToStream
107+ prop "fromListN" testFromListN
108+ prop "foldMany with writeN concats to original"
109+ (foldManyWith A.writeN)