Commits

Aleksey Khudyakov  committed caa6f02

Add package with quickcheck instances (not used yet)

  • Participants
  • Parent commits 93bf21b

Comments (0)

Files changed (4)

File histogram-fill-quickcheck/Data/Histogram/QuickCheck.hs

+-- Yes I DO want orphans here
+{-# OPTIONS_GHC -fno-warn-orphans #-}
+{-# LANGUAGE FlexibleInstances #-}
+-- | Arbitrary instances for histogram-fill
+module Data.Histogram.QuickCheck where
+
+import Control.Applicative
+import Test.QuickCheck
+import qualified Data.Vector.Unboxed as U
+
+import Data.Histogram
+import Data.Histogram.Bin.MaybeBin
+
+
+
+----------------------------------------------------------------
+-- Bin instances
+----------------------------------------------------------------
+
+instance Arbitrary BinI where
+  arbitrary = do
+    let maxI = 100
+    lo <- choose (-maxI , maxI)
+    hi <- choose (lo    , maxI)
+    return $ binI lo hi
+
+instance Arbitrary BinInt where
+  arbitrary = do
+    let maxI = 100
+    base <- choose (-maxI,maxI)
+    step <- choose (1,10)
+    n    <- choose (1,1000)
+    return $ BinInt base step n
+
+instance (Arbitrary a, Ord a, Enum a) => Arbitrary (BinEnum a) where
+  arbitrary = do
+    l <- arbitrary
+    h <- suchThat arbitrary (>= l)
+    return $ binEnum l h
+
+instance Arbitrary (BinF Float) where
+  arbitrary = do
+    lo <- choose (-1.0e+3-1 , 1.0e+3)
+    n  <- choose (1, 1000)
+    hi <- choose (lo , 1.0e+3+1)
+    return $ binF lo n hi
+
+instance Arbitrary (BinF Double) where
+  arbitrary = do
+    lo <- choose (-1.0e+6-1 , 1.0e+6)
+    n  <- choose (1, 1000*1000)
+    hi <- choose (lo , 1.0e+6+1)
+    return $ binF lo n hi
+
+instance Arbitrary BinD where
+  arbitrary = do
+    lo <- choose (-1.0e+6-1 , 1.0e+6)
+    n  <- choose (1, 1000*1000)
+    hi <- choose (lo , 1.0e+6+1)
+    return $ binD lo n hi
+
+instance Arbitrary LogBinD where
+  arbitrary = do
+    lo <- choose (1.0e-6 , 1.0e+6)
+    n  <- choose (1, 1000*1000)
+    hi <- choose (lo , 1.0e+6+1)
+    return $ logBinD lo n hi
+
+instance Arbitrary bin => Arbitrary (MaybeBin bin) where
+  arbitrary = MaybeBin <$> arbitrary
+
+instance (Arbitrary bx, Arbitrary by) => Arbitrary (Bin2D bx by) where
+    arbitrary = Bin2D <$> arbitrary <*> arbitrary
+
+----------------------------------------------------------------
+-- Histogram instance
+----------------------------------------------------------------
+
+instance (Bin bin, U.Unbox a, Arbitrary bin, Arbitrary a) => Arbitrary (Histogram bin a) where
+    arbitrary = do
+      bin <- suchThat arbitrary ((<333) . nBins)
+      histogramUO bin <$> arbitrary <*> (U.fromList <$> vectorOf (nBins bin) arbitrary)
+
+
+----------------------------------------------------------------
+-- Arbitrary for bin values
+----------------------------------------------------------------
+
+-- | It's difficult to generate values that will fall into allowed
+--   range of the bin. Simple @inRange x ===> ...@ won't do because QC
+--   will generate large and larger values and eventually will give up.
+class ArbitraryBin bin where
+  -- | Generates arbitrary bin value that lies in range
+  arbitraryBinVal :: bin -> Gen (BinValue bin)
+
+instance ArbitraryBin BinI where
+  arbitraryBinVal bin = choose (lowerLimit bin, lowerLimit bin)
+
+instance (Enum e, Ord e) => ArbitraryBin (BinEnum e) where
+  arbitraryBinVal bin =
+    toEnum <$> choose (fromEnum $ lowerLimit bin, fromEnum $ lowerLimit bin)
+
+instance (ArbitraryBin bX, ArbitraryBin bY) => ArbitraryBin (Bin2D bX bY) where
+  arbitraryBinVal (Bin2D bX bY) =
+    (,) <$> arbitraryBinVal bX <*> arbitraryBinVal bY

File histogram-fill-quickcheck/LICENSE

+Copyright (c)2009-2012, Aleksey Khudyakov
+
+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 asd 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.

File histogram-fill-quickcheck/Setup.hs

+import Distribution.Simple
+main = defaultMain

File histogram-fill-quickcheck/histogram-fill-quickcheck.cabal

+Name:           histogram-fill-quickcheck
+Version:        0.6.2.0
+Synopsis:       QuickCheck instances for histogram-fill
+Description:
+  QuickCheck instances for histogram-fill package
+
+Cabal-Version:  >= 1.6
+License:        BSD3
+License-File:   LICENSE
+Author:         Alexey Khudyakov
+Maintainer:     Alexey Khudyakov <alexey.skladnoy@gmail.com>
+Homepage:       http://bitbucket.org/Shimuuar/histogram-fill-cereal/
+Category:       Data
+Build-Type:     Simple
+
+source-repository head
+  type:     hg
+  location: http://bitbucket.org/Shimuuar/histogram-fill
+source-repository head
+  type:     git
+  location: http://github.com/Shimuuar/histogram-fill
+
+Library
+  Ghc-options:          -Wall
+  Ghc-prof-options:     -auto-all
+  Build-Depends:
+    base           >= 3 && <5,
+    vector         >= 0.7,
+    histogram-fill >= 0.6,
+    QuickCheck     >= 2
+  Exposed-modules:
+    Data.Histogram.QuickCheck