From 919a057b2965eefbbc1a0c46e3e813712efa6ce7 Mon Sep 17 00:00:00 2001 From: Jon Kaimmer Date: Wed, 14 Jun 2023 11:31:37 -0700 Subject: [PATCH] updated files with my JUnit assignment --- src/main/java/StringManipulation.java | 89 +++++++++++- src/test/java/StringManipulationTest.java | 158 +++++++++++++++------- 2 files changed, 194 insertions(+), 53 deletions(-) diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..7d13b24 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,32 +1,107 @@ +/* + Jon Kaimmer + CS410 Software Engineering + Spring 2023 + Junit Testing Assignment + + Spring Manipulation + */ + +import java.lang.IllegalArgumentException; +import java.lang.IndexOutOfBoundsException; +import java.lang.NullPointerException; +import java.util.*; + + public class StringManipulation implements StringManipulationInterface { + public String s; + @Override public String getString() { - return null; + return s; } @Override public void setString(String string) { + s = string; } @Override public int count() { - return 0; + if (s == null || s.isEmpty()){ + return 0; + } + String[] words = s.split("\\s+"); + return words.length; } @Override public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + + if(s == null){ + throw new NullPointerException("string cannot be null"); + } + if (n <= 0) { + throw new IllegalArgumentException("n must be greater than zero."); + } + if (n > s.length()){ + throw new IndexOutOfBoundsException("n is greater than the string length."); + } + + StringBuilder sb = new StringBuilder(); + + for (int i=0 ; i < s.length() ; i++){ + if((i+1)%n == 0){ + if (maintainSpacing){ + sb.append(' '); + } + } else { + sb.append(s.charAt(i)); + } + } + + return sb.toString(); } @Override - public String[] getSubStrings(int startWord, int endWord) { - return null; + public String[] getSubStrings(int startWord, int endWord){ + + if (startWord <= 0 || endWord <= 0 || startWord > endWord) { + throw new IllegalArgumentException("Invalid input: startWord and endWord should be greater than 0, and startWord should not be greater than endWord."); + } + + String[] words = s.split("\\s+"); // Split the string into words + + if (endWord > words.length) { + throw new IndexOutOfBoundsException("The string has less than " + endWord + " words in it."); + } + + String[] subStrings = new String[endWord - startWord + 1]; + + for (int i = startWord - 1; i < endWord; i++) { // Array indices start at 0, but word positions start at 1 + subStrings[i - startWord + 1] = words[i]; // Adjust indices for the same reason + } + + return subStrings; } @Override - public String restoreString(int[] indices) { - return null; + public String restoreString(int[] indices){ + if (s.length() != indices.length) { + throw new IllegalArgumentException("The length of the string and the indices array must be the same."); + } + + char[] shuffled = new char[s.length()]; + + for (int i = 0; i < s.length(); i++) { + if (indices[i] < 0 || indices[i] >= s.length()) { + throw new IndexOutOfBoundsException("Index " + indices[i] + " is out of bounds."); + } + shuffled[indices[i]] = s.charAt(i); + } + + return new String(shuffled); } diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 6692c2c..d48d28e 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -1,3 +1,12 @@ +/* + Jon Kaimmer + CS410 Software Engineering + Spring 2023 + Junit Testing Assignment + + Spring Manipulation Test + */ + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -20,66 +29,87 @@ public void tearDown() { } @Test - public void testCount1() { + public void testCount1_NormalString() { manipulatedstring.setString("This is my string"); int length = manipulatedstring.count(); assertEquals(4, length); } @Test - public void testCount2() { - fail("Not yet implemented"); + public void testCount2_EmptyString() { + manipulatedstring.setString(""); + int length = manipulatedstring.count(); + assertEquals(0, length); } @Test - public void testCount3() { - fail("Not yet implemented"); + public void testCount3_Null() { + manipulatedstring.setString(null); + int length = manipulatedstring.count(); + assertEquals(0, length); } @Test - public void testCount4() { - fail("Not yet implemented"); + public void testCount4_TabSeperatedWords() { + manipulatedstring.setString("This is my string"); + int length = manipulatedstring.count(); + assertEquals(4, length); } @Test - public void testRemoveNthCharacter1() { + public void testRemoveNthCharacter1_DontMaintainSpacing() { manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); assertEquals("I' bttr uts0e 16tsinths trn6 rgh?", manipulatedstring.removeNthCharacter(3, false)); } @Test - public void testRemoveNthCharacter2() { + public void testRemoveNthCharacter2_DoMaintainSpacing() { 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)); } @Test - public void testRemoveNthCharacter3() { - fail("Not yet implemented"); - } + public void testRemoveNthCharacter3_EmptyString_IndexOutOfBoundsException() { + manipulatedstring.setString(""); - @Test - public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + assertThrows(IndexOutOfBoundsException.class, + ()-> { + manipulatedstring.removeNthCharacter(1, false); + }); } @Test - public void testRemoveNthCharacter5() { - fail("Not yet implemented"); + public void testRemoveNthCharacter4_NullString_ThrowNullPointerException() { + manipulatedstring.setString(null); + + assertThrows(NullPointerException.class, + ()-> { + manipulatedstring.removeNthCharacter(1, false); + }); } @Test - public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + public void testRemoveNthCharacter5_nEqualsZero_ThrowIllegalArgument() { + manipulatedstring.setString("I am a String!"); + + assertThrows(IllegalArgumentException.class, + ()-> { + manipulatedstring.removeNthCharacter(0, false); + }); } @Test - public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + public void testRemoveNthCharacter6_nLargerThanString_ThrowIndexOutOfBounds() { + manipulatedstring.setString("I am a String!"); + + assertThrows(IndexOutOfBoundsException.class, + ()-> { + manipulatedstring.removeNthCharacter(15, false); + }); } @Test - public void testGeSubStrings1() { + public void testGetSubStrings1_TestWords() { manipulatedstring.setString("This is my string"); String [] sStings = manipulatedstring.getSubStrings(3, 4); @@ -88,28 +118,53 @@ public void testGeSubStrings1() { } @Test - public void testGeSubStrings2() { - fail("Not yet implemented"); + public void testGetSubStrings2_ThrowIllegalArgumentException_startWordLessThanZero() { + manipulatedstring.setString("I am a String!"); + + assertThrows(IllegalArgumentException.class, + ()-> { + String [] sStings = manipulatedstring.getSubStrings(-1, 4); + }); } @Test - public void testGeSubStrings3() { - fail("Not yet implemented"); + public void testGetSubStrings3_ThrowIllegalArgumentException_endWordLessThanZero() { + manipulatedstring.setString("I am a String!"); + + assertThrows(IllegalArgumentException.class, + ()-> { + String [] sStings = manipulatedstring.getSubStrings(1, -1); + }); } @Test - public void testGeSubStrings4() { - fail("Not yet implemented"); + public void testGetSubStrings4_ThrowIllegalArgumentException_endWordLessThanstartWord() { + manipulatedstring.setString("I am a String!"); + + assertThrows(IllegalArgumentException.class, + ()-> { + String [] sStings = manipulatedstring.getSubStrings(2, 1); + }); } @Test - public void testGeSubStrings5() { - fail("Not yet implemented"); + public void testGetSubStrings5_ThrowIndexOutOfBoundsException_endWordGreaterThanNumberOfWords() { + manipulatedstring.setString("I am a String!"); + + assertThrows(IndexOutOfBoundsException.class, + ()-> { + String [] sStings = manipulatedstring.getSubStrings(1, 5); + }); } @Test - public void testGeSubStrings6() { - fail("Not yet implemented"); + public void testGetSubStrings6_EmptyString() { + manipulatedstring.setString(""); + + assertThrows(IndexOutOfBoundsException.class, + ()-> { + String [] sStings = manipulatedstring.getSubStrings(1, 5); + }); } @Test - public void testRestoreString1() + public void testRestoreString1_3LetterWord() { manipulatedstring.setString("art"); int [] array; @@ -119,31 +174,42 @@ public void testRestoreString1() } @Test - public void testRestoreString2() - { - fail("Not yet implemented"); - - } - - @Test - public void testRestoreString3() + public void testRestoreString2_ThrowIllegalArgumentException() { - fail("Not yet implemented"); + manipulatedstring.setString("art"); + int [] array; + array=new int[]{1,0}; + assertThrows(IllegalArgumentException.class, + ()-> { + String restoreString = manipulatedstring.restoreString(array); + }); } @Test - public void testRestoreString4() + public void testRestoreString3_ThrowIndexOutOfBoundsException_GreaterThan() { - fail("Not yet implemented"); + manipulatedstring.setString("art"); + int [] array; + array=new int[]{1,0,10}; + assertThrows(IndexOutOfBoundsException.class, + ()-> { + String restoreString = manipulatedstring.restoreString(array); + }); } @Test - public void testRestoreString5() + public void testRestoreString4_ThrowIndexOutOfBoundsException_LessThan() { - fail("Not yet implemented"); + manipulatedstring.setString("art"); + int [] array; + array=new int[]{1,0,-10}; + assertThrows(IndexOutOfBoundsException.class, + ()-> { + String restoreString = manipulatedstring.restoreString(array); + }); } }