1
1
mirror of https://github.com/github/semantic.git synced 2024-12-22 14:21:31 +03:00
semantic/test/Data/RandomWalkSimilarity/Spec.hs

49 lines
2.6 KiB
Haskell
Raw Normal View History

{-# LANGUAGE DataKinds #-}
module Data.RandomWalkSimilarity.Spec where
import Data.RandomWalkSimilarity
import Data.Record
2016-06-27 22:37:32 +03:00
import Diff
import Info
2016-06-27 22:37:32 +03:00
import Patch
import Prologue
2016-06-27 22:37:32 +03:00
import Syntax
import Term
import Term.Arbitrary
import Test.Hspec
import Test.Hspec.QuickCheck
2016-06-27 18:46:22 +03:00
import Test.QuickCheck
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
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)
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-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
let compare a b = if ((==) `on` category . extract) a b then Just (pure (Replace a b)) else Nothing
let toTerm' = featureVectorDecorator (category . headF) 2 2 15 . toTerm
prop "produces correct diffs" . forAll (scale (`div` 4) arbitrary) $
\ (as, bs) -> let tas = toTerm' <$> (as :: [ArbitraryTerm Text (Record '[Category])])
tbs = toTerm' <$> (bs :: [ArbitraryTerm Text (Record '[Category])])
root = cofree . ((Program .: RNil) :<) . Indexed
diff = free (Free (pure (Program .: RNil) :< Indexed (stripDiff <$> rws compare tas tbs))) in
(beforeTerm diff, afterTerm diff) `shouldBe` (Just (root (stripTerm <$> tas)), Just (root (stripTerm <$> tbs)))
2016-08-11 18:53:51 +03:00
let toTerm'' c (ArbitraryTerm r f) = toTerm' (ArbitraryTerm (setCategory r c) f)
2016-08-11 18:53:51 +03:00
prop "produces unbiased deletions" $
\ a b c -> let (a', b') = (toTerm'' c a, toTerm'' c (b :: ArbitraryTerm Text (Record '[Category]))) in
fmap stripDiff (rws compare [ a', b' ] [ a' ]) `shouldBe` fmap stripDiff (reverse (rws compare [ b', a' ] [ a' ]))
2016-08-11 18:53:51 +03:00
prop "produces unbiased insertions" $
\ a b c -> let (a', b') = (toTerm'' c a, toTerm'' c (b :: ArbitraryTerm Text (Record '[Category]))) in
fmap stripDiff (rws compare [ a' ] [ a', b' ]) `shouldBe` fmap stripDiff (reverse (rws compare [ a' ] [ b', a' ]))