<commit_before>{-|

This module contains some generic tests cases for CryptoStore
instances. Whenever a new instance of CryptoStore is defined, consider
using them

-}

module Raaz.Test.CryptoStore
       ( testStoreLoad
       ) where

import Data.Typeable
import Foreign.Marshal.Alloc
import Foreign.Storable
import Test.Framework(Test)
import Test.Framework.Providers.QuickCheck2(testProperty)
import Test.QuickCheck(Property, Arbitrary)
import Test.QuickCheck.Monadic(run, assert, monadicIO)

import Raaz.Types( cryptoAlignment, CryptoStore(..))


-- | This is where the actual store/load is performed.
storeLoad :: (CryptoStore a, Eq a) => a -> IO Bool
storeLoad a = allocaBytesAligned (sizeOf a) cryptoAlignment runStoreLoad
  where runStoreLoad ptr = do store ptr a
                              y <- load ptr
                              return $ y == a

-- | This is the property generator. The first value is an unused
-- value and is given to satisfy the typechecker.
prop_StoreLoad :: ( CryptoStore a
                  , Eq a
                  , Show a
                  )
               => a  -- ^ Dummy argument not used
               -> a  -- ^ load and storing is done on this.
               -> Property
prop_StoreLoad _ a = monadicIO $ do y <- run $ storeLoad a
                                    assert y

-- | This test checks whether storing followed by loading gives the
-- same value for your instance. The typeable instance is used print
-- the type when running the test and for nothing else. A typical use
-- would look something like.
--
-- > data Foo = ... deriving (Typeable, Eq, Show)
-- > instance CryptoStore Foo where
-- >      ...
-- > instance Aribitrary Foo where
-- >      ...
-- > main = defaultMain [ testStoreLoad (undefined :: Foo) ]
--
--
testStoreLoad :: ( CryptoStore a
                 , Eq a
                 , Show a
                 , Arbitrary a
                 , Typeable a
                 )
              => a    -- ^ dummy argument (not used)
              -> Test

testStoreLoad a = testProperty ("Store/Load " ++ aType)
                               $ prop_StoreLoad a
     where aType = show $ typeOf a
<commit_msg>rearranged the type name in test description<commit_after>{-|

This module contains some generic tests cases for CryptoStore
instances. Whenever a new instance of CryptoStore is defined, consider
using them

-}

module Raaz.Test.CryptoStore
       ( testStoreLoad
       ) where

import Data.Typeable
import Foreign.Marshal.Alloc
import Foreign.Storable
import Test.Framework(Test)
import Test.Framework.Providers.QuickCheck2(testProperty)
import Test.QuickCheck(Property, Arbitrary)
import Test.QuickCheck.Monadic(run, assert, monadicIO)

import Raaz.Types( cryptoAlignment, CryptoStore(..))


-- | This is where the actual store/load is performed.
storeLoad :: (CryptoStore a, Eq a) => a -> IO Bool
storeLoad a = allocaBytesAligned (sizeOf a) cryptoAlignment runStoreLoad
  where runStoreLoad ptr = do store ptr a
                              y <- load ptr
                              return $ y == a

-- | This is the property generator. The first value is an unused
-- value and is given to satisfy the typechecker.
prop_StoreLoad :: ( CryptoStore a
                  , Eq a
                  , Show a
                  )
               => a  -- ^ Dummy argument not used
               -> a  -- ^ load and storing is done on this.
               -> Property
prop_StoreLoad _ a = monadicIO $ do y <- run $ storeLoad a
                                    assert y

-- | This test checks whether storing followed by loading gives the
-- same value for your instance. The typeable instance is used print
-- the type when running the test and for nothing else. A typical use
-- would look something like.
--
-- > data Foo = ... deriving (Typeable, Eq, Show)
-- > instance CryptoStore Foo where
-- >      ...
-- > instance Aribitrary Foo where
-- >      ...
-- > main = defaultMain [ testStoreLoad (undefined :: Foo) ]
--
--
testStoreLoad :: ( CryptoStore a
                 , Eq a
                 , Show a
                 , Arbitrary a
                 , Typeable a
                 )
              => a    -- ^ dummy argument (not used)
              -> Test

testStoreLoad a = testProperty (aType ++ ": Store/Load ")
                               $ prop_StoreLoad a
     where aType = show $ typeOf a

{'fraction_non_alphanumeric': 0.08369441277080958, 'fraction_numerical': 0.00045610034207525655, 'mean_word_length': 1.8591916558018253, 'pattern_counts': {'":': 1, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1201155', 'n_tokens_mistral': 1165, 'n_tokens_neox': 1124, 'n_words': 444}