Revision bfe6c954

b/htools/Ganeti/HTools/QC.hs
298 298
         random_key (def_value+1) == Just def_value
299 299
        where _types = def_value :: Integer
300 300

  
301
-- | Test that functional if' behaves like the syntactic sugar if.
302
prop_Utils_if'if :: Bool -> Int -> Int -> Bool
303
prop_Utils_if'if cnd a b = Utils.if' cnd a b == if cnd then a else b
304

  
305
-- | Test select
306
prop_Utils_select :: Int   -- ^ Default result
307
                  -> [Int] -- ^ List of False values
308
                  -> [Int] -- ^ List of True values
309
                  -> Bool  -- ^ Try undef result (if a true value exists)
310
                  -> Bool  -- ^ Try undef true value (if a true value exists)
311
                  -> Bool  -- ^ Try undef false value (if a true value exists)
312
                  -> Bool  -- ^ Test result
313
prop_Utils_select di lst1 lst2 rundefd rundeft rundeff =
314
  Utils.select def cndlist == expectedresult
315
  where has_nondef_result = not (null lst2)
316
        try_undefd = has_nondef_result && rundefd
317
        try_undeft = has_nondef_result && rundeft
318
        try_undeff = has_nondef_result && rundeff
319
        def = Utils.if' try_undefd undefined di
320
        utl = Utils.if' try_undeft [(True, undefined)] []
321
        ufl = Utils.if' try_undeff [(False, undefined)] []
322
        expectedresult = Utils.if' has_nondef_result (head lst2) def
323
        flist = map (\e -> (False, e)) lst1
324
        tlist = map (\e -> (True, e)) lst2
325
        cndlist = flist ++ tlist ++ utl ++ ufl
326

  
301 327
-- | Test list for the Utils module.
302 328
testUtils =
303 329
  [ run prop_Utils_commaJoinSplit
304 330
  , run prop_Utils_commaSplitJoin
305 331
  , run prop_Utils_fromObjWithDefault
332
  , run prop_Utils_if'if
333
  , run prop_Utils_select
306 334
  ]
307 335

  
308 336
-- ** PeerMap tests
b/htools/Ganeti/HTools/Utils.hs
28 28
    , debugXy
29 29
    , sepSplit
30 30
    , stdDev
31
    , if'
32
    , select
31 33
    , commaJoin
32 34
    , readEitherString
33 35
    , JSRecord
......
106 108
      av = foldl' (\accu em -> let d = em - mv in accu + d * d) 0.0 lst
107 109
  in sqrt (av / ll) -- stddev
108 110

  
111
-- *  Logical functions
112

  
113
-- Avoid syntactic sugar and enhance readability. These functions are proposed
114
-- by some for inclusion in the Prelude, and at the moment they are present
115
-- (with various definitions) in the utility-ht package. Some rationale and
116
-- discussion is available at <http://www.haskell.org/haskellwiki/If-then-else>
117

  
118
-- | \"if\" as a function, rather than as syntactic sugar.
119
if' :: Bool -- ^ condition
120
    -> a    -- ^ \"then\" result
121
    -> a    -- ^ \"else\" result
122
    -> a    -- ^ \"then\" or "else" result depending on the condition
123
if' True x _ = x
124
if' _    _ y = y
125

  
126
-- | Return the first result with a True condition, or the default otherwise.
127
select :: a            -- ^ default result
128
       -> [(Bool, a)]  -- ^ list of \"condition, result\"
129
       -> a            -- ^ first result which has a True condition, or default
130
select def = maybe def snd . find fst
131

  
109 132
-- * JSON-related functions
110 133

  
111 134
-- | A type alias for the list-based representation of J.JSObject.

Also available in: Unified diff