-
Notifications
You must be signed in to change notification settings - Fork 11
celine_chiou updated skeleton with my code for peer review #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,98 @@ | ||
| // Celine Chiou | ||
| // CS 410 JUnit Testing Assignment | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| public class StringManipulation implements StringManipulationInterface { | ||
| private String currentString; | ||
| private int count; | ||
|
|
||
| @Override | ||
| public String getString() { | ||
| return null; | ||
| if (currentString == null) { | ||
| return null; | ||
| } else { | ||
| return currentString; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void setString(String string) { | ||
| this.currentString = string; | ||
| } | ||
|
|
||
| @Override | ||
| public int count() { | ||
| return 0; | ||
| // leading spaces and extra spaces are removed | ||
| // words are placed into string array and number of words = count | ||
| for (int i = 0; i < currentString.length(); i++) { | ||
| String trimmedString = currentString.trim().replaceAll("\\s+", " "); | ||
| String[] wordCount = trimmedString.split(" "); | ||
| count = wordCount.length; | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| @Override | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| return null; | ||
| StringBuilder finalString = new StringBuilder(); | ||
|
|
||
| // throws IndexOutOfBoundsException If n is greater than the string length. | ||
| if (n > getString().length()) { | ||
| throw new IndexOutOfBoundsException("Index Out Of Bounds Exception: n is greater than the string length"); | ||
| } | ||
|
|
||
| // throws IllegalArgumentException If "n" less than or equal to zero. | ||
| if (n <= 0) { | ||
| throw new IllegalArgumentException("Illegal Argument Exception: n is less than or equal to 0"); | ||
| } | ||
|
|
||
| for (int i = 0; i < currentString.length(); i++) { | ||
| if ((i + 1) % n != 0) { // accounts for when i % n is not 0 | ||
| finalString.append(currentString.charAt(i)); | ||
| } else if (maintainSpacing) { | ||
| finalString.append(" "); | ||
| } | ||
| } | ||
| return finalString.toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| return null; | ||
| public String[] getSubStrings(int startWord, int endWord){ | ||
| // throws IllegalArgumentException If either "startWord" or "endWord" are invalid (i.e., | ||
| // "startWord" <= 0, "endWord" <= 0, or "startWord" > "endWord") | ||
| if (startWord <= 0 || endWord <= 0 || startWord > endWord) { | ||
| throw new IllegalArgumentException("Illegal Argument Exception: Invalid startWord or endWord"); | ||
| } | ||
|
|
||
| // this portion will remove leading/extra spaces | ||
| // words are added to array, split by spaces | ||
| String trimmedString = currentString.trim().replaceAll("\\s+", " "); | ||
| String[] wordArray = trimmedString.split(" "); | ||
|
|
||
| // throws IndexOutOfBoundsException If the string has less than "endWord" words in it | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be neater if you placed this code with the other exception if-statement so all the code that handles exceptions for this method are in one place, maybe even as an else-if statement as opposed to a completely separate if-statement. |
||
| if (endWord > wordArray.length) { | ||
| throw new IndexOutOfBoundsException("Index Out Of Bounds Exception: String has fewer words than endWord"); | ||
| } | ||
| return Arrays.copyOfRange(wordArray, startWord - 1,endWord); | ||
| } | ||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| return null; | ||
| } | ||
| public String restoreString(int[] indices){ | ||
| char[] shuffledArray = new char[indices.length]; | ||
|
|
||
| // throws IllegalArgumentException if not s.length == indices.length == n | ||
| if (indices.length != getString().length()) { | ||
| throw new IllegalArgumentException("Illegal Argument Exception: Length of string and indices array do not match"); | ||
| } | ||
|
|
||
| for (int i = 0; i < indices.length; i++) { | ||
| // if statement that throws IndexOutOfBoundsException if indices[i]< 0 or indices[i]>= string length | ||
| if(indices[i] < 0 || indices[i] >= indices.length) { | ||
| throw new IndexOutOfBoundsException("Index Out Of Bounds Exception: Invalid index"); | ||
| } | ||
| shuffledArray[indices[i]] = getString().charAt(i); | ||
| } | ||
| return new String(shuffledArray); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| // Celine Chiou | ||
| // CS 410 JUnit Testing Assignment | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
@@ -26,19 +29,28 @@ public void testCount1() { | |
| assertEquals(4, length); | ||
| } | ||
|
|
||
| // This tests if the method will count a string of one word | ||
| @Test | ||
| public void testCount2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("hello"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(1, length); | ||
| } | ||
|
|
||
| // This tests if the method will count a string of words with leading spaces | ||
| @Test | ||
| public void testCount3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString(" should have 4 words"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(4, length); | ||
| } | ||
|
|
||
| // This tests if the method will count a string of words with lots of spaces between multiple words | ||
| @Test | ||
| public void testCount4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("lots of spaces"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(3,length); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -53,29 +65,47 @@ public void testRemoveNthCharacter2() { | |
| assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", manipulatedstring.removeNthCharacter(3, true)); | ||
| } | ||
|
|
||
| // This tests if the method will delete excess symbols at 3n and does not replace with spaces | ||
| @Test | ||
| public void testRemoveNthCharacter3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("My@ n@am@e @is@ C@el@in@e"); | ||
| assertEquals("My name is Celine", manipulatedstring.removeNthCharacter(3,false)); | ||
| } | ||
|
|
||
| // This tests if the method will return index out of bounds exception where n is greater than the string size and maintaining spacing is true | ||
| @Test | ||
| public void testRemoveNthCharacter4() { | ||
| fail("Not yet implemented"); | ||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.setString("test"); | ||
| manipulatedstring.removeNthCharacter(10, true); | ||
| }); | ||
| } | ||
|
|
||
| // This tests if the method will return index out of bounds exception where n is greater than the string size and maintaining spacing is false | ||
| @Test | ||
| public void testRemoveNthCharacter5() { | ||
| fail("Not yet implemented"); | ||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.setString("test"); | ||
| manipulatedstring.removeNthCharacter(8, false); | ||
| }); | ||
| } | ||
|
|
||
| // This tests if the method will return illegal argument exception where n is 0 and maintaining spacing is true | ||
| @Test | ||
| public void testRemoveNthCharacter6() { | ||
| fail("Not yet implemented"); | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.setString("test"); | ||
| manipulatedstring.removeNthCharacter(0,true); | ||
| }); | ||
| } | ||
|
|
||
| // This tests if the method will return illegal argument exception where n is less than 0 and maintaining spacing is true | ||
| @Test | ||
| public void testRemoveNthCharacter7() { | ||
| fail("Not yet implemented"); | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.setString("test"); | ||
| manipulatedstring.removeNthCharacter(-5,true); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -87,25 +117,49 @@ public void testGeSubStrings1() { | |
| assertEquals(sStings[1], "string"); | ||
| } | ||
|
|
||
| // This tests if the method will return many words from the middle of a longer sentence | ||
| @Test | ||
| public void testGeSubStrings2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is my test string sentence that should only return a few words"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Somewhat redundant test case since it does the same thing as the first test. |
||
| String [] sStings = manipulatedstring.getSubStrings(5, 9); | ||
| String[] expected = { "string", "sentence", "that", "should", "only" }; | ||
| assertArrayEquals(expected, sStings); | ||
| } | ||
|
|
||
| // This tests if the method will return illegal argument exception where "startWord" > "endWord" | ||
| @Test | ||
| public void testGeSubStrings3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is my test string"); | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.getSubStrings(2, 0); | ||
| }); | ||
| } | ||
|
|
||
| // This tests if the method will return illegal argument exception where "endWord" < 0 | ||
| @Test | ||
| public void testGeSubStrings4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is my test string"); | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any way to ensure that this test case is throwing the illegal argument exception in response to the negative number argument and not because the start word is greater than the end word, given that both throw the same exception type. |
||
| manipulatedstring.getSubStrings(1, -3); | ||
| }); | ||
| } | ||
|
|
||
| // This tests if the method will return illegal argument exception where "startWord" < 0 | ||
| @Test | ||
| public void testGeSubStrings5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is my test string"); | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.getSubStrings(-1, 3); | ||
| }); | ||
| } | ||
|
|
||
| // This tests if the method will return index out of bounds exception where the string has less than "endWord" words in it | ||
| @Test | ||
| public void testGeSubStrings6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is my test string"); | ||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.getSubStrings(1, 6); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -118,32 +172,47 @@ public void testRestoreString1() | |
| assertEquals(restoreString, "rat"); | ||
| } | ||
|
|
||
| // This tests if the method will return illegal argument exception where lengths do not match (s.length == indices.length == n) | ||
| @Test | ||
| public void testRestoreString2() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedstring.setString("UnitTest"); | ||
| int[] indices = { 4, 5, 6, 7, 0, 2, 1 }; | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.restoreString(indices); | ||
| }); | ||
| } | ||
|
|
||
| // This tests if the method will return index out of bounds exception where indices[i]= string length | ||
| @Test | ||
| public void testRestoreString3() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedstring.setString("UnitTest"); | ||
| int[] indices = { 4, 5, 6, 7, 0, 2, 1, 8 }; | ||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.restoreString(indices); | ||
| }); | ||
| } | ||
|
|
||
| // This tests if the method will return index out of bounds exception where indices[i]> string length | ||
| @Test | ||
| public void testRestoreString4() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedstring.setString("UnitTest"); | ||
| int[] indices = { 4, 5, 6, 7, 0, 2, 1, 9 }; | ||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.restoreString(indices); | ||
| }); | ||
| } | ||
|
|
||
| // This tests if the method will return index out of bounds exception where indices[i]< 0 | ||
| @Test | ||
| public void testRestoreString5() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedstring.setString("UnitTest"); | ||
| int[] indices = { 4, 5, 6, 7, 0, 2, 1, -1 }; | ||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.restoreString(indices); | ||
| }); | ||
| } | ||
|
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
given that the word count value is only used when called by the count() function, would it not be better to have count be local to the count function as opposed to to having it as a private data member.