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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
|
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Main where
-- Testing modules
import Hedgehog
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import Test.Tasty
import Test.Tasty.Hedgehog
-- Modules under test
import qualified Data.BitVector.Sized as BV
import qualified Data.BitVector.Sized.Unsigned as BV
import qualified Data.BitVector.Sized.Signed as BV
-- Auxiliary modules
import Control.Monad.Random
import qualified Data.Bits as Bits
import qualified Data.ByteString as BS
import Data.Maybe (isJust, fromJust)
import Data.Parameterized.NatRepr
import Data.Parameterized.Some
import Data.Parameterized.Pair
import Data.String ( fromString )
import Data.Word
import Numeric.Natural
-- | This is a wrapper around 'testPropertyNamed' that somewhat addresses a
-- deprecation warning. Newer versions of the test library deprecated
-- 'testProperty' in favor of 'testPropertyNamed', which is intended to provide
-- better human-readable instructions to reproduce test failures.
--
-- However, doing that requires all properties to have a top-level definition
-- corresponding to them, which we do not here. We just re-use the string
-- description as the test name, which will produce incorrect instructions to
-- reproduce failures, but will provide sufficient context clues for a developer
-- to figure out which test failed.
--
-- The alternative is to refactor all of the tests into top-level properties to
-- use the API as intended.
testPropertyString :: String -> Property -> TestTree
testPropertyString desc = testPropertyNamed desc (fromString desc)
----------------------------------------
-- Utilities
forcePos :: (1 <= w => NatRepr w -> a)
-> NatRepr w -> a
forcePos f w = case isZeroOrGT1 w of
Left Refl -> error "Main.forcePos: encountered 0 nat"
Right LeqProof -> f w
----------------------------------------
-- Homomorphisms
un :: Show a
=> Gen (Some NatRepr)
-- ^ generator for width
-> (forall w . NatRepr w -> a -> BV.BV w)
-- ^ morphism
-> (forall w . NatRepr w -> Gen a)
-- ^ generator for arg
-> (forall w . NatRepr w -> a -> a)
-- ^ unary operator on domain
-> (forall w . NatRepr w -> BV.BV w -> BV.BV w)
-- ^ unary operator on codomain
-> Property
un genW p gen aOp bOp = property $ do
Some w <- forAll genW
a <- forAll (gen w)
p w (aOp w a) === bOp w (p w a)
bin :: Show a
=> Gen (Some NatRepr)
-- ^ generator for width
-> (forall w. NatRepr w -> a -> BV.BV w)
-- ^ morphism on domains
-> (forall w. NatRepr w -> Gen a)
-- ^ generator for first arg
-> (forall w. NatRepr w -> Gen a)
-- ^ generator for second arg
-> (forall w. NatRepr w -> a -> a -> a)
-- ^ binary operator on domain
-> (forall w. NatRepr w -> BV.BV w -> BV.BV w -> BV.BV w)
-- ^ binary operator on codomain
-> Property
bin genW p gen1 gen2 aOp bOp = property $ do
Some w <- forAll genW
a1 <- forAll (gen1 w)
a2 <- forAll (gen2 w)
-- compute f (a1 `aOp` a2)
let a1_a2 = aOp w a1 a2
let pa1_a2 = p w a1_a2
-- compute f (a1) `bOp` f (a2)
let pa1 = p w a1
let pa2 = p w a2
let pa1_pa2 = bOp w pa1 pa2
pa1_a2 === pa1_pa2
binPred :: Show a
=> Gen (Some NatRepr)
-- ^ generator for width
-> (forall w. NatRepr w -> a -> BV.BV w)
-- ^ morphism on domains
-> (forall w . NatRepr w -> Gen a)
-- ^ generator for first arg
-> (forall w . NatRepr w -> Gen a)
-- ^ generator for second arg
-> (forall w . NatRepr w -> a -> a -> Bool)
-- ^ binary predicate on domain
-> (forall w . NatRepr w -> BV.BV w -> BV.BV w -> Bool)
-- ^ binary predicate on codomain
-> Property
binPred genW p gen1 gen2 aPred bPred = property $ do
Some w <- forAll genW
a1 <- forAll (gen1 w)
a2 <- forAll (gen2 w)
let a1_a2 = aPred w a1 a2
let pa1 = p w a1
let pa2 = p w a2
let pa1_pa2 = bPred w pa1 pa2
a1_a2 === pa1_pa2
----------------------------------------
-- Ranges
anyWidth :: Gen (Some NatRepr)
anyWidth = mkNatRepr <$> (Gen.integral $ Range.linear 0 128)
byteWidth :: Gen (Some NatRepr)
byteWidth = mkNatRepr <$> (8*) <$> (Gen.integral $ Range.linear 0 16)
anyPosWidth :: Gen (Some NatRepr)
anyPosWidth = mkNatRepr <$> (Gen.integral $ Range.linear 1 128)
anyWidthGT1 :: Gen (Some NatRepr)
anyWidthGT1 = mkNatRepr <$> (Gen.integral $ Range.linear 2 128)
smallPosWidth :: Gen (Some NatRepr)
smallPosWidth = mkNatRepr <$> (Gen.integral $ Range.linear 1 4)
data NatReprLte w where
NatReprLte :: (i <= w) => NatRepr i -> NatReprLte w
deriving instance Show (NatReprLte w)
natReprLte :: NatRepr w -> Gen (NatReprLte w)
natReprLte w = do
n <- Gen.integral $ Range.linear 0 (natValue w)
Some i <- return $ mkNatRepr n
Just LeqProof <- return $ i `testLeq` w
return $ NatReprLte i
data NatReprLt w where
NatReprLt :: (i+1 <= w) => NatRepr i -> NatReprLt w
deriving instance Show (NatReprLt w)
natReprLt :: NatRepr w -> Gen (NatReprLt w)
natReprLt w = do
n <- Gen.integral $ Range.linear 0 (natValue w - 1)
Some i <- return $ mkNatRepr n
NatCaseLT LeqProof <- return $ i `testNatCases` w
return $ NatReprLt i
data NatReprPosLt w where
NatReprPosLt :: (1 <= i, i+1 <= w) => NatRepr i -> NatReprPosLt w
deriving instance Show (NatReprPosLt w)
natReprPosLt :: NatRepr w -> Gen (NatReprPosLt w)
natReprPosLt w = do
n <- Gen.integral $ Range.linear 1 (natValue w - 1)
Some i <- return $ mkNatRepr n
NatCaseLT LeqProof <- return $ i `testNatCases` w
Right LeqProof <- return $ isZeroOrGT1 i
return $ NatReprPosLt i
bytes :: Gen [Word8]
bytes = Gen.list (Range.linear 0 16) $ Gen.word8 Range.linearBounded
bits :: Gen [Bool]
bits = Gen.list (Range.linear 0 128) $ Gen.bool
unsigned :: NatRepr w -> Gen Integer
unsigned w = Gen.integral $ Range.linear 0 (maxUnsigned w)
unsignedPos :: NatRepr w -> Gen Integer
unsignedPos w = Gen.integral $ Range.linear 1 (maxUnsigned w)
largeUnsigned :: NatRepr w -> Gen Integer
largeUnsigned w = Gen.integral $ Range.linear 0 (maxUnsigned w')
where w' = incNat w
signed :: NatRepr w -> Gen Integer
signed w = case isZeroOrGT1 w of
Left Refl -> error "Main.signed: w = 0"
Right LeqProof -> Gen.integral $ Range.linearFrom 0 (minSigned w) (maxSigned w)
signedPos :: NatRepr w -> Gen Integer
signedPos w = case isZeroOrGT1 w of
Left Refl -> error "Main.posBounded: w = 0"
Right LeqProof -> Gen.integral $ Range.linear 1 (maxSigned w)
signedNeg :: NatRepr w -> Gen Integer
signedNeg w = case isZeroOrGT1 w of
Left Refl -> error "Main.posBounded: w = 0"
Right LeqProof -> Gen.integral $ Range.linearFrom (-1) (minSigned w) (-1)
largeSigned :: NatRepr w -> Gen Integer
largeSigned w = Gen.integral $ Range.linearFrom 0 (- maxUnsigned w') (maxUnsigned w')
where w' = incNat w
genPair :: Gen a -> Gen a -> Gen (a, a)
genPair gen gen' = do
a <- gen
a' <- gen'
return (a, a')
----------------------------------------
-- Tests
bitwiseHomTests :: TestTree
bitwiseHomTests = testGroup "bitwise homomorphisms tests"
[ testPropertyString "and" $ bin anyWidth BV.mkBV
largeUnsigned largeUnsigned
(const (Bits..&.)) (const BV.and)
, testPropertyString "or" $ bin anyWidth BV.mkBV
largeUnsigned largeUnsigned
(const (Bits..|.)) (const BV.or)
, testPropertyString "xor" $ bin anyWidth BV.mkBV
largeUnsigned largeUnsigned
(const Bits.xor) (const BV.xor)
, testPropertyString "complement" $ un anyWidth BV.mkBV
largeUnsigned
(const Bits.complement) BV.complement
]
arithHomTests :: TestTree
arithHomTests = testGroup "arithmetic homomorphisms tests"
[ testPropertyString "add" $ bin anyWidth BV.mkBV
largeSigned largeSigned
(const (+)) BV.add
, testPropertyString "sub" $ bin anyWidth BV.mkBV
largeSigned largeSigned
(const (-)) BV.sub
, testPropertyString "mul" $ bin anyWidth BV.mkBV
largeSigned largeSigned
(const (*)) BV.mul
, testPropertyString "uquot" $ bin anyPosWidth BV.mkBV
unsigned unsignedPos
(const quot) (const BV.uquot)
, testPropertyString "urem" $ bin anyPosWidth BV.mkBV
unsigned unsignedPos
(const rem) (const BV.urem)
, testPropertyString "squot-pos-denom" $ bin anyWidthGT1 BV.mkBV
signed signedPos
(const quot) (forcePos BV.squot)
, testPropertyString "squot-neg-denom" $ bin anyWidthGT1 BV.mkBV
signed signedNeg
(const quot) (forcePos BV.squot)
, testPropertyString "srem-pos-denom" $ bin anyWidthGT1 BV.mkBV
signed signedPos
(const rem) (forcePos BV.srem)
, testPropertyString "srem-neg-denom" $ bin anyWidthGT1 BV.mkBV
signed signedNeg
(const rem) (forcePos BV.srem)
, testPropertyString "sdiv-pos-denom" $ bin anyWidthGT1 BV.mkBV
signed signedPos
(const div) (forcePos BV.sdiv)
, testPropertyString "sdiv-neg-denom" $ bin anyWidthGT1 BV.mkBV
signed signedNeg
(const div) (forcePos BV.sdiv)
, testPropertyString "smod-pos-denom" $ bin anyWidthGT1 BV.mkBV
signed signedPos
(const mod) (forcePos BV.smod)
, testPropertyString "smod-neg-denom" $ bin anyWidthGT1 BV.mkBV
signed signedNeg
(const mod) (forcePos BV.smod)
, testPropertyString "abs" $ un anyPosWidth BV.mkBV
signed
(const abs) (forcePos BV.abs)
, testPropertyString "negate" $ un anyPosWidth BV.mkBV
largeSigned
(const negate) BV.negate
, testPropertyString "signBit" $ un anyPosWidth BV.mkBV
signed
(\_ a -> if a < 0 then 1 else 0) (forcePos BV.signBit)
, testPropertyString "signum" $ un anyPosWidth BV.mkBV
signed
(\_ a -> signum a) (forcePos BV.signum)
, testPropertyString "slt" $ binPred anyPosWidth BV.mkBV
signed signed
(const (<)) (forcePos BV.slt)
, testPropertyString "sle" $ binPred anyPosWidth BV.mkBV
signed signed
(const (<=)) (forcePos BV.sle)
, testPropertyString "ult" $ binPred anyWidth BV.mkBV
unsigned unsigned
(const (<)) (const BV.ult)
, testPropertyString "ule" $ binPred anyWidth BV.mkBV
unsigned unsigned
(const (<=)) (const BV.ule)
, testPropertyString "umin" $ bin anyWidth BV.mkBV
unsigned unsigned
(const min) (const BV.umin)
, testPropertyString "umax" $ bin anyWidth BV.mkBV
unsigned unsigned
(const max) (const BV.umax)
, testPropertyString "smin" $ bin anyPosWidth BV.mkBV
signed signed
(const min) (forcePos BV.smin)
, testPropertyString "smax" $ bin anyPosWidth BV.mkBV
signed signed
(const max) (forcePos BV.smax)
]
serdeTest :: Gen (Some NatRepr)
-> (forall w . NatRepr w -> BV.BV w -> Maybe a)
-> (a -> Pair NatRepr BV.BV)
-> Property
serdeTest wGen ser de = property $ do
Some w <- forAll wGen
i <- forAll (largeUnsigned w)
let bv = BV.mkBV w i
let a = ser w bv
assert (isJust a)
Pair w' bv' <- return $ de $ fromJust a
assert (isJust (w' `testEquality` w))
Just Refl <- return $ w' `testEquality` w
bv' === bv
deserTest :: (Show a, Eq a)
=> Gen a
-> (a -> Int)
-> (a -> Pair NatRepr BV.BV)
-> (forall w . NatRepr w -> BV.BV w -> Maybe a)
-> Property
deserTest genA lenA de ser = property $ do
a <- forAll genA
Some w' <- return $ mkNatRepr (fromIntegral (lenA a))
Pair w bv <- return $ de $ a
assert (isJust (w' `testEquality` w))
Just Refl <- return $ w' `testEquality` w
ser w bv === Just a
serdeTests :: TestTree
serdeTests = testGroup "serialization/deseriallization tests"
[ testPropertyString "bitsBE" $
serdeTest anyWidth (\w bv -> Just (BV.asBitsBE w bv)) BV.bitsBE
, testPropertyString "bitsLE" $
serdeTest anyWidth (\w bv -> Just (BV.asBitsLE w bv)) BV.bitsLE
, testPropertyString "bytesBE" $
serdeTest byteWidth BV.asBytesBE BV.bytesBE
, testPropertyString "bytesLE" $
serdeTest byteWidth BV.asBytesLE BV.bytesLE
, testPropertyString "bytestringBE" $
serdeTest byteWidth BV.asBytestringBE BV.bytestringBE
, testPropertyString "bytestringLE" $
serdeTest byteWidth BV.asBytestringLE BV.bytestringLE
]
deserTests :: TestTree
deserTests = testGroup "deserialization/serialization tests"
[ testPropertyString "asBitsBE" $
deserTest bits length BV.bitsBE (\w bv -> Just (BV.asBitsBE w bv))
, testPropertyString "asBitsLE" $
deserTest bits length BV.bitsLE (\w bv -> Just (BV.asBitsLE w bv))
, testPropertyString "asBytesBE" $
deserTest bytes ((*8) . length) BV.bytesBE BV.asBytesBE
, testPropertyString "asBytesLE" $
deserTest bytes ((*8) . length) BV.bytesLE BV.asBytesLE
, testPropertyString "asBytesBE" $
deserTest (BS.pack <$> bytes) ((*8) . BS.length) BV.bytestringBE BV.asBytestringBE
, testPropertyString "asBytesLE" $
deserTest (BS.pack <$> bytes) ((*8) . BS.length) BV.bytestringLE BV.asBytestringLE
]
checkBounds :: MonadTest m => Integer -> NatRepr w -> m ()
checkBounds x w = do
diff 0 (<=) x
diff x (<=) (2 ^ natValue w)
wfCtor :: Gen (Some NatRepr)
-- ^ generator for width
-> (forall w . NatRepr w -> Integer -> Maybe (BV.BV w))
-- ^ constructor
-> Property
wfCtor genW ctor = property $ do
Some w <- forAll genW
x <- forAll (largeSigned w)
case ctor w x of
Just (BV.BV x') -> checkBounds x' w
Nothing -> return ()
wfCtor' :: NatRepr w
-- ^ fixed width of constructor
-> (Integer -> BV.BV w)
-- ^ embedding of integer into constructor arg
-> Property
wfCtor' w ctor = property $ do
x <- forAll (largeSigned w)
let BV.BV x' = ctor x
checkBounds x' w
wfUnary :: Gen (Some NatRepr)
-- ^ generator for width
-> (forall w . NatRepr w -> BV.BV w -> BV.BV w)
-- ^ unary operator
-> Property
wfUnary genW op = property $ do
Some w <- forAll genW
bv <- BV.mkBV w <$> forAll (unsigned w)
let BV.BV x' = op w bv
checkBounds x' w
wfUnaryMaybe :: Gen (Some NatRepr)
-- ^ generator for width
-> (forall w . NatRepr w -> BV.BV w -> Maybe (BV.BV w))
-- ^ unary operator
-> Property
wfUnaryMaybe genW op = property $ do
Some w <- forAll genW
bv <- BV.mkBV w <$> forAll (unsigned w)
case op w bv of
Just (BV.BV x') -> checkBounds x' w
Nothing -> return ()
wfBinary :: Gen (Some NatRepr)
-- ^ generator for width
-> (forall w . NatRepr w -> BV.BV w -> BV.BV w -> BV.BV w)
-- ^ binary operator
-> Property
wfBinary genW op = property $ do
Some w <- forAll genW
bv1 <- BV.mkBV w <$> forAll (unsigned w)
bv2 <- BV.mkBV w <$> forAll (unsigned w)
let BV.BV x' = op w bv1 bv2
checkBounds x' w
wfBinaryDiv :: Gen (Some NatRepr)
-- ^ generator for width
-> (forall w . NatRepr w -> BV.BV w -> BV.BV w -> BV.BV w)
-- ^ binary division-like operator
-> Property
wfBinaryDiv genW op = property $ do
Some w <- forAll genW
bv1 <- BV.mkBV w <$> forAll (unsigned w)
bv2 <- BV.mkBV w <$> forAll (unsignedPos w)
let BV.BV x' = op w bv1 bv2
checkBounds x' w
wfBinaryN :: Gen (Some NatRepr)
-- ^ generator for width
-> (forall w . NatRepr w -> BV.BV w -> Natural -> BV.BV w)
-- ^ binary operator with Natural arg
-> Property
wfBinaryN genW op = property $ do
Some w <- forAll genW
bv <- BV.mkBV w <$> forAll (unsigned w)
n <- fromInteger <$> forAll (largeUnsigned w)
let BV.BV x' = op w bv n
checkBounds x' w
wfBit :: Gen (Some NatRepr)
-- ^ generator for width
-> (forall w ix . ix+1 <= w => NatRepr w -> NatRepr ix -> BV.BV w -> BV.BV w)
-- ^ bit twiddling function
-> Property
wfBit genW f = property $ do
Some w <- forAll genW
bv <- BV.mkBV w <$> forAll (unsigned w)
NatReprLt ix <- forAll (natReprLt w)
let BV.BV x = f w ix bv
checkBounds x w
wfBitN :: Gen (Some NatRepr)
-- ^ generator for width
-> (forall w . NatRepr w -> Natural -> BV.BV w -> BV.BV w)
-- ^ bit twiddling function
-> Property
wfBitN genW f = property $ do
Some w <- forAll genW
bv <- BV.mkBV w <$> forAll (unsigned w)
n <- fromInteger <$> forAll (largeUnsigned w)
let BV.BV x = f w n bv
checkBounds x w
wellFormedTests :: TestTree
wellFormedTests = testGroup "well-formedness tests"
[ testPropertyString "mkBV" $ wfCtor anyWidth (fmap Just . BV.mkBV)
, testPropertyString "mkBVUnsigned" $ wfCtor anyWidth BV.mkBVUnsigned
, testPropertyString "mkBVSigned" $ wfCtor anyPosWidth (forcePos BV.mkBVSigned)
, testPropertyString "signedClamp" $ wfCtor anyPosWidth (fmap Just . forcePos BV.signedClamp)
, testPropertyString "minUnsigned" $ wfCtor anyWidth (\w _ -> Just (BV.minUnsigned w))
, testPropertyString "maxUnsigned" $ wfCtor anyWidth (\w _ -> Just (BV.maxUnsigned w))
, testPropertyString "minSigned" $ wfCtor anyPosWidth (\w _ -> Just (forcePos BV.minSigned w))
, testPropertyString "maxSigned" $ wfCtor anyPosWidth (\w _ -> Just (forcePos BV.maxSigned w))
, testPropertyString "bool" $ wfCtor' knownNat (BV.bool . odd)
, testPropertyString "word8" $ wfCtor' knownNat (BV.word8 . fromInteger)
, testPropertyString "word16" $ wfCtor' knownNat (BV.word16 . fromInteger)
, testPropertyString "word32" $ wfCtor' knownNat (BV.word32 . fromInteger)
, testPropertyString "word64" $ wfCtor' knownNat (BV.word64 . fromInteger)
, testPropertyString "int8" $ wfCtor' knownNat (BV.int8 . fromInteger)
, testPropertyString "int16" $ wfCtor' knownNat (BV.int16 . fromInteger)
, testPropertyString "int32" $ wfCtor' knownNat (BV.int32 . fromInteger)
, testPropertyString "int64" $ wfCtor' knownNat (BV.int64 . fromInteger)
, testPropertyString "and" $ wfBinary anyWidth (const BV.and)
, testPropertyString "or" $ wfBinary anyWidth (const BV.or)
, testPropertyString "xor" $ wfBinary anyWidth (const BV.xor)
, testPropertyString "complement" $ wfUnary anyWidth BV.complement
, testPropertyString "shl" $ wfBinaryN anyWidth BV.shl
, testPropertyString "ashr" $ wfBinaryN anyPosWidth (forcePos BV.ashr)
, testPropertyString "lshr" $ wfBinaryN anyWidth BV.lshr
, testPropertyString "rotateL" $ wfBinaryN anyWidth BV.rotateL
, testPropertyString "rotateR" $ wfBinaryN anyWidth BV.rotateR
, testPropertyString "bit" $ property $ do
Some w <- forAll anyPosWidth
NatReprLt i <- forAll (natReprLt w)
let BV.BV x = BV.bit w i
checkBounds x w
, testPropertyString "bit'" $ property $ do
Some w <- forAll anyPosWidth
n <- forAll $ Gen.integral $ Range.linear 0 (2 * natValue w)
let BV.BV x = BV.bit' w n
checkBounds x w
, testPropertyString "setBit" $ wfBit anyPosWidth (const BV.setBit)
, testPropertyString "setBit'" $ wfBitN anyPosWidth BV.setBit'
, testPropertyString "clearBit" $ wfBit anyPosWidth BV.clearBit
, testPropertyString "clearBit'" $ wfBitN anyPosWidth BV.clearBit'
, testPropertyString "complementBit" $ wfBit anyPosWidth (const BV.complementBit)
, testPropertyString "complementBit'" $ wfBitN anyPosWidth BV.complementBit'
, testPropertyString "popCount" $ wfUnary anyWidth (const BV.popCount)
, testPropertyString "ctz" $ wfUnary anyWidth BV.ctz
, testPropertyString "clz" $ wfUnary anyWidth BV.clz
, testPropertyString "truncBits" $ property $ do
Some w <- forAll anyWidth
bv <- BV.mkBV w <$> forAll (unsigned w)
n <- forAll $ Gen.integral $ Range.linear 0 (2 * natValue w)
let BV.BV x = BV.truncBits n bv
checkBounds x w
, testPropertyString "add" $ wfBinary anyWidth BV.add
, testPropertyString "sub" $ wfBinary anyWidth BV.sub
, testPropertyString "mul" $ wfBinary anyWidth BV.mul
, testPropertyString "uquot" $ wfBinaryDiv anyPosWidth (const BV.uquot)
, testPropertyString "urem" $ wfBinaryDiv anyPosWidth (const BV.urem)
, testPropertyString "squot" $ wfBinaryDiv anyPosWidth (forcePos BV.squot)
, testPropertyString "srem" $ wfBinaryDiv anyPosWidth (forcePos BV.srem)
, testPropertyString "sdiv" $ wfBinaryDiv anyPosWidth (forcePos BV.sdiv)
, testPropertyString "smod" $ wfBinaryDiv anyPosWidth (forcePos BV.smod)
, testPropertyString "abs" $ wfUnary anyPosWidth (forcePos BV.abs)
, testPropertyString "negate" $ wfUnary anyWidth BV.negate
, testPropertyString "signBit" $ wfUnary anyPosWidth (forcePos BV.signBit)
, testPropertyString "signum" $ wfUnary anyPosWidth (forcePos BV.signum)
, testPropertyString "umin" $ wfBinary anyWidth (const BV.umin)
, testPropertyString "umax" $ wfBinary anyWidth (const BV.umax)
, testPropertyString "smin" $ wfBinary anyPosWidth (forcePos BV.smin)
, testPropertyString "smax" $ wfBinary anyPosWidth (forcePos BV.smax)
, testPropertyString "concat" $ property $ do
Some w <- forAll anyWidth
Some w' <- forAll anyWidth
bv <- BV.mkBV w <$> forAll (unsigned w)
bv' <- BV.mkBV w' <$> forAll (unsigned w')
let BV.BV x = BV.concat w w' bv bv'
checkBounds x (w `addNat` w')
, testPropertyString "select" $ property $ do
Some w <- forAll anyWidth
bv <- BV.mkBV w <$> forAll (unsigned w)
NatReprLte ix <- forAll (natReprLte w)
Just LeqProof <- return $ ix `testLeq` w
NatReprLte w' <- forAll (natReprLte (w `subNat` ix))
Just LeqProof <- return $ (ix `addNat` w') `testLeq` w
let BV.BV x = BV.select ix w' bv
checkBounds x w'
, testPropertyString "select'" $ property $ do
Some w <- forAll anyWidth
Some w' <- forAll anyWidth
bv <- BV.mkBV w <$> forAll (unsigned w)
n <- forAll $ Gen.integral $ Range.linear 0 (2 * natValue w)
let BV.BV x = BV.select' n w' bv
checkBounds x w'
, testPropertyString "zext" $ property $ do
Some w' <- forAll anyPosWidth
NatReprLt w <- forAll (natReprLt w')
bv <- BV.mkBV w <$> forAll (unsigned w)
let BV.BV x = BV.zext w' bv
checkBounds x w'
, testPropertyString "sext" $ property $ do
Some w' <- forAll anyWidthGT1
NatReprPosLt w <- forAll (natReprPosLt w')
bv <- BV.mkBV w <$> forAll (unsigned w)
let BV.BV x = BV.sext w w' bv
checkBounds x w'
, testPropertyString "trunc" $ property $ do
Some w <- forAll anyPosWidth
NatReprLt w' <- forAll (natReprLt w)
bv <- BV.mkBV w <$> forAll (unsigned w)
let BV.BV x = BV.trunc w' bv
checkBounds x w'
, testPropertyString "trunc'" $ property $ do
Some w <- forAll anyWidth
Some w' <- forAll anyWidth
bv <- BV.mkBV w <$> forAll (unsigned w)
let BV.BV x = BV.trunc' w' bv
checkBounds x w'
, testPropertyString "zresize" $ property $ do
Some w <- forAll anyWidth
Some w' <- forAll anyWidth
bv <- BV.mkBV w <$> forAll (unsigned w)
let BV.BV x = BV.zresize w' bv
checkBounds x w'
, testPropertyString "sresize" $ property $ do
Some w <- forAll anyPosWidth
Just LeqProof <- return $ isPosNat w
Some w' <- forAll anyWidth
bv <- BV.mkBV w <$> forAll (unsigned w)
let BV.BV x = BV.sresize w w' bv
checkBounds x w'
, testPropertyString "mulWide" $ property $ do
Some w <- forAll anyWidth
Some w' <- forAll anyWidth
bv <- BV.mkBV w <$> forAll (unsigned w)
bv' <- BV.mkBV w' <$> forAll (unsigned w')
let BV.BV x = BV.mulWide w w' bv bv'
checkBounds x (w `addNat` w')
, testPropertyString "succUnsigned" $ wfUnaryMaybe anyWidth BV.succUnsigned
, testPropertyString "succSigned" $ wfUnaryMaybe anyPosWidth (forcePos BV.succUnsigned)
, testPropertyString "predUnsigned" $ wfUnaryMaybe anyWidth BV.predUnsigned
, testPropertyString "predSigned" $ wfUnaryMaybe anyPosWidth (forcePos BV.predUnsigned)
]
testRandomR :: (Ord (f w), Random (f w), Show (f w), Show a)
=> NatRepr w
-> (forall w' . NatRepr w' -> a -> f w')
-> (NatRepr w -> Gen a)
-> Property
testRandomR w mk gen = property $ do
x <- mk w <$> forAll (gen w)
y <- mk w <$> forAll (gen w)
let l = min x y
h = max x y
rand <- liftIO $ getRandomR (l, h)
rand' <- liftIO $ getRandomR (h, l)
diff l (<=) rand
diff rand (<=) h
diff l (<=) rand'
diff rand' (<=) h
randomTests :: TestTree
randomTests = testGroup "tests for random generation"
[ testPropertyString "random unsigned well-formed" $ property $ do
BV.UnsignedBV (BV.BV x) :: BV.UnsignedBV 32 <- liftIO $ getRandom
checkBounds x (knownNat @32)
, testPropertyString "random signed well-formed" $ property $ do
BV.SignedBV (BV.BV x) :: BV.SignedBV 32 <- liftIO $ getRandom
checkBounds x (knownNat @32)
, testPropertyString "randomR unsigned well-formed and in bounds" $
testRandomR (knownNat @32) BV.mkUnsignedBV unsigned
, testPropertyString "randomR signed well-formed and in bounds" $
testRandomR (knownNat @32) BV.mkSignedBV unsigned
]
tests :: TestTree
tests = testGroup "bv-sized tests"
[ arithHomTests
, bitwiseHomTests
, serdeTests
, deserTests
, wellFormedTests
, randomTests
]
main :: IO ()
main = defaultMain tests
|