2016-08-05 02:19:03 +03:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2016-06-23 00:59:28 +03:00
|
|
|
module Data.RandomWalkSimilarity.Spec where
|
|
|
|
|
|
|
|
import Data.RandomWalkSimilarity
|
2016-08-05 02:19:03 +03:00
|
|
|
import Data.Record
|
2016-06-27 22:37:32 +03:00
|
|
|
import Diff
|
2016-08-10 16:38:41 +03:00
|
|
|
import Info
|
2016-06-27 22:37:32 +03:00
|
|
|
import Patch
|
2016-06-23 00:59:28 +03:00
|
|
|
import Prologue
|
2016-06-27 22:37:32 +03:00
|
|
|
import Syntax
|
2016-06-23 00:59:28 +03:00
|
|
|
import Term
|
|
|
|
import Term.Arbitrary
|
|
|
|
import Test.Hspec
|
|
|
|
import Test.Hspec.QuickCheck
|
2016-06-27 18:46:22 +03:00
|
|
|
import Test.QuickCheck
|
2016-06-23 00:59:28 +03:00
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = parallel $ do
|
2016-08-10 18:32:42 +03:00
|
|
|
let positively = succ . abs
|
2016-08-09 23:23:38 +03:00
|
|
|
describe "pqGramDecorator" $ do
|
2016-08-10 16:05:12 +03:00
|
|
|
prop "produces grams with stems of the specified length" $
|
|
|
|
\ (term, p, q) -> pqGramDecorator (rhead . headF) (positively p) (positively q) (toTerm term :: Term Text (Record '[Text])) `shouldSatisfy` all ((== (positively p)) . length . stem . rhead)
|
2016-06-23 00:59:28 +03:00
|
|
|
|
2016-08-10 16:05:12 +03:00
|
|
|
prop "produces grams with bases of the specified width" $
|
|
|
|
\ (term, p, q) -> pqGramDecorator (rhead . headF) (positively p) (positively q) (toTerm term :: Term Text (Record '[Text])) `shouldSatisfy` all ((== (positively q)) . length . base . rhead)
|
2016-06-23 00:59:28 +03:00
|
|
|
|
2016-08-09 23:34:10 +03:00
|
|
|
describe "featureVectorDecorator" $ do
|
|
|
|
prop "produces a vector of the specified dimension" $
|
|
|
|
\ (term, p, q, d) -> featureVectorDecorator (rhead . headF) (positively p) (positively q) (positively d) (toTerm term :: Term Text (Record '[Text])) `shouldSatisfy` all ((== (positively d)) . length . rhead)
|
2016-06-27 22:37:32 +03:00
|
|
|
|
|
|
|
describe "rws" $ do
|
2016-08-12 16:53:16 +03:00
|
|
|
let compare a b = if ((==) `on` category . extract) a b then Just (replacing a b) else Nothing
|
2016-08-12 18:59:09 +03:00
|
|
|
let decorate = featureVectorDecorator (category . headF) 2 2 15
|
|
|
|
let toTerm' = decorate . toTerm
|
2016-06-30 19:37:41 +03:00
|
|
|
prop "produces correct diffs" . forAll (scale (`div` 4) arbitrary) $
|
2016-08-11 17:27:11 +03:00
|
|
|
\ (as, bs) -> let tas = toTerm' <$> (as :: [ArbitraryTerm Text (Record '[Category])])
|
|
|
|
tbs = toTerm' <$> (bs :: [ArbitraryTerm Text (Record '[Category])])
|
2016-08-11 19:54:14 +03:00
|
|
|
root = cofree . ((Program .: RNil) :<) . Indexed
|
2016-08-11 19:50:56 +03:00
|
|
|
diff = free (Free (pure (Program .: RNil) :< Indexed (stripDiff <$> rws compare tas tbs))) in
|
2016-08-11 19:54:14 +03:00
|
|
|
(beforeTerm diff, afterTerm diff) `shouldBe` (Just (root (stripTerm <$> tas)), Just (root (stripTerm <$> tbs)))
|
2016-08-11 18:53:51 +03:00
|
|
|
|
2016-08-12 18:59:09 +03:00
|
|
|
it "produces unbiased insertions within branches" $
|
|
|
|
let (a, b) = (decorate (cofree ((StringLiteral .: RNil) :< Indexed [ cofree ((StringLiteral .: RNil) :< Leaf "a") ])), decorate (cofree ((StringLiteral .: RNil) :< Indexed [ cofree ((StringLiteral .: RNil) :< Leaf "b") ]))) in
|
|
|
|
fmap stripDiff (rws compare [ b ] [ a, b ]) `shouldBe` fmap stripDiff [ inserting a, replacing b b ]
|