Debian Codegraph @

tests/Data/OrdPSQ/Tests.hs

raw
1 module Data.OrdPSQ.Tests
2 ( tests
3 ) where
4
5 import Data.List (isInfixOf)
6 import Test.HUnit (Assertion, assert)
7 import Test.Tasty (TestTree)
8 import Test.Tasty.HUnit (testCase)
9 import Test.Tasty.QuickCheck (testProperty)
10
11 import Data.OrdPSQ.Internal
12 import Data.PSQ.Class.Gen ()
13 import Data.PSQ.Class.Util
14
15 --------------------------------------------------------------------------------
16 -- Index of tests
17 --------------------------------------------------------------------------------
18
19 tests :: [TestTree]
20 tests =
21 [ testCase "showElem" test_showElem
22 , testCase "showLTree" test_showLTree
23 , testCase "invalidLTree" test_invalidLTree
24 , testCase "balanceErrors" test_balanceErrors
25 , testProperty "toAscList" prop_toAscList
26 ]
27
28
29 --------------------------------------------------------------------------------
30 -- Tests the result of 'moduleError' for internal issues
31 --------------------------------------------------------------------------------
32
33 assertModuleError :: String -> String -> a -> Assertion
34 assertModuleError fun msg = assertErrorCall $ \e -> do
35 assert $ fun `isInfixOf` e
36 assert $ msg `isInfixOf` e
37
38
39 --------------------------------------------------------------------------------
40 -- HUnit tests
41 --------------------------------------------------------------------------------
42
43 test_showElem :: Assertion
44 test_showElem =
45 assert $ length (coverShowInstance (E 0 0 'A' :: Elem Int Int Char)) > 0
46
47 test_showLTree :: Assertion
48 test_showLTree = do
49 assert $ length (coverShowInstance t1) > 0
50 assert $ length (coverShowInstance t2) > 0
51 assert $ length (coverShowInstance t3) > 0
52 where
53 t1, t2, t3 :: LTree Int Int Char
54 t1 = Start
55 t2 = LLoser 1 e Start 0 Start
56 t3 = RLoser 1 e Start 0 Start
57 e = E 0 0 'A'
58
59 test_invalidLTree :: Assertion
60 test_invalidLTree = do
61 assertModuleError "left" "empty" (left (Start :: LTree Int Int Char))
62 assertModuleError "right" "empty" (right (Start :: LTree Int Int Char))
63 assertModuleError "maxKey" "empty" (maxKey (empty :: OrdPSQ Int Int Char))
64
65 test_balanceErrors :: Assertion
66 test_balanceErrors = do
67 assertModuleError "lsingleLeft" msg (lsingleLeft 0 0 'A' nil 0 nil)
68 assertModuleError "rsingleLeft" msg (rsingleLeft 0 0 'A' nil 0 nil)
69 assertModuleError "lsingleRight" msg (lsingleRight 0 0 'A' nil 0 nil)
70 assertModuleError "rsingleRight" msg (rsingleRight 0 0 'A' nil 0 nil)
71 assertModuleError "ldoubleLeft" msg (ldoubleLeft 0 0 'A' nil 0 nil)
72 assertModuleError "rdoubleLeft" msg (rdoubleLeft 0 0 'A' nil 0 nil)
73 assertModuleError "ldoubleRight" msg (ldoubleRight 0 0 'A' nil 0 nil)
74 assertModuleError "rdoubleRight" msg (rdoubleRight 0 0 'A' nil 0 nil)
75 where
76 nil = Start :: LTree Int Int Char
77 msg = "malformed"
78
79
80 --------------------------------------------------------------------------------
81 -- QuickCheck properties
82 --------------------------------------------------------------------------------
83
84 prop_toAscList :: OrdPSQ Int Int Char -> Bool
85 prop_toAscList t = isUniqueSorted [k | (k, _, _) <- toAscList t]
86 where
87 isUniqueSorted (x : y : zs) = x < y && isUniqueSorted (y : zs)
88 isUniqueSorted [_] = True
89 isUniqueSorted [] = True