File: Timestamp.hs

package info (click to toggle)
haskell-cabal-install 3.10.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 3,400 kB
  • sloc: haskell: 52,202; sh: 80; makefile: 9
file content (61 lines) | stat: -rw-r--r-- 1,866 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module UnitTests.Distribution.Client.IndexUtils.Timestamp (tests) where

import Distribution.Parsec (simpleParsec)
import Distribution.Pretty (prettyShow)
import Data.Time
import Data.Time.Clock.POSIX

import Distribution.Client.IndexUtils.Timestamp

import Test.Tasty
import Test.Tasty.QuickCheck

tests :: [TestTree]
tests =
    [ testProperty "Timestamp1" prop_timestamp1
    , testProperty "Timestamp2" prop_timestamp2
    , testProperty "Timestamp3" prop_timestamp3
    , testProperty "Timestamp4" prop_timestamp4
    , testProperty "Timestamp5" prop_timestamp5
    ]

-- test unixtime format parsing
prop_timestamp1 :: NonNegative Int -> Bool
prop_timestamp1 (NonNegative t0) = Just t == simpleParsec ('@':show t0)
  where
    t = toEnum t0 :: Timestamp

-- test prettyShow/simpleParse roundtrip
prop_timestamp2 :: Int -> Bool
prop_timestamp2 t0
  | t /= nullTimestamp  = simpleParsec (prettyShow t) == Just t
  | otherwise           = prettyShow t == ""
  where
    t = toEnum t0 :: Timestamp

-- test prettyShow against reference impl
prop_timestamp3 :: Int -> Bool
prop_timestamp3 t0
  | t /= nullTimestamp  = refDisp t == prettyShow t
  | otherwise           = prettyShow t == ""
  where
    t = toEnum t0 :: Timestamp

    refDisp = maybe undefined (formatTime undefined "%FT%TZ")
              . timestampToUTCTime

-- test utcTimeToTimestamp/timestampToUTCTime roundtrip
prop_timestamp4 :: Int -> Bool
prop_timestamp4 t0
  | t /= nullTimestamp  = (utcTimeToTimestamp =<< timestampToUTCTime t) == Just t
  | otherwise           = timestampToUTCTime t == Nothing
  where
    t = toEnum t0 :: Timestamp

prop_timestamp5 :: Int -> Bool
prop_timestamp5 t0
  | t /= nullTimestamp = timestampToUTCTime t == Just ut
  | otherwise          = timestampToUTCTime t == Nothing
  where
    t = toEnum t0 :: Timestamp
    ut = posixSecondsToUTCTime (fromIntegral t0)