diff options
author | Joey Hess <joey@kitenet.net> | 2013-10-08 17:35:25 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2013-10-08 17:35:25 -0400 |
commit | 92c4e5bffa7a82b78b5e5e79fa3d97f82d8dff0f (patch) | |
tree | 856f649d3149fd7fdfb4b8590c04c38b7ad6435a | |
parent | 8567a32c6c994f22990073f71d6836e70f90a201 (diff) |
quickcheck duration
-rw-r--r-- | Test.hs | 2 | ||||
-rw-r--r-- | Utility/HumanTime.hs | 18 |
2 files changed, 17 insertions, 3 deletions
@@ -60,6 +60,7 @@ import qualified Utility.Matcher import qualified Utility.Exception import qualified Utility.Hash import qualified Utility.Scheduled +import qualified Utility.HumanTime #ifndef mingw32_HOST_OS import qualified GitAnnex import qualified Remote.Helper.Encryptable @@ -140,6 +141,7 @@ quickcheck = , check "prop_parse_show_TrustLog" Logs.Trust.prop_parse_show_TrustLog , check "prop_hashes_stable" Utility.Hash.prop_hashes_stable , check "prop_schedule_roundtrips" Utility.Scheduled.prop_schedule_roundtrips + , check "prop_duration_roundtrips" Utility.HumanTime.prop_duration_roundtrips ] where check desc prop = do diff --git a/Utility/HumanTime.hs b/Utility/HumanTime.hs index 9edc8df53..c55c862ca 100644 --- a/Utility/HumanTime.hs +++ b/Utility/HumanTime.hs @@ -10,13 +10,16 @@ module Utility.HumanTime ( durationToPOSIXTime, parseDuration, fromDuration, + prop_duration_roundtrips ) where import Utility.PartialPrelude import Utility.Applicative +import Utility.QuickCheck import Data.Time.Clock.POSIX (POSIXTime) import Data.Char +import Control.Applicative import qualified Data.Map as M newtype Duration = Duration { durationSeconds :: Integer } @@ -37,14 +40,16 @@ parseDuration = Duration <$$> go 0 go (n + num * u) rest fromDuration :: Duration -> String -fromDuration = concat . map showunit . go [] units . durationSeconds +fromDuration Duration { durationSeconds = d } + | d == 0 = "0s" + | otherwise = concat $ map showunit $ go [] units d where showunit (u, n) | n > 0 = show n ++ [u] | otherwise = "" go c [] _ = reverse c - go c ((u, n):us) d = - let (q,r) = d `quotRem` n + go c ((u, n):us) v = + let (q,r) = v `quotRem` n in go ((u, q):c) us r units :: [(Char, Integer)] @@ -70,3 +75,10 @@ hsecs = msecs * 60 msecs :: Integer msecs = 60 + +-- Durations cannot be negative. +instance Arbitrary Duration where + arbitrary = Duration <$> nonNegative arbitrary + +prop_duration_roundtrips :: Duration -> Bool +prop_duration_roundtrips d = parseDuration (fromDuration d) == Just d |