From f7c660c0f6107b58cad9391aad8ec5c2f4443990 Mon Sep 17 00:00:00 2001 From: Zoe Abrams Date: Thu, 29 Oct 2020 18:50:50 -0700 Subject: [PATCH 1/3] Improve tests Add new tests, some of which are failing. --- .../mills/cs180a/StringUtilitiesTester.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/edu/mills/cs180a/StringUtilitiesTester.java b/src/edu/mills/cs180a/StringUtilitiesTester.java index a38617b..0ca8c41 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -1,8 +1,11 @@ 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 @@ -22,4 +25,28 @@ void isSubstring_False_EmptyString() { void isSubstring_True_Length1Substring() { assertTrue(StringUtilities.isSubstring("A", "ABC")); } + + @Test + void isSubstring_True_Length2Substring() { + assertTrue(StringUtilities.isSubstring("C D", "ABC DEF")); + } + + @Test + void isSubstring_True_Length3Substring() { + assertTrue(StringUtilities.isSubstring("GHI", "ABC DEF GHI")); + } + + + @ParameterizedTest + @CsvSource({"The cat is in the hat,cat is in", "Tra lalalalala,tra", "Mo Ghile Mear,Ghile"}) + void isSubstring_True_VariedStringsAndSubstrings(String input, String subInput) { + assertTrue(StringUtilities.isSubstring(subInput, input)); + } + + @ParameterizedTest + @CsvSource({"The cat is in the hat,hotpot", "Tra lalalalala,homeward is", + "Mo Ghile Mear,run away"}) + void isSubstring_False_VariedStringsAndSubstrings(String input, String subInput) { + assertFalse(StringUtilities.isSubstring(subInput, input)); + } } From d06af79f9b7265ed4d392ba8365409c3f1b85890 Mon Sep 17 00:00:00 2001 From: Zoe Abrams Date: Fri, 30 Oct 2020 01:14:26 -0700 Subject: [PATCH 2/3] Add additional tests Add additional tests for ease of debugging and specific tests to verify the bug: If the first letter of the substring is the last letter of the string, the current function returns true, even if the rest of the substring is not part of the string. --- .../mills/cs180a/StringUtilitiesTester.java | 55 ++++++++++++++++++- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/src/edu/mills/cs180a/StringUtilitiesTester.java b/src/edu/mills/cs180a/StringUtilitiesTester.java index 0ca8c41..324849f 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -16,9 +16,8 @@ void isSubstring_False_null() { } @Test - void isSubstring_False_EmptyString() { + void isSubstring_True_EmptyString() { assertTrue(StringUtilities.isSubstring("", "foo")); - // TODO: write rest } @Test @@ -36,9 +35,59 @@ void isSubstring_True_Length3Substring() { assertTrue(StringUtilities.isSubstring("GHI", "ABC DEF GHI")); } + @Test + void isSubstring_False_Length1Substring() { + assertFalse(StringUtilities.isSubstring("D", "ABC")); + } + + @Test + void isSubstring_False_Length2Substring() { + assertFalse(StringUtilities.isSubstring("C F", "ABC DEF")); + } + + @Test + void isSubstring_False_Length3Substring() { + assertFalse(StringUtilities.isSubstring("XYZ ABC", "ABC DEF GHI")); + } + + @Test + void isSubstring_True_SubstringAtStart() { + assertTrue(StringUtilities.isSubstring("ABC", "ABC DEF GHI")); + } + + @Test + void isSubstring_True_SubstringInMiddle() { + assertTrue(StringUtilities.isSubstring("DEF", "ABC DEF GHI")); + } + + @Test + void isSubstring_True_SubstringAtEnd() { + assertTrue(StringUtilities.isSubstring("GHI", "ABC DEF GHI")); + } + + @Test + void isSubstring_False_SubstringAtStart() { + assertFalse(StringUtilities.isSubstring("ADF", "ABC DEF GHI")); + } + + @Test + void isSubstring_False_SubstringInMiddle() { + assertFalse(StringUtilities.isSubstring("C DEG", "ABC DEF GHI")); + } + + @Test + void isSubstring_False_SubstringAtEnd() { + assertFalse(StringUtilities.isSubstring("Ijk", "ABC DEF GHI")); + } + + @ParameterizedTest + @CsvSource({"The cart, tottenham", "Tra lalalalala,apple pie", "Mo Ghile Mear,run away"}) + void isSubstring_Fail_FirstLetterOfSubstringLastLetterOfString(String input, String subInput) { + assertFalse(StringUtilities.isSubstring(subInput, input)); + } @ParameterizedTest - @CsvSource({"The cat is in the hat,cat is in", "Tra lalalalala,tra", "Mo Ghile Mear,Ghile"}) + @CsvSource({"The cat is in the hat,cat is in", "Tra lalalalala,Tra", "Mo Ghile Mear,Ghile"}) void isSubstring_True_VariedStringsAndSubstrings(String input, String subInput) { assertTrue(StringUtilities.isSubstring(subInput, input)); } From b3f6eda13c69720639a295285dfb0e2f86711cd2 Mon Sep 17 00:00:00 2001 From: Zoe Abrams Date: Fri, 30 Oct 2020 03:04:27 -0700 Subject: [PATCH 3/3] Fix isSubstringHelper method Added a check to ensure that the code wasn't defaulting to true for cases where the substring fell at the end of the string but the part of the string where the substring started was not long enough to contain the entire substring. Modified a pertinent test to focus on the broken part of the code (isSubstringHelper) by shortening the text string. --- src/edu/mills/cs180a/StringUtilities.java | 7 +++++++ src/edu/mills/cs180a/StringUtilitiesTester.java | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/edu/mills/cs180a/StringUtilities.java b/src/edu/mills/cs180a/StringUtilities.java index 31f03b8..7d10a83 100644 --- a/src/edu/mills/cs180a/StringUtilities.java +++ b/src/edu/mills/cs180a/StringUtilities.java @@ -40,12 +40,19 @@ 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) { + // Return false if the substring is longer than the part of the text after + // the beginning of the substring + if (substring.length() - 1 > text.substring(offset).length()) { + 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; } diff --git a/src/edu/mills/cs180a/StringUtilitiesTester.java b/src/edu/mills/cs180a/StringUtilitiesTester.java index 324849f..a9eba67 100644 --- a/src/edu/mills/cs180a/StringUtilitiesTester.java +++ b/src/edu/mills/cs180a/StringUtilitiesTester.java @@ -77,7 +77,7 @@ void isSubstring_False_SubstringInMiddle() { @Test void isSubstring_False_SubstringAtEnd() { - assertFalse(StringUtilities.isSubstring("Ijk", "ABC DEF GHI")); + assertFalse(StringUtilities.isSubstring("Ijk", "GHI")); } @ParameterizedTest