1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE TypeApplications #-}
{-# Language CPP #-}
{-# Language DataKinds #-}
{-# Language ExplicitForAll #-}
{-# Language FlexibleInstances #-}
{-# Language LambdaCase #-}
{-# Language OverloadedStrings #-}
{-# Language ScopedTypeVariables #-}
{-# Language StandaloneDeriving #-}
{-# Language TypeFamilies #-}
{-# Language TypeOperators #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
#if __GLASGOW_HASKELL__ >= 805
{-# Language NoStarIsType #-}
#endif
module Test.Vector
( vecTests
, SomeVector(..)
, genSomeVector
, genVectorOfLength
, genOrdering
, orderingEndomorphisms
, orderingToStringFuns
)
where
import Data.Functor.Const (Const(..))
import Data.Functor.WithIndex (imap)
import Data.Foldable.WithIndex (ifoldMap)
import Data.Maybe (isJust)
import qualified Data.List as List
import qualified Data.Parameterized.Context as Ctx
import Data.Parameterized.Fin
import Data.Parameterized.NatRepr
import Data.Parameterized.Some
import Data.Parameterized.Vector
import Data.Semigroup
import GHC.TypeLits (KnownNat)
import Hedgehog
import qualified Hedgehog.Gen as HG
import Hedgehog.Range
import Numeric.Natural (Natural)
import Prelude hiding (take, reverse, length)
import qualified Prelude as P
import Test.Fin (genFin)
import Test.Tasty
import Test.Tasty.Hedgehog
import Test.Context (genSomePayloadList, mkUAsgn)
#if __GLASGOW_HASKELL__ >= 806
import qualified Hedgehog.Classes as HC
import Test.Tasty.HUnit (assertBool, testCase)
#endif
data SomeVector a = forall n. SomeVector (Vector n a)
instance Show a => Show (SomeVector a) where
show (SomeVector v) = show v
genVectorOfLength :: (Monad m) => NatRepr n -> GenT m a -> GenT m (Vector (n + 1) a)
genVectorOfLength n genElem =
do let w = widthVal n
l <- HG.list (linear (w + 1) (w + 1)) genElem
case testLeq (knownNat @1) (incNat n) of
Nothing -> error "testLeq in genSomeVector"
Just LeqProof ->
case fromList (incNat n) l of
Just v -> return v
Nothing -> error ("fromList failure for size " <> show w)
genSomeVector :: (Monad m) => GenT m a -> GenT m (SomeVector a)
genSomeVector genElem =
do Some len <- mkNatRepr <$> HG.integral (linear 0 (99 :: Natural))
SomeVector <$> genVectorOfLength len genElem
genVectorKnownLength :: (1 <= n, KnownNat n, Monad m) => GenT m a -> GenT m (Vector n a)
genVectorKnownLength genElem =
do let n = knownNat
w = widthVal n
l <- HG.list (constant w w) genElem
case fromList n l of
Just v -> return v
Nothing -> error ("fromList failure for size " <> show w)
genOrdering :: Monad m => GenT m Ordering
genOrdering = HG.element [ LT, EQ, GT ]
instance Show (a -> b) where
show _ = "unshowable"
-- Used to test e.g., 'fmap (g . f) = fmap g . fmap f' and 'imap (const f) =
-- fmap f'.
orderingEndomorphisms :: [Ordering -> Ordering]
orderingEndomorphisms =
[ const EQ
, id
, \case
EQ -> EQ
LT -> GT
GT -> LT
, \case
LT -> EQ
EQ -> GT
GT -> LT
]
-- | Used to test ifoldMap.
orderingToStringFuns :: [ Ordering -> String ]
orderingToStringFuns =
[ const "s"
, show
]
prop_reverse100 :: Property
prop_reverse100 = property $
do SomeVector v <- forAll $ genSomeVector genOrdering
case testLeq (knownNat @1) (length v) of
Nothing -> pure ()
Just LeqProof -> v === (reverse $ reverse v)
prop_reverseSingleton :: Property
prop_reverseSingleton = property $
do l <- (:[]) <$> forAll genOrdering
Just v <- return $ fromList (knownNat @1) l
v === reverse v
prop_splitJoin :: Property
prop_splitJoin = property $
do let n = knownNat @5
v <- forAll $ genVectorKnownLength @(5 * 5) genOrdering
v === (join n $ split n (knownNat @5) v)
prop_cons :: Property
prop_cons = property $
do let n = knownNat @20
w = widthVal n
l <- forAll $ HG.list (constant w w) genOrdering
x <- forAll genOrdering
(cons x <$> fromList n l) === fromList (incNat n) (x:l)
prop_snoc :: Property
prop_snoc = property $
do let n = knownNat @20
w = widthVal n
l <- forAll $ HG.list (constant w w) genOrdering
x <- forAll genOrdering
(flip snoc x <$> fromList n l) === fromList (incNat n) (l ++ [x])
prop_snocUnsnoc :: Property
prop_snocUnsnoc = property $
do let n = knownNat @20
w = widthVal n
l <- forAll $ HG.list (constant w w) genOrdering
x <- forAll genOrdering
(fst . unsnoc . flip snoc x <$> fromList n l) === Just x
prop_generate :: Property
prop_generate = property $
do let n = knownNat @55
w = widthVal n
funs :: [ Int -> Ordering ] -- some miscellaneous functions to generate Vector values
funs = [ const EQ
, \i -> if i < 10 then LT else if i > 15 then GT else EQ
, \i -> if i == 0 then EQ else GT
]
f <- forAll $ HG.element funs
Just (generate n (f . widthVal)) === fromList (incNat n) (map f [0..w])
prop_unfold :: Property
prop_unfold = property $
do let n = knownNat @55
w = widthVal n
funs :: [ Ordering -> (Ordering, Ordering) ] -- some miscellaneous functions to generate Vector values
funs = [ const (EQ, EQ)
, \case
LT -> (LT, GT)
GT -> (GT, LT)
EQ -> (EQ, EQ)
]
f <- forAll $ HG.element funs
o <- forAll $ HG.element [EQ, LT, GT]
Just (unfoldr n f o) === fromList (incNat n) (P.take (w + 1) (List.unfoldr (Just . f) o))
prop_toFromAssignment :: Property
prop_toFromAssignment = property $
do vals <- forAll genSomePayloadList
Some a <- return $ mkUAsgn vals
let sz = Ctx.size a
case Ctx.viewSize sz of
Ctx.ZeroSize -> pure ()
Ctx.IncSize _ ->
let a' =
toAssignment
sz
(\_idx val -> Const val)
(fromAssignment Some a)
in do assert $
isJust $
testEquality
(Ctx.sizeToNatRepr sz)
(Ctx.sizeToNatRepr (Ctx.size a'))
viewSome
(\lastElem ->
assert $
isJust $
testEquality
(a Ctx.! Ctx.lastIndex sz) lastElem)
(getConst (a' Ctx.! Ctx.lastIndex sz))
prop_fmapId :: Property
prop_fmapId = property $
do SomeVector v <- forAll $ genSomeVector genOrdering
fmap id v === v
prop_fmapCompose :: Property
prop_fmapCompose = property $
do SomeVector v <- forAll $ genSomeVector genOrdering
f <- forAll $ HG.element orderingEndomorphisms
g <- forAll $ HG.element orderingEndomorphisms
fmap (g . f) v === fmap g (fmap f v)
prop_iterateNRange :: Property
prop_iterateNRange = property $
do Some len <- mkNatRepr <$> forAll (HG.integral (linear 0 (99 :: Natural)))
toList (iterateN len (+1) 0) === [0..(natValue len)]
prop_indicesOfRange :: Property
prop_indicesOfRange = property $
do SomeVector v <- forAll $ genSomeVector genOrdering
toList (fmap (viewFin natValue) (indicesOf v)) === [0..(natValue (length v) - 1)]
prop_imapConst :: Property
prop_imapConst = property $
do f <- forAll $ HG.element orderingEndomorphisms
SomeVector v <- forAll $ genSomeVector genOrdering
imap (const f) v === fmap f v
prop_ifoldMapConst :: Property
prop_ifoldMapConst = property $
do f <- forAll $ HG.element orderingToStringFuns
SomeVector v <- forAll $ genSomeVector genOrdering
ifoldMap (const f) v === foldMap f v
prop_imapConstIndicesOf :: Property
prop_imapConstIndicesOf = property $
do SomeVector v <- forAll $ genSomeVector genOrdering
imap const v === indicesOf v
prop_imapElemAt :: Property
prop_imapElemAt = property $
do SomeVector v <- forAll $ genSomeVector genOrdering
imap (\i _ -> viewFin (\x -> elemAt x v) i) v === v
prop_OrdEqVectorIndex :: Property
prop_OrdEqVectorIndex = property $
do i <- forAll $ genFin (knownNat @10)
j <- forAll $ genFin (knownNat @10)
(i == j) === (compare i j == EQ)
-- We use @Ordering@ just because it's simple
vecTests :: IO TestTree
vecTests = testGroup "Vector" <$> return
[ testPropertyNamed "reverse100" "prop_reverse100" prop_reverse100
, testPropertyNamed "reverseSingleton" "prop_reverseSingleton" prop_reverseSingleton
, testPropertyNamed "split-join" "prop_splitJoin" prop_splitJoin
-- @cons@ is the same for vectors or lists
, testPropertyNamed "cons" "prop_cons" prop_cons
-- @snoc@ is like appending to a list
, testPropertyNamed "snoc" "prop_snoc" prop_snoc
-- @snoc@ and @unsnoc@ are inverses
, testPropertyNamed "snoc/unsnoc" "prop_snocUnsnoc" prop_snocUnsnoc
-- @generate@ is like mapping a function over indices
, testPropertyNamed "generate" "prop_generate" prop_generate
-- @unfold@ works like @unfold@ on lists
, testPropertyNamed "unfold" "prop_unfold" prop_unfold
-- Converting to and from assignments preserves size and last element
, testPropertyNamed "to-from-assignment" "prop_toFromAssignment" prop_toFromAssignment
-- NOTE: We don't use hedgehog-classes here, because the way the types work
-- would require this to only tests vectors of some fixed size.
--
-- Also, for 'fmap-compose', hedgehog-classes only tests two fixed functions
-- over integers.
, testPropertyNamed "fmap-id" "prop_fmapId" prop_fmapId
, testPropertyNamed "fmap-compose" "prop_fmapCompose" prop_fmapCompose
, testPropertyNamed "iterateN-range" "prop_iterateNRange" prop_iterateNRange
, testPropertyNamed "indicesOf-range" "prop_indicesOfRange" prop_indicesOfRange
, testPropertyNamed "imap-const" "prop_imapConst" prop_imapConst
, testPropertyNamed "ifoldMap-const" "prop_ifoldMapConst" prop_ifoldMapConst
, testPropertyNamed "imap-const-indicesOf" "prop_imapConstIndicesOf" prop_imapConstIndicesOf
, testPropertyNamed "imap-elemAt" "prop_imapElemAt" prop_imapElemAt
, testPropertyNamed "Ord-Eq-VectorIndex" "prop_OrdEqVectorIndex" prop_OrdEqVectorIndex
#if __GLASGOW_HASKELL__ >= 806
-- Test a few different sizes since the types force each test to use a
-- specific size vector.
, testCase "Eq-Vector-laws-1" $
assertBool "Eq-Vector-laws-1" =<<
HC.lawsCheck (HC.eqLaws (genVectorKnownLength @1 genOrdering))
, testCase "Eq-Vector-laws-10" $
assertBool "Eq-Vector-laws-10" =<<
HC.lawsCheck (HC.eqLaws (genVectorKnownLength @10 genOrdering))
, testCase "Show-Vector-laws-1" $
assertBool "Show-Vector-laws-1" =<<
HC.lawsCheck (HC.showLaws (genVectorKnownLength @1 genOrdering))
, testCase "Show-Vector-laws-10" $
assertBool "Show-Vector-laws-10" =<<
HC.lawsCheck (HC.showLaws (genVectorKnownLength @10 genOrdering))
, testCase "Foldable-Vector-laws-1" $
assertBool "Foldable-Vector-laws-1" =<<
HC.lawsCheck (HC.foldableLaws (genVectorKnownLength @1))
, testCase "Foldable-Vector-laws-10" $
assertBool "Foldable-Vector-laws-10" =<<
HC.lawsCheck (HC.foldableLaws (genVectorKnownLength @10))
, testCase "Traversable-Vector-laws-1" $
assertBool "Traversable-Vector-laws-1" =<<
HC.lawsCheck (HC.traversableLaws (genVectorKnownLength @1))
, testCase "Traversable-Vector-laws-10" $
assertBool "Traversable-Vector-laws-10" =<<
HC.lawsCheck (HC.traversableLaws (genVectorKnownLength @10))
#endif
]
|