Commit eff6caba authored by Hans-Peter Deifel's avatar Hans-Peter Deifel 🐢

tests: Add tests for SumBag

parent 9627b877
...@@ -136,6 +136,7 @@ test-suite spec ...@@ -136,6 +136,7 @@ test-suite spec
, Data.OpenUnionSpec , Data.OpenUnionSpec
, Data.List.UtilsSpec , Data.List.UtilsSpec
, Data.Float.UtilsSpec , Data.Float.UtilsSpec
, Data.SumBagSpec
, Copar.Functors.PowersetSpec , Copar.Functors.PowersetSpec
, Copar.Functors.GroupValuedSpec , Copar.Functors.GroupValuedSpec
, Copar.Functors.BagSpec , Copar.Functors.BagSpec
......
module Data.SumBagSpec (spec) where
import Test.Hspec
import Data.Monoid ( Sum(..) )
import Data.Coerce
import qualified Data.SumBag as SumBag
spec :: Spec
spec = do
insertSpec
deleteSpec
sumSpec
insertSpec :: Spec
insertSpec = describe "insert" $ do
it "works with one element" $ do
SumBag.elem (si 1) (SumBag.insert (si 1) SumBag.empty) `shouldBe` True
it "can handle 100 elements in order" $
let bag = foldr SumBag.insert SumBag.empty (map si [1 .. 100])
in and (map (flip SumBag.elem bag) (coerce @[Int] [1..100])) `shouldBe` True
it "can handle 100 elements in reverse order" $
let bag = foldr SumBag.insert SumBag.empty (reverse $ map si [1 .. 100])
in and (map (flip SumBag.elem bag) (coerce @[Int] [1..100])) `shouldBe` True
it "can handle 100 elements in strange order" $
let bag = foldr SumBag.insert SumBag.empty (map si ([1 .. 50] ++ [100,99..51]))
in and (map (flip SumBag.elem bag) (coerce @[Int] [1..100])) `shouldBe` True
it "works with duplicate elements" $
let bag = iterate (SumBag.insert (si 1)) SumBag.empty !! 10
in SumBag.toAscList bag `shouldBe` (replicate 10 (si 1))
deleteSpec :: Spec
deleteSpec = describe "delete" $ do
it "does nothing on the empty List" $
SumBag.delete (si 1) SumBag.empty `shouldBe` SumBag.empty
it "does nothing when the element is not there" $
let bag = SumBag.fromList (coerce @[Int] [2..10])
in SumBag.delete (si 1) bag `shouldBe` bag
it "deletes a single element" $
SumBag.delete (si 1) (SumBag.singleton (si 1)) `shouldBe` SumBag.empty
it "deletes multiple different elements" $
let bag = SumBag.fromList (coerce @[Int] [1..100])
in foldr SumBag.delete bag (map si [1..50]) `shouldBe` SumBag.fromList (map si [51..100])
it "deletes multiple equal elements" $
let bag = SumBag.fromList (replicate 100 (si 1))
in foldr SumBag.delete bag (replicate 50 (si 1))
`shouldBe` SumBag.fromList (replicate 50 (si 1))
sumSpec :: Spec
sumSpec = describe "sum" $ do
it "sums the empty bag to mempty" $
SumBag.sum @(Sum Int) (SumBag.empty) `shouldBe` mempty
it "computes the correct sum of [1..100]" $
SumBag.sum (SumBag.fromList (map si [1..100])) `shouldBe` Sum 5050
it "computes the correct sum of one hunderd twos" $
SumBag.sum (SumBag.fromList (replicate 100 (si 2))) `shouldBe` Sum 200
si :: Int -> Sum Int
si = Sum @Int
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment