Created
March 5, 2018 13:09
-
-
Save edipetres/ff673951075057d2c19b6d64ac842b8c to your computer and use it in GitHub Desktop.
Testing-course: JUnit exercise with Ronnie part 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package impl; | |
import java.io.IOException; | |
public interface Handler { | |
/** | |
* Read a file | |
* @param filename The name of the file to read | |
* @return The contents of the file as string | |
* @throws IOException if file is missing or locked | |
*/ | |
public String readFile(String filename) throws IOException; | |
/** | |
* Converts the String's content to an int array | |
* @param content The file content as a String | |
* @return an int array of numbers | |
*/ | |
public int[] convertToArray(String content); | |
/** | |
* Sorts an unordered array | |
* @param unsorted The array that has to be sorted | |
* @return the sorted array | |
*/ | |
public int[] sortArray(int[] unsorted); | |
/** | |
* Tells us if a pair of numbers in the array can add up to the sum given | |
* @param sum The amount a pair needs to equal | |
* @param array The array whose values should add up to sum | |
* @return truth value of the claim | |
*/ | |
public boolean addsUpTo(int sum, int[] array); | |
/** | |
* Calculates if the array only contains prime numbers | |
* @param array The sorted array containing the numbers to investigate | |
* @return Whether the array consists of only primes or not | |
*/ | |
public boolean isOnlyPrimes(int[] array); | |
/** | |
* Extracts the even numbers from the array given | |
* @param array The sorted array containing the numbers to investigate | |
* @return An array of even numbers and an empty array if there is none. | |
*/ | |
public int[] getEvenNumbers(int[] array); | |
/** | |
* Calculate the sum of the numbers in the array | |
* @param array The sorted array containing the numbers to investigate | |
* @return The sum of the numbers | |
*/ | |
public int calculateSum(int[] array); | |
/** | |
* Merge two integer arrays into one | |
* @param array1 First integer array to merge | |
* @param array2 Second integer array to merge | |
* @return The merged array | |
*/ | |
public int[] mergeArrays(int[] array1, int[] array2); | |
/** | |
* Reverse an ordinary array of numbers | |
* @param array The array whose numbers need to be changed | |
* @return The reversed array | |
*/ | |
public int[] reverseArray(int[] array); | |
/** | |
* Removes the duplicate values | |
* @param array It may or may not have duplicate values | |
* @return An array with only the unique values | |
*/ | |
public int[] removeDuplicates(int[] array); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package impl; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import impl.Handler; | |
/** | |
* | |
* @author eddmond | |
*/ | |
public class HandlerImpl implements Handler { | |
public static final String FILENAME = "numbers.txt"; | |
@Override | |
public int[] sortArray(int[] array) { | |
Arrays.sort(array); | |
return array; | |
} | |
@Override | |
public String readFile(String filename) throws IOException { | |
String contents = new String(Files.readAllBytes(Paths.get(filename))); | |
return contents; | |
} | |
@Override | |
public int[] convertToArray(String content) { | |
String[] strContent = content.split(" "); | |
// count number of values | |
int numberOfIntValues = 0; | |
for (String strNum : strContent) { | |
try { | |
int num = Integer.parseInt(strNum); | |
numberOfIntValues++; | |
} catch (Exception ex) { | |
} | |
} | |
// create array as big as needed | |
int[] intArray = new int[numberOfIntValues]; | |
int i = 0; | |
for (String strNum : strContent) { | |
try { | |
int number = Integer.parseInt(strNum); | |
intArray[i] = number; | |
} catch (Exception e) { | |
System.out.println("Caught exception: " + e); | |
} | |
i++; | |
} | |
return intArray; | |
} | |
@Override | |
public boolean addsUpTo(int sum, int[] array) { | |
int count = 0; | |
for (int number : array) { | |
count =+ number; | |
} | |
if (sum == count) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
@Override | |
public boolean isOnlyPrimes(int[] array) { | |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public int[] getEvenNumbers(int[] array) { | |
int[] evens = new int[array.length]; | |
int index = 0; | |
for (int number : array) { | |
if (number % 2 == 0) { | |
evens[index] = number; | |
index++; | |
} | |
} | |
return evens; | |
} | |
@Override | |
public int calculateSum(int[] array) { | |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public int[] mergeArrays(int[] array1, int[] array2) { | |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public int[] reverseArray(int[] array) { | |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. | |
} | |
@Override | |
public int[] removeDuplicates(int[] array) { | |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package impl; | |
import static org.junit.jupiter.api.Assertions.*; | |
import org.junit.jupiter.api.Test; | |
import java.io.IOException; | |
class HandlerImplTest { | |
private final Handler handler = new HandlerImpl(); | |
@Test | |
public void testReadFile() { | |
String expected = "5 12 65 33 2 11 10 98 66 -12 -9 0 10 12"; | |
try { | |
String actual = handler.readFile("numbers.txt"); | |
assertEquals(expected, actual); | |
} catch (IOException ex) { | |
fail("File not found!"); | |
} catch (Exception e) { | |
fail(e.getMessage()); | |
} | |
} | |
@Test | |
public void testSortArray() { | |
int[] unsorted = new int[] {1, 4, 5, 3, 2, -1, 0}; | |
int[] expected = new int[] {-1, 0, 1, 2, 3, 4, 5}; | |
int[] actual = handler.sortArray(unsorted); | |
assertArrayEquals(expected, actual); | |
} | |
@Test | |
public void testConvertToArray() { | |
String content = "0 1 2 3 4 5"; | |
int[] actual = handler.convertToArray(content); | |
int[] expected = new int[] {0, 1, 2, 3, 4, 5}; | |
assertArrayEquals(expected, actual); | |
} | |
@Test | |
public void testConvertToArrayInvalidValues() { | |
String content = "mdasmodfkas fsdjiofsd"; | |
int[] actual = handler.convertToArray(content); | |
int expectedLenght = 0; | |
assertEquals(expectedLenght, actual.length); | |
} | |
@Test | |
public void testEvens() { | |
// equivalence classes: | |
// even numbers: [-2, 0, 10] | |
int[] numbers = new int[] {-3, 0, 1, 4}; | |
int[] expected = new int[] {0, 4}; | |
int[] actual = handler.getEvenNumbers(numbers); | |
assertArrayEquals(expected, actual); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment