diff --git a/.travis.yml b/.travis.yml index 0fbc4f2..72c3016 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: java -jdk: openjdk17 +jdk: openjdk19 jobs: include: - script: mvn clean verify package diff --git a/pom.xml b/pom.xml index 6330c14..7562579 100644 --- a/pom.xml +++ b/pom.xml @@ -1,73 +1,140 @@ - + 4.0.0 - - org.example - UnitTestAssignment - 1.0-SNAPSHOT - + 1111 + 1111 + 1.0 - 15 - 15 + 4.2.1 + 3.3 + 2.7.7 + 1.8 + 1.8 + UTF-8 + 0.6.0 + 4.2.1 + + + + org.springframework.boot + spring-boot-dependencies + 2.3.0.RELEASE + pom + import + + + + + org.springframework.boot + spring-boot-starter-actuator + provided + + + javax + javaee-api + 8.0 + provided + + + org.eclipse.persistence + org.eclipse.persistence.jpa + ${version.eclipselink} + provided + + + com.wizzdi + flexicore-api + ${flexicore-api.version} + provided + + + io.swagger.core.v3 + swagger-jaxrs2 + 2.1.2 + provided + + + org.pf4j + pf4j-spring + ${pf4j-spring.version} + provided + + + org.springframework.boot + spring-boot-starter-test + test + + + junit + junit + + + org.junit.jupiter - junit-jupiter-api - 5.7.2 + junit-jupiter-engine test + - org.hamcrest - hamcrest-library - 2.2 + org.junit.platform + junit-platform-launcher + test + + + org.junit.vintage + junit-vintage-engine + test + + + com.wizzdi + FlexiCore + ${FlexiCore.version} + test + + + com.wizzdi + flexicore-entities-provider + 1.0.1 test + + + true + src/main/resources + + - org.apache.maven.plugins - maven-surefire-plugin - 3.0.0-M5 + maven-compiler-plugin + 3.8.1 - org.jacoco - jacoco-maven-plugin - 0.8.7 + maven-shade-plugin + 3.2.1 + package - prepare-agent - - - - report - test - - report - - - - jacoco-check - - check + shade - - - PACKAGE - - - LINE - COVEREDRATIO - 0.9 - - - - + false + true + ${java.io.tmpdir}/dependency-reduced-pom.xml + + + + ${artifactId} + ${version} + + + + diff --git a/src/main/java/StringManipulation.java b/src/main/java/StringManipulation.java index 4deb6f1..07602ca 100644 --- a/src/main/java/StringManipulation.java +++ b/src/main/java/StringManipulation.java @@ -1,5 +1,5 @@ public class StringManipulation implements StringManipulationInterface { - + String myString; @Override public String getString() { return null; @@ -7,27 +7,173 @@ public String getString() { @Override public void setString(String string) { + this.myString = string; } @Override public int count() { - return 0; + int tally = 0; + if(myString == null || myString.length() == 0){ + return tally; + } + // " here is a string" 4 + // "a here is a string" 4 + // "a " 1 + // "here" 1 + for (int i = 0; i < myString.length(); i++){ + char currentChar = myString.charAt(i); + if(currentChar != ' '){ + tally++; + while(currentChar != ' '){ + i++; + if(i >= myString.length()){ + break; + } + currentChar = myString.charAt(i); + } + } + while (currentChar == ' '){ + i++; + if(i >= myString.length()){ + break; + } + currentChar = myString.charAt(i); + } + + + } + return tally; } @Override + // n = 3; aaabbbaaaccc -> aabbaacc + // public String removeNthCharacter(int n, boolean maintainSpacing) { - return null; + if(n <= 0){ + throw new IllegalArgumentException("n must be greater than or equal to 0"); + } + else if (n > myString.length()){ + throw new IndexOutOfBoundsException(n); + } + String buildString = ""; + if (maintainSpacing) { + if (n == 1){ + for (int i = 0; i < myString.length(); i++){ + buildString += " "; + } + return buildString; + } + int prior = 0; + for(int i = 0; i < myString.length(); i++){ + if ((i+1) % n == 0){ + String slice = myString.substring(prior, i); + buildString += slice; + buildString += " "; + prior = i + 1; + } + if (i == myString.length() - 1 && myString.length() % n != 0){ + String slice = myString.substring(prior); + buildString += slice; + } + } + } + else { + // todo need to update criteria for n == 1 + if (n == 1){ + return buildString; + } + int prior = 0; + for(int i = 0; i < myString.length(); i++){ + if ((i+1) % n == 0){ + String slice = myString.substring(prior, i); + buildString += slice; + prior = i + 1; + } + if (i == myString.length() - 1 && myString.length() % n != 0){ + String slice = myString.substring(prior); + buildString += slice; + } + } + } + return buildString; } @Override - public String[] getSubStrings(int startWord, int endWord) { - return null; + public String[] getSubStrings(int startWord, int endWord){ + if (startWord <= 0 || endWord <= 0 || endWord < startWord){ + throw new IllegalArgumentException(); + } + int numWords = this.count(); + if (endWord > numWords){ + throw new IndexOutOfBoundsException(endWord); + } + String[] array = new String[1 + endWord - startWord]; + for (int i = 0; i < array.length; i++){ + array[i] = null; + } + int arrayPosition = 0; + int wordCount = 0; + while(array[array.length - 1] == null){ + char firstChar = myString.charAt(0); + for (int i = 0; i < myString.length(); i++){ + char currentChar = myString.charAt(i); + while (currentChar == ' '){ + i++; + if(i >= myString.length()){ + return array; + } + currentChar = myString.charAt(i); + } + String currentWord = ""; + while(currentChar != ' '){ + i++; + currentWord += String.valueOf(currentChar); + if(i == myString.length()){ + array[arrayPosition] = currentWord; + return array; + } + currentChar = myString.charAt(i); + } + wordCount++; + if(wordCount >= startWord){ + array[arrayPosition] = currentWord; + arrayPosition++; + } + if (arrayPosition >= array.length){ + return array; + } + } + } + return array; } @Override - public String restoreString(int[] indices) { - return null; + public String restoreString(int[] indices){ + if(indices.length != myString.length()){ + throw new IllegalArgumentException(); + } + else { + for (int i = 0; i < indices.length; i++){ + int num = indices[i]; + if(num > myString.length() - 1 || num < 0){ + throw new IndexOutOfBoundsException(i); + } + } + } + String convString = ""; + String[] arrayString = new String[myString.length()]; + String[] converted = new String[myString.length()]; + for(int i = 0; i < myString.length(); i++){ + String currentLetter = String.valueOf(myString.charAt(i)); + arrayString[i] = currentLetter; + } + for(int i = 0; i < myString.length(); i++){ + int index = indices[i]; + converted[i] = arrayString[index]; + } + for (int i = 0; i < myString.length(); i++){ + convString += converted[i]; + } + return convString; } - - } diff --git a/src/test/java/StringManipulationTest.java b/src/test/java/StringManipulationTest.java index 6692c2c..747438c 100644 --- a/src/test/java/StringManipulationTest.java +++ b/src/test/java/StringManipulationTest.java @@ -1,7 +1,3 @@ -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.*; @@ -9,12 +5,12 @@ public class StringManipulationTest { private StringManipulationInterface manipulatedstring; - @BeforeEach + @Before public void setUp() { manipulatedstring = new StringManipulation(); } - @AfterEach + @After public void tearDown() { manipulatedstring = null; } @@ -28,17 +24,26 @@ public void testCount1() { @Test public void testCount2() { - fail("Not yet implemented"); + // test checks for leading whitespace being ignored + manipulatedstring.setString(" this is string"); + int num = manipulatedstring.count(); + assertEquals(3, num); } @Test public void testCount3() { - fail("Not yet implemented"); + // test checks for a single word in string + manipulatedstring.setString("string"); + int num = manipulatedstring.count(); + assertEquals(1, num); } - @Test + @org.junit.Test public void testCount4() { - fail("Not yet implemented"); + // test checks for a string that ends in whitespace + manipulatedstring.setString("a string "); + int num = manipulatedstring.count(); + assertEquals(2, num); } @Test @@ -55,27 +60,37 @@ public void testRemoveNthCharacter2() { @Test public void testRemoveNthCharacter3() { - fail("Not yet implemented"); + // test removes only last character + manipulatedstring.setString("last."); + assertEquals("last",manipulatedstring.removeNthCharacter(5, false)); } @Test public void testRemoveNthCharacter4() { - fail("Not yet implemented"); + // tests for no whitepace while maintiaining spacing + manipulatedstring.setString("maintainspacing"); + assertEquals("ma nt in pa in ", manipulatedstring.removeNthCharacter(3,true)); } @Test public void testRemoveNthCharacter5() { - fail("Not yet implemented"); - } + // test removes every character without spacing + manipulatedstring.setString("test remove every character(1) without spacing"); + assertEquals("", manipulatedstring.removeNthCharacter(1, false)); + } @Test public void testRemoveNthCharacter6() { - fail("Not yet implemented"); + //remove each char but keep spacing + manipulatedstring.setString("string"); + assertEquals(" ", manipulatedstring.removeNthCharacter(1,true)); } @Test public void testRemoveNthCharacter7() { - fail("Not yet implemented"); + // test n > 9 + manipulatedstring.setString("one last test for anyone"); + assertEquals("one last est for ayone", manipulatedstring.removeNthCharacter(10, false)); } @Test @@ -89,25 +104,42 @@ public void testGeSubStrings1() { @Test public void testGeSubStrings2() { - fail("Not yet implemented"); + manipulatedstring.setString("This is my string"); + String [] sStings = manipulatedstring.getSubStrings(1, 4); + assertEquals(sStings[0], "This"); + assertEquals(sStings[3], "string"); } + @Test public void testGeSubStrings3() { - fail("Not yet implemented"); + // test for ignoring leading whitespace + manipulatedstring.setString(" This is my string"); + String [] sStings = manipulatedstring.getSubStrings(1, 4); + assertEquals(sStings[0], "This"); + assertEquals(sStings[3], "string"); } @Test public void testGeSubStrings4() { - fail("Not yet implemented"); + // test for ignoring whitespace in string + manipulatedstring.setString("This is my string"); + String [] sStings = manipulatedstring.getSubStrings(1, 4); + assertEquals(sStings[0], "This"); + assertEquals(sStings[3], "string"); } @Test public void testGeSubStrings5() { - fail("Not yet implemented"); + // test for correctly retrieving one substring in one word + manipulatedstring.setString("onlyoneword"); + String [] sStings = manipulatedstring.getSubStrings(1,1); + assertEquals("onlyoneword", sStings[0]); } @Test public void testGeSubStrings6() { - fail("Not yet implemented"); + // test for correctly retrieving one substring in multiple word string + manipulatedstring.setString("only one substring"); + String [] sStings = manipulatedstring.getSubStrings(1,1); + assertEquals("only", sStings[0]); } - @Test public void testRestoreString1() { @@ -121,29 +153,45 @@ public void testRestoreString1() @Test public void testRestoreString2() { - fail("Not yet implemented"); - + // test swapping end and beginning chars + manipulatedstring.setString("foot"); + int [] array; + array=new int[]{3,1,2,0}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "toof"); } @Test public void testRestoreString3() { - fail("Not yet implemented"); - + // test reversing order + manipulatedstring.setString("racecar"); + int [] array; + array=new int[]{6,5,4,3,2,1,0}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString, "racecar"); } @Test public void testRestoreString4() { - fail("Not yet implemented"); - + // test single char word + manipulatedstring.setString("a"); + int [] array; + array=new int[]{0}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString,"a"); } @Test public void testRestoreString5() { - fail("Not yet implemented"); - + // test empty string + manipulatedstring.setString(""); + int [] array; + array=new int[]{}; + String restoreString = manipulatedstring.restoreString(array); + assertEquals(restoreString,""); } }