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

41 lines
1.8 KiB
Haskell
Raw Normal View History

2016-06-23 16:52:15 +03:00
{-# LANGUAGE DataKinds #-}
module Data.RandomWalkSimilarity.Spec where
2016-06-28 21:09:51 +03:00
import Category
import Data.DList as DList hiding (toList)
import Data.RandomWalkSimilarity
import qualified Data.Set as Set
2016-06-27 22:37:32 +03:00
import Diff
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
describe "pqGrams" $ do
2016-06-23 17:11:48 +03:00
prop "produces grams with stems of the specified length" . forAll (arbitrary `suchThat` (\ (_, p, q) -> p > 0 && q > 0)) $
2016-06-27 22:32:09 +03:00
\ (term, p, q) -> pqGrams p q identity (toTerm term :: Term Text Text) `shouldSatisfy` all ((== p) . length . stem)
2016-06-23 17:11:48 +03:00
prop "produces grams with bases of the specified length" . forAll (arbitrary `suchThat` (\ (_, p, q) -> p > 0 && q > 0)) $
2016-06-27 22:32:09 +03:00
\ (term, p, q) -> pqGrams p q identity (toTerm term :: Term Text Text) `shouldSatisfy` all ((== q) . length . base)
describe "featureVector" $ do
2016-06-27 22:37:32 +03:00
prop "produces a vector of the specified dimension" . forAll (arbitrary `suchThat` ((> 0) . Prologue.snd)) $
2016-06-27 22:32:09 +03:00
\ (grams, d) -> length (featureVector d (fromList (grams :: [Gram Text]))) `shouldBe` d
2016-06-27 22:37:32 +03:00
describe "rws" $ do
let compare a b = if extract a == extract b then Just (pure (Replace a b)) else Nothing
prop "produces correct diffs" . forAll (scale (`div` 4) arbitrary) $
\ (as, bs) -> let tas = toTerm <$> as
tbs = toTerm <$> bs
diff = free (Free (pure Program :< Indexed (rws compare identity tas tbs :: [Diff Text Category]))) in
(childrenOf <$> beforeTerm diff, childrenOf <$> afterTerm diff) `shouldBe` (Just (Set.fromList tas), Just (Set.fromList tbs))
childrenOf :: (Ord leaf, Ord annotation) => Term leaf annotation -> Set.Set (Term leaf annotation)
2016-06-30 18:28:18 +03:00
childrenOf = Set.fromList . toList . unwrap