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
103 changes: 97 additions & 6 deletions src/main/java/StringManipulation.java
Comment thread
ACBxBC marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,33 +1,124 @@
//Alvin Bautista
//CS 410 - JUNIT Assignment
//06112023

public class StringManipulation implements StringManipulationInterface {

private String string; //stores the string to manipulate

@Override
public String getString() {
return null;
return this.string; //return the string stored in this object
}

@Override
public void setString(String string) {
this.string = string; //set the string stored in this object
}

@Override
public int count() {
return 0;
//check if the string is null or empty
if (this.string == null || this.string.trim().isEmpty()) {
return 0;
}
//split the string into words and return the number of words
return this.string.trim().split("\\s+").length;
Comment thread
ACBxBC marked this conversation as resolved.
}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
//validate n
if (n <= 0) {
throw new IllegalArgumentException("n should be greater than 0");
}

//special case when n is 1
if (n == 1) {
return this.string; //return original string
}

//validate the string
if (this.string == null) {
throw new IndexOutOfBoundsException("String is null");
}

//validate that n is not greater than string length
if (n > this.string.length()) {
throw new IndexOutOfBoundsException("n is greater than the string length");
}

//initialize a StringBuilder to build the result
StringBuilder result = new StringBuilder();

//loop through characters of the string
for (int i = 1; i <= this.string.length(); i++) {
//append character to result if its position is not a multiple of n
if (i % n != 0) {
result.append(this.string.charAt(i - 1));
} else {
//append space to result if maintainSpacing is true and position is multiple of n
if (maintainSpacing) {
result.append(' ');
}
}
}

//return the resulting string
return result.toString();
}

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
//validate startWord and endWord
if (startWord <= 0 || endWord <= 0 || startWord > endWord) {
throw new IllegalArgumentException("Invalid startWord or endWord");
}

//validate the string
if (this.string == null) {
throw new IndexOutOfBoundsException("String is null");
}

//split string into words
String[] words = this.string.split("\\s+");

//check if there are enough words to get the substring
if (endWord > words.length) {
throw new IndexOutOfBoundsException("String has fewer than endWord words");
}

//create array to store the substrings
String[] subStrings = new String[endWord - startWord + 1];

//copy the substrings into the array
System.arraycopy(words, startWord - 1, subStrings, 0, subStrings.length);

//return the substrings array
return subStrings;
}

@Override
public String restoreString(int[] indices) {
return null;
}
//validate the indices and the string
if (indices == null || this.string == null || indices.length != this.string.length() || indices.length == 0) {
throw new IllegalArgumentException("String and indices should have the same non-zero length");
}

//create an array to store characters in the restored order
char[] restored = new char[this.string.length()];

//loop through the indices
for (int i = 0; i < indices.length; i++) {
//validate each index
if (indices[i] < 0 || indices[i] >= this.string.length()) {
throw new IndexOutOfBoundsException("Index out of bounds");
}
//place each character at the correct position according to the indices array
restored[indices[i]] = this.string.charAt(i);
}

//return the restored string
return new String(restored);
}
Comment thread
ACBxBC marked this conversation as resolved.
}
163 changes: 107 additions & 56 deletions src/test/java/StringManipulationTest.java
Original file line number Diff line number Diff line change
@@ -1,149 +1,200 @@
//Alvin Bautista
//CS 410 - JUNIT Assignment
//06112023

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;


import static org.junit.jupiter.api.Assertions.*;

public class StringManipulationTest {

private StringManipulationInterface manipulatedstring;
private StringManipulationInterface manipulatedString; //object to be tested

@BeforeEach
public void setUp() {
manipulatedstring = new StringManipulation();
manipulatedString = new StringManipulation(); //initialize object before each test
}

@AfterEach
public void tearDown() {
manipulatedstring = null;
manipulatedString = null; //cleanup after each test
}

@Test
public void testCount1() {
manipulatedstring.setString("This is my string");
int length = manipulatedstring.count();
assertEquals(4, length);
manipulatedString.setString("This is my string"); //setting string
int length = manipulatedString.count(); //count words
assertEquals(4, length); //assert expected value
}

@Test
public void testCount2() {
fail("Not yet implemented");
manipulatedString.setString("One more test case"); //setting string
assertEquals(4, manipulatedString.count()); //directly asserting expected value
}

@Test
public void testCount3() {
fail("Not yet implemented");
manipulatedString.setString(""); //setting empty string
assertEquals(0, manipulatedString.count()); //asserting count is 0
}

@Test
public void testCount4() {
fail("Not yet implemented");
manipulatedString.setString("Single"); //setting single word
assertEquals(1, manipulatedString.count()); //asserting count is 1
}

@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));
manipulatedString.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); //setting string
assertEquals("I' bttr uts0e 16tsinths trn6 rgh?", manipulatedString.removeNthCharacter(3, false)); //asserting removal of characters
}

@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));
manipulatedString.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); //setting string
assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", manipulatedString.removeNthCharacter(3, true)); //asserting removal with spaces
}

@Test
public void testRemoveNthCharacter3() {
fail("Not yet implemented");
manipulatedString.setString("Example text for test case."); //setting string
assertEquals("Eapetx o etcs.", manipulatedString.removeNthCharacter(2, false)); //asserting removal of characters
}

@Test
public void testRemoveNthCharacter4() {
fail("Not yet implemented");
manipulatedString.setString("Example text for test case."); //setting string
assertEquals("Example text for test case.", manipulatedString.removeNthCharacter(1, false)); //asserting no change for n=1
}

@Test
public void testRemoveNthCharacter5() {
fail("Not yet implemented");
manipulatedString.setString("Small"); //setting string
assertEquals("Sal", manipulatedString.removeNthCharacter(2, false)); //asserting removal of characters
}

