Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 35 additions & 39 deletions src/edu/mills/cs180a/StringUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,46 @@
* 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;
}

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() || text.isEmpty()) {
return false;
}
}
// 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))
for (int i = 0; i < text.length(); i++) {
// Check current character of the full string matches start of substring.
if (text.charAt(i) == substring.charAt(0)) {
// Check the rest of the strings matches substring without first character.
if (isSubstringHelper(substring, text, i + 1)) {
return true;
}
}
}
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 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))
return false;
}
return true;
}
}
76 changes: 59 additions & 17 deletions src/edu/mills/cs180a/StringUtilitiesTester.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,67 @@
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() {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good.

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() {
assertFalse(StringUtilities.isSubstring("foo", ""));
}

@Test
void isSubstring_False_EmptySubstring() {
assertFalse(StringUtilities.isSubstring("", "foo"));
}

@Test
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_ContainSingleSubstring(String substring, String Text) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter names should never start with a capital letter.

assertTrue(StringUtilities.isSubstring(substring, Text));
}

@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"})
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) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the name of this method.

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));
}
}