1
0
Fork 0
PR1-Spreadsheet/Axel/tests/de/hs_mannheim/informatik/spreadsheet/AxelTest.java

163 lines
6.6 KiB
Java

package de.hs_mannheim.informatik.spreadsheet;
import static org.junit.jupiter.api.Assertions.*;
class AxelTest {
// --------- Class Vars ---------
Spreadsheet testspr = new Spreadsheet(11,11);
// --------- Input tests ---------
@org.junit.jupiter.api.Test
@org.junit.jupiter.api.DisplayName("Command input error check")
void userCommandErrorCheck() {
String[] successTestList = {"*exit", "*save", "*load", "*help", "*eXiT", "*SAVE", "*LoAd", "*hElP"};
String[] failureTestList = {"exit", "save", "load", "help", "eXiT", "SAVE", "LoAd", "hElP", "*exit*", "*save*", "*load*", "*help*", "*eXiT*", "*SAVE*", "*LoAd*", "*hElP*", "*test", "*TEST", "*saev", "*xeit"};
for (String successTest : successTestList) {
assertTrue(Axel.userCommandErrorCheck(successTest));
}
for (String failureTest : failureTestList) {
assertFalse(Axel.userCommandErrorCheck(failureTest));
}
}
@org.junit.jupiter.api.Test
@org.junit.jupiter.api.DisplayName("Position input error check")
void userPositionErrorCheck() {
//? Overall format test
String[] successTestList = {"A1", "B14", "C79", "E99", "F1", "G99", "J1", "M98", "N67", "o45", "p23", "q12", "r2"};
String[] failureTestList = {"A100", "Z101", "AA1", "AB2", "ZZ1", "A1A", "FHT", "AAA", "G3U", "ZZZ", "2A", "47H", "AAAA", "test", "TEST", "save", "*save", "*exit"};
for (String successTest : successTestList) {
assertTrue(Axel.userPositionErrorCheck(successTest));
}
for (String failureTest : failureTestList) {
assertFalse(Axel.userPositionErrorCheck(failureTest));
}
//? Out of bounds test
Spreadsheet testSpr = new Spreadsheet(6,8);
String[] successBoundsTest = {"A1", "a1", "b3", "c5", "A3", "H6", "h6"};
String[] failureBoundsTest = {"i2", "J6", "B7", "K9", "Z99"};
for (String successTest : successBoundsTest) {
assertTrue(Axel.userPositionBoundsErrorCheck(successTest, testSpr));
}
for (String failureTest : failureBoundsTest) {
assertFalse(Axel.userPositionBoundsErrorCheck(failureTest, testSpr));
}
}
@org.junit.jupiter.api.Test
@org.junit.jupiter.api.DisplayName("Value input error check")
void userValueErrorCheck() {
String[] successTestList = {"0", "1", "-1", "1234", "4324", "-465"};
String[] failureTestList = {"a", "aa", "A!", "A1", "wef", "-a", "a-","-1a"};
for (String successTest : successTestList) {
assertTrue(Axel.userValueErrorCheck(successTest));
}
for (String failureTest : failureTestList) {
assertFalse(Axel.userValueErrorCheck(failureTest));
}
}
@org.junit.jupiter.api.Test
@org.junit.jupiter.api.DisplayName("Formula input error check")
void userFormulaErrorCheck() {
}
// --------- Calculation tests ---------
@org.junit.jupiter.api.Test
@org.junit.jupiter.api.DisplayName("Normal calculation")
void calculate() {
putTestSpr(testspr);
String[] successTestList = {"=2*3", "=2+3", "=5-2", "=6/2", "7/2", "101/2", "=5+6-10*2", "=4-3+50-1/2", "=A1", "=K11", "=A1+A2+A3+A4", "=K11/2/5*2-15", "=a1+A2*a4-6+E7", "=a2-A1+J4-3"};
long[] successTestResult = {6, 5, 3, 3, 3, 50, 2, 25, 1, 100, 10, 5, 6, 2};
String[] failureTestList = {"5+1", "7-4", "6*2", "15/5", "5+4", "10-7", "5*3", "9/3", "=A1", "=K11", "=A1+A3", "=K11-10/2+B1"};
long[] failureTestResult = {5, 7, 10, 4, 8, 4, 16, 2, 2, 200, 3, 40};
for (int i = 0; i < successTestList.length; i++) {
// Uppercasing the formula happens in the code too, just in a different place which is not tested
String formula = successTestList[i].toUpperCase();
assertEquals(successTestResult[i], testspr.calculate(formula));
}
for (int i = 0; i < failureTestList.length; i++) {
String formula = failureTestList[i].toUpperCase();
assertNotEquals(failureTestResult[i], testspr.calculate(failureTestList[i]));
}
}
@org.junit.jupiter.api.Test
@org.junit.jupiter.api.DisplayName("Is CellName")
void isValueCellName(){
String[] successValueTestList = {"7", "0", "1", "420", "424", "23212"};
String[] successCellNameTestList = {"A1", "K11", "B6", "g5", "k11"};
for (String successValue : successValueTestList){
successValue = successValue.toUpperCase();
assertEquals("value", testspr.isValueCellName(successValue));
}
for (String successCellName : successCellNameTestList){
successCellName = successCellName.toUpperCase();
assertEquals("cellName", testspr.isValueCellName(successCellName));
}
}
@org.junit.jupiter.api.Test
@org.junit.jupiter.api.DisplayName("Isolate Corner")
void isolateFunctionCorners(){
String[] successTestList = {"SUMME(A1:B3)", "SUMME(a3:k5)", "PRODUKT(C3:G6)", "PRODUKT(c2:g5)", "MITTELWERT(A2:j9)", "mittelwert(b4:H8)", "STABW(c7:g7)", "MIN(a1:A6)", "max(f4:h7)", "MAX(E2:j7)"};
String[][] successResultList = {{"A1", "B3"}, {"A3", "K5"}, {"C3", "G6"}, {"C2", "G5"}, {"A2", "J9"}, {"B4", "H8"}, {"C7", "G7"}, {"A1", "A6"}, {"F4", "H7"}, {"E2", "J7"}};
for (int i = 0; i < successTestList.length; i++){
String formula = successTestList[i].toUpperCase();
assertArrayEquals(successResultList[i], testspr.isolateFunctionCorners(formula));
}
}
@org.junit.jupiter.api.Test
@org.junit.jupiter.api.DisplayName("Extract whole block")
void wholeFunctionBlock(){
String[] successTestList = {"SUMME(A1:C4)", "summe(F4:H7)", "Max(D4:D7)", "min(a1:d1)"};
String[][] successResultList = {{"A1", "B1", "C1", "A2", "B2", "C2", "A3", "B3", "C3", "A4", "B4", "C4"}, {"F4", "G4", "H4", "F5", "G5", "H5", "F6", "G6", "H6", "F7", "G7", "H7"}, {"D4", "D5", "D6", "D7"}, {"A1", "B1", "C1", "D1"}};
for (int i = 0; i < successTestList.length; i++){
String testElement = successTestList[i].toUpperCase();
assertArrayEquals(successResultList[i], testspr.wholeFunctionBlock(testElement));
}
}
private void putTestSpr(Spreadsheet testspr){
testspr.put("A1", "1");
testspr.put("A2", "2");
testspr.put("A3", "3");
testspr.put("A4", "4");
testspr.put("B1", "5");
testspr.put("B2", "6");
testspr.put("B3", "7");
testspr.put("B4", "8");
testspr.put("K1", "200");
testspr.put("K11", "100");
testspr.put("J4", "4");
testspr.put("I10", "69");
}
}