]> Git — Sourcephile - haskell/treemap.git/blob - test/HUnit/Strict.hs
stack: bump to lts-12.25
[haskell/treemap.git] / test / HUnit / Strict.hs
1 module HUnit.Strict where
2
3 import Data.Function (($), id, const)
4 import Data.Int (Int)
5 import Data.Monoid ((<>))
6 import Prelude (Integer, undefined)
7 import qualified Data.Map.Strict as Map
8 import qualified Data.Strict.Maybe as Strict
9
10 import Test.Tasty
11 import Test.Tasty.HUnit
12
13 import Data.TreeMap.Strict (TreeMap(..), (<|))
14 import qualified Data.TreeMap.Strict as TreeMap
15
16 hunits :: TestTree
17 hunits = testGroup "Strict"
18 [ testGroup "insert"
19 [ testCase "[] 0" $
20 TreeMap.insert const ((0::Int)<|[]) () TreeMap.empty
21 @?= (TreeMap $ Map.fromList [ (0::Int, TreeMap.leaf ()) ])
22 , testCase "[] 0/1" $
23 TreeMap.insert const ((0::Int)<|[1]) () TreeMap.empty
24 @?=
25 (TreeMap $
26 Map.fromList
27 [ (0::Int, TreeMap.Node
28 { TreeMap.node_value = Strict.Nothing
29 , TreeMap.node_size = 1
30 , TreeMap.node_descendants =
31 TreeMap.singleton ((1::Int)<|[]) ()
32 })
33 ])
34 ]
35 , testGroup "mapByDepthFirst"
36 [ testCase "[0, 0/1, 0/1/2, 1, 1/2/3]" $
37 TreeMap.mapByDepthFirst
38 (\descendants value ->
39 Map.foldl'
40 (\acc v -> (<>) acc $
41 Strict.fromMaybe undefined $
42 TreeMap.node_value v
43 )
44 (Strict.fromMaybe [] value)
45 (TreeMap.nodes descendants)
46 )
47 (TreeMap.fromList const
48 [ ((0::Integer)<|[], [0::Integer])
49 , (0<|[1], [0,1])
50 , (0<|[1,2], [0,1,2])
51 , (1<|[], [1])
52 , (1<|[2,3], [1,2,3])
53 ])
54 @?=
55 TreeMap.fromList const
56 [ ((0::Integer)<|[], [0,0,1,0,1,2])
57 , (0<|[1], [0,1,0,1,2])
58 , (0<|[1,2], [0,1,2])
59 , (1<|[], [1,1,2,3])
60 , (1<|[2], [1,2,3])
61 , (1<|[2,3], [1,2,3])
62 ]
63 , testCase "[0/0]" $
64 TreeMap.mapByDepthFirst
65 (\descendants value ->
66 Map.foldl'
67 (\acc v -> (<>) acc $
68 Strict.fromMaybe undefined $
69 TreeMap.node_value v
70 )
71 (Strict.fromMaybe [] value)
72 (TreeMap.nodes descendants)
73 )
74 (TreeMap.fromList const
75 [ ((0::Integer)<|[0], [0::Integer,0])
76 ])
77 @?=
78 TreeMap.fromList const
79 [ ((0::Integer)<|[], [0,0])
80 , (0<|[0], [0,0])
81 ]
82 ]
83 , testGroup "flatten"
84 [ testCase "[0, 0/1, 0/1/2]" $
85 TreeMap.flatten id
86 (TreeMap.fromList const
87 [ ((0::Integer)<|[], ())
88 , (0<|[1], ())
89 , (0<|[1,2], ())
90 ])
91 @?=
92 Map.fromList
93 [ ((0::Integer)<|[], ())
94 , (0<|[1], ())
95 , (0<|[1,2], ())
96 ]
97 , testCase "[1, 1/2, 1/22, 1/2/3, 1/2/33, 11, 11/2, 11/2/3, 11/2/33]" $
98 TreeMap.flatten id
99 (TreeMap.fromList const
100 [ ((1::Integer)<|[], ())
101 , (1<|[2], ())
102 , (1<|[22], ())
103 , (1<|[2,3], ())
104 , (1<|[2,33], ())
105 , (11<|[], ())
106 , (11<|[2], ())
107 , (11<|[2,3], ())
108 , (11<|[2,33], ())
109 ])
110 @?=
111 Map.fromList
112 [ ((1::Integer)<|[], ())
113 , (1<|[2], ())
114 , (1<|[22], ())
115 , (1<|[2,3], ())
116 , (1<|[2,33], ())
117 , (11<|[], ())
118 , (11<|[2], ())
119 , (11<|[2,3], ())
120 , (11<|[2,33], ())
121 ]
122 ]
123 , testGroup "lookupAlong"
124 [ testCase "0/1/2/3 [0, 0/1, 0/1/2, 0/1/2/3]" $
125 TreeMap.lookupAlong
126 (0<|[1,2,3])
127 (TreeMap.fromList const
128 [ ((0::Integer)<|[], [0])
129 , (0<|[1], [0,1])
130 , (0<|[1,2], [0,1,2])
131 , (0<|[1,2,3], [0,1,2,3])
132 ])
133 @?=
134 [ [0::Integer]
135 , [0,1]
136 , [0,1,2]
137 , [0,1,2,3]
138 ]
139 , testCase "0/1/2/3 [0, 0/1]" $
140 TreeMap.lookupAlong
141 (0<|[1,2,3])
142 (TreeMap.fromList const
143 [ ((0::Integer)<|[], [0])
144 , (0<|[1], [0,1])
145 ])
146 @?=
147 [ [0::Integer]
148 , [0,1]
149 ]
150 ]
151 ]