Skip to content

Commit 313142d

Browse files
author
Divesh Otwani
authored
Merge pull request #290 from tweag/unifying-tests
Unifying testing frameworks
2 parents ea89efc + 1ad2a4d commit 313142d

File tree

10 files changed

+326
-205
lines changed

10 files changed

+326
-205
lines changed

examples/Main.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Main where
2+
3+
import Test.Tasty
4+
import Test.Foreign (foreignGCTests)
5+
import Test.Quicksort (quickSortTests)
6+
7+
main :: IO ()
8+
main = defaultMain allTests
9+
10+
allTests :: TestTree
11+
allTests = testGroup "All tests"
12+
[ foreignGCTests
13+
, quickSortTests
14+
]
15+

examples/Simple/Quicksort.hs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,14 @@
22
{-# LANGUAGE NoImplicitPrelude #-}
33

44
-- | This module implements quicksort with mutable arrays from linear-base
5-
module Simple.Quicksort
6-
( testQuicksort
7-
, quickSort
8-
)
9-
where
5+
module Simple.Quicksort (quickSort) where
106

117
import GHC.Stack
128
import qualified Data.Array.Mutable.Linear as Array
139
import Data.Array.Mutable.Linear (Array)
1410
import Data.Unrestricted.Linear
1511
import Prelude.Linear hiding (partition)
1612

17-
18-
-- # Testing
19-
-------------------------------------------------------------------------------
20-
21-
testQuicksort :: [Int] -> Bool
22-
testQuicksort xs = sort xs == quickSort xs
23-
24-
2513
-- # Quicksort
2614
-------------------------------------------------------------------------------
2715

examples/Simple/TopSort.hs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,6 @@ import Data.HashMap.Mutable.Linear (HashMap)
1616
import Data.Bifunctor.Linear (second)
1717
import Data.Maybe.Linear (catMaybes)
1818
import qualified Data.Functor.Linear as Data
19-
import Test.HUnit hiding (Node)
20-
21-
22-
-- # All Tests
23-
-------------------------------------------------------------------------------
24-
25-
test1 :: Test
26-
test1 = topsort [(1,[2,3]), (2, [4]), (3,[4]), (4,[])] ~=?
27-
[1,2,3,4]
28-
29-
test2 :: Test
30-
test2 = topsort [(5,[2,0]), (4,[0,1]), (0,[]), (2,[3]), (3,[1]), (1,[])] ~=?
31-
[5,2,3,4,0,1]
32-
33-
test3 :: Test
34-
test3 = topsort
35-
[ (1,[2]), (2,[4,5]), (3,[9,7]), (4,[7,8,10]), (5,[10]), (6,[10])
36-
, (7,[]),(8,[]),(9,[]),(10,[])
37-
] ~=?
38-
[1,2,4,8,5,3,9,7,6,10]
39-
40-
topsortTests :: IO Counts
41-
topsortTests = runTestTT $ TestList [test1, test2, test3]
4219

4320
-- # The topological sort of a DAG
4421
-------------------------------------------------------------------------------

examples/Spec.hs

Lines changed: 0 additions & 164 deletions
This file was deleted.

examples/Test/Foreign.hs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+

examples/Test/Quicksort.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Test.Quicksort (quickSortTests) where
2+
3+
import Data.List (sort)
4+
import Simple.Quicksort (quickSort)
5+
import Test.Tasty
6+
import Test.Tasty.Hedgehog (testProperty)
7+
import Hedgehog
8+
import qualified Hedgehog.Gen as Gen
9+
import qualified Hedgehog.Range as Range
10+
11+
quickSortTests :: TestTree
12+
quickSortTests = testProperty "quicksort sorts" testQuicksort
13+
14+
testQuicksort :: Property
15+
testQuicksort = property $ do
16+
xs <- forAll $ Gen.list (Range.linear 0 1000) (Gen.int $ Range.linear 0 100)
17+
sort xs === quickSort xs
18+

linear-base.cabal

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,14 @@ library
9999
test-suite test
100100
type: exitcode-stdio-1.0
101101
hs-source-dirs: test
102-
main-is: Spec.hs
102+
main-is: Main.hs
103103
other-modules:
104+
Test.Data.Destination
104105
Test.Data.Mutable.Array
105106
Test.Data.Mutable.Vector
106107
Test.Data.Mutable.HashMap
107108
Test.Data.Mutable.Set
109+
Test.Data.Polarized
108110
build-depends:
109111
base,
110112
linear-base,
@@ -120,8 +122,10 @@ test-suite test
120122
test-suite examples
121123
type: exitcode-stdio-1.0
122124
hs-source-dirs: examples
123-
main-is: Spec.hs
125+
main-is: Main.hs
124126
other-modules:
127+
Test.Foreign
128+
Test.Quicksort
125129
Foreign.List
126130
Foreign.Heap
127131
Simple.FileIO
@@ -130,10 +134,10 @@ test-suite examples
130134
Simple.TopSort
131135
build-depends:
132136
base,
133-
hspec,
134-
HUnit,
135137
linear-base,
136-
QuickCheck,
138+
tasty,
139+
tasty-hedgehog,
140+
hedgehog,
137141
storable-tuple,
138142
vector,
139143
text

0 commit comments

Comments
 (0)