2024-09-07 16:44:41 +01:00
|
|
|
structure StringSetTests =
|
|
|
|
|
struct
|
|
|
|
|
fun assertTrue (isTrue, msg) =
|
|
|
|
|
if isTrue then ()
|
|
|
|
|
else
|
|
|
|
|
(print (msg ^ "\n"); raise Empty)
|
|
|
|
|
|
|
|
|
|
fun assertFalse (isFalse, msg) = assertTrue (not isFalse, msg)
|
|
|
|
|
|
|
|
|
|
(* below tests ported from https://github.com/kpol/trie/blob/master/src/KTrie.Tests/TrieTests.cs *)
|
|
|
|
|
|
|
|
|
|
fun testExists () =
|
|
|
|
|
let
|
|
|
|
|
val trie = StringSet.fromList ["abc", "abde"]
|
|
|
|
|
|
|
|
|
|
val _ = assertTrue (StringSet.exists ("abc", trie), "abc should exist")
|
|
|
|
|
val _ = assertTrue (StringSet.exists ("abde", trie), "abde should exist")
|
|
|
|
|
|
|
|
|
|
val _ = assertFalse (StringSet.exists ("a", trie), "a should not exist")
|
|
|
|
|
val _ = assertFalse (StringSet.exists ("ab", trie), "ab should not exist")
|
|
|
|
|
val _ = assertFalse (StringSet.exists ("abcd", trie), "abcd should not exist")
|
|
|
|
|
val _ = assertFalse (StringSet.exists ("abce", trie), "abce should not exist")
|
|
|
|
|
val _ = assertFalse (StringSet.exists ("x", trie), "x should not exist")
|
|
|
|
|
in
|
|
|
|
|
print "StringSet.exists passed all tests\n"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun testGetPrefixList () =
|
|
|
|
|
let
|
|
|
|
|
val trie = StringSet.fromList ["abc", "abde", "abx", "abxx"]
|
|
|
|
|
|
|
|
|
|
val aMatches = StringSet.getPrefixList ("a", trie) = ["abc", "abde", "abx", "abxx"]
|
|
|
|
|
val _ = assertTrue (aMatches, "a matches")
|
|
|
|
|
|
|
|
|
|
val xMatches = StringSet.getPrefixList ("x", trie) = []
|
|
|
|
|
val _ = assertTrue (xMatches, "x matches")
|
|
|
|
|
|
|
|
|
|
val abMatches = StringSet.getPrefixList ("ab", trie) = ["abc", "abde", "abx", "abxx"]
|
|
|
|
|
val _ = assertTrue (abMatches, "ab matches")
|
|
|
|
|
|
|
|
|
|
val abcMatches = StringSet.getPrefixList ("abc", trie) = ["abc"]
|
|
|
|
|
val _ = assertTrue (abcMatches, "abc matches")
|
|
|
|
|
|
|
|
|
|
val abccMatches = StringSet.getPrefixList ("abcc", trie) = []
|
|
|
|
|
val _ = assertTrue (abccMatches, "abcc matches")
|
|
|
|
|
|
|
|
|
|
val abxMatches = StringSet.getPrefixList ("abx", trie) = ["abx", "abxx"]
|
|
|
|
|
val _ = assertTrue (abxMatches, "abx matches")
|
|
|
|
|
in
|
|
|
|
|
print "StringSet.getPrefixList passed all tests\n"
|
|
|
|
|
end
|
|
|
|
|
|
2024-09-07 16:50:31 +01:00
|
|
|
fun remove1 () =
|
2024-09-07 20:56:21 +01:00
|
|
|
let
|
|
|
|
|
val trie = StringSet.fromList ["a", "ab", "abc"]
|
|
|
|
|
|
|
|
|
|
val _ = assertTrue (StringSet.exists ("a", trie), "a exists before remove1")
|
|
|
|
|
|
|
|
|
|
val trie = StringSet.remove ("a", trie)
|
|
|
|
|
val _ = assertFalse (StringSet.exists ("a", trie), "a does not exist after remove1")
|
|
|
|
|
|
|
|
|
|
val _ = assertTrue (StringSet.exists ("ab", trie), "ab still exists after remove1")
|
|
|
|
|
val _ = assertTrue (StringSet.exists ("abc", trie), "abc still exists after remove1")
|
|
|
|
|
in
|
|
|
|
|
print "StringSet.remove: passed remove1\n"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun remove2 () =
|
|
|
|
|
let
|
|
|
|
|
val trie = StringSet.fromList ["a", "ab", "abc", "abd"]
|
|
|
|
|
|
|
|
|
|
val _ = assertTrue (StringSet.exists ("ab", trie), "ab exists before remove2")
|
|
|
|
|
val trie = StringSet.remove ("ab", trie)
|
|
|
|
|
val _ = assertFalse (StringSet.exists ("ab", trie), "ab no longer exists after remove2")
|
|
|
|
|
|
|
|
|
|
val _ = assertTrue (StringSet.exists ("a", trie), "remove2 contains a")
|
|
|
|
|
val _ = assertTrue (StringSet.exists ("abc", trie), "remove2 contains abc")
|
|
|
|
|
val _ = assertTrue (StringSet.exists ("abd", trie), "remove2 contains abd")
|
|
|
|
|
in
|
|
|
|
|
print "StringSet.remove: passed remove2\n"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun remove3 () =
|
|
|
|
|
let
|
|
|
|
|
val trie = StringSet.fromList ["abc"]
|
|
|
|
|
|
|
|
|
|
val _ = assertTrue (StringSet.exists ("abc", trie), "abc exists before remove3")
|
|
|
|
|
val trie = StringSet.remove ("abc", trie)
|
|
|
|
|
val _ = assertFalse (StringSet.exists ("abc", trie), "abc no longer exists after remove3")
|
|
|
|
|
|
|
|
|
|
val _ = assertTrue (StringSet.isEmpty trie, "trie is empty after remove3")
|
2024-09-07 16:50:31 +01:00
|
|
|
|
2024-09-07 20:56:21 +01:00
|
|
|
val trie = StringSet.insert ("abc", trie)
|
|
|
|
|
val _ = assertTrue (StringSet.exists ("abc", trie), "abc exists after insertion in remove3")
|
|
|
|
|
in
|
|
|
|
|
print "StringSet.remove: passed remove3\n"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun remove4 () =
|
|
|
|
|
let
|
|
|
|
|
val trie = StringSet.fromList ["abc", "abcd"]
|
|
|
|
|
val _ = assertTrue (StringSet.exists ("abc", trie), "abc exists before remove4")
|
|
|
|
|
|
|
|
|
|
val trie = StringSet.remove ("abc", trie)
|
|
|
|
|
val _ = assertTrue (StringSet.exists ("abcd", trie), "abcd still exists after removing abc in remove4")
|
|
|
|
|
val _ = assertFalse (StringSet.exists ("abc", trie), "abc doesn't exist after remove in remove4")
|
|
|
|
|
in
|
|
|
|
|
print "StringSet.remove: passed remove4\n"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun remove5 () =
|
|
|
|
|
let
|
|
|
|
|
val trie = StringSet.fromList ["abc", "ab", "ade", "abcde", "x"]
|
|
|
|
|
|
|
|
|
|
val trie2 = StringSet.remove ("xy", trie)
|
|
|
|
|
val _ = assertTrue (trie = trie2, "removing key (xy) which doesn't exist in trie returns same trie")
|
|
|
|
|
|
|
|
|
|
val trie3 = StringSet.remove ("abcd", trie)
|
|
|
|
|
val _ = assertTrue (trie = trie3, "removing key (abcd) which doesn't exist in trie returns same trie")
|
|
|
|
|
|
|
|
|
|
val _ = assertTrue (StringSet.exists ("abcde", trie), "abcde exists before remove in remove5")
|
|
|
|
|
val trie = StringSet.remove ("abcde", trie)
|
|
|
|
|
val _ = assertFalse (StringSet.exists ("abcde", trie), "abcde does not exist after remove in remove5")
|
2024-09-07 16:50:31 +01:00
|
|
|
|
2024-09-07 20:56:21 +01:00
|
|
|
val _ = assertTrue (StringSet.exists ("x", trie), "x exists before remove in remove5")
|
|
|
|
|
val trie = StringSet.remove ("x", trie)
|
2024-09-07 21:35:31 +01:00
|
|
|
val _ = assertFalse (StringSet.exists ("x", trie), "x does not exist after remove in remove5")
|
2024-09-07 16:50:31 +01:00
|
|
|
|
2024-09-07 20:56:21 +01:00
|
|
|
val _ = assertTrue (StringSet.exists ("abc", trie), "abc exists before remove in remove5")
|
|
|
|
|
val trie = StringSet.remove ("abc", trie)
|
2024-09-07 21:35:31 +01:00
|
|
|
val _ = assertFalse (StringSet.exists ("abc", trie), "abc does not exist after remove in remove5")
|
2024-09-07 20:56:21 +01:00
|
|
|
|
|
|
|
|
val _ = assertTrue (StringSet.exists ("ab", trie), "trie still contains ab after removals in remove5")
|
|
|
|
|
val _ = assertTrue (StringSet.exists ("ade", trie), "trie still contains ade after removals in remove5")
|
|
|
|
|
in
|
|
|
|
|
print "StringSet.remove: passed remove5\n"
|
|
|
|
|
end
|
2024-09-07 16:50:31 +01:00
|
|
|
|
2024-09-07 16:44:41 +01:00
|
|
|
fun run () =
|
|
|
|
|
let
|
|
|
|
|
val _ = testExists ()
|
|
|
|
|
val _ = testGetPrefixList ()
|
2024-09-07 20:56:21 +01:00
|
|
|
|
2024-09-07 16:50:31 +01:00
|
|
|
val _ = remove1 ()
|
2024-09-07 20:56:21 +01:00
|
|
|
val _ = remove2 ()
|
|
|
|
|
val _ = remove3 ()
|
|
|
|
|
val _ = remove4 ()
|
|
|
|
|
val _ = remove5 ()
|
2024-09-07 16:44:41 +01:00
|
|
|
in
|
|
|
|
|
()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
val _ = StringSetTests.run ()
|