<commit_before>-- | Specification of Pos.Types.Slotting.

module Test.Pos.Types.SlottingSpec
       ( spec
       ) where

import           Test.Hspec            (Spec, describe)
import           Test.Hspec.QuickCheck (prop)
import           Test.QuickCheck       (Property, (===))
import           Universum

import           Pos.Arbitrary.Core    ()
import           Pos.Types             (SlotId, flattenSlotId, unflattenSlotId)

spec :: Spec
spec = describe "SlotId" $ do
    describe "Ord" $ do
        prop "is consistent with flatten/unflatten"
            flattenOrdConsistency

    describe "flattening" $ do
        prop "unflattening after flattening returns original SlotId"
            flattenThenUnflatten

flattenOrdConsistency :: SlotId -> SlotId -> Property
flattenOrdConsistency a b = a `compare` b === flattenSlotId a `compare` flattenSlotId b

flattenThenUnflatten :: SlotId -> Property
flattenThenUnflatten si = si === unflattenSlotId (flattenSlotId si)<commit_msg>[CSL-1396] Add tests to 'instance Enum EpochOrSLot'<commit_after>-- | Specification of Pos.Types.Slotting.

module Test.Pos.Types.SlottingSpec
       ( spec
       ) where

import           Universum

import           Test.Hspec            (Expectation, Spec, anyErrorCall, describe)
import           Test.Hspec.QuickCheck (prop)
import           Test.QuickCheck       (NonNegative (..), Positive (..), Property, (===),
                                        (==>))

import           Pos.Arbitrary.Core    (EoSToIntOverflow (..))
import           Pos.Types             (EpochIndex (..), EpochOrSlot (..), SlotId,
                                        flattenSlotId, unflattenSlotId)
import           Test.Pos.Util         (shouldThrowException, (.=.))

spec :: Spec
spec = describe "Slotting" $ do
    describe "SlotId" $ do
        describe "Ord" $ do
            prop "is consistent with flatten/unflatten"
                flattenOrdConsistency

        describe "flattening" $ do
            prop "unflattening after flattening returns original SlotId"
                flattenThenUnflatten

    describe "EpochOrSlot" $ do
        prop "succ . pred = id" predThenSucc
        prop "Using 'pred' with 'minBound :: EpochOrSlot' triggers an exception"
            predToMinBound
        prop "pred . succ = id" succThenPred
        prop "Using 'succ' with 'maxBound :: EpochOrSlot' triggers an exception"
            succToMaxBound
        prop "from . toEnum = id @Int" toFromEnum
        prop "toEnum . fromEnum = id @EpochOrSlot" fromToEnum
        prop "calling 'fromEnum' with a result greater than 'maxBound :: Int' will raise \
              \ an exception" fromEnumOverflow
        prop "calling 'toEnum' with a negative number will raise an exception"
            toEnumNegative

flattenOrdConsistency :: SlotId -> SlotId -> Property
flattenOrdConsistency a b = a `compare` b === flattenSlotId a `compare` flattenSlotId b

flattenThenUnflatten :: SlotId -> Property
flattenThenUnflatten si = si === unflattenSlotId (flattenSlotId si)

predThenSucc :: EpochOrSlot -> Property
predThenSucc eos = eos > minBound ==> succ (pred eos) === eos

predToMinBound :: Expectation
predToMinBound =
    shouldThrowException pred anyErrorCall (EpochOrSlot $ Left (EpochIndex 0))

succThenPred :: EpochOrSlot -> Property
succThenPred eos = eos < maxBound ==> pred (succ eos) === eos

succToMaxBound :: Expectation
succToMaxBound = shouldThrowException succ anyErrorCall (maxBound :: EpochOrSlot)

toFromEnum :: NonNegative Int -> Property
toFromEnum (getNonNegative -> int) = fromEnum (toEnum @EpochOrSlot int) === int

fromToEnum :: EpochOrSlot -> Property
fromToEnum = toEnum . fromEnum .=. identity

fromEnumOverflow :: EoSToIntOverflow-> Expectation
fromEnumOverflow (getInt -> eos) =
    shouldThrowException (fromEnum @EpochOrSlot) anyErrorCall eos

toEnumNegative :: Positive Int -> Expectation
toEnumNegative (negate . getPositive -> int) =
    shouldThrowException (toEnum @EpochOrSlot) anyErrorCall int
{'fraction_non_alphanumeric': 0.08888332498748122, 'fraction_numerical': 0.0012518778167250877, 'mean_word_length': 2.692236598890943, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6014626', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1136, 'n_words': 327}