add ScopeMask (incl. property test)

fogti
Aug 26, 2024, 6:49 PM
2KOAQWUTFV673P2CLZEIZIVOCPTMXNKDILOVWP6PHRJWAXEGLC6QC

Dependencies

Change contents

  • file addition: test (d--r------)
    [3.709]
  • file addition: Main.hs (----------)
    [0.16]
    module Main (main) where
    import Control.Monad (unless)
    import Test.Tasty
    import Test.Tasty.Falsify
    import qualified Test.Falsify.Generator as Gen
    import qualified Test.Falsify.Range as Ran
    import qualified GardGround.Utils.ScopeMask as ScM
    main :: IO ()
    main = defaultMain $
    testProperty "scope mask list roundtrip" $ do
    x <- gen . Gen.inRange $ Ran.between (0, 10000000)
    let x' = toEnum x :: Integer
    let ScM.ScopeMask y = ScM.fromList . ScM.toList $ ScM.ScopeMask x'
    unless (x' == y) $ testFailed "roundtrip failed"
  • file addition: ScopeMask.hs (----------)
    [3.769]
    {-# LANGUAGE DerivingVia #-}
    module GardGround.Utils.ScopeMask where
    -- this mostly a direct copy of https://github.com/AndrasKovacs/smalltt/blob/b5fc9b3747dcbf8c71ca9f976afb7b574b630996/src/LvlSet.hs
    -- see LICENSE.smalltt for license information
    import Data.Bits
    import Data.Foldable (foldl')
    newtype ScopeMask = ScopeMask Integer deriving (Eq, Bits) via Integer
    instance Semigroup ScopeMask where
    (<>) = (.|.)
    {-# INLINE (<>) #-}
    instance Monoid ScopeMask where
    mempty = ScopeMask 0
    {-# INLINE mempty #-}
    insert :: Int -> ScopeMask -> ScopeMask
    insert x (ScopeMask s) = ScopeMask (unsafeShiftL 1 x .|. s)
    {-# INLINE insert #-}
    member :: Int -> ScopeMask -> Bool
    member x (ScopeMask s) = (unsafeShiftL 1 x .&. s) /= 0
    {-# INLINE member #-}
    toList :: ScopeMask -> [Int]
    toList s' = reverse $ intern 0 s'
    where
    -- | `intern offset smask`
    intern :: Int -> ScopeMask -> [Int]
    intern _ (ScopeMask 0) = []
    intern i (ScopeMask s) =
    let nxt = intern (i + 1) (ScopeMask $ unsafeShiftR s 1) in
    if 1 .&. s /= 0 then i:nxt else nxt
    fromList :: [Int] -> ScopeMask
    fromList = foldl' (flip insert) mempty
    instance Show ScopeMask where
    show = show . toList
  • replacement in core/gardground-core.cabal at line 16
    [3.4964][3.4964:4990]()
    import: warnings
    [3.4964]
    [3.4990]
    import: warnings
  • edit in core/gardground-core.cabal at line 22
    [3.5078]
    [2.10763]
    GardGround.Utils.ScopeMask
  • edit in core/gardground-core.cabal at line 31
    [2.10900]
    [3.5101]
    DerivingVia
  • edit in core/gardground-core.cabal at line 56
    [3.5639]
    test-suite gardground-test
    import: warnings
    default-language: Haskell2010
    type: exitcode-stdio-1.0
    hs-source-dirs: test
    main-is: Main.hs
    build-depends:
    base (>= 4.16.0.0 && <4.19)
    , falsify >= 0.2
    , tasty >= 1.5
    , gardground-core
  • file addition: LICENSE.smalltt (----------)
    [3.709]
    Copyright 2021 András Kovács
    Permission is hereby granted, free of charge, to any person obtaining a copy of
    this software and associated documentation files (the "Software"), to deal in
    the Software without restriction, including without limitation the rights to
    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
    the Software, and to permit persons to whom the Software is furnished to do so,
    subject to the following conditions:
    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
    FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
    IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  • edit in cabal.project at line 1
    [3.5678]
    [3.5679]
    tests: True