@@ -4,10 +4,11 @@
44module Main (main) where
55
66import Data.Functor.Identity (runIdentity)
7−import GHC.Word (Word8)
7+import Foreign.Storable (Storable(..))
88
99import Test.Hspec.QuickCheck
10−import Test.QuickCheck (Property, forAll, Gen, vectorOf, arbitrary, (===))
10+import Test.QuickCheck (Property, forAll, Gen, vectorOf, arbitrary, (===),
11+ choose)
1112
1213import Test.Hspec as H
1314
@@ -22,19 +23,70 @@ maxTestCount = 100
2223maxTestCount = 10
2324#endif
2425
25−randomLengthArray :: Word8 -> Property
26−randomLengthArray len =
27− forAll (vectorOf (fromIntegral len) (arbitrary :: Gen Int)) $ \list ->
28− let arr = runIdentity $ A.fromStreamN (fromIntegral len)
29− $ S.fromList list
30− in A.length arr === fromIntegral len
26+allocOverhead :: Int
27+allocOverhead = 2 * sizeOf (undefined :: Int)
3128
32−randomArrayRoundTrip :: Word8 -> Property
33−randomArrayRoundTrip len =
34− forAll (vectorOf (fromIntegral len) (arbitrary :: Gen Int)) $ \list ->
35− let arr = runIdentity $ A.fromStreamN (fromIntegral len)
36− $ S.fromList list
37− in runIdentity (S.toList (A.toStream arr)) === list
29+-- XXX this should be in sync with the defaultChunkSize in Array code, or we
30+-- should expose that and use that. For fast testing we could reduce the
31+-- defaultChunkSize under CPP conditionals.
32+--
33+defaultChunkSize :: Int
34+defaultChunkSize = 32 * k - allocOverhead
35+ where k = 1024
36+
37+maxArrLen :: Int
38+maxArrLen = defaultChunkSize * 8
39+
40+testLength :: Property
41+testLength =
42+ forAll (choose (0, maxArrLen)) $ \len ->
43+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
44+ let arr = runIdentity $ A.fromStreamN len
45+ $ S.fromList list
46+ in A.length arr === len
47+
48+testFromToStreamN :: Property
49+testFromToStreamN =
50+ forAll (choose (0, maxArrLen)) $ \len ->
51+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
52+ let arr = runIdentity $ A.fromStreamN len
53+ $ S.fromList list
54+ in runIdentity (S.toList (A.toStream arr)) === list
55+
56+testArraysOf :: Property
57+testArraysOf =
58+ forAll (choose (0, maxArrLen)) $ \len ->
59+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
60+ let xs = runIdentity
61+ $ S.toList
62+ $ S.concatMap A.toStream
63+ $ A.arraysOf 240
64+ $ S.fromList list
65+ in xs === list
66+
67+testFlattenArrays :: Property
68+testFlattenArrays =
69+ forAll (choose (0, maxArrLen)) $ \len ->
70+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
71+ let xs = runIdentity
72+ $ S.toList
73+ $ A.flattenArrays
74+ $ A.arraysOf 240
75+ $ S.fromList list
76+ in xs === list
77+
78+{-
79+testFromToStream :: Property
80+testFromToStream =
81+ forAll (choose (0, maxArrLen)) $ \len ->
82+ forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
83+ let xs = runIdentity
84+ $ S.toList
85+ $ A.toStream
86+ $ runIdentity. A.fromStream
87+ $ S.fromList list
88+ in xs === list
89+-}
3890
3991main :: IO ()
4092main = hspec
@@ -42,5 +94,8 @@ main = hspec
4294 $ modifyMaxSuccess (const maxTestCount)
4395 $ do
4496 describe "Construction" $ do
45− prop "length . fromStreamN n === n" $ randomLengthArray
46− prop "fromStreamN n . toStream === id" $ randomArrayRoundTrip
97+ prop "length . fromStreamN n === n" $ testLength
98+ prop "toStream . fromStreamN n === id" $ testFromToStreamN
99+ prop "arraysOf concats to original" $ testArraysOf
100+ prop "flattenArrays concats to original" $ testFlattenArrays
101+ -- prop "toStream . fromStream === id" $ testFromToStream