/** * substring-tests.c * A quick hack to do a variety of tests with substrings. */ #include #include #include "substring.h" /** * Check all substrings of length minsize or more. */ void check_substrings (char *text, int minsize) { int textlen = strlen(text); char pattern[textlen+1]; // Fill the pattern with terminators for (int i = 0; i <= textlen; i++) { pattern[i] = 0; } // For each nontrivial size for (int size = minsize; size <= textlen; size++) { // For each position for (int i = 0; i <= textlen-size; i++) { // Generate the substring strncpy (pattern, text+i, size); // Make sure that it's at the right place int pos = substring (text, pattern); if (pos != i) { fprintf (stderr, "For substring (\"%s\",\"%s\"), expected %d, got %d\n", text, pattern, i, pos); } // if } // for i } // for size } // check_substrings int main (int argc, char *argv[]) { check_substrings ("Kernighan&Ritchie meet Rabin/Karp IN SamR's CSC 301", 2); check_substrings ("beware the jabberwock, my son", 3); check_substrings ("he took his vorpal sword in hand", 3); check_substrings ("abcdefghijklmnopqrstuvwxyz", 1); return 0; } // main