@Test public void testRandomly() { for (int i = 0; i < 1000; i++) { String pattern = randomPattern(); KmpStringMatcher mat = new KmpStringMatcher(pattern); mat.checkStructure(); for (int j = 0; j < 100; j++) test(pattern, mat, randomText(pattern)); } }
void checkStructure() { if (lsp.length != pattern.length()) throw new AssertionError(); for (int i = 0; i < lsp.length; i++) { if (lsp[i] < 0 || lsp[i] > i) throw new AssertionError(); int j = i; while (j >= 1 && !pattern.substring(0, j).equals(pattern.substring(i + 1 - j, i + 1))) j--; if (lsp[i] != j) throw new AssertionError(); } }
KmpStringMatcher { void checkStructure() { if (lsp.length != pattern.length()) throw new AssertionError(); for (int i = 0; i < lsp.length; i++) { if (lsp[i] < 0 || lsp[i] > i) throw new AssertionError(); int j = i; while (j >= 1 && !pattern.substring(0, j).equals(pattern.substring(i + 1 - j, i + 1))) j--; if (lsp[i] != j) throw new AssertionError(); } } }
KmpStringMatcher { void checkStructure() { if (lsp.length != pattern.length()) throw new AssertionError(); for (int i = 0; i < lsp.length; i++) { if (lsp[i] < 0 || lsp[i] > i) throw new AssertionError(); int j = i; while (j >= 1 && !pattern.substring(0, j).equals(pattern.substring(i + 1 - j, i + 1))) j--; if (lsp[i] != j) throw new AssertionError(); } } KmpStringMatcher(String patt); }
KmpStringMatcher { void checkStructure() { if (lsp.length != pattern.length()) throw new AssertionError(); for (int i = 0; i < lsp.length; i++) { if (lsp[i] < 0 || lsp[i] > i) throw new AssertionError(); int j = i; while (j >= 1 && !pattern.substring(0, j).equals(pattern.substring(i + 1 - j, i + 1))) j--; if (lsp[i] != j) throw new AssertionError(); } } KmpStringMatcher(String patt); int search(String text); }
KmpStringMatcher { void checkStructure() { if (lsp.length != pattern.length()) throw new AssertionError(); for (int i = 0; i < lsp.length; i++) { if (lsp[i] < 0 || lsp[i] > i) throw new AssertionError(); int j = i; while (j >= 1 && !pattern.substring(0, j).equals(pattern.substring(i + 1 - j, i + 1))) j--; if (lsp[i] != j) throw new AssertionError(); } } KmpStringMatcher(String patt); int search(String text);  }