JUnit5 function tests done.
parent
33bdf1fabc
commit
69c01f6bec
|
@ -1,9 +1,10 @@
|
||||||
=2,,,=SUMME(A1:A6),,,,,,,,
|
=MITTELWERT(B1:C10),=10,=234,,,,,,,
|
||||||
,,,,,,,,,,,
|
,,,,,,,,,
|
||||||
,,,,,,,,,,,
|
,,,,,,,,,
|
||||||
,,,,,,,,,,,
|
,,,,,,,,,
|
||||||
,,,,,,,,,,,
|
,,,,,,,,,
|
||||||
,,,,,,,,,,,
|
,,,,,,,,,
|
||||||
,,,,,,,,,,,
|
,,,,,,,,,
|
||||||
,,,,,,,,,,,
|
,,,,,,,,,
|
||||||
,,,,,,,,,,,
|
,,,,,,,,,
|
||||||
|
,,,,,,,,,
|
||||||
|
|
|
|
@ -7,7 +7,7 @@ import java.util.Scanner;
|
||||||
/**
|
/**
|
||||||
* Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim.
|
* Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim.
|
||||||
*
|
*
|
||||||
* @author Oliver Hummel
|
* @author Oliver Hummel, Selim Eser (2211482)
|
||||||
*/
|
*/
|
||||||
public class Axel {
|
public class Axel {
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ package de.hs_mannheim.informatik.spreadsheet;
|
||||||
* Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim.
|
* Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim.
|
||||||
* A cell needs to be able to hold a formula and a value
|
* A cell needs to be able to hold a formula and a value
|
||||||
*
|
*
|
||||||
* @author Oliver Hummel
|
* @author Oliver Hummel, Selim Eser (2211482)
|
||||||
*/
|
*/
|
||||||
public class Cell {
|
public class Cell {
|
||||||
private String formula = "";
|
private String formula = "";
|
||||||
|
|
|
@ -14,7 +14,7 @@ import java.util.regex.Pattern;
|
||||||
* A simplified spreadsheet class for the PR1 programming lab at Hochschule Mannheim.
|
* A simplified spreadsheet class for the PR1 programming lab at Hochschule Mannheim.
|
||||||
* One aspect worth mentioning is that it only supports long numbers, not doubles.
|
* One aspect worth mentioning is that it only supports long numbers, not doubles.
|
||||||
*
|
*
|
||||||
* @author Selim Eser (2211482)
|
* @author Oliver Hummel, Selim Eser (2211482)
|
||||||
*/
|
*/
|
||||||
public class Spreadsheet {
|
public class Spreadsheet {
|
||||||
Cell[][] cells;
|
Cell[][] cells;
|
||||||
|
|
|
@ -1,10 +1,198 @@
|
||||||
package de.hs_mannheim.informatik.spreadsheet;
|
package de.hs_mannheim.informatik.spreadsheet;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
class SpreadsheetTest {
|
class SpreadsheetTest {
|
||||||
|
|
||||||
@org.junit.jupiter.api.Test
|
String[][] cells = {{"1","2"},{"2","4"},{"3","6"}};
|
||||||
void sum() {
|
|
||||||
|
@Test
|
||||||
|
void sumTest() {
|
||||||
|
|
||||||
|
double result = 0;
|
||||||
|
|
||||||
|
for(int r = 0; r<cells.length; r++)
|
||||||
|
for(int c = 0; c<cells[r].length; c++)
|
||||||
|
if(!cells[r][c].isEmpty())
|
||||||
|
result += Double.parseDouble(cells[r][c]);
|
||||||
|
|
||||||
|
assertEquals(18, result);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void productTest() {
|
||||||
|
|
||||||
|
double result = 0;
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
for(int r = 0; r<cells.length; r++)
|
||||||
|
for(int c = 0; c<cells[r].length; c++)
|
||||||
|
if(!cells[r][c].isEmpty())
|
||||||
|
if(counter>0)
|
||||||
|
result *= Double.parseDouble(cells[r][c]);
|
||||||
|
else {
|
||||||
|
result = Double.parseDouble(cells[r][c]);
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
assertEquals(288,result);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void averageTest() {
|
||||||
|
|
||||||
|
double result = 0;
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
for(int r = 0; r<cells.length; r++)
|
||||||
|
for(int c = 0; c<cells[r].length; c++)
|
||||||
|
if(!cells[r][c].isEmpty()) {
|
||||||
|
result += Double.parseDouble(cells[r][c]);
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(result!=0)
|
||||||
|
result /= counter;
|
||||||
|
|
||||||
|
assertEquals(3,result);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void standardDeviationTest() {
|
||||||
|
ArrayList<String> cellNames = new ArrayList<>();
|
||||||
|
|
||||||
|
double result = 0;
|
||||||
|
double avg = 0;
|
||||||
|
int counter = 0;
|
||||||
|
for(int r = 0; r<cells.length; r++)
|
||||||
|
for(int c = 0; c<cells[r].length; c++)
|
||||||
|
if(!cells[r][c].isEmpty()){
|
||||||
|
avg += Double.parseDouble(cells[r][c]);
|
||||||
|
counter++;
|
||||||
|
cellNames.add(cells[r][c]);
|
||||||
|
}
|
||||||
|
avg /= counter;
|
||||||
|
//average/ add cell names to list
|
||||||
|
|
||||||
|
ArrayList<String> copyCellNames = new ArrayList<>(cellNames);
|
||||||
|
double[] frequency = new double[cellNames.size()];
|
||||||
|
Arrays.fill(frequency,1);
|
||||||
|
ArrayList<Double> relativeFrequency = new ArrayList<>();
|
||||||
|
double mem = 0;
|
||||||
|
|
||||||
|
for(int i = 0; i< cellNames.size(); i++) {
|
||||||
|
for(int t = 0; t<cellNames.size(); t++){
|
||||||
|
if((t!=i)&&(!cellNames.get(i).isEmpty())&&(cellNames.get(i).equals(cellNames.get(t)))){
|
||||||
|
cellNames.remove(t);
|
||||||
|
frequency[i] ++;
|
||||||
|
t = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//delete all duplicates
|
||||||
|
|
||||||
|
for(int i = 0; i< cellNames.size(); i++)
|
||||||
|
relativeFrequency.add(i,frequency[i]/copyCellNames.size());
|
||||||
|
|
||||||
|
if(cellNames.get(0).isEmpty())
|
||||||
|
mem = ((0 - avg)*(0 - avg))
|
||||||
|
* relativeFrequency.get(0);
|
||||||
|
|
||||||
|
else
|
||||||
|
mem = ((Double.parseDouble(cellNames.get(0)) - avg)*(Double.parseDouble(cellNames.get(0)) - avg))
|
||||||
|
* relativeFrequency.get(0);
|
||||||
|
|
||||||
|
for(int i = 1; i<cellNames.size(); i++)
|
||||||
|
if(cellNames.get(i).isEmpty())
|
||||||
|
mem += ((0 - avg)*(0 - avg))
|
||||||
|
* relativeFrequency.get(i);
|
||||||
|
else
|
||||||
|
mem += ((Double.parseDouble(cellNames.get(i)) - avg)*(Double.parseDouble(cellNames.get(i)) - avg))
|
||||||
|
* relativeFrequency.get(i);
|
||||||
|
result = Math.sqrt(mem);
|
||||||
|
|
||||||
|
assertEquals(1.632993161855452,result);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void minTest() {
|
||||||
|
ArrayList<String> cellNames = new ArrayList<>();
|
||||||
|
|
||||||
|
for(int r = 0; r< cells.length; r++)
|
||||||
|
for(int c = 0; c<cells[r].length; c++)
|
||||||
|
if(!cells[r][c].isEmpty())
|
||||||
|
cellNames.add(cells[r][c]);
|
||||||
|
|
||||||
|
double result = Double.parseDouble(cellNames.get(0));
|
||||||
|
for(int i = 0; i< cellNames.size(); i++)
|
||||||
|
if (result > Double.parseDouble(cellNames.get(i)))
|
||||||
|
result = Double.parseDouble(cellNames.get(i));
|
||||||
|
|
||||||
|
assertEquals(1,result);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void maxTest() {
|
||||||
|
ArrayList<String> cellNames = new ArrayList<>();
|
||||||
|
|
||||||
|
for(int r = 0; r< cells.length; r++)
|
||||||
|
for(int c = 0; c<cells[r].length; c++)
|
||||||
|
if(!cells[r][c].isEmpty())
|
||||||
|
cellNames.add(cells[r][c]);
|
||||||
|
|
||||||
|
double result = Double.parseDouble(cellNames.get(0));
|
||||||
|
for(int i = 0; i< cellNames.size(); i++)
|
||||||
|
if (result < Double.parseDouble(cellNames.get(i)))
|
||||||
|
result = Double.parseDouble(cellNames.get(i));
|
||||||
|
|
||||||
|
assertEquals(6,result);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void calculate() throws ArithmeticException {
|
||||||
|
Matcher m = Pattern.compile("([A-Z][1-9][0-9]*)|[-+*/]|[0-9]+").matcher("15+"+cells[1][1]+"*5");
|
||||||
|
|
||||||
|
double result = 0;
|
||||||
|
double currentOperand = 0;
|
||||||
|
String currentOperator = "+";
|
||||||
|
boolean firstOperator = true;
|
||||||
|
|
||||||
|
while (m.find()) {
|
||||||
|
String s = m.group();
|
||||||
|
|
||||||
|
if (s.matches(("[0-9]+")))
|
||||||
|
currentOperand = Double.parseDouble(s);
|
||||||
|
else {
|
||||||
|
if (!firstOperator) {
|
||||||
|
result = evaluateOperator(result, currentOperand, currentOperator);
|
||||||
|
} else {
|
||||||
|
result = currentOperand;
|
||||||
|
firstOperator = false;
|
||||||
|
}
|
||||||
|
currentOperator = s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!firstOperator) {
|
||||||
|
result = evaluateOperator(result, currentOperand, currentOperator);
|
||||||
|
} else
|
||||||
|
result = currentOperand;
|
||||||
|
|
||||||
|
assertEquals(95,result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private double evaluateOperator(double leftOperand, double rightOperand, String currentOperator) {
|
||||||
|
switch (currentOperator) {
|
||||||
|
case "+":
|
||||||
|
return (leftOperand + rightOperand);
|
||||||
|
case "-":
|
||||||
|
return (leftOperand - rightOperand);
|
||||||
|
case "*":
|
||||||
|
return (leftOperand * rightOperand);
|
||||||
|
case "/":
|
||||||
|
return (leftOperand / rightOperand);
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("Invalid operator: " + currentOperator);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue