|
| 1 | +{-# LANGUAGE LinearTypes #-} |
| 2 | +{-# LANGUAGE NoImplicitPrelude #-} |
| 3 | +{-# LANGUAGE RankNTypes #-} |
| 4 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 5 | +{-# LANGUAGE TypeApplications #-} |
| 6 | + |
| 7 | +module Test.Foreign (foreignGCTests) where |
| 8 | + |
| 9 | +import Data.Typeable |
| 10 | +import Control.Monad (void) |
| 11 | +import Control.Exception hiding (assert) |
| 12 | +import qualified Foreign.Heap as Heap |
| 13 | +import Foreign.List (List) |
| 14 | +import qualified Foreign.List as List |
| 15 | +import qualified Foreign.Marshal.Pure as Manual |
| 16 | +import qualified Prelude |
| 17 | +import Prelude.Linear |
| 18 | +import Test.Tasty |
| 19 | +import Test.Tasty.Hedgehog (testProperty) |
| 20 | +import Hedgehog |
| 21 | +import qualified Hedgehog.Gen as Gen |
| 22 | +import qualified Hedgehog.Range as Range |
| 23 | + |
| 24 | + |
| 25 | +-- # Organizing tests |
| 26 | +------------------------------------------------------------------------------- |
| 27 | + |
| 28 | +foreignGCTests :: TestTree |
| 29 | +foreignGCTests = testGroup "foreignGCTests" |
| 30 | + [ listExampleTests |
| 31 | + , heapExampleTests |
| 32 | + ] |
| 33 | + |
| 34 | +listExampleTests :: TestTree |
| 35 | +listExampleTests = testGroup "list tests" |
| 36 | + [ testProperty "List.toList . List.fromList = id" invertNonGCList |
| 37 | + , testProperty "map id = id" mapIdNonGCList |
| 38 | + , testProperty "memory freed post-exception" testExecptionOnMem |
| 39 | + ] |
| 40 | + |
| 41 | +heapExampleTests :: TestTree |
| 42 | +heapExampleTests = testGroup "heap tests" |
| 43 | + [ testProperty "sort = heapsort" nonGCHeapSort ] |
| 44 | + |
| 45 | + |
| 46 | +-- # Internal library |
| 47 | +------------------------------------------------------------------------------- |
| 48 | + |
| 49 | +list :: Gen [Int] |
| 50 | +list = Gen.list (Range.linear 0 1000) (Gen.int (Range.linear 0 100)) |
| 51 | + |
| 52 | +eqList :: forall a. (Manual.Representable a, Movable a, Eq a) => |
| 53 | + List a %1-> List a %1-> Ur Bool |
| 54 | +eqList l1 l2 = move $ (List.toList l1) == (List.toList l2) |
| 55 | + |
| 56 | +data InjectedError = InjectedError |
| 57 | + deriving (Typeable, Show) |
| 58 | + |
| 59 | +instance Exception InjectedError |
| 60 | + |
| 61 | + |
| 62 | +-- # Properties |
| 63 | +------------------------------------------------------------------------------- |
| 64 | + |
| 65 | +invertNonGCList :: Property |
| 66 | +invertNonGCList = property Prelude.$ do |
| 67 | + xs <- forAll list |
| 68 | + let xs' = unur $ |
| 69 | + Manual.withPool (\p -> move $ List.toList $ List.ofList xs p) |
| 70 | + xs === xs' |
| 71 | + |
| 72 | +mapIdNonGCList :: Property |
| 73 | +mapIdNonGCList = property Prelude.$ do |
| 74 | + xs <- forAll list |
| 75 | + let boolTest = unur $ Manual.withPool $ \p -> |
| 76 | + dup3 p & \(p0,p1,p2) -> |
| 77 | + eqList (List.ofList xs p0) (List.map id (List.ofList xs p1) p2) |
| 78 | + assert boolTest |
| 79 | + |
| 80 | +testExecptionOnMem :: Property |
| 81 | +testExecptionOnMem = property Prelude.$ do |
| 82 | + xs <- forAll list |
| 83 | + let bs = xs ++ (throw InjectedError) |
| 84 | + let writeBadList = Manual.withPool (move . List.toList . List.ofRList bs) |
| 85 | + let ignoreCatch = \_ -> Prelude.return () |
| 86 | + evalIO (catch @InjectedError (void (evaluate writeBadList)) ignoreCatch) |
| 87 | + |
| 88 | +nonGCHeapSort :: Property |
| 89 | +nonGCHeapSort = property Prelude.$ do |
| 90 | + xs <- forAll list |
| 91 | + let ys :: [(Int,())] = zip xs $ Prelude.replicate (Prelude.length xs) () |
| 92 | + (Heap.sort ys) === (reverse $ sort ys) |
| 93 | + |
0 commit comments