From 2ba010b6cbbbe7e33dade3215780ddd51cd23129 Mon Sep 17 00:00:00 2001 From: devra Date: Tue, 9 Jan 2024 01:24:19 +0100 Subject: [PATCH] JUnit-tests implementiert!!! --- Axel/.classpath | 1 + .../informatik/spreadsheet/Axel.java | 10 --- .../informatik/spreadsheet/Spreadsheet.java | 18 ++-- .../informatik/spreadsheet/Tests.java | 85 +++++++++++++++++++ 4 files changed, 95 insertions(+), 19 deletions(-) create mode 100644 Axel/src/de/hs_mannheim/informatik/spreadsheet/Tests.java diff --git a/Axel/.classpath b/Axel/.classpath index af6f9dc..fbed15b 100644 --- a/Axel/.classpath +++ b/Axel/.classpath @@ -2,5 +2,6 @@ + diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java index a4f9609..3eadb66 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Axel.java @@ -206,16 +206,6 @@ public class Axel { if(spr1.checkIfCellExists(parts[0])) { spr1.put(parts[0], "=" + parts[1]); } -// else { -// System.err.println("Zelle '"+ parts[0]+ "' existiert nicht!"); -// } - -// if((spr1.getRow(parts[0])+1)<=spr1.cells.length&&(spr1.getCol(parts[0])+1)<=spr1.cells[0].length) { -// spr1.put(parts[0], "=" + parts[1]); -// } -// else { -// System.err.println("Zelle '"+ parts[0]+ "' existiert nicht!"); -// } System.out.println(spr1); } else if (userInput.toLowerCase().contains("exit")) { diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index cff1f6c..982e889 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -167,7 +167,7 @@ public class Spreadsheet { result = "" + mit(onlyCells[0], onlyCells[1]); } } - else if (formula.startsWith("STABW(")) { // e.g. STABW(C6:D8) -> minimal-wert + else if (formula.startsWith("STABW(")) { // e.g. STABW(C6:D8) -> standardabweichung String[] onlyCells = formula.substring(6, formula.length() - 1).split(":"); if(checkIfCellExists(onlyCells[0])&&checkIfCellExists(onlyCells[1])) { @@ -175,7 +175,7 @@ public class Spreadsheet { } } - else if (formula.startsWith("MIN(")) { + else if (formula.startsWith("MIN(")) { // e.g. min(C6:D8) -> minimal-wert String[] onlyCells = formula.substring(4, formula.length() - 1).split(":"); if(checkIfCellExists(onlyCells[0])&&checkIfCellExists(onlyCells[1])) { result = "" + min(onlyCells[0], onlyCells[1]); @@ -212,7 +212,7 @@ public class Spreadsheet { * @param endCellName The name of the cell in the lower right corner of the rectangle. * @return The sum calculated. */ - private long sum(String startCellName, String endCellName) { + public long sum(String startCellName, String endCellName) { int werte[]=getValues(startCellName,endCellName); int summe=0; @@ -228,7 +228,7 @@ public class Spreadsheet { * @param endCellName The name of the cell in the lower right corner of the rectangle. * @return The product calculated. */ - private long pro(String startCellName, String endCellName) { + public long pro(String startCellName, String endCellName) { int werte[]=getValues(startCellName,endCellName); int produkt=1; @@ -244,7 +244,7 @@ public class Spreadsheet { * @param endCellName The name of the cell in the lower right corner of the rectangle. * @return The average calculated. */ - private long mit(String startCellName, String endCellName) { + public long mit(String startCellName, String endCellName) { int werte[]=getValues(startCellName,endCellName); int summe=0; @@ -260,7 +260,7 @@ public class Spreadsheet { * @param endCellName The name of the cell in the lower right corner of the rectangle. * @return The standard deviation calculated. */ - private double statbw(String startCellName, String endCellName) { + public double statbw(String startCellName, String endCellName) { int werte[]=getValues(startCellName,endCellName); if (werte.length < 2) { @@ -292,7 +292,7 @@ public class Spreadsheet { * @param endCellName The name of the cell in the lower right corner of the rectangle. * @return The minimum-value calculated. */ - private long min(String startCellName, String endCellName) { + public long min(String startCellName, String endCellName) { int werte[]=getValues(startCellName,endCellName); if (werte.length == 0) { @@ -316,7 +316,7 @@ public class Spreadsheet { * @param endCellName The name of the cell in the lower right corner of the rectangle. * @return The maximum-value calculated. */ - private long max(String startCellName, String endCellName) { + public long max(String startCellName, String endCellName) { int werte[]=getValues(startCellName,endCellName); if (werte.length == 0) { @@ -377,7 +377,7 @@ public class Spreadsheet { * @param formula The expression to be evaluated. * @return The result calculated. */ - private long calculate(String formula) throws ArithmeticException { + public long calculate(String formula) throws ArithmeticException { //System.out.println(formula); Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula); diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Tests.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Tests.java new file mode 100644 index 0000000..3b91ba2 --- /dev/null +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Tests.java @@ -0,0 +1,85 @@ +package de.hs_mannheim.informatik.spreadsheet; + +import static org.junit.Assert.assertEquals; + +import org.junit.jupiter.api.Test; + +class Tests { + Spreadsheet spr2=new Spreadsheet(10,10); + + //Grundrechenarten + @Test + void testAddition() { + + assertEquals(spr2.calculate("=1+1"),2); + } + + @Test + void testSubtraction() { + assertEquals(spr2.calculate("=5-3"), 2); + } + + @Test + void testMultiplication() { + assertEquals(spr2.calculate("=2*3"), 6); + } + + @Test + void testDivision() { + assertEquals(spr2.calculate("=10/2"), 5); + } + + + + //Komplexere Methoden + @Test + void testSum() { + spr2.put("A1", "1"); + spr2.put("A2", "2"); + spr2.put("A3", "3"); + assertEquals(spr2.sum("A1","A3"), 6); + } + + @Test + void testProduct() { + spr2.put("A1", "1"); + spr2.put("A2", "2"); + spr2.put("A3", "3"); + assertEquals(spr2.pro("A1","A3"), 6); + } + + @Test + void testMedian() { + spr2.put("A1", "1"); + spr2.put("A2", "2"); + spr2.put("A3", "3"); + assertEquals(spr2.mit("A1","A3"), 2); + } + + @Test + void testStandardDeviation() { + spr2.put("A1", "1"); + spr2.put("A2", "2"); + spr2.put("A3", "3"); + assertEquals(spr2.statbw("A1","A3"),0.8, 0.1); + } + + @Test + void testMin() { + spr2.put("A1", "1"); + spr2.put("A2", "2"); + spr2.put("A3", "3"); + assertEquals(spr2.min("A1","A3"),1); + } + + @Test + void testMax() { + spr2.put("A1", "1"); + spr2.put("A2", "2"); + spr2.put("A3", "3"); + assertEquals(spr2.max("A1","A3"),3); + } + + + +}