Skip to content

Commit

Permalink
Adding some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brenden committed Apr 23, 2017
1 parent 2a4c0d1 commit 9e06b96
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Tests.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module Tests (..) where

import Debug
import Array
import Html exposing (..)
import List exposing (..)
import String
import UkkonenTree exposing (..)
import UkkonenAlgorithm exposing (..)


main =
let
testWords =
[ "banana"
, "tctcatcaa#ggaaccattg@tccatctcgc"
, "abacabadabacabae"
, "asdfsfasdfasdfx"
, "aaaaaaa"
, "abababa"
, "abcabxabcd"
, "abcabcdefbcabcd"
, "abcabcdefhabcabcdw"
, "abcabcdefbcabcd"
]
in
text <|
if (all checkValidSuffixTree testWords) then
"All tests passed."
else
"Some tests failed. Check console for details."


checkValidSuffixTree : String -> Bool
checkValidSuffixTree input =
let
finalState =
List.head <| List.reverse <| UkkonenAlgorithm.steps (input ++ "$")
in
case finalState of
Just finalState ->
let
suffixes =
Debug.log "expected" <| List.sort <| suffixesOfString input

suffixesFromTree =
Debug.log " actual" <|
List.sort <|
UkkonenTree.suffixes
finalState.tree
0
(String.fromList <| Array.toList <| finalState.string)
in
Debug.log "matches" <| suffixes == List.sort suffixesFromTree

Nothing ->
False


suffixesOfString : String -> List String
suffixesOfString string =
let
len =
String.length string
in
List.map
(\n -> String.right n string)
[0..len]
34 changes: 34 additions & 0 deletions UkkonenTree.elm
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,37 @@ toString' level rootId tree =
newLine =
"""
"""


{- Get the string an edge represents -}
edgeString : UkkonenEdge -> String -> String
edgeString edge string =
let labelEnd = case edge.labelEnd of
Definite val -> val
EndOfString -> -1
in
slice
edge.labelStart
labelEnd
string


{-| Gets all suffixes represented in the tree
-}
suffixes : UkkonenTree -> NodeId -> String -> List String
suffixes tree rootId string =
let
root = getNode rootId tree
in
if Dict.size root.edges == 0 then
[""]
else
List.concatMap
(\ edge ->
let edgeLabel = edgeString edge string
in
List.map
(\ childSuffix -> edgeLabel ++ childSuffix)
(suffixes tree edge.pointingTo string)
)
(Dict.values root.edges)

0 comments on commit 9e06b96

Please sign in to comment.