From 033ddfacfb4f9f0a1d6e4bf69f0a843544e3520f Mon Sep 17 00:00:00 2001 From: Katrina Eaton Date: Sat, 31 Oct 2020 01:05:45 -0700 Subject: [PATCH 1/4] Add tests for isSubstring(), some of which fail --- .../mills/cs180a/StringUtilitiesTester.java | 72 ++++++++++++++----- 1 file changed, 55 insertions(+), 17 deletions(-) diff --git a/src/edu/mills/cs180a/StringUtilitiesTester.java b/src/edu/mills/cs180a/StringUtilitiesTester.java index a38617b..e9db56b 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -1,25 +1,63 @@ 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", "'',race car"}) + void isSubstring_True_EmptyString(String emptySubstring, String text) { + assertTrue(StringUtilities.isSubstring(emptySubstring, text)); + // TODO: write rest + } + + @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"}) + 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)); + } } From f59d8e0f22ed6797d439470c0e825a729fa4c50a Mon Sep 17 00:00:00 2001 From: Katrina Eaton Date: Sat, 31 Oct 2020 01:38:27 -0700 Subject: [PATCH 2/4] Add edge cases for isSubstring() tests, some failing --- src/edu/mills/cs180a/StringUtilitiesTester.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/edu/mills/cs180a/StringUtilitiesTester.java b/src/edu/mills/cs180a/StringUtilitiesTester.java index e9db56b..d40d001 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -16,10 +16,9 @@ void isSubstring_False_null() { } @ParameterizedTest - @CsvSource({"'',''", "'',' '", "'',' '", "'',XYZ", "'',race car"}) + @CsvSource({"'',''", "'',' '", "'',' '", "'',XYZ", "'',X Z", "'', XYZ", "'',XYZ "}) void isSubstring_True_EmptyString(String emptySubstring, String text) { assertTrue(StringUtilities.isSubstring(emptySubstring, text)); - // TODO: write rest } @ParameterizedTest @@ -49,7 +48,7 @@ void isSubstring_False_Length2Substring(String substring, String text) { } @ParameterizedTest - @CsvSource({"' ',' '", "xyz,xyz", "XYZ,XYZ"}) + @CsvSource({"' ',' '", "xyz,xyz", "xyz,' xyz '", "XYZ,XYZ"}) void isSubstring_True_Length3Substring(String substring, String text) { assertTrue(StringUtilities.isSubstring(substring, text)); } From c345f8b7e7fe511622adfabadf14bc4718328499 Mon Sep 17 00:00:00 2001 From: Katrina Eaton Date: Mon, 2 Nov 2020 13:23:52 -0800 Subject: [PATCH 3/4] debug and fix isSubstringHelper() --- src/edu/mills/cs180a/StringUtilities.java | 95 +++++++++++-------- .../mills/cs180a/StringUtilitiesTester.java | 2 +- 2 files changed, 55 insertions(+), 42 deletions(-) diff --git a/src/edu/mills/cs180a/StringUtilities.java b/src/edu/mills/cs180a/StringUtilities.java index 31f03b8..7c217cd 100644 --- a/src/edu/mills/cs180a/StringUtilities.java +++ b/src/edu/mills/cs180a/StringUtilities.java @@ -6,50 +6,63 @@ * 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 offset isn't longer than the text string + if (offset > text.length()) { + return false; + } + + // 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 d40d001..b60c9be 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -22,7 +22,7 @@ void isSubstring_True_EmptyString(String emptySubstring, String text) { } @ParameterizedTest - @CsvSource({"' ',' '", "' ',' '", "' ',x z", "' ',xyz ", "' ', xyz", "x,xyz", "y,xyz", + @CsvSource({"' ',' '", "' ',' '", "' ','x z'", "' ','xyz '", "' ',' xyz'", "x,xyz", "y,xyz", "z,xyz"}) void isSubstring_True_Length1Substring(String substring, String text) { assertTrue(StringUtilities.isSubstring(substring, text)); From 656aec8b4c94f0bed4c0d458910f01b08ada483a Mon Sep 17 00:00:00 2001 From: Katrina Eaton Date: Tue, 3 Nov 2020 03:50:57 -0800 Subject: [PATCH 4/4] Remove redundant substring/test length check --- src/edu/mills/cs180a/StringUtilities.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/edu/mills/cs180a/StringUtilities.java b/src/edu/mills/cs180a/StringUtilities.java index 7c217cd..e393ed8 100644 --- a/src/edu/mills/cs180a/StringUtilities.java +++ b/src/edu/mills/cs180a/StringUtilities.java @@ -41,10 +41,6 @@ public static boolean isSubstring(String substring, String text) { // check if substring appears at the given offset in text private static boolean isSubstringHelper(String substring, String text, int offset) { - // check that the offset isn't longer than the text string - if (offset > text.length()) { - return false; - } // check that the substring isn't longer than the remaining text if ((substring.length() - 1) > (text.length() - offset)) {