diff --git a/psc-package.json b/psc-package.json index 02a645084c7441ac1383012df17f07792f413432..ed5da2f2d1a1934f0aa1c691f0a14d34e4793e05 100644 --- a/psc-package.json +++ b/psc-package.json @@ -3,6 +3,8 @@ "set": "master", "source": "https://github.com/np/package-sets.git", "depends": [ + "spec-quickcheck", + "spec-discovery", "uint", "js-timers", "psci-support", diff --git a/test/Gargantext/Utils/KarpRabin/Spec.purs b/test/Gargantext/Utils/KarpRabin/Spec.purs new file mode 100644 index 0000000000000000000000000000000000000000..76209dc9a6dd923e18639b48eea52398ff7d3e35 --- /dev/null +++ b/test/Gargantext/Utils/KarpRabin/Spec.purs @@ -0,0 +1,66 @@ +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 diff --git a/test/Main.purs b/test/Main.purs index 4541a2e6f5cfd2cf3569c9e5b938ceaa6b5a7734..aad4aedbfe1ab28a4aea2f4d59b79881a21364af 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -1,9 +1,10 @@ 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]