diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..8b8ae58 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -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 + 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); + } } diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 6692c2c..085fdee 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -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"); + 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, () -> { + 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); + }); } - -} +} \ No newline at end of file