Some tests for KarpRabin

parent b148b947
......@@ -3,6 +3,8 @@
"set": "master",
"source": "https://github.com/np/package-sets.git",
"depends": [
"spec-quickcheck",
"spec-discovery",
"uint",
"js-timers",
"psci-support",
......
module Gargantext.Utils.KarpRabin.Spec where
import Prelude
import Data.Array (index)
import Data.Foldable (all)
import Data.Maybe (Maybe(..), isJust)
import Data.String (drop, stripPrefix, Pattern(..))
import Data.Tuple (Tuple(..))
import Gargantext.Utils.KarpRabin (indicesOfAny)
-- import Test.QuickCheck ((===), (/==), (<?>), Result(..))
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual)
import Test.Spec.QuickCheck (quickCheck')
validIndices :: Array String -> String -> Boolean
validIndices pats input = all validIndex (indicesOfAny pats input)
where
validIndex (Tuple i ps) = all validPat ps
where
input' = drop i input
validPat p =
case index pats p of
Just pat -> isJust (stripPrefix (Pattern pat) input')
-- <?> (show input' <> " should start with " <> show pat)
Nothing -> false -- Failed "out of bounds pattern"
spec :: Spec Unit
spec =
describe "KarpRabin" do
it "works on a single pattern matching two times" do
let pats = ["ab"]
let input = "abcbab"
let output = [Tuple 0 [0], Tuple 4 [0]]
indicesOfAny pats input `shouldEqual` output
it "works on a many unmatching patterns" do
let pats = ["abd","e","bac","abcbabe"]
let input = "abcbab"
let output = []
indicesOfAny pats input `shouldEqual` output
it "works on a simple case" do
let pats = ["ab","cb","bc","bca"]
let input = "abcbab"
let output = [Tuple 0 [0]
,Tuple 1 [2]
,Tuple 2 [1]
,Tuple 4 [0]
]
indicesOfAny pats input `shouldEqual` output
it "works with overlaps" do
let pats = ["aba"]
let input = "ababa"
let output = [Tuple 0 [0]
,Tuple 2 [0]
]
indicesOfAny pats input `shouldEqual` output
it "returns valid indices" do
validIndices ["a","ab","ba","abc","aba","abab","abcde"]
"ababarbabacbbababcaccacabbababa"
`shouldEqual` true
it "returns valid indices 2000 random samples" do
quickCheck' 2000 validIndices
module Test.Main where
import Prelude
--import Control.Monad.Eff (Eff)
--import Control.Monad.Eff.Console (CONSOLE, log)
import Effect (Effect)
import Test.Spec.Discovery (discover)
import Test.Spec.Reporter.Console (consoleReporter)
import Test.Spec.Runner (run)
--main :: forall e. Eff (console :: CONSOLE | e) Unit
--main = do
-- log "You should add some tests."
main :: Effect Unit
main = discover "Gargantext\\..*Spec" >>= run [consoleReporter]
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