packages feed

stopwatch (empty) → 0.1.0

raw patch · 8 files changed

+151/−0 lines, 8 filesdep +basedep +clockdep +hspecsetup-changed

Dependencies added: base, clock, hspec, stopwatch, transformers

Files

+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Revision history for stopwatch++## 0.1.0  -- 2015-09-13++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Toshio Ito++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Toshio Ito nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,13 @@+# stopwatch++A simple stopwatch utility for Haskell.++## How to run tests++```+cabal configure --enable-tests && cabal build && cabal test+```++## Author++Toshio Ito <debug.ito@gmail.com>
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Control/StopWatch.hs view
@@ -0,0 +1,24 @@+-- |+-- Module: Control.StopWatch+-- Description: A simple stopwatch utility+-- Maintainer: Toshio Ito <debug.ito@gmail.com>+--+-- A simple stopwatch utility function(s).+--+module Control.StopWatch (+  stopWatch+) where++import Control.Monad.IO.Class (MonadIO, liftIO)+import System.Clock (TimeSpec, getTime, Clock(Monotonic), diffTimeSpec)++-- | Execute the given computation, measure the time it takes and+-- return the result.+stopWatch :: MonadIO m => m a -> m (a, TimeSpec)+stopWatch act = do+  start <- liftIO $ getTime Monotonic+  ret <- act+  end <- liftIO $ getTime Monotonic+  return (ret, end `diffTimeSpec` start)++-- implementation borrowed from Turtle.Prelude.time (v1.2.1).
+ stopwatch.cabal view
@@ -0,0 +1,48 @@+name:                   stopwatch+version:                0.1.0+author:                 Toshio Ito <debug.ito@gmail.com>+maintainer:             Toshio Ito <debug.ito@gmail.com>+license:                BSD3+license-file:           LICENSE+synopsis:               A simple stopwatch utility+description:            A simple stopwatch utility+category:               Control+cabal-version:          >= 1.10+build-type:             Simple+extra-source-files:     README.md, ChangeLog.md+homepage:               https://github.com/debug-ito/stopwatch+bug-reports:            https://github.com/debug-ito/stopwatch/issues++library+  default-language:     Haskell2010+  hs-source-dirs:       src+  ghc-options:          -Wall+  exposed-modules:      Control.StopWatch+  -- other-modules:+  build-depends:        base >=2 && <6,+                        transformers >=0.3.0 && <0.5,+                        clock >=0.5 && <0.6++-- executable stopwatch+--   default-language:     Haskell2010+--   hs-source-dirs:       src+--   main-is:              Main.hs+--   ghc-options:          -Wall+--   -- other-modules:       +--   -- other-extensions:    +--   build-depends:        base >=4 && <5++test-suite spec+  type:                 exitcode-stdio-1.0+  default-language:     Haskell2010+  hs-source-dirs:       test+  ghc-options:          -Wall+  main-is:              Spec.hs+  other-modules:        Control.StopWatchSpec+  build-depends:        base, stopwatch,+                        hspec,+                        clock++source-repository head+  type:                 git+  location:             https://github.com/debug-ito/stopwatch.git
+ test/Control/StopWatchSpec.hs view
@@ -0,0 +1,28 @@+module Control.StopWatchSpec (main, spec) where++import Control.Concurrent (threadDelay)++import Test.Hspec++import Control.StopWatch (stopWatch)+import System.Clock (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)++specStopWatch :: Int -> Int -> Spec+specStopWatch trange target = it ("should work for target " ++ (show target) ++ "msec and range " ++ (show trange) ++ "msec") $+  testStopWatch trange target++spec :: Spec+spec = do+  describe "stopWatch" $ do+    let trange = 30+    mapM_  (specStopWatch trange) [50, 100, 500, 1000]
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}