From d613c0dc0c35d269fd7da572a608c98079b8c61a Mon Sep 17 00:00:00 2001 From: Sarah O'Reilly Date: Wed, 30 Sep 2020 19:25:09 -0700 Subject: [PATCH 1/5] Add tests for isSubstring, some failing --- .../mills/cs180a/StringUtilitiesTester.java | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/edu/mills/cs180a/StringUtilitiesTester.java b/src/edu/mills/cs180a/StringUtilitiesTester.java index a38617b..ac7a299 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -1,25 +1,35 @@ 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_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_True_Length1Substring() { - assertTrue(StringUtilities.isSubstring("A", "ABC")); - } + @Test + void isSubstring_True_EmptyString() { + assertTrue(StringUtilities.isSubstring("", "foo")); + } + + @ParameterizedTest + @CsvSource(value = {"A, ABC", "BC, ABCDE", "ABCD, ABCD", "D, ABCD", "A, A"}) + void isSubstring_True_ValidSubstring(String substring, String text) { + assertTrue(StringUtilities.isSubstring("A", "ABC")); + } + + @ParameterizedTest + @CsvSource(value = {"Z, ABC", "ABD, ABC", "CBC, ABC", "ABC, FBC"}) + void isSubstring_False_InvalidSubstring(String substring, String text) { + assertFalse(StringUtilities.isSubstring(substring, text)); + } } From fd8c7bfa1a91dd69766a75bfde1523cb34e8a579 Mon Sep 17 00:00:00 2001 From: Sarah O'Reilly Date: Wed, 30 Sep 2020 22:27:46 -0700 Subject: [PATCH 2/5] Fix bug to check for end of text --- src/edu/mills/cs180a/StringUtilities.java | 82 ++++++++++--------- .../mills/cs180a/StringUtilitiesTester.java | 7 +- 2 files changed, 51 insertions(+), 38 deletions(-) diff --git a/src/edu/mills/cs180a/StringUtilities.java b/src/edu/mills/cs180a/StringUtilities.java index 31f03b8..16fa176 100644 --- a/src/edu/mills/cs180a/StringUtilities.java +++ b/src/edu/mills/cs180a/StringUtilities.java @@ -6,50 +6,58 @@ * 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)) + // 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 + if (offset < text.length()) { + 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; + } + if (offset + 1 == text.length() && i + 1 != substring.length()) { + // need to check if we are at the end of the text but not at end of substring + return false; + } + } + return true; + } return false; } - 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 ac7a299..5fd663b 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -28,8 +28,13 @@ void isSubstring_True_ValidSubstring(String substring, String text) { } @ParameterizedTest - @CsvSource(value = {"Z, ABC", "ABD, ABC", "CBC, ABC", "ABC, FBC"}) + @CsvSource(value = {"Z, ABC", "ABD, ABC", "ABC, FBC", "CBC, ABC"}) void isSubstring_False_InvalidSubstring(String substring, String text) { assertFalse(StringUtilities.isSubstring(substring, text)); } + + @Test + void isSubstring_False_CBCinABC() { + assertFalse(StringUtilities.isSubstring("BCD", "ABC")); + } } From 7ac30d3d461015e8ac5a3f8ac6e10a35c2e8aed7 Mon Sep 17 00:00:00 2001 From: Sarah O'Reilly Date: Wed, 30 Sep 2020 22:29:45 -0700 Subject: [PATCH 3/5] Change test name --- src/edu/mills/cs180a/StringUtilitiesTester.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/edu/mills/cs180a/StringUtilitiesTester.java b/src/edu/mills/cs180a/StringUtilitiesTester.java index 5fd663b..c5398fb 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -34,7 +34,7 @@ void isSubstring_False_InvalidSubstring(String substring, String text) { } @Test - void isSubstring_False_CBCinABC() { + void isSubstring_False_SubstringMatchesEndButIsLonger() { assertFalse(StringUtilities.isSubstring("BCD", "ABC")); } } From 961f17b10d601bd24a7694e0e7237f00b4cc17cd Mon Sep 17 00:00:00 2001 From: Sarah O'Reilly Date: Wed, 30 Sep 2020 22:47:06 -0700 Subject: [PATCH 4/5] Add test for getLength() --- src/edu/mills/cs180a/StringUtilities.java | 4 ++-- src/edu/mills/cs180a/StringUtilitiesTester.java | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/edu/mills/cs180a/StringUtilities.java b/src/edu/mills/cs180a/StringUtilities.java index 16fa176..ad8de95 100644 --- a/src/edu/mills/cs180a/StringUtilities.java +++ b/src/edu/mills/cs180a/StringUtilities.java @@ -42,8 +42,8 @@ public static boolean isSubstring(String substring, String text) { private static boolean isSubstringHelper(String substring, String text, int offset) { // i is used as an index for substring, offset is used for text if (offset < text.length()) { - for (int i = 1; // The character with index 0 has already been tested. - i < substring.length() && offset < text.length(); i++, offset++) { + // The character with index 0 has already been tested. + for (int i = 1; i < substring.length() && offset < text.length(); i++, offset++) { if (text.charAt(offset) != substring.charAt(i)) { return false; } diff --git a/src/edu/mills/cs180a/StringUtilitiesTester.java b/src/edu/mills/cs180a/StringUtilitiesTester.java index c5398fb..fb4a4d4 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -1,5 +1,6 @@ package edu.mills.cs180a; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -37,4 +38,9 @@ void isSubstring_False_InvalidSubstring(String substring, String text) { void isSubstring_False_SubstringMatchesEndButIsLonger() { assertFalse(StringUtilities.isSubstring("BCD", "ABC")); } + + @Test + void getLength_returnsLength_ValidString() { + assertEquals(6, StringUtilities.getLength("String")); + } } From 46359b61ff8f559e69e8a3068c258a9c3af5b4cf Mon Sep 17 00:00:00 2001 From: Sarah O'Reilly Date: Wed, 30 Sep 2020 23:30:09 -0700 Subject: [PATCH 5/5] Fix single character bug --- src/edu/mills/cs180a/StringUtilities.java | 6 +++++- src/edu/mills/cs180a/StringUtilitiesTester.java | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/edu/mills/cs180a/StringUtilities.java b/src/edu/mills/cs180a/StringUtilities.java index ad8de95..23e0dc6 100644 --- a/src/edu/mills/cs180a/StringUtilities.java +++ b/src/edu/mills/cs180a/StringUtilities.java @@ -28,7 +28,11 @@ public static boolean isSubstring(String substring, String 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 substring is only one char no need to check further + if (substring.length() == 1) { + return true; + } + // Otherwise, see if the rest of the strings match. if (isSubstringHelper(substring, text, i + 1)) { return true; } diff --git a/src/edu/mills/cs180a/StringUtilitiesTester.java b/src/edu/mills/cs180a/StringUtilitiesTester.java index fb4a4d4..4c195e3 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -25,7 +25,7 @@ void isSubstring_True_EmptyString() { @ParameterizedTest @CsvSource(value = {"A, ABC", "BC, ABCDE", "ABCD, ABCD", "D, ABCD", "A, A"}) void isSubstring_True_ValidSubstring(String substring, String text) { - assertTrue(StringUtilities.isSubstring("A", "ABC")); + assertTrue(StringUtilities.isSubstring(substring, text)); } @ParameterizedTest