diff --git a/src/edu/mills/cs180a/StringUtilities.java b/src/edu/mills/cs180a/StringUtilities.java index 31f03b8..b16b2a6 100644 --- a/src/edu/mills/cs180a/StringUtilities.java +++ b/src/edu/mills/cs180a/StringUtilities.java @@ -6,50 +6,56 @@ * Static utility class for testing whether a string is a substring of another string. */ public class StringUtilities { - private 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); + /** + * 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; - } + 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; + 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; } - 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; + // check if substring appears at the given offset in text + private static boolean isSubstringHelper(String substring, String text, int offset) { + if (substring.equals(text)) { + return true; + } + if (substring.length() > 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(); i++, offset++) { + if (text.charAt(offset) != substring.charAt(i)) + return false; + } + return true; } - return true; - } - protected static int getLength(String s) { - return s.length(); - } + 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..59150dc 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -1,25 +1,53 @@ 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)); + } + + @Test + void isSubstring_False_EmptyString() { + assertTrue(StringUtilities.isSubstring("", "foo")); + assertTrue(StringUtilities.isSubstring("", "bar")); + assertTrue(StringUtilities.isSubstring("", "")); + } + + @Test + void isSubstring_True_Length1Substring() { + assertTrue(StringUtilities.isSubstring("A", "ABC")); + } + + @Test + void isSubstring_False_Length1Substring() { + assertFalse(StringUtilities.isSubstring("A", "DEF")); + } + + @Test + void getLength_CorrectLength_NonZeroLengths() { + assertTrue(StringUtilities.getLength("length") == 6); + assertTrue(StringUtilities.getLength("size") == 4); + assertTrue(StringUtilities.getLength("bigstring") == 9); + } + + @ParameterizedTest + @CsvSource({"ABC, ABCD", "AB, ABCD", "A, ABC", "A, A", "ABC, ABC"}) + void isSubstring_True_substring(String substring, String text) { + assertTrue(StringUtilities.isSubstring(substring, text)); + } + + @ParameterizedTest + @CsvSource({"AB, A", "ABCd, ABC", "ASDFGW, ASDF", "SDF, SD", "ABCDE, ABC", "AQ, A"}) + void isSubstring_False_substringLongerThanString(String substring, String text) { + assertFalse(StringUtilities.isSubstring(substring, text)); + } }