From 20f886d8d39f4f94b0d864ecdcd407d1af1dbbd5 Mon Sep 17 00:00:00 2001 From: Charles Thomas Date: Wed, 30 Sep 2020 13:23:23 -0700 Subject: [PATCH 1/4] Add tests of isSubstring and getLength --- .../mills/cs180a/StringUtilitiesTester.java | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/edu/mills/cs180a/StringUtilitiesTester.java b/src/edu/mills/cs180a/StringUtilitiesTester.java index a38617b..0acdf96 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -1,25 +1,39 @@ 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; 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_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_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_True_Length1Substring() { + assertTrue(StringUtilities.isSubstring("A", "ABC")); + } + + @Test + void isSubstring_False_Length1Substring() { + assertFalse(StringUtilities.isSubstring("A", "DEF")); + } + + @Test + void getLength_True_Length6_4_9String() { + assertTrue(StringUtilities.getLength("length") == 6); + assertTrue(StringUtilities.getLength("size") == 4); + assertTrue(StringUtilities.getLength("bigstring") == 9); + } } From 1d9305600258f015de07fcab388fb92182a2b563 Mon Sep 17 00:00:00 2001 From: Charles Thomas Date: Wed, 30 Sep 2020 13:29:52 -0700 Subject: [PATCH 2/4] Add tests of isSubstring and getLength, some fail --- src/edu/mills/cs180a/StringUtilitiesTester.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/edu/mills/cs180a/StringUtilitiesTester.java b/src/edu/mills/cs180a/StringUtilitiesTester.java index 0acdf96..feaaa62 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -30,6 +30,11 @@ void isSubstring_False_Length1Substring() { assertFalse(StringUtilities.isSubstring("A", "DEF")); } + @Test + void isSubstring_True_Length3Substring() { + assertTrue(StringUtilities.isSubstring("ABC", "DEF")); + } + @Test void getLength_True_Length6_4_9String() { assertTrue(StringUtilities.getLength("length") == 6); From f45399ea99c564e4af8e587b4701704a1997ad66 Mon Sep 17 00:00:00 2001 From: Charles Thomas Date: Wed, 30 Sep 2020 16:47:53 -0700 Subject: [PATCH 3/4] Fix bug, substring bigger than text --- src/edu/mills/cs180a/StringUtilities.java | 82 ++++++++++--------- .../mills/cs180a/StringUtilitiesTester.java | 19 +++-- 2 files changed, 58 insertions(+), 43 deletions(-) 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 feaaa62..6ccbf8d 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -4,6 +4,8 @@ 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 @@ -30,15 +32,22 @@ void isSubstring_False_Length1Substring() { assertFalse(StringUtilities.isSubstring("A", "DEF")); } - @Test - void isSubstring_True_Length3Substring() { - assertTrue(StringUtilities.isSubstring("ABC", "DEF")); - } - @Test void getLength_True_Length6_4_9String() { 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)); + } } From 7b5d4e7af9985aa2c8a9907121495cca2f416e4e Mon Sep 17 00:00:00 2001 From: Charles Thomas Date: Wed, 14 Oct 2020 12:40:08 -0700 Subject: [PATCH 4/4] Change getLength test method name to correct format --- 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 6ccbf8d..59150dc 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -33,7 +33,7 @@ void isSubstring_False_Length1Substring() { } @Test - void getLength_True_Length6_4_9String() { + void getLength_CorrectLength_NonZeroLengths() { assertTrue(StringUtilities.getLength("length") == 6); assertTrue(StringUtilities.getLength("size") == 4); assertTrue(StringUtilities.getLength("bigstring") == 9);