Main.hs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. {-
  2. - Copyright (C) 2019 Koz Ross <koz.ross@retro-freedom.nz>
  3. -
  4. - This program is free software: you can redistribute it and/or modify
  5. - it under the terms of the GNU General Public License as published by
  6. - the Free Software Foundation, either version 3 of the License, or
  7. - (at your option) any later version.
  8. -
  9. - This program is distributed in the hope that it will be useful,
  10. - but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. - GNU General Public License for more details.
  13. -
  14. - You should have received a copy of the GNU General Public License
  15. - along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. -}
  17. {-# LANGUAGE TypeInType #-}
  18. {-# LANGUAGE ScopedTypeVariables #-}
  19. {-# LANGUAGE TypeApplications #-}
  20. {-# LANGUAGE DeriveGeneric #-}
  21. {-# LANGUAGE DeriveAnyClass #-}
  22. module Main where
  23. import Data.Kind (Type)
  24. import GHC.TypeNats
  25. import GHC.Generics (Generic)
  26. import Data.Word (Word8, Word16, Word64)
  27. import Hedgehog
  28. import Hedgehog.Classes
  29. import Data.Finitary (Finitary(..))
  30. import Data.Finite (Finite)
  31. import Data.Proxy (Proxy(..))
  32. import Control.Monad.Loops (andM)
  33. import qualified Hedgehog.Gen as G
  34. import qualified Hedgehog.Range as R
  35. import Data.Finitary.Pack (Pack)
  36. data Foo = Bar | Baz Word8 Word8 | Quux Word16
  37. deriving (Eq, Show, Generic, Finitary)
  38. data Big = Big Word64 Word64
  39. deriving (Eq, Show, Generic, Finitary)
  40. -- Generators
  41. choose :: forall (a :: Type) m . (MonadGen m, Finitary a) => m a
  42. choose = fromFinite <$> chooseFinite
  43. chooseFinite :: forall (n :: Nat) m . (KnownNat n, MonadGen m) => m (Finite n)
  44. chooseFinite = fromIntegral <$> G.integral (R.linear 0 limit)
  45. where limit = subtract @Integer 1 . fromIntegral . natVal @n $ Proxy
  46. main :: IO Bool
  47. main = andM . fmap lawsCheck $ [binaryLaws @(Pack Foo) choose,
  48. binaryLaws @(Pack Big) choose,
  49. binaryLaws @(Pack Int) choose,
  50. storableLaws @(Pack Foo) choose,
  51. storableLaws @(Pack Big) choose,
  52. storableLaws @(Pack Int) choose]