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
63 changes: 56 additions & 7 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,82 @@
public class StringManipulation implements StringManipulationInterface {
private String string;

@Override
public String getString() {
return null;
return string;
}

@Override
public void setString(String string) {
this.string = string;
}

@Override
public int count() {
return 0;
if (string == null) {
return 0;
}
String[] words = string.trim().split("\\s+");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trim() is probably an unnecessary step to separate word by whitespace.

return words.length;
}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
if (string == null) {
return null;
}
if (n <= 0) {
throw new IllegalArgumentException("n must be greater than zero");
}
if (n > string.length()) {
throw new IndexOutOfBoundsException("n exceeds string length");
}
StringBuilder result = new StringBuilder();
char[] chars = string.toCharArray();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like you ever do anything with the chars variable.

int count = 0;
for (char c : string.toCharArray()) {
count++;
if (count % n != 0) {
result.append(c);
} else if (maintainSpacing) {
result.append(' ');
}
}
return result.toString();
}

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
public String[] getSubStrings(int startWord, int endWord){
if (string == null) {
return null;
}
String[] words = string.trim().split("\\s+");
int numWords = words.length;

if (startWord <= 0 || endWord <= 0 || startWord > endWord) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be more of a style choice, but wouldn't it make more sense to throw exceptions before executing business logic?

throw new IllegalArgumentException("Invalid startWord or endWord values");
}
if (endWord > numWords) {
throw new IndexOutOfBoundsException("The string has fewer than endWord words");
}
return java.util.Arrays.copyOfRange(words, startWord - 1, endWord);
}

@Override
public String restoreString(int[] indices) {
return null;
if (string == null || indices == null || string.length() != indices.length) {
throw new IllegalArgumentException("Invalid arguments: string length must be equal to indices length");
}
char[] chars = string.toCharArray();
char[] restoredChars = new char[chars.length];
for (int i = 0; i < indices.length; i++) {
int index = indices[i];
if (index < 0 || index >= chars.length) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make more sense to verify the integrity of your data prior to when you start executing the logic of the method?

throw new IndexOutOfBoundsException("Invalid index: indices[i] must be within the string length and unique");
}
restoredChars[i] = chars[index];
}
return new String(restoredChars);
}


}
114 changes: 82 additions & 32 deletions src/test/java/StringManipulationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,142 +7,192 @@

public class StringManipulationTest {

private StringManipulationInterface manipulatedstring;
private StringManipulationInterface manipulatedString;

@BeforeEach
public void setUp() {
manipulatedstring = new StringManipulation();
manipulatedString = new StringManipulation();
}

@AfterEach
public void tearDown() {
manipulatedstring = null;
manipulatedString = null;
}

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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider adding more variety or some edge cases to your testing. Maybe add some sentences with unconventional spacing for characters.

manipulatedString.setString("I like video games");
int length = manipulatedString.count();
assertEquals(4, length);
}

@Test
public void testCount3() {
fail("Not yet implemented");
manipulatedString.setString("What");
int length = manipulatedString.count();
assertEquals(1, length);
}

@Test
public void testCount4() {
fail("Not yet implemented");
manipulatedString.setString("Why there are so many test cases to create");
int length = manipulatedString.count();
assertEquals(9, length);
}

@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?");
assertEquals("I' bttr uts0e 16tsinths trn6 rgh?", manipulatedString.removeNthCharacter(3, false));
}

@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?");
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");
manipulatedString.setString("Holy mama");
assertEquals("Hl aa", manipulatedString.removeNthCharacter(2, false));
}

@Test
public void testRemoveNthCharacter4() {
fail("Not yet implemented");
manipulatedString.setString("Holy mama");
assertEquals("H l a a", manipulatedString.removeNthCharacter(2, true));
}

@Test
public void testRemoveNthCharacter5() {
fail("Not yet implemented");
manipulatedString.setString("Holy mama");
assertEquals("Hol ma a", manipulatedString.removeNthCharacter(4, true));
}

@Test
public void testRemoveNthCharacter6() {
fail("Not yet implemented");
manipulatedString.setString("Exception testing1");
assertEquals("Don't mind this", manipulatedString.removeNthCharacter(0, true));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JUnit does provide a assertThrows() method to test exception handling. It would probably be better than relying on the exception methods returning string.

}

@Test
public void testRemoveNthCharacter7() {
fail("Not yet implemented");
manipulatedString.setString("Exception testing2");
assertEquals("Don't mind this", manipulatedString.removeNthCharacter(19, true));
}

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

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

@Test
public void testGeSubStrings2() {
fail("Not yet implemented");
manipulatedString.setString("This is my string");
String [] sStings = manipulatedString.getSubStrings(1, 2);

assertEquals(sStings[0], "This");
assertEquals(sStings[1], "is");
}
@Test
public void testGeSubStrings3() {
fail("Not yet implemented");
manipulatedString.setString("Why there are so many test cases to create");
String [] sStings = manipulatedString.getSubStrings(1, 2);

assertEquals(sStings[0], "Why");
assertEquals(sStings[1], "there");
}
@Test
public void testGeSubStrings4() {
fail("Not yet implemented");
manipulatedString.setString("Why there are so many test cases to create");
String [] sStings = manipulatedString.getSubStrings(1, 3);

assertEquals(sStings[0], "Why");
assertEquals(sStings[1], "there");
assertEquals(sStings[2], "are");
}
@Test
public void testGeSubStrings5() {
fail("Not yet implemented");
manipulatedString.setString("Why there are so many test cases to create");
String [] sStings = manipulatedString.getSubStrings(0, 6);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this testing if an exception is thrown? What are the mechanics behind this?

assertEquals(sStings[0], "many");
assertEquals(sStings[1], "test");
// Exception1
}
@Test
public void testGeSubStrings6() {
fail("Not yet implemented");
manipulatedString.setString("Why there are so many test cases to create");
String [] sStings = manipulatedString.getSubStrings(5, 11);

assertEquals(sStings[0], "many");
assertEquals(sStings[1], "test");
assertEquals(sStings[2], "cases");
// Exception2
}

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

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

manipulatedString.setString("art");
int [] array;
array=new int[]{2,0,1};
String restoreString = manipulatedString.restoreString(array);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if the array argument uses duplicate values?

assertEquals("tar", restoreString);
}

@Test
public void testRestoreString3()
{
fail("Not yet implemented");
manipulatedString.setString("art");
int [] array;
array=new int[]{2,1,0};
String restoreString = manipulatedString.restoreString(array);
assertEquals("tra", restoreString);

}

@Test
public void testRestoreString4()
{
fail("Not yet implemented");
manipulatedString.setString("art");
int [] array;
array=new int[]{1,0,2,3};
String restoreString = manipulatedString.restoreString(array);
assertEquals("Exception1", restoreString);

}

@Test
public void testRestoreString5()
{
fail("Not yet implemented");
manipulatedString.setString("art");
int [] array;
array=new int[]{1,5,2};
String restoreString = manipulatedString.restoreString(array);
assertEquals("Exception2", restoreString);

}

Expand Down