| 1 |
{-# LANGUAGE TemplateHaskell #-} |
| 2 |
|
| 3 |
{-| Unittests for time utilities |
| 4 |
|
| 5 |
-} |
| 6 |
|
| 7 |
{- |
| 8 |
|
| 9 |
Copyright (C) 2014 Google Inc. |
| 10 |
All rights reserved. |
| 11 |
|
| 12 |
Redistribution and use in source and binary forms, with or without |
| 13 |
modification, are permitted provided that the following conditions are |
| 14 |
met: |
| 15 |
|
| 16 |
1. Redistributions of source code must retain the above copyright notice, |
| 17 |
this list of conditions and the following disclaimer. |
| 18 |
|
| 19 |
2. Redistributions in binary form must reproduce the above copyright |
| 20 |
notice, this list of conditions and the following disclaimer in the |
| 21 |
documentation and/or other materials provided with the distribution. |
| 22 |
|
| 23 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 24 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 25 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 26 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 27 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 28 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 29 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 30 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 31 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 32 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 |
|
| 35 |
-} |
| 36 |
|
| 37 |
module Test.Ganeti.Utils.Time |
| 38 |
( testUtils_Time |
| 39 |
) where |
| 40 |
|
| 41 |
import Ganeti.Utils.Time |
| 42 |
(TimeDiff(TimeDiff), noTimeDiff, |
| 43 |
addToClockTime, diffClockTimes, clockTimeToString) |
| 44 |
import System.Time (ClockTime(TOD)) |
| 45 |
|
| 46 |
import Test.QuickCheck |
| 47 |
|
| 48 |
import Test.Ganeti.TestHelper |
| 49 |
import Test.Ganeti.TestCommon |
| 50 |
|
| 51 |
|
| 52 |
prop_clockTimeToString :: Integer -> Integer -> Property |
| 53 |
prop_clockTimeToString ts pico = |
| 54 |
clockTimeToString (TOD ts pico) ==? show ts |
| 55 |
|
| 56 |
genPicoseconds :: Gen Integer |
| 57 |
genPicoseconds = choose (0, 999999999999) |
| 58 |
|
| 59 |
genTimeDiff :: Gen TimeDiff |
| 60 |
genTimeDiff = TimeDiff <$> arbitrary <*> genPicoseconds |
| 61 |
|
| 62 |
genClockTime :: Gen ClockTime |
| 63 |
genClockTime = TOD <$> choose (946681200, 2082754800) <*> genPicoseconds |
| 64 |
|
| 65 |
prop_addToClockTime_identity :: Property |
| 66 |
prop_addToClockTime_identity = forAll genClockTime addToClockTime_identity |
| 67 |
|
| 68 |
addToClockTime_identity :: ClockTime -> Property |
| 69 |
addToClockTime_identity a = |
| 70 |
addToClockTime noTimeDiff a ==? a |
| 71 |
|
| 72 |
{- | |
| 73 |
Verify our work-around for ghc bug #2519. |
| 74 |
Taking `diffClockTimes` form `System.Time`, this test fails with an exception. |
| 75 |
-} |
| 76 |
prop_timediffAdd :: Property |
| 77 |
prop_timediffAdd = |
| 78 |
forAll genClockTime $ \a -> |
| 79 |
forAll genClockTime $ \b -> |
| 80 |
forAll genClockTime $ \c -> timediffAdd a b c |
| 81 |
|
| 82 |
timediffAdd :: ClockTime -> ClockTime -> ClockTime -> Property |
| 83 |
timediffAdd a b c = |
| 84 |
let fwd = diffClockTimes a b |
| 85 |
back = diffClockTimes b a |
| 86 |
in addToClockTime fwd (addToClockTime back c) ==? c |
| 87 |
|
| 88 |
prop_timediffAddCommutative :: Property |
| 89 |
prop_timediffAddCommutative = |
| 90 |
forAll genTimeDiff $ \a -> |
| 91 |
forAll genTimeDiff $ \b -> |
| 92 |
forAll genClockTime $ \c -> timediffAddCommutative a b c |
| 93 |
|
| 94 |
timediffAddCommutative :: TimeDiff -> TimeDiff -> ClockTime -> Property |
| 95 |
timediffAddCommutative a b c = |
| 96 |
addToClockTime a (addToClockTime b c) |
| 97 |
==? |
| 98 |
addToClockTime b (addToClockTime a c) |
| 99 |
|
| 100 |
|
| 101 |
testSuite "Utils/Time" |
| 102 |
[ 'prop_clockTimeToString |
| 103 |
, 'prop_addToClockTime_identity |
| 104 |
, 'prop_timediffAdd |
| 105 |
, 'prop_timediffAddCommutative |
| 106 |
] |