diff --git a/.classpath b/.classpath index fb50116..3e0fb27 100644 --- a/.classpath +++ b/.classpath @@ -2,5 +2,6 @@ + diff --git a/src/kernel/Cell.java b/src/kernel/Cell.java index 6e11457..11e9530 100644 --- a/src/kernel/Cell.java +++ b/src/kernel/Cell.java @@ -56,6 +56,7 @@ public class Cell { } public void setFormula(Formula formula) { + this.formula = formula; this.updateValue(); this.spread(); diff --git a/src/kernel/exception/CycleFoundedException.java b/src/kernel/exception/CycleFoundedException.java new file mode 100644 index 0000000..bd1918c --- /dev/null +++ b/src/kernel/exception/CycleFoundedException.java @@ -0,0 +1,5 @@ +package kernel.exception; + +public class CycleFoundedException extends Exception { + +} diff --git a/src/kernel/function/Average.java b/src/kernel/function/Average.java index a3f3527..f504d58 100644 --- a/src/kernel/function/Average.java +++ b/src/kernel/function/Average.java @@ -15,6 +15,7 @@ public class Average extends Function { } public Double eval() { + OptionalDouble average = this.listCells.stream() .mapToDouble(Cell::getValue) .average(); diff --git a/src/kernel/operation/Addition.java b/src/kernel/operation/Addition.java index 9b3d175..5e3d7d7 100644 --- a/src/kernel/operation/Addition.java +++ b/src/kernel/operation/Addition.java @@ -11,6 +11,7 @@ public class Addition extends BinaryOperation { } public Double eval() { + return this.leftCell.getValue() + this.rightCell.getValue(); } } diff --git a/src/kernel/test/Test.java b/src/kernel/test/Test.java new file mode 100644 index 0000000..5a4f572 --- /dev/null +++ b/src/kernel/test/Test.java @@ -0,0 +1,133 @@ +package kernel.test; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import kernel.Cell; +import kernel.function.Sum; +import kernel.operation.Addition; + + +public class Test { + + @org.junit.Test + public void CreateCellValueTest() { + Cell A1= new Cell("A",1,25.); + assertEquals(A1.getValue(),25.,0); + assertEquals(A1.containFormula(),false); + assertEquals(A1.getUsedIn().size(),0); + assertEquals(A1.getId(),"A1"); + } + + + @org.junit.Test + public void CreateCellBinaryOperationTest() { + Cell A1= new Cell("A",1,25.); + Cell A2= new Cell("A",2,35.); + + + Cell A3= new Cell("A",3,new Addition(A1,A2)); + + assertEquals(A3.getValue(),60.,0); + assertEquals(A3.containFormula(),true); + assertEquals(A3.getUsedIn().size(),0); + assertEquals(A3.getId(),"A3"); + + assertEquals(A3.toString(),"A1 + A2"); + assertEquals(A1.getUsedIn().size(),1); + assertEquals(A2.getUsedIn().size(),1); + + + } + + + @org.junit.Test + public void CreateCellFunctionTest() { + Cell A1= new Cell("A",1,25.); + Cell A2= new Cell("A",2,35.); + Cell A3= new Cell("A",3,new Addition(A1,A2)); + Cell A5= new Cell("A",5,45.); + List sumList = new ArrayList<>(); + sumList.add(A1); + sumList.add(A2); + sumList.add(A3); + sumList.add(A5); + Cell A4= new Cell("A",4,new Sum(sumList)); + + assertEquals(A4.getValue(),165.,0); + assertEquals(A4.containFormula(),true); + assertEquals(A4.getUsedIn().size(),0); + + + assertEquals(A4.toString(),"SOMME(A1,A2,A3,A5)"); + assertEquals(A4.getDevelopedFormula(),"SOMME(A1,A2,A1 + A2,A5)"); + assertEquals(A1.getUsedIn().size(),2); + assertEquals(A2.getUsedIn().size(),2); + assertEquals(A3.getUsedIn().size(),1); + assertEquals(A5.getUsedIn().size(),1); + + + } + + + @org.junit.Test + public void ModifyCellValueTest() { + Cell A1= new Cell("A",1,25.); + Cell A2= new Cell("A",2,35.); + Cell A3= new Cell("A",3,new Addition(A1,A2)); + assertEquals(A1.getValue(),25.,0); + assertEquals(A2.getValue(),35.,0); + assertEquals(A3.getValue(),60.,0); + A1.setValue(45.); + assertEquals(A1.getValue(),45.,0); + assertEquals(A3.getValue(),80.,0); + + } + + @org.junit.Test + public void ModifyCellFormulaTest() { + Cell A1= new Cell("A",1,25.); + Cell A2= new Cell("A",2,35.); + Cell A3= new Cell("A",3,new Addition(A1,A2)); + Cell A5= new Cell("A",5,45.); + List sumList = new ArrayList<>(); + sumList.add(A1); + sumList.add(A2); + sumList.add(A3); + sumList.add(A5); + Cell A4= new Cell("A",4,new Sum(sumList)); + + assertEquals(A4.getValue(),165.,0); + assertEquals(A4.containFormula(),true); + assertEquals(A4.getUsedIn().size(),0); + + assertEquals(A1.containFormula(),false); + A1.setFormula(new Addition(A2,A5)); + assertEquals(A1.containFormula(),true); + + assertEquals(A1.getValue(),80.,0); + assertEquals(A3.getValue(),115.,0); + assertEquals(A4.getValue(),275.,0); + + assertEquals(A1.getUsedIn().size(),2); + assertEquals(A2.getUsedIn().size(),2); + assertEquals(A3.getUsedIn().size(),1); + assertEquals(A5.getUsedIn().size(),1); + + + assertEquals(A1.toString(),"A2 + A5"); + assertEquals(A4.getDevelopedFormula(),"SOMME(A2 + A5,A2,A1 + A2,A5)"); + + + assertEquals(A1.getUsedIn().size(),2); + assertEquals(A2.getUsedIn().size(),3); + assertEquals(A3.getUsedIn().size(),1); + assertEquals(A5.getUsedIn().size(),2); + + + } + + +} -- libgit2 0.21.2