Skip to content

Commit

Permalink
Add simple test of equivalence of unstream variants
Browse files Browse the repository at this point in the history
It turns out we don't exercise munstream in the test suite at all. (Easy check
is to replace definition with undefined and run test) This is to check
equivalence of all variants.

This is necessary for any changes to unstream machinery. Such as ones that
discussed in haskell#301, haskell#388, haskell#406
  • Loading branch information
Shimuuar committed Sep 12, 2021
1 parent de5774e commit 741b810
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions vector/tests/Tests/Vector/Boxed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ tests =
testBoolBoxedVector (undefined :: Data.Vector.Vector Bool)
, testGroup "Int" $
testNumericBoxedVector (undefined :: Data.Vector.Vector Int)
, testGroup "unstream" $ testUnstream (undefined :: Data.Vector.Vector Int)
]
1 change: 1 addition & 0 deletions vector/tests/Tests/Vector/Primitive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ tests =
, testGroup "Double" $
testNumericPrimitiveVector
(undefined :: Data.Vector.Primitive.Vector Double)
, testGroup "unstream" $ testUnstream (undefined :: Data.Vector.Primitive.Vector Int)
]
30 changes: 30 additions & 0 deletions vector/tests/Tests/Vector/Property.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Tests.Vector.Property
, testNumFunctions
, testNestedVectorFunctions
, testDataFunctions
, testUnstream
-- re-exports
, Data
, Random
Expand Down Expand Up @@ -797,3 +798,32 @@ testDataFunctions _ = $(testProperties ['prop_glength])

toA :: Data b => b -> Int
toA x = maybe (glength x) (const 1) (cast x :: Maybe a)

testUnstream :: forall v. (CommonContext Int v) => v Int -> [TestTree]
{-# INLINE testUnstream #-}
testUnstream _ =
[ testProperty "unstream == vunstream (exact)" $ \(n :: Int) ->
let v1,v2 :: v Int
v1 = runST $ V.freeze =<< MV.unstream (streamExact n)
v2 = runST $ V.freeze =<< MV.vunstream (streamExact n)
in v1 == v2
, testProperty "unstream == vunstream (unknown)" $ \(n :: Int) ->
let v1,v2 :: v Int
v1 = runST $ V.freeze =<< MV.unstream (streamUnknown n)
v2 = runST $ V.freeze =<< MV.vunstream (streamUnknown n)
in v1 == v2
--
, testProperty "unstreamR ~= vunstream (exact)" $ \(n :: Int) ->
let v1,v2 :: v Int
v1 = runST $ V.freeze =<< MV.unstreamR (streamExact n)
v2 = runST $ V.freeze =<< MV.vunstream (streamExact n)
in V.reverse v1 == v2
, testProperty "unstreamR ~= vunstream (unknown)" $ \(n :: Int) ->
let v1,v2 :: v Int
v1 = runST $ V.freeze =<< MV.unstreamR (streamUnknown n)
v2 = runST $ V.freeze =<< MV.vunstream (streamUnknown n)
in V.reverse v1 == v2
]
where
streamExact n = S.generate (abs n) id
streamUnknown = S.unfoldr (\i -> if i > 0 then (Just (i-1,i-1)) else Nothing) . abs
1 change: 1 addition & 0 deletions vector/tests/Tests/Vector/Storable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ tests =
testNumericStorableVector (undefined :: Data.Vector.Storable.Vector Int)
, testGroup "Data.Vector.Storable.Vector (Double)" $
testNumericStorableVector (undefined :: Data.Vector.Storable.Vector Double)
, testGroup "unstream" $ testUnstream (undefined :: Data.Vector.Storable.Vector Int)
]
1 change: 1 addition & 0 deletions vector/tests/Tests/Vector/Unboxed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ tests =
, testGroup "(Int,Bool,Int)" $
testTupleUnboxedVector
(undefined :: Data.Vector.Unboxed.Vector (Int, Bool, Int))
, testGroup "unstream" $ testUnstream (undefined :: Data.Vector.Unboxed.Vector Int)
]

0 comments on commit 741b810

Please sign in to comment.