2023-12-27 19:52:33 +01:00
|
|
|
package de.hs_mannheim.informatik.spreadsheet;
|
|
|
|
|
2023-12-28 01:36:37 +01:00
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2023-12-27 19:52:33 +01:00
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
|
|
|
class SpreadsheetTest {
|
|
|
|
|
2023-12-28 01:36:37 +01:00
|
|
|
String[][] cells = {{"1","2"},{"2","4"},{"3","6"}};
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
2023-12-27 19:52:33 +01:00
|
|
|
}
|
|
|
|
}
|