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