From 3c02a47c2e789f987a1c8e826200cc48cb39ac8f Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Sat, 7 Sep 2024 16:44:41 +0100 Subject: [PATCH] add getPrefixList tests --- tests/string-set-tests.mlb | 4 +++ tests/string-set-tests.sml | 62 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/string-set-tests.mlb create mode 100644 tests/string-set-tests.sml diff --git a/tests/string-set-tests.mlb b/tests/string-set-tests.mlb new file mode 100644 index 0000000..087fbbf --- /dev/null +++ b/tests/string-set-tests.mlb @@ -0,0 +1,4 @@ +$(SML_LIB)/basis/basis.mlb + +../src/string-set.sml +string-set-tests.sml diff --git a/tests/string-set-tests.sml b/tests/string-set-tests.sml new file mode 100644 index 0000000..f3111f7 --- /dev/null +++ b/tests/string-set-tests.sml @@ -0,0 +1,62 @@ +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 + + fun run () = + let + val _ = testExists () + val _ = testGetPrefixList () + in + () + end +end + +val _ = StringSetTests.run ()