From 00ee5fa9a7f4a7d6333f1bf929a43f0e7402aa58 Mon Sep 17 00:00:00 2001 From: Makie Date: Sat, 3 Oct 2020 05:49:29 -0700 Subject: [PATCH 1/4] Add test of isSubstring(), some of which fail --- src/edu/mills/cs180a/StringUtilities.java | 83 ++++++++++--------- .../mills/cs180a/StringUtilitiesTester.java | 66 +++++++++++---- 2 files changed, 91 insertions(+), 58 deletions(-) diff --git a/src/edu/mills/cs180a/StringUtilities.java b/src/edu/mills/cs180a/StringUtilities.java index 31f03b8..819e201 100644 --- a/src/edu/mills/cs180a/StringUtilities.java +++ b/src/edu/mills/cs180a/StringUtilities.java @@ -6,50 +6,51 @@ * 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, Check the rest of the strings match. + if (isSubstringHelper(substring, text, i + 1)) { + return true; + } + } } - } - // 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(); - } + private static boolean isSubstringHelper(String substring, String text, int offset) { + // Return false if substring length is greater than the rest of the text + if ((substring.length() - 1) > (text.length() - offset)) + return false; + + // check if substring appears at the given offset in text + for (int i = 1; i < substring.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..4055dbb 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -1,25 +1,57 @@ package edu.mills.cs180a; +import static org.junit.Assert.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")); + // TODO: write rest + } + + @Test + void isSubstring_True_Length1Substring() { + assertTrue(StringUtilities.isSubstring("A", "ABC")); + } + + @ParameterizedTest + @CsvSource({"A, BAAA", "B,ZXCVB", "E,qwqwqwqwqwqwE", "F,asdFFF"}) + void isSubstring_True_ContainSubstring(String substring, String Text) { + assertTrue(StringUtilities.isSubstring(substring, Text)); + } + + @Test + void isSubstring_True_LastLengthSubstring() { + assertTrue(StringUtilities.isSubstring("A", "XYZA")); + } + + @ParameterizedTest + @CsvSource({"X,ZABC", "X,ZABC", "9,ZABC"}) + void isSubstring_False_NotContainSingleSubstring(String substring, String Text) { + assertFalse(StringUtilities.isSubstring(substring, Text)); + } + + @ParameterizedTest + @CsvSource({"XZ, ZABC", "ZX, ZABC", "CX, ZABC", "AC, ZABC"}) + void isSubstring_False_NotContain2Substring(String substring, String Text) { + assertFalse(StringUtilities.isSubstring(substring, Text)); + } + + @ParameterizedTest + @CsvSource({"ZABCDE, ZABC", "ABCD, ZABC", "BCEFG, ZABC"}) + void isSubstring_False_LargeSubstring(String substring, String Text) { + assertFalse(StringUtilities.isSubstring(substring, Text)); + } } From 20a2a90dbd3aed5897c5559d7155b48cd83bc7d5 Mon Sep 17 00:00:00 2001 From: Makie Date: Sat, 3 Oct 2020 06:53:09 -0700 Subject: [PATCH 2/4] Add test to improve coverage --- src/edu/mills/cs180a/StringUtilities.java | 12 ++++++------ src/edu/mills/cs180a/StringUtilitiesTester.java | 6 ++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/edu/mills/cs180a/StringUtilities.java b/src/edu/mills/cs180a/StringUtilities.java index 819e201..8ae120e 100644 --- a/src/edu/mills/cs180a/StringUtilities.java +++ b/src/edu/mills/cs180a/StringUtilities.java @@ -24,11 +24,11 @@ public static boolean isSubstring(String substring, String text) { 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. + // Check current character of the full string matches start of substring. if (text.charAt(i) == substring.charAt(0)) { - - // If so, Check the rest of the strings match. + // Check the rest of the strings matches substring without first character. if (isSubstringHelper(substring, text, i + 1)) { return true; } @@ -38,11 +38,11 @@ public static boolean isSubstring(String substring, String text) { } private static boolean isSubstringHelper(String substring, String text, int offset) { - // Return false if substring length is greater than the rest of the text - if ((substring.length() - 1) > (text.length() - offset)) + // Return false if substring length is greater than the rest of the text. + if ((getLength(substring) - 1) > (getLength(text) - offset)) return false; - // check if substring appears at the given offset in text + // check substring appears at the given offset in text. for (int i = 1; i < substring.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 4055dbb..ed2bc22 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -49,6 +49,12 @@ void isSubstring_False_NotContain2Substring(String substring, String Text) { assertFalse(StringUtilities.isSubstring(substring, Text)); } + @ParameterizedTest + @CsvSource({"ZABC, ZABC", "ABC, ZABC", "BCEFG, ZABCEFGHIFK"}) + void isSubstring_False_MatchSubstring(String substring, String Text) { + assertTrue(StringUtilities.isSubstring(substring, Text)); + } + @ParameterizedTest @CsvSource({"ZABCDE, ZABC", "ABCD, ZABC", "BCEFG, ZABC"}) void isSubstring_False_LargeSubstring(String substring, String Text) { From 2ef25e99953424855cd1a97e7af822734a44d304 Mon Sep 17 00:00:00 2001 From: Makie Date: Sat, 3 Oct 2020 17:14:56 -0700 Subject: [PATCH 3/4] Fix methods name and remove method --- src/edu/mills/cs180a/StringUtilities.java | 12 +++---- .../mills/cs180a/StringUtilitiesTester.java | 32 +++++++++++-------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/edu/mills/cs180a/StringUtilities.java b/src/edu/mills/cs180a/StringUtilities.java index 8ae120e..158b6b7 100644 --- a/src/edu/mills/cs180a/StringUtilities.java +++ b/src/edu/mills/cs180a/StringUtilities.java @@ -21,8 +21,8 @@ public static boolean isSubstring(String substring, String text) { Objects.requireNonNull(substring); Objects.requireNonNull(text); - if (substring.isEmpty()) { - return true; + if (substring.isEmpty() || text.isEmpty()) { + return false; } for (int i = 0; i < text.length(); i++) { @@ -38,8 +38,8 @@ public static boolean isSubstring(String substring, String text) { } private static boolean isSubstringHelper(String substring, String text, int offset) { - // Return false if substring length is greater than the rest of the text. - if ((getLength(substring) - 1) > (getLength(text) - offset)) + // Return false if length of substring is greater than length of rest of text. + if ((substring.length() - 1) > (text.length() - offset)) return false; // check substring appears at the given offset in text. @@ -49,8 +49,4 @@ private static boolean isSubstringHelper(String substring, String text, int offs } 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 ed2bc22..b9da748 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -15,10 +15,14 @@ void isSubstring_False_null() { assertThrows(NullPointerException.class, () -> StringUtilities.isSubstring(null, null)); } + @Test + void isSubstring_False_EmptySubstring() { + assertFalse(StringUtilities.isSubstring("", "foo")); + } + @Test void isSubstring_False_EmptyString() { - assertTrue(StringUtilities.isSubstring("", "foo")); - // TODO: write rest + assertFalse(StringUtilities.isSubstring("foo", "")); } @Test @@ -26,19 +30,25 @@ void isSubstring_True_Length1Substring() { assertTrue(StringUtilities.isSubstring("A", "ABC")); } + @Test + void isSubstring_True_LastLengthSubstring() { + assertTrue(StringUtilities.isSubstring("A", "XYZA")); + } + @ParameterizedTest - @CsvSource({"A, BAAA", "B,ZXCVB", "E,qwqwqwqwqwqwE", "F,asdFFF"}) - void isSubstring_True_ContainSubstring(String substring, String Text) { + @CsvSource({"A, BAAA", "B, ZXCVB", "E, qwqwqwqwqwqwE", "F, asdFFF"}) + void isSubstring_True_ContainSingleSubstring(String substring, String Text) { assertTrue(StringUtilities.isSubstring(substring, Text)); } - @Test - void isSubstring_True_LastLengthSubstring() { - assertTrue(StringUtilities.isSubstring("A", "XYZA")); + @ParameterizedTest + @CsvSource({"ZABC, ZABC", "ABC, ZABC", "BCEFG, ZABCEFGHIFK"}) + void isSubstring_True_ContainMultiSubstring(String substring, String Text) { + assertTrue(StringUtilities.isSubstring(substring, Text)); } @ParameterizedTest - @CsvSource({"X,ZABC", "X,ZABC", "9,ZABC"}) + @CsvSource({"X, ZABC", "X, ZABC", "9, ZABC"}) void isSubstring_False_NotContainSingleSubstring(String substring, String Text) { assertFalse(StringUtilities.isSubstring(substring, Text)); } @@ -49,12 +59,6 @@ void isSubstring_False_NotContain2Substring(String substring, String Text) { assertFalse(StringUtilities.isSubstring(substring, Text)); } - @ParameterizedTest - @CsvSource({"ZABC, ZABC", "ABC, ZABC", "BCEFG, ZABCEFGHIFK"}) - void isSubstring_False_MatchSubstring(String substring, String Text) { - assertTrue(StringUtilities.isSubstring(substring, Text)); - } - @ParameterizedTest @CsvSource({"ZABCDE, ZABC", "ABCD, ZABC", "BCEFG, ZABC"}) void isSubstring_False_LargeSubstring(String substring, String Text) { From c58f518dba0a9b341165180cbd4863f2a71937a5 Mon Sep 17 00:00:00 2001 From: Makie Date: Sat, 3 Oct 2020 17:20:39 -0700 Subject: [PATCH 4/4] Add test of isSubstring() and fix code --- src/edu/mills/cs180a/StringUtilities.java | 1 - src/edu/mills/cs180a/StringUtilitiesTester.java | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/edu/mills/cs180a/StringUtilities.java b/src/edu/mills/cs180a/StringUtilities.java index 158b6b7..af06a1f 100644 --- a/src/edu/mills/cs180a/StringUtilities.java +++ b/src/edu/mills/cs180a/StringUtilities.java @@ -41,7 +41,6 @@ private static boolean isSubstringHelper(String substring, String text, int offs // Return false if length of substring is greater than length of rest of text. if ((substring.length() - 1) > (text.length() - offset)) return false; - // check substring appears at the given offset in text. for (int i = 1; i < substring.length(); i++, offset++) { if (text.charAt(offset) != substring.charAt(i)) diff --git a/src/edu/mills/cs180a/StringUtilitiesTester.java b/src/edu/mills/cs180a/StringUtilitiesTester.java index b9da748..5966fa3 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -16,13 +16,13 @@ void isSubstring_False_null() { } @Test - void isSubstring_False_EmptySubstring() { - assertFalse(StringUtilities.isSubstring("", "foo")); + void isSubstring_False_EmptyString() { + assertFalse(StringUtilities.isSubstring("foo", "")); } @Test - void isSubstring_False_EmptyString() { - assertFalse(StringUtilities.isSubstring("foo", "")); + void isSubstring_False_EmptySubstring() { + assertFalse(StringUtilities.isSubstring("", "foo")); } @Test