2KOAQWUTFV673P2CLZEIZIVOCPTMXNKDILOVWP6PHRJWAXEGLC6QC
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"
{-# 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
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
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.