1 {-# LANGUAGE NamedFieldPuns #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE ScopedTypeVariables #-}
4 {-# LANGUAGE TupleSections #-}
5 module Sieve.Test where
7 import Data.Function (($), (.), flip, on)
8 import Data.Functor ((<$>))
9 import Data.List (concatMap, foldl, foldr, unwords, reverse)
10 import Data.Maybe (Maybe(..), fromJust)
11 import Data.Monoid ((<>))
12 import Prelude (Integer)
14 import Test.Tasty.HUnit
15 import Text.Show (Show(..))
17 import qualified Data.Interval as Interval
18 import qualified Data.Interval.Sieve as Interval.Sieve
21 tests = testGroup "Sieve"
25 let is = fromJust <$> mis in
26 let e = fromJust <$> me in
28 (flip (Interval.Sieve.union . Interval.Sieve.singleton))
29 Interval.Sieve.empty is in
31 (Interval.Sieve.union . Interval.Sieve.singleton)
32 Interval.Sieve.empty is in
33 [ testCase (unwords $ (show . Interval.Pretty) <$> is) $
34 Interval.Sieve.intervals sil @?= e
35 , testCase (unwords $ (show . Interval.Pretty) <$> reverse is) $
36 Interval.Sieve.intervals sir @?= e
39 [ ( [ (Interval.<=..<) 0 (5::Integer)
40 , (Interval.<=..<=) 5 9
42 , [ (Interval.<=..<=) 0 9 ]
44 , ( [ (Interval.<=..<=) 0 5
45 , (Interval.<=..<=) 0 9
47 , [ (Interval.<=..<=) 0 9 ]
49 , ( [ (Interval.<=..<=) 0 4
50 , (Interval.<=..<=) 5 9
51 , (Interval.<=..<=) 3 6
53 , [ (Interval.<=..<=) 0 9 ]
55 , ( [ (Interval.<=..<=) 1 4
56 , (Interval.<=..<=) 5 8
58 , [ (Interval.<=..<=) 1 4
59 , (Interval.<=..<=) 5 8
62 , ( [ (Interval.<=..<=) 1 8
63 , (Interval.<=..<=) 0 9
65 , [ (Interval.<=..<=) 0 9 ]
67 , ( [ (Interval.<=..<=) 1 4
68 , (Interval.<=..<=) 5 8
69 , (Interval.<=..<=) 0 9
71 , [ (Interval.<=..<=) 0 9 ]
76 let is = fromJust <$> mis in
77 let js = fromJust <$> mjs in
78 let e = fromJust <$> me in
80 (flip (Interval.Sieve.union . Interval.Sieve.singleton))
81 Interval.Sieve.empty is in
83 (flip (Interval.Sieve.union . Interval.Sieve.singleton))
84 Interval.Sieve.empty js in
85 [ testCase (unwords ((show . Interval.Pretty) <$> is) <> " u " <>
86 unwords ((show . Interval.Pretty) <$> js)) $
87 Interval.Sieve.intervals (Interval.Sieve.union iu ju) @?= e
88 , testCase (unwords ((show . Interval.Pretty) <$> js) <> " u " <>
89 unwords ((show . Interval.Pretty) <$> is)) $
90 Interval.Sieve.intervals (Interval.Sieve.union ju iu) @?= e
93 [ ( [ (Interval.<=..<=) 0 (1::Integer)
94 , (Interval.<=..<=) 2 4
96 , [ (Interval.<=..<=) 0 3
98 , [ (Interval.<=..<=) 0 4
101 , ( [ (Interval.<=..<=) 0 1
102 , (Interval.<=..<=) 2 3
103 , (Interval.<=..<=) 4 5
104 , (Interval.<=..<=) 6 7
106 , [ (Interval.<=..<=) 1 2
107 , (Interval.<=..<=) 3 4
108 , (Interval.<=..<=) 5 6
110 , [ (Interval.<=..<=) 0 7
113 , ( [ (Interval.<=..<=) 0 1
114 , (Interval.<=..<=) 2 3
116 , [ (Interval.<=..<=) 4 5
118 , [ (Interval.<=..<=) 0 1
119 , (Interval.<=..<=) 2 3
120 , (Interval.<=..<=) 4 5
123 , ( [ (Interval.<=..<=) 0 1
124 , (Interval.<=..<=) 4 5
126 , [ (Interval.<=..<=) 2 3
128 , [ (Interval.<=..<=) 0 1
129 , (Interval.<=..<=) 2 3
130 , (Interval.<=..<=) 4 5
134 , testGroup "intersection" $
137 let is = fromJust <$> mis in
138 let js = fromJust <$> mjs in
139 let e = fromJust <$> me in
141 (flip (Interval.Sieve.union . Interval.Sieve.singleton))
142 Interval.Sieve.empty is in
144 (flip (Interval.Sieve.union . Interval.Sieve.singleton))
145 Interval.Sieve.empty js in
146 [ testCase (unwords ((show . Interval.Pretty) <$> is) <> " n " <>
147 unwords ((show . Interval.Pretty) <$> js)) $
148 Interval.Sieve.intervals (Interval.Sieve.intersection iu ju) @?= e
149 , testCase (unwords ((show . Interval.Pretty) <$> js) <> " n " <>
150 unwords ((show . Interval.Pretty) <$> is)) $
151 Interval.Sieve.intervals (Interval.Sieve.intersection ju iu) @?= e
154 [ ( [ (Interval.<=..<) 0 (5::Integer) ]
155 , [ (Interval.<=..<=) 5 9 ]
158 , ( [ (Interval.<=..<=) 0 5 ]
159 , [ (Interval.<=..<=) 5 9 ]
160 , [ (Interval.<=..<=) 5 5 ]
162 , ( [ (Interval.<=..<=) 0 5 ]
163 , [ (Interval.<=..<=) 0 9 ]
164 , [ (Interval.<=..<=) 0 5 ]
166 , ( [ (Interval.<=..<=) 0 4
167 , (Interval.<=..<=) 5 9
169 , [ (Interval.<=..<=) 3 6 ]
170 , [ (Interval.<=..<=) 3 4
171 , (Interval.<=..<=) 5 6
174 , ( [ (Interval.<=..<=) 1 4
175 , (Interval.<=..<=) 6 8
177 , [ (Interval.<=..<=) 2 3
178 , (Interval.<=..<=) 5 7
180 , [ (Interval.<=..<=) 2 3
181 , (Interval.<=..<=) 6 7
185 , testGroup "complement" $
188 let is = fromJust <$> mis in
189 let e = fromJust <$> me in
191 (flip (Interval.Sieve.union . Interval.Sieve.singleton))
192 Interval.Sieve.empty is in
193 [ testCase (show (Interval.Pretty $
194 Interval.Sieve.fmap_interval
195 (Interval.fmap_unsafe Interval.Pretty) iu)) $
196 Interval.Sieve.intervals (Interval.Sieve.complement iu) @?= e
199 [ ( [ ((Interval.<=..<) `on` Interval.Limited) 0 (5::Integer)
200 , ((Interval.<=..<=) `on` Interval.Limited) 5 9
202 , [ Just $ (Interval...<) 0
203 , Just $ (Interval.<..) 9
206 , ( [ Just Interval.unlimited ]
210 , [ Just Interval.unlimited ]
212 , ( [ Just $ (Interval...<) 0
213 , Just $ (Interval.<..) 0
215 , [ Just $ Interval.point $ Interval.Limited 0
218 , ( [ ((Interval.<=..<) `on` Interval.Limited) 0 1
219 , ((Interval.<=..<) `on` Interval.Limited) 2 3
220 , ((Interval.<..<=) `on` Interval.Limited) 3 4
222 , [ Just $ (Interval...<) 0
223 , ((Interval.<=..<) `on` Interval.Limited) 1 2
224 , Just $ Interval.point $ Interval.Limited 3
225 , Just $ (Interval.<..) 4
229 , testGroup "complement_with" $
232 let ib = fromJust mib in
233 let is = fromJust <$> mis in
234 let e = fromJust <$> me in
236 (flip (Interval.Sieve.union . Interval.Sieve.singleton))
237 Interval.Sieve.empty is in
238 [ testCase (show (Interval.Pretty iu)) $
239 Interval.Sieve.intervals
240 (Interval.Sieve.complement_with ib iu) @?= e
243 [ ( (Interval.<=..<=) (-10) (10::Integer)
244 , [ (Interval.<=..<) 0 5
245 , (Interval.<=..<=) 5 9
247 , [ (Interval.<=..<) (-10) 0
248 , (Interval.<..<=) 9 10
251 , ( (Interval.<=..<=) (-10) 10
252 , [ (Interval.<=..<=) (-10) 10 ]
255 , ( (Interval.<=..<=) (-10) 10
257 , [ (Interval.<=..<=) (-10) 10 ]
259 , ( (Interval.<=..<=) (-10) 10
260 , [ (Interval.<=..<) (-10) 0
261 , (Interval.<..<=) 0 10
263 , [ Just $ Interval.point 0
266 , ( (Interval.<=..<=) (-10) 10
267 , [ Just $ Interval.point 0
269 , [ (Interval.<=..<) (-10) 0
270 , (Interval.<..<=) 0 10
273 , ( (Interval.<=..<=) 0 10
274 , [ (Interval.<..<=) 0 10
276 , [ Just $ Interval.point 0
279 , ( (Interval.<=..<=) 0 10
280 , [ (Interval.<=..<) 0 10
282 , [ Just $ Interval.point 10
285 , ( Just $ Interval.point 0
288 , [ Just $ Interval.point 0
291 , ( Just $ Interval.point 0
292 , [ Just $ Interval.point 0