stopwatch 0.1.0 → 0.1.0.1
raw patch · 4 files changed
+57/−15 lines, 4 filesdep ~clockdep ~hspecdep ~transformersPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: clock, hspec, transformers
API changes (from Hackage documentation)
Files
- ChangeLog.md +9/−0
- README.md +8/−0
- stopwatch.cabal +20/−6
- test/Control/StopWatchSpec.hs +20/−9
ChangeLog.md view
@@ -1,5 +1,14 @@ # Revision history for stopwatch +## 0.1.0.1 -- 2016-05-03++* Expand dependency version ranges for `transformers` and `clock`.++* Loosen the test. Now it checks only lower bounds of the measured+ delay, not upper bounds. This is because in some environments, it's+ difficult to ensure the upper bounds.++ ## 0.1.0 -- 2015-09-13 * First version. Released on an unsuspecting world.
README.md view
@@ -8,6 +8,14 @@ cabal configure --enable-tests && cabal build && cabal test ``` +## Important test flag++- `test_delay_upper_bound`: Default: False. If True, the test checks+ the return value of `stopWatch` with upper bounds. If False, it+ doesn't check upper bounds. It checks lower bounds only.+++ ## Author Toshio Ito <debug.ito@gmail.com>
stopwatch.cabal view
@@ -1,5 +1,5 @@ name: stopwatch-version: 0.1.0+version: 0.1.0.1 author: Toshio Ito <debug.ito@gmail.com> maintainer: Toshio Ito <debug.ito@gmail.com> license: BSD3@@ -16,12 +16,13 @@ library default-language: Haskell2010 hs-source-dirs: src- ghc-options: -Wall+ ghc-options: -Wall -fno-warn-unused-imports exposed-modules: Control.StopWatch -- other-modules: build-depends: base >=2 && <6,- transformers >=0.3.0 && <0.5,- clock >=0.5 && <0.6+ transformers >=0.3.0 && <0.6,+ -- clock <0.5 is buggy!+ clock >=0.5 && <0.8 -- executable stopwatch -- default-language: Haskell2010@@ -32,15 +33,28 @@ -- -- other-extensions: -- build-depends: base >=4 && <5 +flag test_delay_upper_bound+ description: Enable test of checking delay upper bound+ default: False++flag test_threaded+ description: Enable -threaded option when building the test+ default: False+ test-suite spec type: exitcode-stdio-1.0 default-language: Haskell2010 hs-source-dirs: test- ghc-options: -Wall+ if flag(test_threaded)+ ghc-options: -Wall -fno-warn-unused-imports "-with-rtsopts=-M512m" -threaded+ else+ ghc-options: -Wall -fno-warn-unused-imports "-with-rtsopts=-M512m"+ if flag(test_delay_upper_bound)+ cpp-options: -DTEST_DELAY_UPPER_BOUND main-is: Spec.hs other-modules: Control.StopWatchSpec build-depends: base, stopwatch,- hspec,+ hspec <2.3, clock source-repository head
test/Control/StopWatchSpec.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE CPP #-} module Control.StopWatchSpec (main, spec) where import Control.Concurrent (threadDelay)@@ -5,21 +6,31 @@ import Test.Hspec import Control.StopWatch (stopWatch)-import System.Clock (sec, nsec)+import System.Clock (TimeSpec(sec, nsec)) main :: IO () main = hspec spec -testStopWatch :: Int -> Int -> Expectation-testStopWatch threshold_range_msec target_msec = do- (_, difftime) <- stopWatch $ threadDelay (target_msec * 1000)- let got_diff_msec = fromIntegral $ (sec difftime) * 1000 + ((nsec difftime) `div` 1000000)- got_diff_msec `shouldSatisfy` (>= target_msec - threshold_range_msec)- got_diff_msec `shouldSatisfy` (<= target_msec + threshold_range_msec)+checker :: Int -> Int -> (String, TimeSpec -> Bool)+checker threshold_range_msec target_msec = (description, checker_func) where+ ts2msec ts = fromIntegral $ (sec ts) * 1000 + ((nsec ts) `div` 1000000)+#ifdef TEST_DELAY_UPPER_BOUND+ description = "target " ++ (show target_msec) ++ "msec and range +- " ++ (show threshold_range_msec) ++ "msec"+ checker_func got = (ts2msec got >= target_msec - threshold_range_msec)+ && (ts2msec got <= target_msec + threshold_range_msec)+#else+ description = "target " ++ (show target_msec) ++ "msec and lower bound -" ++ (show threshold_range_msec) ++ "msec"+ checker_func got = (ts2msec got >= target_msec - threshold_range_msec)+#endif specStopWatch :: Int -> Int -> Spec-specStopWatch trange target = it ("should work for target " ++ (show target) ++ "msec and range " ++ (show trange) ++ "msec") $- testStopWatch trange target+specStopWatch threshold_range_msec target_msec =+ it ("should work for " ++ description) $ do+ (_, difftime) <- stopWatch $ threadDelay (target_msec * 1000)+ difftime `shouldSatisfy` checkerFunc+ where+ (description, checkerFunc) = checker threshold_range_msec target_msec+ spec :: Spec spec = do