diff --git a/src/edu/mills/cs180a/StringUtilities.java b/src/edu/mills/cs180a/StringUtilities.java index 31f03b8..e393ed8 100644 --- a/src/edu/mills/cs180a/StringUtilities.java +++ b/src/edu/mills/cs180a/StringUtilities.java @@ -6,50 +6,59 @@ * Static utility class for testing whether a string is a substring of another string. */ public class StringUtilities { - private StringUtilities() {} - - /** - * Tests whether the potential substring is in the full string. The empty string is considered a - * substring of every full string. - * - * @param substring the potential substring - * @param text the full string - * @return true if the substring is contained in the full string, false otherwise - * @throws NullPointerException if either argument is null - */ - public static boolean isSubstring(String substring, String text) { - Objects.requireNonNull(substring); - Objects.requireNonNull(text); - - if (substring.isEmpty()) { - return true; - } + private StringUtilities() {} + + /** + * Tests whether the potential substring is in the full string. The empty string is considered a + * substring of every full string. + * + * @param substring the potential substring + * @param text the full string + * @return true if the substring is contained in the full string, false otherwise + * @throws NullPointerException if either argument is null + */ + public static boolean isSubstring(String substring, String text) { + Objects.requireNonNull(substring); + Objects.requireNonNull(text); - for (int i = 0; i < text.length(); i++) { - // Check if current character of the full string matches start of substring. - if (text.charAt(i) == substring.charAt(0)) { - // If so, see if the rest of the strings match. - if (isSubstringHelper(substring, text, i + 1)) { - return true; + if (substring.isEmpty()) { + return true; + } + + for (int i = 0; i < text.length(); i++) { + // Check if current character of the full string matches start of substring. + if (text.charAt(i) == substring.charAt(0)) { + // If so, see if the rest of the strings match. + + if (isSubstringHelper(substring, text, i + 1)) { + return true; + } + } + // If not, keep iterating through the full string. } - } - // If not, keep iterating through the full string. - } - return false; - } - - // check if substring appears at the given offset in text - private static boolean isSubstringHelper(String substring, String text, int offset) { - // i is used as an index for substring, offset is used for text - for (int i = 1; // The character with index 0 has already been tested. - i < substring.length() && offset < text.length(); i++, offset++) { - if (text.charAt(offset) != substring.charAt(i)) return false; } - return true; - } - protected static int getLength(String s) { - return s.length(); - } + // check if substring appears at the given offset in text + private static boolean isSubstringHelper(String substring, String text, int offset) { + + // check that the substring isn't longer than the remaining text + if ((substring.length() - 1) > (text.length() - offset)) { + return false; + } + + // i is used as an index for substring, offset is used for text + for (int i = 1; // The character with index 0 has already been tested. + i < substring.length() && offset < text.length(); i++, offset++) { + if (text.charAt(offset) != substring.charAt(i)) { + return false; + } + } + return true; + + } + + protected static int getLength(String s) { + return s.length(); + } } diff --git a/src/edu/mills/cs180a/StringUtilitiesTester.java b/src/edu/mills/cs180a/StringUtilitiesTester.java index a38617b..b60c9be 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -1,25 +1,62 @@ package edu.mills.cs180a; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; class StringUtilitiesTester { - @Test - void isSubstring_False_null() { - assertThrows(NullPointerException.class, () -> StringUtilities.isSubstring(null, "foo")); - assertThrows(NullPointerException.class, () -> StringUtilities.isSubstring("foo", null)); - assertThrows(NullPointerException.class, () -> StringUtilities.isSubstring(null, null)); - } - - @Test - void isSubstring_False_EmptyString() { - assertTrue(StringUtilities.isSubstring("", "foo")); - // TODO: write rest - } - - @Test - void isSubstring_True_Length1Substring() { - assertTrue(StringUtilities.isSubstring("A", "ABC")); - } + @Test + void isSubstring_False_null() { + assertThrows(NullPointerException.class, () -> StringUtilities.isSubstring(null, "foo")); + assertThrows(NullPointerException.class, () -> StringUtilities.isSubstring("foo", null)); + assertThrows(NullPointerException.class, () -> StringUtilities.isSubstring(null, null)); + } + + @ParameterizedTest + @CsvSource({"'',''", "'',' '", "'',' '", "'',XYZ", "'',X Z", "'', XYZ", "'',XYZ "}) + void isSubstring_True_EmptyString(String emptySubstring, String text) { + assertTrue(StringUtilities.isSubstring(emptySubstring, text)); + } + + @ParameterizedTest + @CsvSource({"' ',' '", "' ',' '", "' ','x z'", "' ','xyz '", "' ',' xyz'", "x,xyz", "y,xyz", + "z,xyz"}) + void isSubstring_True_Length1Substring(String substring, String text) { + assertTrue(StringUtilities.isSubstring(substring, text)); + } + + @ParameterizedTest + @CsvSource({"' ',''", "X,xyz", "x,XYZ", "x,''", "a,xyz"}) + void isSubstring_False_Length1Substring(String substring, String text) { + assertFalse(StringUtilities.isSubstring(substring, text)); + } + + @ParameterizedTest + @CsvSource({"' ',' '", "' ',' '", "xy,xyz", "yz,xyz", "ab, ab", "AB,AB"}) + void isSubstring_True_Length2Substring(String substring, String text) { + assertTrue(StringUtilities.isSubstring(substring, text)); + } + + @ParameterizedTest + @CsvSource({"' ',''", "' ',' '", "xz,xyz", "Xy,xyz", "xY,xyz", "XY,xyz", "xy,XYZ", + "ba,ab", "ab,AB", "ab,a"}) + void isSubstring_False_Length2Substring(String substring, String text) { + assertFalse(StringUtilities.isSubstring(substring, text)); + } + + @ParameterizedTest + @CsvSource({"' ',' '", "xyz,xyz", "xyz,' xyz '", "XYZ,XYZ"}) + void isSubstring_True_Length3Substring(String substring, String text) { + assertTrue(StringUtilities.isSubstring(substring, text)); + } + + @ParameterizedTest + @CsvSource({"' ',''", "' ',' '", "' ',' '", "yzx,xyz", "Xyz,xyz", "xYz,xyz", "XYZ,xyz", + "xyz,XYZ", "XyZ,xYz", "xyZ,xyz"}) + void isSubstring_False_Length3Substring(String substring, String text) { + assertFalse(StringUtilities.isSubstring(substring, text)); + } }