Skip to content

Commit

Permalink
Merge pull request #4240 from NorfairKing/unit-to-property
Browse files Browse the repository at this point in the history
Turned unit tests into property tests
  • Loading branch information
snoyberg authored Aug 19, 2018
2 parents a6ece48 + 7b57b08 commit 103d324
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions subs/pantry/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,4 @@ tests:
- hspec
- exceptions
- hedgehog
- QuickCheck
31 changes: 17 additions & 14 deletions subs/pantry/test/Pantry/Internal/StaticBytesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ module Pantry.Internal.StaticBytesSpec (spec) where

import RIO
import Pantry.Internal.StaticBytes
import Control.Monad (replicateM)
import qualified Data.ByteString as B
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Primitive as VP
import qualified Data.Vector.Storable as VS
import Test.Hspec
import Test.Hspec.QuickCheck
import Test.QuickCheck
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE

Expand All @@ -23,41 +25,42 @@ spec = do

tests :: (Eq dbytes, Show dbytes, DynamicBytes dbytes) => ([Word8] -> dbytes) -> Spec
tests pack = do
it "disallows 4 bytes" $ do
toStaticExact (pack [1..4]) `shouldBe` (Left NotEnoughBytes :: Either StaticBytesException Bytes8)
it "toStaticExact matches ByteString" $ do
let octets = [1..8]
it "disallows 4 bytes" $ property $ \(w1,w2,w3,w4) ->
toStaticExact (pack [w1,w2,w3,w4]) `shouldBe` (Left NotEnoughBytes :: Either StaticBytesException Bytes8)
it "toStaticExact matches ByteString" $ property $ \(w1,w2,w3,w4) -> property $ \(w5,w6,w7,w8) -> do
let octets = [w1,w2,w3,w4,w5,w6,w7,w8]
(expected :: Bytes8) = either impureThrow id $ toStaticExact (B.pack octets)
actual = either impureThrow id $ toStaticExact (pack octets)
actual `shouldBe` expected

it "fromStatic round trips" $ do
let octets = [1..8]
it "fromStatic round trips" $ property $ \(w1,w2,w3,w4) -> property $ \(w5,w6,w7,w8) -> do
let octets = [w1,w2,w3,w4,w5,w6,w7,w8]
v1 = pack octets
(b8 :: Bytes8) = either impureThrow id $ toStaticExact v1
v2 = fromStatic b8
v2 `shouldBe` v1

it "allows 8 bytes" $ do
let bs = pack [1..8]
it "allows 8 bytes" $ property $ \(w1,w2,w3,w4) -> property $ \(w5,w6,w7,w8) -> do
let bs = pack [w1,w2,w3,w4,w5,w6,w7,w8]
case toStaticExact bs of
Left e -> throwIO e
Right b8 -> fromStatic (b8 :: Bytes8) `shouldBe` bs
toStaticExact bs `shouldBe` (Left NotEnoughBytes :: Either StaticBytesException Bytes16)
it "padding is the same as trailing nulls" $ do
let bs1 = pack $ [1..4] ++ replicate 4 0
bs2 = pack [1..4]
it "padding is the same as trailing nulls" $ property $ \(w1,w2,w3,w4) -> do
let ws = [w1,w2,w3,w4]
bs1 = pack $ ws ++ replicate 4 0
bs2 = pack ws
Right (toStaticPadTruncate bs2 :: Bytes8) `shouldBe` toStaticExact bs1

prop "handles bytes16" $ \octets -> do
let bs = pack $ take 16 octets
(b16 :: Bytes16) = either impureThrow id $ toStaticPad bs
fromStatic b16 `shouldBe` pack (take 16 (octets ++ replicate 16 0))

it "spot check bytes16" $ do
let bs = pack $ replicate 16 0
it "spot check bytes16" $ forAll (replicateM 16 arbitrary) $ \ws -> do
let bs = pack ws
(b16 :: Bytes16) = either impureThrow id $ toStaticPad bs
fromStatic b16 `shouldBe` pack (replicate 16 0)
fromStatic b16 `shouldBe` pack ws

prop "handles bytes32" $ \octets -> do
let bs = pack $ take 32 octets
Expand Down

0 comments on commit 103d324

Please sign in to comment.