Insertionsort implementiert und Tests erweitert

main
Philipp3107 2022-12-22 12:29:04 +01:00
parent d7a505607d
commit 073ef835ed
2 changed files with 106 additions and 12 deletions

View File

@ -11,21 +11,21 @@ public class C3PO extends RobotBasics {
super(id, name); super(id, name);
} }
public String ausgabe(int[] input) throws RobotException{ // public String ausgabe(int[] input) throws RobotException{
if(input.length != 0 && !checkArray(input)){ // if(input.length != 0 && !checkArray(input)){
return Arrays.stream(input) // return Arrays.stream(input)
.mapToObj(Integer::toString) // .mapToObj(Integer::toString)
.collect(Collectors.joining("; ")); // .collect(Collectors.joining("; "));
}else{ // }else{
throw new RobotException(robotExceptions.MAGICVALUE, getName()); // throw new RobotException(robotExceptions.MAGICVALUE, getName());
// throw new RobotMagicValueException(getName() + " has an unknown Error. Code 42."); // // throw new RobotMagicValueException(getName() + " has an unknown Error. Code 42.");
} // }
} // }
@Override @Override
public String speak(int[] input) throws RobotException { public String speak(int[] input) throws RobotException {
if(isPowerOn()){ if(isPowerOn()){
try{ try{
return ausgabe(input); return ausgabe(input, ";");
}catch(RobotException re){ }catch(RobotException re){
return re.toString(); return re.toString();
} }
@ -39,7 +39,7 @@ public class C3PO extends RobotBasics {
public int[] sorting(int[] input) throws RobotException{ public int[] sorting(int[] input) throws RobotException{
if(input.length != 0 ){ if(input.length != 0 ){
if(checkArray(input)){ if(checkArray(input)){
//sort return insertionSort(input);
}else{ }else{
throw new RobotException(robotExceptions.MAGICVALUE, getName()); throw new RobotException(robotExceptions.MAGICVALUE, getName());
//throw new RobotMagicValueException(getName() + " has an unknown error. Code 42"); //throw new RobotMagicValueException(getName() + " has an unknown error. Code 42");

View File

@ -1,6 +1,8 @@
package domain; package domain;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import robot.exceptions.RobotException;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class C3POTest { class C3POTest {
@ -42,6 +44,98 @@ class C3POTest {
assertTrue(Herbert.isPowerOn()); 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()));
}
}
} }