Skip to content

Commit

Permalink
WIP: Fix #257
Browse files Browse the repository at this point in the history
Fix the test case to trigger the sliceCheck
  • Loading branch information
lehins committed Jan 30, 2020
1 parent e49e948 commit 2135f12
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Data/Vector/Internal/Check.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,6 @@ checkSlice :: String -> Int -> Checks -> String -> Int -> Int -> Int -> a -> a
{-# INLINE checkSlice #-}
checkSlice file line kind loc i m n x
= check file line kind loc (checkSlice_msg i m n)
(i >= 0 && m >= 0 && i+m <= n) x
-- (i >= 0 && m >= 0 && i+m <= n) x
(i >= 0 && m >= 0 && m <= n - i) x

2 changes: 1 addition & 1 deletion tests/Tests/Vector/Unboxed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Tests.Vector.Unboxed (tests) where
import Test.Framework
import qualified Data.Vector.Unboxed
import Tests.Vector.Property
import Data.Data


testGeneralUnboxedVector :: forall a. (CommonContext a Data.Vector.Unboxed.Vector, Data.Vector.Unboxed.Unbox a, Ord a, Data a) => Data.Vector.Unboxed.Vector a -> [Test]
testGeneralUnboxedVector dummy = concatMap ($ dummy)
Expand Down
44 changes: 43 additions & 1 deletion tests/Tests/Vector/UnitTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@
module Tests.Vector.UnitTests (tests) where

import Control.Applicative as Applicative
import Control.Exception
import Control.Monad.Primitive
import Control.Monad.ST
import qualified Data.List as List
import qualified Data.Vector.Generic as Generic
import qualified Data.Vector.Generic.Mutable as MGeneric
import qualified Data.Vector as Boxed
import qualified Data.Vector.Primitive as Primitive
import qualified Data.Vector.Storable as Storable
import qualified Data.Vector.Unboxed as Unboxed
import Foreign.Ptr
import Foreign.Storable
import Text.Printf

import Test.Framework
import Test.Framework.Providers.HUnit (testCase)
import Test.HUnit (Assertion, assertBool)
import Test.HUnit (Assertion, assertBool, assertFailure)

newtype Aligned a = Aligned { getAligned :: a }

Expand Down Expand Up @@ -42,8 +49,43 @@ tests =
, testCase "Aligned Int" $
checkAddressAlignment alignedIntVec
]
, testGroup "Regression tests"
[ testGroup "checkSlice (slice crash #257)"
[ testCase "Boxed" $ checkSliceOverflow Boxed.slice
, testCase "Primitive" $ checkSliceOverflow Primitive.slice
, testCase "Storable" $ checkSliceOverflow Storable.slice
, testCase "Unboxed" $ checkSliceOverflow Unboxed.slice
]
]
]

checkSliceOverflow ::
Generic.Vector v Int => (Int -> Int -> v Int -> v Int) -> Assertion
checkSliceOverflow slice' = do
eRes <- try (pure $! slice' 1 m vec)
case eRes of
Right _ ->
assertFailure "Data.Vector.Internal.Check.checkSlice has overflown"
Left (ErrorCall err) ->
let assertMsg =
List.concat
[ "Expected slice function to produce an 'error' ending with: \""
, errSuffix
, "\" instead got: \""
, err
]
in assertBool assertMsg (errSuffix `List.isSuffixOf` err)
where
m = maxBound :: Int
errSuffix =
"(slice): invalid slice (1," ++ show m ++ "," ++ show (length xs) ++ ")"
xs = [1, 2, 3, 4, 5] :: [Int]
-- Ensure vector is not build from a stream
vec = runST $ do
mv <- MGeneric.new 5
mapM_ (\(i, e) -> MGeneric.write mv i e) $ List.zip [0..] xs
Generic.freeze mv

alignedDoubleVec :: Storable.Vector (Aligned Double)
alignedDoubleVec = Storable.fromList $ map Aligned [1, 2, 3, 4, 5]

Expand Down

0 comments on commit 2135f12

Please sign in to comment.