1
0
Fork 0
PR1-Spreadsheet/Axel/test/de/hs_mannheim/informatik/spreadsheet/SpreadsheetTest.java

198 lines
6.2 KiB
Java

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.*;
class SpreadsheetTest {
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);
}
}
}