-
Notifications
You must be signed in to change notification settings - Fork 11
Added StringManipulation.java and StringManipulationTest.java files #3
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 @@ | ||
| import java.util.Arrays; | ||
|
|
||
| // Description: This class implements various methods that manipulates strings. | ||
| public class StringManipulation implements StringManipulationInterface { | ||
|
|
||
| private String str = null; | ||
| @Override | ||
| public String getString() { | ||
| return null; | ||
| return this.str; | ||
| } | ||
|
|
||
| @Override | ||
| public void setString(String string) { | ||
| this.str = string; | ||
| } | ||
|
|
||
| @Override | ||
| public int count() { | ||
| return 0; | ||
| if(this.str == null || this.str.isEmpty()){ | ||
| return 0; | ||
| } | ||
|
|
||
| String[] words = this.str.split("\\s+"); | ||
|
|
||
| int wordCount = 0; | ||
| for (String word : words) { | ||
| // Count words and numerical values as a word. | ||
| // Ignore punctuation. | ||
| if (word.matches(".*[a-zA-Z0-9].*")) { | ||
| wordCount++; | ||
| } | ||
| } | ||
| return wordCount; | ||
| } | ||
|
|
||
| @Override | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| return null; | ||
| if (n > str.length()) { | ||
| throw new IndexOutOfBoundsException("The argument is out of bound."); | ||
| } else if (n <= 0) { | ||
| throw new IllegalArgumentException("n index cannot be less than or equal to 0."); | ||
| } | ||
|
|
||
| StringBuilder stringBuilder = new StringBuilder(str); | ||
|
|
||
| // Loop through multiples of n | ||
| for (int i = 1; n * i <= str.length(); i++) { | ||
| int index = n * i - 1; | ||
| if (maintainSpacing) { | ||
| stringBuilder.setCharAt(index, ' '); | ||
| } else { | ||
| // Decrease index by i to account for previously deleted characters | ||
| stringBuilder.deleteCharAt(index - (i - 1)); | ||
| } | ||
| } | ||
|
|
||
| return stringBuilder.toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| return null; | ||
| public String[] getSubStrings(int startWord, int endWord){ | ||
| // Validate the inputs | ||
| if (startWord <= 0 || endWord <= 0 || startWord > endWord) { | ||
| throw new IllegalArgumentException("StartWord must be greater than 0 and less than or equal to EndWord."); | ||
| } | ||
|
|
||
| // Split the string into words | ||
| String[] words = this.str.trim().split("\\s"); | ||
|
|
||
| // Validate that endWord does not exceed the number of words | ||
| if (endWord > words.length) { | ||
| throw new IndexOutOfBoundsException("The positions must be within the bounds"); | ||
| } | ||
|
|
||
| // Return the subarray of words | ||
| return Arrays.copyOfRange(words, startWord - 1, endWord); | ||
| } | ||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| return null; | ||
| } | ||
|
|
||
| // Check if the length of the string and indices array are the same | ||
| if (str.length() != indices.length) { | ||
| throw new IllegalArgumentException("Length of string and indices array must be equal."); | ||
| } | ||
|
|
||
| // Array of characters to rebuild the string. | ||
| char[] shuffledString = new char[str.length()]; | ||
| for (int i = 0; i < indices.length; i++) { | ||
| int index = indices[i]; | ||
| // Check if the given indice is within the bounds of the string | ||
| if (index < 0 || index >= str.length()) { | ||
| throw new IndexOutOfBoundsException("Index " + index + " is out of bounds."); | ||
| } | ||
| // Assign the character to the char array at the index position | ||
| shuffledString[index] = str.charAt(i); | ||
| } | ||
| return new String(shuffledString); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class StringManipulationTest { | ||
|
|
@@ -19,63 +19,136 @@ public void tearDown() { | |
| manipulatedstring = null; | ||
| } | ||
|
|
||
| @Test | ||
| public void testgetString(){ | ||
| manipulatedstring.setString("Hello!"); | ||
| String expected = "Hello!"; | ||
| String actual = manipulatedstring.getString(); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCount1() { | ||
| manipulatedstring.setString("This is my string"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(4, length); | ||
| } | ||
|
|
||
| //Test when string has multiple punctuations. | ||
| @Test | ||
| public void testCount2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This isn't my string!"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(4, length); | ||
| } | ||
|
|
||
| // Test when string is empty. | ||
| // Expected return is 0 | ||
| @DisplayName("Count empty string") | ||
| @Test | ||
| public void testCount3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString(""); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(0, length); | ||
| } | ||
|
|
||
| // Test when string is null | ||
| // Expected return is 0 | ||
| @Test | ||
| @DisplayName("Count null") | ||
| public void testCount4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString(null); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(0, length); | ||
| } | ||
|
|
||
| // Test when string has only empty space character. | ||
| // Expected return is 0 | ||
| @Test | ||
| public void testCount5() { | ||
| manipulatedstring.setString(" "); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(0, length); } | ||
|
|
||
| // Test when string has space separated punctuations. | ||
| // Expected return is 0 | ||
| @Test | ||
| @DisplayName("Count punctuation") | ||
| public void testCount6() { | ||
| manipulatedstring.setString("! ? ] "); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(0, length); | ||
| } | ||
|
|
||
| // Test when string has numerical value. | ||
| // Expected return is 1 | ||
| @Test | ||
| @DisplayName("Count numeric") | ||
| public void testCount7() { | ||
| manipulatedstring.setString("! 123 "); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(1, length); | ||
| } | ||
|
|
||
| // Test if the method removes the right characters without maintaining spacing | ||
| @Test | ||
| public void testRemoveNthCharacter1() { | ||
| manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| assertEquals("I' bttr uts0e 16tsinths trn6 rgh?", manipulatedstring.removeNthCharacter(3, false)); | ||
| String expected = "I' bttr uts0e 16tsinths trn6 rgh?"; | ||
| String actual = manipulatedstring.removeNthCharacter(3, false); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| // Test if the method removes the right characters while maintaining spacing | ||
| @Test | ||
| public void testRemoveNthCharacter2() { | ||
| manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", manipulatedstring.removeNthCharacter(3, true)); | ||
| String expected = "I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?"; | ||
| String actual = manipulatedstring.removeNthCharacter(3, true); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| // Throw IndexOutOfBoundException if the given index is exceeds the size of the string | ||
| @Test | ||
| public void testRemoveNthCharacter3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is 28 character string."); | ||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.removeNthCharacter(29, true);}); | ||
| } | ||
|
|
||
| // Throw IndexOutOfBoundsException if the given index is less than or equal to 0 | ||
| @Test | ||
| public void testRemoveNthCharacter4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is 28 character string."); | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.removeNthCharacter(0, true);}); | ||
| } | ||
|
|
||
| // Deleting all characters without maintaining spacing. | ||
| @Test | ||
| public void testRemoveNthCharacter5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is 28 character string."); | ||
| String expected = ""; | ||
| String actual = manipulatedstring.removeNthCharacter(1, false); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| // Deleting each character while maintaining spacing. | ||
| @Test | ||
| public void testRemoveNthCharacter6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is 28 character string."); | ||
| String expected = " "; | ||
| String actual = manipulatedstring.removeNthCharacter(1, true); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| // Deleting only the last character without adding a space. | ||
| @Test | ||
| public void testRemoveNthCharacter7() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is 28 character string."); | ||
| String expected = "This is 28 character string"; | ||
| String actual = manipulatedstring.removeNthCharacter(28, false); | ||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -87,63 +160,101 @@ public void testGeSubStrings1() { | |
| assertEquals(sStings[1], "string"); | ||
| } | ||
|
|
||
| // Test when the startWord index is less than or equal to 0 | ||
| // Throws IllegalArgumentException | ||
| @Test | ||
| public void testGeSubStrings2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is my string"); | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.getSubStrings(0, 2);}); | ||
| } | ||
|
|
||
| // Test when the endWord index is less than or equal to 0 | ||
| // Throws IllegalArgumentException | ||
| @Test | ||
| public void testGeSubStrings3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is my string"); | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.getSubStrings(1, 0);}); | ||
| } | ||
|
|
||
| // Test when the startWord index is greater than endWord index | ||
| // Throws IllegalArgumentException | ||
| @Test | ||
| public void testGeSubStrings4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is my string"); | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.getSubStrings(2, 1);}); | ||
| } | ||
|
|
||
| // Test when the endWord index is greater than the word count of string | ||
| // Throws IndexOutOfBoundException | ||
| @Test | ||
| public void testGeSubStrings5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is my string"); | ||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.getSubStrings(2, 5);}); | ||
| } | ||
|
|
||
| // Test when the startWord and endWord are equal | ||
| @Test | ||
| public void testGeSubStrings6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is my string"); | ||
| String [] sStings = manipulatedstring.getSubStrings(4, 4); | ||
| String expected = "string"; | ||
| String actual = sStings[0]; | ||
|
|
||
| assertEquals(expected, actual); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString1() | ||
| { | ||
| manipulatedstring.setString("art"); | ||
| int [] array; | ||
| array=new int[]{1,0,2}; | ||
| array = new int[]{1,0,2}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, "rat"); | ||
| } | ||
|
|
||
| // Throw IllegalArgumentException if the indices length is greater than the string length. {5,6,7,8,9,10,11,0,1,2,3,4,12}; | ||
| @Test | ||
| public void testRestoreString2() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedstring.setString("JUnitTesting!"); | ||
| int[] indices = {5,6,7,8,9,10,11}; | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| manipulatedstring.restoreString(indices);}); | ||
| } | ||
|
|
||
| // Throw IndexOutOfBoundsException if any indice is less than 0. | ||
| @Test | ||
| public void testRestoreString3() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedstring.setString("JUnitTesting!"); | ||
| int[] indices = {5,6,7,8,9,10,11,0,1,2,3,4,-8}; | ||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.restoreString(indices);}); | ||
| } | ||
|
|
||
| // Throw IndexOutOfBoundsException if any indice is greater than the string length | ||
| @Test | ||
| public void testRestoreString4() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedstring.setString("JUnitTesting!"); | ||
| int[] indices = {5,6,7,8,9,10,11,0,1,2,3,4,13}; | ||
| assertThrows(IndexOutOfBoundsException.class, () -> { | ||
| manipulatedstring.restoreString(indices);}); | ||
| } | ||
|
|
||
| // Test when each character is the same in the string | ||
|
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. This is an interesting test case, I wonder in what ways it would break/why it would be needed
Author
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. Yes, it is an interesting test case. I am not sure how you would break this case too. I run out of ideas and made up a case lol. Maybe it could be useful when implementation takes shortcuts or makes assumptions if the characters are the same. This test case ensures that the code does not make invalid assumptions. |
||
| @Test | ||
| public void testRestoreString5() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| manipulatedstring.setString("aaa"); | ||
| int [] indices = {1,0,2}; | ||
| String restoreString = manipulatedstring.restoreString(indices); | ||
| assertEquals(restoreString, "aaa"); | ||
| } | ||
|
|
||
| } | ||
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.
I like that you added comments describing a bit about the test. Try looking into the @DisplayName annotation for an addition nice way to format test details
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.
Oh, that's really cool. I'll use that