Set 3a Ex 7&8: better error messages for infinite loops

This commit is contained in:
Joel Kaasinen
2021-05-24 07:35:12 +03:00
parent 8f6a8d3fd2
commit 92d27bf6e4
+4 -4
View File
@@ -114,19 +114,19 @@ ex6_powers_large = m_powers 27 -- 5^27 still fits in Int
ex7_while_number = property $ do ex7_while_number = property $ do
n <- choose (0,20 :: Integer) n <- choose (0,20 :: Integer)
return $ counterexample ("while (/="++show n++") (+1) 0") $ return $ counterexample ("while (/="++show n++") (+1) 0") $ within timeLimit $
while (/=n) (+1) 0 ?== n while (/=n) (+1) 0 ?== n
ex7_while_string = property $ do ex7_while_string = property $ do
n <- word n <- word
let w = n++n let w = n++n
p = (/=n) p = (/=n)
return $ counterexample ("while (/="++show n++") tail "++show w) $ return $ counterexample ("while (/="++show n++") tail "++show w) $ within timeLimit $
while p tail w == n while p tail w == n
ex8_whileRight_Left = forAllBlind word $ \w -> ex8_whileRight_Left = forAllBlind word $ \w ->
forAllBlind (choose (1::Int,10)) $ \i -> forAllBlind (choose (1::Int,10)) $ \i ->
counterexample ("let f _ = Left " ++ show w ++ " in whileRight f " ++ show i) $ counterexample ("let f _ = Left " ++ show w ++ " in whileRight f " ++ show i) $ within timeLimit $
let f _ = Left w in whileRight f i ?== w let f _ = Left w in whileRight f i ?== w
ex8_whileRight_step = property $ do ex8_whileRight_step = property $ do
@@ -137,7 +137,7 @@ ex8_whileRight_step = property $ do
let log = ceiling . logBase 2 . fromIntegral let log = ceiling . logBase 2 . fromIntegral
divUp x y = ceiling (fromIntegral x / fromIntegral y) divUp x y = ceiling (fromIntegral x / fromIntegral y)
answer = start * 2 ^ (log (limit `divUp` start)) answer = start * 2 ^ (log (limit `divUp` start))
return $ counterexample ("whileRight (step " ++ show limit ++ ") " ++ show start) $ return $ counterexample ("whileRight (step " ++ show limit ++ ") " ++ show start) $ within timeLimit $
whileRight (step limit) start ?== answer whileRight (step limit) start ?== answer
ex9_1 = property $ do ex9_1 = property $ do