@Test
public void testRemoveNthCharacter6() {
fail("Not yet implemented");
manipulatedString.setString("Small"); //setting string
assertEquals("S a l", manipulatedString.removeNthCharacter(2, true)); //asserting removal with spaces
Comment thread
ACBxBC marked this conversation as resolved.
}

@Test
public void testRemoveNthCharacter7() {
fail("Not yet implemented");
assertThrows(IndexOutOfBoundsException.class, () -> manipulatedString.removeNthCharacter(20, true)); //asserting exception for out of bounds
}

@Test
public void testGeSubStrings1() {
manipulatedstring.setString("This is my string");
String [] sStings = manipulatedstring.getSubStrings(3, 4);
public void testGetSubStrings1() {
manipulatedString.setString("This is my string"); //setting string
String [] sStings = manipulatedString.getSubStrings(3, 4); //get substring array

assertEquals(sStings[0], "my");
assertEquals(sStings[0], "my"); //asserting elements
assertEquals(sStings[1], "string");
}

@Test
public void testGeSubStrings2() {
fail("Not yet implemented");
public void testGetSubStrings2() {
manipulatedString.setString("A B C D E"); //setting string
String[] subStrings = manipulatedString.getSubStrings(2, 4); //get substring array
assertEquals("B", subStrings[0]); //asserting elements
assertEquals("C", subStrings[1]);
assertEquals("D", subStrings[2]);
Comment thread
ACBxBC marked this conversation as resolved.
}

@Test
public void testGeSubStrings3() {
fail("Not yet implemented");
public void testGetSubStrings3() {
manipulatedString.setString("One Two"); //setting string
String[] subStrings = manipulatedString.getSubStrings(1, 2); //get substring array
assertEquals("One", subStrings[0]); //asserting elements
assertEquals("Two", subStrings[1]);
}

@Test
public void testGeSubStrings4() {
fail("Not yet implemented");
public void testGetSubStrings4() {
assertThrows(IllegalArgumentException.class, () -> manipulatedString.getSubStrings(3, 2)); //asserting exception for invalid arguments
}

@Test
public void testGeSubStrings5() {
fail("Not yet implemented");
public void testGetSubStrings5() {
manipulatedString.setString("Short"); //setting string
assertThrows(IndexOutOfBoundsException.class, () -> manipulatedString.getSubStrings(1, 2)); //asserting exception for out of bounds
}

@Test
public void testGeSubStrings6() {
fail("Not yet implemented");
public void testGetSubStrings6() {
manipulatedString.setString("Just One"); //setting string
String[] subStrings = manipulatedString.getSubStrings(1, 1); //get substring array
assertEquals("Just", subStrings[0]); //asserting element
}

@Test
public void testRestoreString1()
{
manipulatedstring.setString("art");
int [] array;
array=new int[]{1,0,2};
String restoreString = manipulatedstring.restoreString(array);
assertEquals(restoreString, "rat");
public void testRestoreString1() {
manipulatedString.setString("art"); //setting string
int[] array = {1, 0, 2}; //indices array
String restoreString = manipulatedString.restoreString(array); //restoring string
assertEquals(restoreString, "rat"); //asserting restored string
}

@Test
public void testRestoreString2()
{
fail("Not yet implemented");

public void testRestoreString2() {
assertThrows(IllegalArgumentException.class, () -> {
manipulatedString.setString("invalid"); //setting string
int[] array = {1, 0}; //invalid indices array
manipulatedString.restoreString(array); //should throw exception
});
}

@Test
public void testRestoreString3()
{
fail("Not yet implemented");

public void testRestoreString3() {
assertThrows(IllegalArgumentException.class, () -> {
manipulatedString.setString("outofbounds"); //setting string
int[] array = {1, 0, 20}; //out of bounds indices array
manipulatedString.restoreString(array); //should throw exception
});
}

@Test
public void testRestoreString4()
{
fail("Not yet implemented");

public void testRestoreString4() {
// Test case to verify the restoreString() method when the input string is "team"
manipulatedString.setString("team");
// and the indices array is {-1, 0, 1, 2}.
int[] array = new int[]{-1, 0, 1, 2};
// It should throw an IndexOutOfBoundsException.
assertThrows(IndexOutOfBoundsException.class, ()->{manipulatedString.restoreString(array);});
}

@Test
public void testRestoreString5()
{
fail("Not yet implemented");
public void testRestoreString5(){
// Test case to verify the restoreString() method when the input string is "group"
manipulatedString.setString("group");
// and the indices array is {5, 0, 1, 2, 3}.
int[] array = new int[]{5, 0, 1, 2, 3};
// It should throw an IndexOutOfBoundsException.
assertThrows(IndexOutOfBoundsException.class, ()->{manipulatedString.restoreString(array);});
}

@Test
public void testRestoreString6() {
// Test case to verify the restoreString() method when the input string is null
manipulatedString.setString(null);
// and the indices array is {2, 3, 5, 4, 1, 0}.
int[] array = new int[]{2, 3, 5, 4, 1, 0};
// It should throw an IllegalArgumentException.
assertThrows(IllegalArgumentException.class, ()->{manipulatedString.restoreString(array);});
}

}
Comment thread
ACBxBC marked this conversation as resolved.
@Test
public void testRestoreString7() {
// Test case to verify the restoreString() method when the input string is "MASTER IS"
manipulatedString.setString("MASTER IS");
// and the indices array is {5, 4, 0, 1, 2, 3, 6, 7, 8}.
int[] array = new int[]{5, 4, 0, 1, 2, 3, 6, 7, 8};
// It should return the expected output "STERAM IS".
assertEquals("STERAM IS", manipulatedString.restoreString(array));
}
}