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, testspr)); } for (String failureTest : failureTestList) { assertFalse(Axel.userPositionErrorCheck(failureTest, testspr)); } //? Out of bounds test Spreadsheet smallTestSpr = 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, smallTestSpr)); } for (String failureTest : failureBoundsTest) { assertFalse(Axel.userPositionBoundsErrorCheck(failureTest, smallTestSpr)); } } @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(); 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)); } } @org.junit.jupiter.api.Test @org.junit.jupiter.api.DisplayName("Sum") void sum(){ putTestSpr(); String[] successTestList = {"SUMME(A1:B2)", "summe(B1:e1)", "Summe(B4:I10)", "SUMME(K1:K11)", "SUMME(F1:H1)", "SUMME(F4:G5)", "SUMME(A3:a3)"}; long[] successResultList = {14, 5, 85, 300, 0, 8, 3}; for (int i = 0; i < successTestList.length; i++) { String testElement = successTestList[i].toUpperCase(); assertEquals(successResultList[i], testspr.sum(testElement)); } } @org.junit.jupiter.api.Test @org.junit.jupiter.api.DisplayName("Product") void prod(){ putTestSpr(); String[] successTestList = {"PRODUKT(A1:B2)", "PRODUKT(A2:B3)", "PRODUKT(B1:C4)", "Produkt(F1:H1)", "produkt(f4:g5)", "PRODUKT(A3:a3)"}; long[] successResultList = {60, 252, 1680, 0, 18, 3}; for (int i = 0; i < successTestList.length; i++) { String testElement = successTestList[i].toUpperCase(); assertEquals(successResultList[i], testspr.prod(testElement)); } } @org.junit.jupiter.api.Test @org.junit.jupiter.api.DisplayName("Average") void avrg(){ putTestSpr(); String[] successTestList = {"MITTELWERT(A1:B4)", "MITTELWERT(A3:B4)", "MITTELWERT(A1:A4)", "mittelwert(K1:K11)","Mittelwert(F1:H1)" ,"MITTELWERT(f4:g5)", "MITTELWERT(A3:a3)"}; long[] successResultList = {4, 5, 2, 150, 0, 2, 3}; for (int i = 0; i < successTestList.length; i++) { String testElement = successTestList[i].toUpperCase(); assertEquals(successResultList[i], testspr.avrg(testElement)); } } @org.junit.jupiter.api.Test @org.junit.jupiter.api.DisplayName("Standard deviation") void stdDevp(){ putTestSpr(); String[] successTestList = {"STABW(A1:B4)", "STABW(A2:B3)", "STABW(A1:A4)", "STABW(K1:K11)", "stabw(f1:H1)", "Stabw(f4:g5)", "STAbW(a3:a3)"}; long[] successResultList = {2, 2, 1, 70, 0, 1, 0}; for (int i = 0; i < successTestList.length; i++) { String testElement = successTestList[i].toUpperCase(); assertEquals(successResultList[i], testspr.stdDevp(testElement)); } } @org.junit.jupiter.api.Test @org.junit.jupiter.api.DisplayName("Min value") void min(){ putTestSpr(); String[] successTestList = {"MIN(A1:B4)", "MIN(A2:B3)", "MIN(A1:A4)", "MIN(K1:K11)", "min(f1:H1)", "Min(f4:g5)", "min(a3:a3)"}; long[] successResultList = {1, 2, 1, 100, 0, 2, 3}; for (int i = 0; i < successTestList.length; i++) { String testElement = successTestList[i].toUpperCase(); assertEquals(successResultList[i], testspr.min(testElement)); } } @org.junit.jupiter.api.Test @org.junit.jupiter.api.DisplayName("Max value") void max(){ putTestSpr(); String[] successTestList = {"MAX(A1:B4)", "MAX(A2:B3)", "MAX(A1:A4)", "MAX(K1:K11)", "max(f1:H1)", "Max(f4:g5)", "max(a3:a3)"}; long[] successResultList = {8, 7, 4, 200, 0, 3, 3}; for (int i = 0; i < successTestList.length; i++) { String testElement = successTestList[i].toUpperCase(); assertEquals(successResultList[i], testspr.max(testElement)); } } private void putTestSpr(){ 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"); testspr.put("F5", "3"); testspr.put("G4", "2"); testspr.put("G5", "3"); } }