From 073ef835ede84a9b53c06237b44bec9e535e2e8b Mon Sep 17 00:00:00 2001 From: Philipp3107 Date: Thu, 22 Dec 2022 12:29:04 +0100 Subject: [PATCH] Insertionsort implementiert und Tests erweitert --- domain/C3PO.java | 24 +++++------ domain/C3POTest.java | 94 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 12 deletions(-) diff --git a/domain/C3PO.java b/domain/C3PO.java index 8390c99..53ad85d 100644 --- a/domain/C3PO.java +++ b/domain/C3PO.java @@ -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"); diff --git a/domain/C3POTest.java b/domain/C3POTest.java index 23fa4d8..d7bdeb9 100644 --- a/domain/C3POTest.java +++ b/domain/C3POTest.java @@ -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())); + } + } + + } \ No newline at end of file