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
7 changes: 7 additions & 0 deletions src/edu/mills/cs180a/StringUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.

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

Expand Down
80 changes: 78 additions & 2 deletions src/edu/mills/cs180a/StringUtilitiesTester.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,13 +16,86 @@ void isSubstring_False_null() {
}

@Test
void isSubstring_False_EmptyString() {
void isSubstring_True_EmptyString() {
assertTrue(StringUtilities.isSubstring("", "foo"));
// TODO: write rest
}

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

@Test
void isSubstring_True_Length2Substring() {
assertTrue(StringUtilities.isSubstring("C D", "ABC DEF"));
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.

This looks like a length-3 substring.

}

@Test
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() {
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.

This looks like a length-3 substring.

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", "GHI"));
}

@ParameterizedTest
@CsvSource({"The cart, tottenham", "Tra lalalalala,apple pie", "Mo Ghile Mear,run away"})
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.

😃

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