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
82 changes: 44 additions & 38 deletions src/edu/mills/cs180a/StringUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
62 changes: 45 additions & 17 deletions src/edu/mills/cs180a/StringUtilitiesTester.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
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_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"));
assertTrue(StringUtilities.isSubstring("", "bar"));
assertTrue(StringUtilities.isSubstring("", ""));
}

@Test
void isSubstring_True_Length1Substring() {
assertTrue(StringUtilities.isSubstring("A", "ABC"));
}

@Test
void isSubstring_False_Length1Substring() {
assertFalse(StringUtilities.isSubstring("A", "DEF"));
}

@Test
void getLength_CorrectLength_NonZeroLengths() {
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));
}
}