diff options
author | Haidong Ji | 2019-03-16 21:52:16 -0500 |
---|---|---|
committer | Haidong Ji | 2019-03-16 21:52:16 -0500 |
commit | b1aea90f00a4bf65253946b9f1017b6c9804535a (patch) | |
tree | ebde562f654c314620da40d1df587e0cfe04c498 /src/test | |
parent | 8fdeec331c424f2a7cfc98619e98a0d2420ddf7c (diff) |
Rabin-Karp string search done!
Fun exercise. Some takeaways:
1. Smallest test case is easy to walk through and verify the logic,
indexing, and such is correct;
2. Persist, don't give up, take it step by step, and you'll reach the goal!
Hooray!
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/HashChainsTest.java | 1 | ||||
-rw-r--r-- | src/test/HashSubstringTest.java | 52 |
2 files changed, 53 insertions, 0 deletions
diff --git a/src/test/HashChainsTest.java b/src/test/HashChainsTest.java index 9a7f192..57255fc 100644 --- a/src/test/HashChainsTest.java +++ b/src/test/HashChainsTest.java @@ -15,5 +15,6 @@ class HashChainsTest { hc.bucketCount = 3; assertEquals(1, hc.hashFunc("add")); assertEquals(1, hc.hashFunc("help")); + assertEquals(2, hc.hashFunc("del")); } }
\ No newline at end of file diff --git a/src/test/HashSubstringTest.java b/src/test/HashSubstringTest.java new file mode 100644 index 0000000..854b329 --- /dev/null +++ b/src/test/HashSubstringTest.java @@ -0,0 +1,52 @@ +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class HashSubstringTest { + @Test + void test() { + HashSubstring.Data data = new HashSubstring.Data("aba", "abacaba"); + assertEquals(2, HashSubstring.getOccurrences(data).size()); + assertEquals(0, HashSubstring.getOccurrences(data).get(0)); + assertEquals(4, HashSubstring.getOccurrences(data).get(1)); + + List<Integer> postions = HashSubstring.rabinKarp("a", "ab"); + assertEquals(1, postions.size()); + assertEquals(0, postions.get(0)); + + postions = HashSubstring.rabinKarp("a", "ba"); + assertEquals(1, postions.size()); + assertEquals(1, postions.get(0)); + + postions = HashSubstring.rabinKarp("aba", "abacaba"); + assertEquals(2, postions.size()); + assertEquals(0, postions.get(0)); + assertEquals(4, postions.get(1)); + } + + @Test + void test1() { + HashSubstring.Data data = new HashSubstring.Data("Test", "testTesttesT"); + assertEquals(1, HashSubstring.getOccurrences(data).size()); + assertEquals(4, HashSubstring.getOccurrences(data).get(0)); + List<Integer> postions = HashSubstring.rabinKarp("Test", "testTesttesT"); + assertEquals(1, postions.size()); + assertEquals(4, postions.get(0)); + } + + @Test + void test2() { + HashSubstring.Data data = new HashSubstring.Data("aaaaa", "baaaaaaa"); + assertEquals(3, HashSubstring.getOccurrences(data).size()); + assertEquals(1, HashSubstring.getOccurrences(data).get(0)); + assertEquals(2, HashSubstring.getOccurrences(data).get(1)); + assertEquals(3, HashSubstring.getOccurrences(data).get(2)); + List<Integer> positions = HashSubstring.rabinKarp("aaaaa", "baaaaaaa"); + assertEquals(3, positions.size()); + assertEquals(1, positions.get(0)); + assertEquals(2, positions.get(1)); + assertEquals(3, positions.get(2)); + } +}
\ No newline at end of file |