Insertionsort implementiert und Tests erweitert
parent
d7a505607d
commit
073ef835ed
|
@ -11,21 +11,21 @@ public class C3PO extends RobotBasics {
|
|||
super(id, name);
|
||||
}
|
||||
|
||||
public String ausgabe(int[] input) throws RobotException{
|
||||
if(input.length != 0 && !checkArray(input)){
|
||||
return Arrays.stream(input)
|
||||
.mapToObj(Integer::toString)
|
||||
.collect(Collectors.joining("; "));
|
||||
}else{
|
||||
throw new RobotException(robotExceptions.MAGICVALUE, getName());
|
||||
// throw new RobotMagicValueException(getName() + " has an unknown Error. Code 42.");
|
||||
}
|
||||
}
|
||||
// public String ausgabe(int[] input) throws RobotException{
|
||||
// if(input.length != 0 && !checkArray(input)){
|
||||
// return Arrays.stream(input)
|
||||
// .mapToObj(Integer::toString)
|
||||
// .collect(Collectors.joining("; "));
|
||||
// }else{
|
||||
// throw new RobotException(robotExceptions.MAGICVALUE, getName());
|
||||
// // throw new RobotMagicValueException(getName() + " has an unknown Error. Code 42.");
|
||||
// }
|
||||
// }
|
||||
@Override
|
||||
public String speak(int[] input) throws RobotException {
|
||||
if(isPowerOn()){
|
||||
try{
|
||||
return ausgabe(input);
|
||||
return ausgabe(input, ";");
|
||||
}catch(RobotException re){
|
||||
return re.toString();
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ public class C3PO extends RobotBasics {
|
|||
public int[] sorting(int[] input) throws RobotException{
|
||||
if(input.length != 0 ){
|
||||
if(checkArray(input)){
|
||||
//sort
|
||||
return insertionSort(input);
|
||||
}else{
|
||||
throw new RobotException(robotExceptions.MAGICVALUE, getName());
|
||||
//throw new RobotMagicValueException(getName() + " has an unknown error. Code 42");
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package domain;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import robot.exceptions.RobotException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class C3POTest {
|
||||
|
@ -42,6 +44,98 @@ class C3POTest {
|
|||
assertTrue(Herbert.isPowerOn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void speak(){
|
||||
Herbert.triggerPowerSwitch();
|
||||
String solution = "12; 2; 4; 5; 12; 2; 4; 7; 56; 433; 23";
|
||||
int[] input = {12, 2, 4, 5, 12, 2, 4, 7, 56, 433, 23};
|
||||
String array = "";
|
||||
try{
|
||||
array = Herbert.speak(input);
|
||||
}catch(RobotException re){
|
||||
System.out.println(re);
|
||||
}
|
||||
assertEquals(0, array.compareTo(solution));
|
||||
}
|
||||
|
||||
@Test
|
||||
void think(){
|
||||
|
||||
int[] solution = { 2, 4, 4, 5, 7, 12, 23, 56, 433};
|
||||
int[] input = { 4, 5, 12, 2, 4, 7, 56, 433, 23};
|
||||
int[] value = new int[9];
|
||||
try{
|
||||
value = Herbert.think(input);
|
||||
}catch(RobotException re){
|
||||
System.out.println(re);
|
||||
}
|
||||
Herbert.triggerPowerSwitch();
|
||||
try{
|
||||
value = Herbert.think(input);
|
||||
}catch(RobotException re){
|
||||
System.out.println(re);
|
||||
}
|
||||
|
||||
for(int i = 0; i < input.length; i++){
|
||||
assertEquals(solution[i], value[i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void magicValueException(){
|
||||
int[] input = {3,2,42};
|
||||
Herbert.triggerPowerSwitch();
|
||||
String expectedMessage = "Herbert has an unknown error. Code 42.";
|
||||
try{
|
||||
int[] solution = Herbert.think(input);
|
||||
}catch(RobotException re){
|
||||
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
||||
}
|
||||
try{
|
||||
String test = Herbert.speak(input);
|
||||
}catch(RobotException re){
|
||||
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void illegalStateException(){
|
||||
int[] input = {3,2,42};
|
||||
String expectedMessage = "Herbert is turned off.";
|
||||
try{
|
||||
int[] solution = Herbert.think(input);
|
||||
}catch(RobotException re){
|
||||
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
||||
}
|
||||
try{
|
||||
String test = Herbert.speak(input);
|
||||
}catch(RobotException re){
|
||||
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void arrayEmptyException(){
|
||||
String expectedMessage = "Herbert got an empty array.";
|
||||
Herbert.triggerPowerSwitch();
|
||||
try{
|
||||
int[] solution = Herbert.think(new int[0]);
|
||||
}catch(RobotException re){
|
||||
System.out.println(re);
|
||||
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
||||
}
|
||||
|
||||
try{
|
||||
String test = Herbert.speak(new int[0]);
|
||||
}catch(RobotException re){
|
||||
System.out.println(re);
|
||||
assertEquals(0, expectedMessage.compareTo(re.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue