Main.hs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. {-# LANGUAGE AllowAmbiguousTypes #-}
  2. {-# LANGUAGE DataKinds #-}
  3. {-# LANGUAGE DeriveAnyClass #-}
  4. {-# LANGUAGE DeriveGeneric #-}
  5. {-# LANGUAGE DerivingVia #-}
  6. {-# LANGUAGE KindSignatures #-}
  7. {-# LANGUAGE OverloadedStrings #-}
  8. {-# LANGUAGE ScopedTypeVariables #-}
  9. {-# LANGUAGE TypeApplications #-}
  10. {-
  11. - Copyright (C) 2019-2020 Koz Ross <koz.ross@retro-freedom.nz>
  12. -
  13. - This program is free software: you can redistribute it and/or modify
  14. - it under the terms of the GNU General Public License as published by
  15. - the Free Software Foundation, either version 3 of the License, or
  16. - (at your option) any later version.
  17. -
  18. - This program is distributed in the hope that it will be useful,
  19. - but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. - GNU General Public License for more details.
  22. -
  23. - You should have received a copy of the GNU General Public License
  24. - along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. -}
  26. module Main where
  27. import Control.DeepSeq (NFData)
  28. import Data.Binary (Binary)
  29. import Data.Finitary (Finitary (..))
  30. import Data.Finitary.Finiteness (Finiteness (..))
  31. import qualified Data.Finitary.PackBits as Safe
  32. import qualified Data.Finitary.PackBits.Unsafe as Unsafe
  33. import Data.Finitary.PackBytes (PackBytes)
  34. import qualified Data.Finitary.PackBytes as PackBytes
  35. import Data.Finitary.PackInto (PackInto)
  36. import Data.Finitary.PackWords (PackWords)
  37. import qualified Data.Finitary.PackWords as PackWords
  38. import Data.Finite (Finite)
  39. import Data.Hashable (Hashable (..))
  40. import Data.Kind (Type)
  41. import Data.Proxy (Proxy (..))
  42. import Data.Word (Word16, Word64, Word8)
  43. import Foreign.Storable (Storable)
  44. import GHC.Generics (Generic)
  45. import GHC.TypeNats
  46. import Hedgehog
  47. import Hedgehog.Classes
  48. import qualified Hedgehog.Gen as G
  49. import qualified Hedgehog.Range as R
  50. data Foo = Bar | Baz Word8 Word8 | Quux Word16
  51. deriving (Eq, Show, Generic, Finitary)
  52. deriving (Ord, Bounded, NFData, Hashable, Binary) via (Finiteness Foo)
  53. data Big = Big Word64 Word64
  54. deriving (Eq, Show, Generic, Finitary)
  55. deriving (Ord, Bounded, NFData, Hashable, Binary) via (Finiteness Big)
  56. -- Generators
  57. choose :: forall (a :: Type) m. (MonadGen m, Finitary a) => m a
  58. choose = fromFinite <$> chooseFinite
  59. chooseFinite :: forall (n :: Nat) m. (KnownNat n, MonadGen m) => m (Finite n)
  60. chooseFinite = fromIntegral <$> G.integral (R.linear 0 limit)
  61. where
  62. limit = subtract @Integer 1 . fromIntegral . natVal @n $ Proxy
  63. finitenessLaws :: (Show a, Binary a, Ord a) => Gen a -> [Laws]
  64. finitenessLaws p = [binaryLaws p, ordLaws p]
  65. packLaws :: (Eq a, Show a, Storable a) => Gen a -> [Laws]
  66. packLaws p = [storableLaws p]
  67. ordIsMonotonic ::
  68. forall (a :: Type) (t :: Type -> Type).
  69. (Finitary a, Show a, Ord a, Ord (t a)) =>
  70. (a -> t a) ->
  71. Property
  72. ordIsMonotonic f = property $ do
  73. x <- forAll $ choose @a
  74. y <- forAll $ choose @a
  75. (x < y) === (f x < f y)
  76. finitenessTests :: [(String, [Laws])]
  77. finitenessTests =
  78. [ ("Small Finiteness", finitenessLaws @Foo choose),
  79. ("Big Finiteness", finitenessLaws @Big choose)
  80. ]
  81. packTests :: [(String, [Laws])]
  82. packTests =
  83. [ ("Small PackBytes", packLaws @(PackBytes Foo) choose),
  84. ("Big PackBytes", packLaws @(PackBytes Big) choose),
  85. ("Small PackWords", packLaws @(PackWords Foo) choose),
  86. ("Big PackWords", packLaws @(PackWords Big) choose),
  87. ("Small packed into Word64", packLaws @(PackInto Foo Word64) choose)
  88. ]
  89. main :: IO Bool
  90. main = (&&) <$> checkLaws <*> checkMonotonicity
  91. where
  92. checkLaws =
  93. (&&) <$> lawsCheckMany finitenessTests <*> lawsCheckMany packTests
  94. checkMonotonicity =
  95. checkParallel . Group "Monotonicity" $
  96. [ ("Small PackBits", ordIsMonotonic @Foo Safe.Packed),
  97. ("Small unsafe PackBits", ordIsMonotonic @Foo Unsafe.Packed),
  98. ("Small PackBytes", ordIsMonotonic @Foo PackBytes.Packed),
  99. ("Small PackWords", ordIsMonotonic @Foo PackWords.Packed),
  100. ("Big PackBits", ordIsMonotonic @Big Safe.Packed),
  101. ("Big unsafe PackBits", ordIsMonotonic @Big Unsafe.Packed),
  102. ("Big PackBytes", ordIsMonotonic @Big PackBytes.Packed),
  103. ("Big PackWords", ordIsMonotonic @Big PackWords.Packed)
  104. ]