diff --git a/src/app/Application.java b/src/app/Application.java index fcd7c07..b0d1902 100644 --- a/src/app/Application.java +++ b/src/app/Application.java @@ -4,7 +4,7 @@ import kernel.Cell; import kernel.Grid; import kernel.exception.CellNotFoundException; import kernel.exception.CreateCycleException; -import kernel.exception.InvalidIntervalLineColumnEception; +import kernel.exception.InvalidIntervalException; import kernel.function.Average; import kernel.function.Sum; import kernel.operation.Addition; @@ -46,7 +46,7 @@ public class Application { averageList.add(grid.getCell("B", 1)); grid.createCell("B", 2, new Average(averageList)); - } catch (CellNotFoundException | CreateCycleException | InvalidIntervalLineColumnEception exception) { + } catch (CellNotFoundException | CreateCycleException | InvalidIntervalException exception) { System.out.println(exception.getMessage()); } diff --git a/src/ihm/TablooProto.java b/src/ihm/TablooProto.java index 4bbb072..164b40c 100644 --- a/src/ihm/TablooProto.java +++ b/src/ihm/TablooProto.java @@ -25,7 +25,7 @@ import javax.swing.table.TableColumn; import kernel.Grid; import kernel.exception.CellNotFoundException; -import kernel.exception.InvalidIntervalLineColumnEception; +import kernel.exception.InvalidIntervalException; public class TablooProto extends JPanel { @@ -207,7 +207,7 @@ public class TablooProto extends JPanel { else try { calc.createCell(this.getColumnName(col), row+1, Double.parseDouble((String)value)); - } catch (InvalidIntervalLineColumnEception e) { + } catch (InvalidIntervalException e) { // TODO Auto-generated catch block e.printStackTrace(); } diff --git a/src/kernel/Cell.java b/src/kernel/Cell.java index 3221591..b1b9f03 100644 --- a/src/kernel/Cell.java +++ b/src/kernel/Cell.java @@ -1,102 +1,102 @@ package kernel; import kernel.exception.CreateCycleException; -import kernel.exception.InvalidIntervalLineColumnEception; +import kernel.exception.InvalidIntervalException; import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class Cell implements Serializable { - - private static final long serialVersionUID = 1L; - private static final int MAX_LIGNES = 20; - - private String column; - private int line; - private double value; - private Formula formula; - private List usedIn = new ArrayList<>(); - - public Cell(String column, int line, double value) throws InvalidIntervalLineColumnEception { - column = column.toUpperCase(); - if (!validateInterval(column, line)) - throw new InvalidIntervalLineColumnEception(); - - this.column = column; - this.line = line; - this.setValue(value); - } - - public Cell(String column, int line, Formula formula) - throws CreateCycleException, InvalidIntervalLineColumnEception { - column = column.toUpperCase(); - if (!validateInterval(column, line)) - throw new InvalidIntervalLineColumnEception(); - - this.column = column; - this.line = line; - this.setFormula(formula); - } - - public double getValue() { - return this.value; - } - - public Formula getFormula() { - return this.formula; - } - - public String getDevelopedFormula() { - return this.containFormula() ? this.formula.getDevelopedFormula() : this.getId(); - } - - public String getId() { - return this.column + this.line; - } - - public List getUsedIn() { - return this.usedIn; - } - - public String toString() { - return this.containFormula() ? this.formula.toString() : this.getId(); - } - - public void updateValue() { - if (this.containFormula()) - this.value = this.formula.eval(); - } - - public boolean containFormula() { - return this.formula != null; - } - - public void setFormula(Formula formula) throws CreateCycleException { - if (formula.createCycle(this)) - throw new CreateCycleException(); - - this.formula = formula; - for (Cell cell : this.formula.getUtilisedCells()) - cell.usedIn.add(this); - this.updateValue(); - this.spreadValue(); - } - - public void setValue(double value) { - this.value = value; - this.formula = null; - this.spreadValue(); - } - - private void spreadValue() { - for (Cell cell : this.usedIn) { - cell.updateValue(); - cell.spreadValue(); - } - } - - private boolean validateInterval(String column, int line) { - return line >= 1 && line <= MAX_LIGNES && column.compareTo("A") >= 0 && column.compareTo("Z") <= 0; - } + + private static final long serialVersionUID = 1L; + private static final int MAX_LIGNES = 20; + + private String column; + private int line; + private double value; + private Formula formula; + private List usedIn = new ArrayList<>(); + + public Cell(String column, int line, double value) throws InvalidIntervalException { + column = column.toUpperCase(); + if (!validateInterval(column, line)) + throw new InvalidIntervalException(); + + this.column = column; + this.line = line; + this.setValue(value); + } + + public Cell(String column, int line, Formula formula) + throws CreateCycleException, InvalidIntervalException { + column = column.toUpperCase(); + if (!validateInterval(column, line)) + throw new InvalidIntervalException(); + + this.column = column; + this.line = line; + this.setFormula(formula); + } + + public double getValue() { + return this.value; + } + + public Formula getFormula() { + return this.formula; + } + + public String getDevelopedFormula() { + return this.containFormula() ? this.formula.getDevelopedFormula() : this.getId(); + } + + public String getId() { + return this.column + this.line; + } + + public List getUsedIn() { + return this.usedIn; + } + + public String toString() { + return this.containFormula() ? this.formula.toString() : this.getId(); + } + + public void updateValue() { + if (this.containFormula()) + this.value = this.formula.eval(); + } + + public boolean containFormula() { + return this.formula != null; + } + + public void setFormula(Formula formula) throws CreateCycleException { + if (formula.createCycle(this)) + throw new CreateCycleException(); + + this.formula = formula; + for (Cell cell : this.formula.getUtilisedCells()) + cell.usedIn.add(this); + this.updateValue(); + this.spreadValue(); + } + + public void setValue(double value) { + this.value = value; + this.formula = null; + this.spreadValue(); + } + + private void spreadValue() { + for (Cell cell : this.usedIn) { + cell.updateValue(); + cell.spreadValue(); + } + } + + private boolean validateInterval(String column, int line) { + return line >= 1 && line <= MAX_LIGNES && column.compareTo("A") >= 0 && column.compareTo("Z") <= 0; + } } diff --git a/src/kernel/Formula.java b/src/kernel/Formula.java index 5f2bd89..a6dc8dd 100644 --- a/src/kernel/Formula.java +++ b/src/kernel/Formula.java @@ -3,14 +3,14 @@ package kernel; import java.util.List; public interface Formula { - - String getDevelopedFormula(); - - String toString(); - - double eval(); - - boolean createCycle(Cell cell); - - List getUtilisedCells(); + + String getDevelopedFormula(); + + String toString(); + + double eval(); + + boolean createCycle(Cell cell); + + List getUtilisedCells(); } diff --git a/src/kernel/Grid.java b/src/kernel/Grid.java index ee6da41..0b6d068 100644 --- a/src/kernel/Grid.java +++ b/src/kernel/Grid.java @@ -2,7 +2,7 @@ package kernel; import kernel.exception.CellNotFoundException; import kernel.exception.CreateCycleException; -import kernel.exception.InvalidIntervalLineColumnEception; +import kernel.exception.InvalidIntervalException; import java.io.Serializable; import java.util.ArrayList; @@ -11,86 +11,86 @@ import java.util.List; import java.util.Map; public class Grid implements Serializable { - - private static final long serialVersionUID = 1L; - - private Map cells = new HashMap<>(); - public static LanguageEnum language = LanguageEnum.FR; - private List listLine = new ArrayList(); - private List listColumn=new ArrayList(); - - public void saveDifferentLineColumn(String column, int line) { - if (!this.listLine.contains(line)) - listLine.add(line); - if (!this.listColumn.contains((int)column.charAt(0))) - listColumn.add((int)column.charAt(0)- (int)'A' + 1); - - - } - public String createCell(String column, int line, double value) throws InvalidIntervalLineColumnEception { - String id = this.getCellId(column, line); - Cell cell = new Cell(column, line, value); - - this.cells.put(id, cell); - saveDifferentLineColumn(column,line); - return id; - } - - public String createCell(String column, int line, Formula formula) throws CreateCycleException, InvalidIntervalLineColumnEception { - String id = this.getCellId(column, line); - Cell cell = new Cell(column, line, formula); - - this.cells.put(id, cell); - saveDifferentLineColumn(column,line); - return id; - } - - public void setValue(String column, int line, double value) throws CellNotFoundException { - this.getCell(column, line).setValue(value); - } - - public void setFormula(String column, int line, Formula formula) throws CellNotFoundException, - CreateCycleException { - this.getCell(column, line).setFormula(formula); - } - - public Cell getCell(String column, int line) throws CellNotFoundException { - Cell cell = this.cells.get(this.getCellId(column, line)); - - if (cell != null) - return cell; - else - throw new CellNotFoundException(); - } - - public List getCells() { - return new ArrayList<>(this.cells.values()); - } - - public List getCellsId() { - return new ArrayList<>(this.cells.keySet()); - } - - public double getValue(String column, int line) throws CellNotFoundException { - return this.getCell(column, line).getValue(); - } - - public String getFormulaAsString(String column, int line) throws CellNotFoundException { - return this.getCell(column, line).toString(); - } - - public String getDevelopedFormula(String column, int line) throws CellNotFoundException { - return this.getCell(column, line).getDevelopedFormula(); - } - - private String getCellId(String column, int line) { - return column + line; - } - public List getTotalColumn() { - return listColumn; - } - public List getTotalLine() { - return listLine; - - } + + private static final long serialVersionUID = 1L; + + private Map cells = new HashMap<>(); + public static LanguageEnum language = LanguageEnum.FR; + private List listLine = new ArrayList<>(); + private List listColumn = new ArrayList<>(); + + public void saveDifferentLineColumn(String column, int line) { + if (!this.listLine.contains(line)) + listLine.add(line); + if (!this.listColumn.contains((int) column.charAt(0))) + listColumn.add((int) column.charAt(0) - (int) 'A' + 1); + } + + public String createCell(String column, int line, double value) throws InvalidIntervalException { + String id = this.getCellId(column, line); + Cell cell = new Cell(column, line, value); + + this.cells.put(id, cell); + saveDifferentLineColumn(column, line); + return id; + } + + public String createCell(String column, int line, Formula formula) throws CreateCycleException, InvalidIntervalException { + String id = this.getCellId(column, line); + Cell cell = new Cell(column, line, formula); + + this.cells.put(id, cell); + saveDifferentLineColumn(column, line); + return id; + } + + public void setValue(String column, int line, double value) throws CellNotFoundException { + this.getCell(column, line).setValue(value); + } + + public void setFormula(String column, int line, Formula formula) throws CellNotFoundException, + CreateCycleException { + this.getCell(column, line).setFormula(formula); + } + + public Cell getCell(String column, int line) throws CellNotFoundException { + Cell cell = this.cells.get(this.getCellId(column, line)); + + if (cell != null) + return cell; + else + throw new CellNotFoundException(); + } + + public List getCells() { + return new ArrayList<>(this.cells.values()); + } + + public List getCellsId() { + return new ArrayList<>(this.cells.keySet()); + } + + public double getValue(String column, int line) throws CellNotFoundException { + return this.getCell(column, line).getValue(); + } + + public String getFormulaAsString(String column, int line) throws CellNotFoundException { + return this.getCell(column, line).toString(); + } + + public String getDevelopedFormula(String column, int line) throws CellNotFoundException { + return this.getCell(column, line).getDevelopedFormula(); + } + + private String getCellId(String column, int line) { + return column + line; + } + + public List getTotalColumn() { + return listColumn; + } + + public List getTotalLine() { + return listLine; + } } diff --git a/src/kernel/LanguageEnum.java b/src/kernel/LanguageEnum.java index ea9771c..5c8fc96 100644 --- a/src/kernel/LanguageEnum.java +++ b/src/kernel/LanguageEnum.java @@ -1,6 +1,6 @@ package kernel; public enum LanguageEnum { - FR, - EN + FR, + EN } diff --git a/src/kernel/exception/CellNotFoundException.java b/src/kernel/exception/CellNotFoundException.java index 7c9c8ab..3b059e4 100644 --- a/src/kernel/exception/CellNotFoundException.java +++ b/src/kernel/exception/CellNotFoundException.java @@ -1,7 +1,4 @@ package kernel.exception; public class CellNotFoundException extends Exception { - - - private static final long serialVersionUID = 1L; } diff --git a/src/kernel/exception/CreateCycleException.java b/src/kernel/exception/CreateCycleException.java index 9340e02..8e20268 100644 --- a/src/kernel/exception/CreateCycleException.java +++ b/src/kernel/exception/CreateCycleException.java @@ -1,6 +1,4 @@ package kernel.exception; public class CreateCycleException extends Exception { - - private static final long serialVersionUID = 1L; } diff --git a/src/kernel/exception/InvalidIntervalException.java b/src/kernel/exception/InvalidIntervalException.java new file mode 100644 index 0000000..65e9bc6 --- /dev/null +++ b/src/kernel/exception/InvalidIntervalException.java @@ -0,0 +1,4 @@ +package kernel.exception; + +public class InvalidIntervalException extends Exception { +} diff --git a/src/kernel/exception/InvalidIntervalLineColumnEception.java b/src/kernel/exception/InvalidIntervalLineColumnEception.java deleted file mode 100644 index e0a361b..0000000 --- a/src/kernel/exception/InvalidIntervalLineColumnEception.java +++ /dev/null @@ -1,8 +0,0 @@ -package kernel.exception; - -public class InvalidIntervalLineColumnEception extends Exception { - - - private static final long serialVersionUID = 1L; - -} diff --git a/src/kernel/function/Average.java b/src/kernel/function/Average.java index 3ebd89d..466f874 100644 --- a/src/kernel/function/Average.java +++ b/src/kernel/function/Average.java @@ -7,20 +7,16 @@ import java.util.List; import java.util.OptionalDouble; public class Average extends Function { - - private static final long serialVersionUID = 1L; - - public Average(List listCells) { - super(listCells); - this.names.put(LanguageEnum.FR, "MOYENNE"); - this.names.put(LanguageEnum.EN, "AVERAGE"); - } - - public double eval() { - OptionalDouble average = this.listCells.stream() - .mapToDouble(Cell::getValue) - .average(); - - return average.isPresent() ? average.getAsDouble() : 0.; - } + + public Average(List listCells) { + super(listCells); + this.names.put(LanguageEnum.FR, "MOYENNE"); + this.names.put(LanguageEnum.EN, "AVERAGE"); + } + + public double eval() { + OptionalDouble average = this.listCells.stream().mapToDouble(Cell::getValue).average(); + + return average.isPresent() ? average.getAsDouble() : 0.; + } } diff --git a/src/kernel/function/Function.java b/src/kernel/function/Function.java index 2aeb671..b42d8ff 100644 --- a/src/kernel/function/Function.java +++ b/src/kernel/function/Function.java @@ -12,41 +12,43 @@ import java.util.Map; import java.util.stream.Collectors; abstract public class Function implements Formula, Serializable { - - private static final long serialVersionUID = 1L; - public Map names = new HashMap<>(); - public List listCells; - - public Function(List listCells) { - this.listCells = listCells; - } - - abstract public double eval(); - - public String getDevelopedFormula() { - return names.get(Grid.language) + "(" - + this.listCells.stream() - .map(Cell::getDevelopedFormula) - .collect(Collectors.joining(",")) + ")"; - } - - public String toString() { - return names.get(Grid.language) + "(" - + this.listCells.stream() - .map(Cell::getId) - .collect(Collectors.joining(",")) + ")"; - } - - public boolean createCycle(Cell cell) { - if (!this.listCells.contains(cell)) - for (Cell currentCell : this.listCells) - if (currentCell.containFormula() && currentCell.getFormula().createCycle(cell)) - return true; - - return true; - } - - public List getUtilisedCells() { - return this.listCells; - } + + private static final long serialVersionUID = 1L; + public Map names = new HashMap<>(); + public List listCells; + + public Function(List listCells) { + this.listCells = listCells; + } + + abstract public double eval(); + + public String getDevelopedFormula() { + return names.get(Grid.language) + "(" + + this.listCells.stream() + .map(Cell::getDevelopedFormula) + .collect(Collectors.joining(",")) + ")"; + } + + public String toString() { + return names.get(Grid.language) + "(" + + this.listCells.stream() + .map(Cell::getId) + .collect(Collectors.joining(",")) + ")"; + } + + public boolean createCycle(Cell cell) { + if (!this.listCells.contains(cell)) { + for (Cell currentCell : this.listCells) + if (currentCell.containFormula() && currentCell.getFormula().createCycle(cell)) + return true; + } else + return true; + + return false; + } + + public List getUtilisedCells() { + return this.listCells; + } } diff --git a/src/kernel/function/Sum.java b/src/kernel/function/Sum.java index a0c9451..02667d3 100644 --- a/src/kernel/function/Sum.java +++ b/src/kernel/function/Sum.java @@ -6,18 +6,14 @@ import kernel.LanguageEnum; import java.util.List; public class Sum extends Function { - - private static final long serialVersionUID = 1L; - - public Sum(List listCells) { - super(listCells); - this.names.put(LanguageEnum.FR, "SOMME"); - this.names.put(LanguageEnum.EN, "SUM"); - } - - public double eval() { - return this.listCells.stream() - .mapToDouble(Cell::getValue) - .sum(); - } + + public Sum(List listCells) { + super(listCells); + this.names.put(LanguageEnum.FR, "SOMME"); + this.names.put(LanguageEnum.EN, "SUM"); + } + + public double eval() { + return this.listCells.stream().mapToDouble(Cell::getValue).sum(); + } } diff --git a/src/kernel/operation/Addition.java b/src/kernel/operation/Addition.java index 8a71503..f239c07 100644 --- a/src/kernel/operation/Addition.java +++ b/src/kernel/operation/Addition.java @@ -3,18 +3,16 @@ package kernel.operation; import kernel.Cell; public class Addition extends BinaryOperation { - - private static final long serialVersionUID = 1L; - - public Addition(Cell leftCell, Cell rightCell) { - super(leftCell, rightCell); - } - - public String getOperator() { - return "+"; - } - - public double eval() { - return this.leftCell.getValue() + this.rightCell.getValue(); - } + + public Addition(Cell leftCell, Cell rightCell) { + super(leftCell, rightCell); + } + + public String getOperator() { + return "+"; + } + + public double eval() { + return this.leftCell.getValue() + this.rightCell.getValue(); + } } diff --git a/src/kernel/operation/BinaryOperation.java b/src/kernel/operation/BinaryOperation.java index 1a5f0ab..8e0003d 100644 --- a/src/kernel/operation/BinaryOperation.java +++ b/src/kernel/operation/BinaryOperation.java @@ -8,46 +8,46 @@ import java.util.ArrayList; import java.util.List; abstract public class BinaryOperation implements Formula, Serializable { - - private static final long serialVersionUID = 1L; - protected Cell leftCell; - protected Cell rightCell; - - public BinaryOperation(Cell leftCell, Cell rightCell) { - this.leftCell = leftCell; - this.rightCell = rightCell; - } - - abstract public double eval(); - - abstract public String getOperator(); - - public String getDevelopedFormula() { - return "(" + this.leftCell.getDevelopedFormula() + this.getOperator() + this.rightCell.getDevelopedFormula() + ")"; - } - - public String toString() { - return this.leftCell.getId() + this.getOperator() + this.rightCell.getId(); - } - - public boolean createCycle(Cell cell) { - if (this.leftCell.containFormula() && !this.rightCell.containFormula()) - return this.leftCell.getFormula().createCycle(cell); - - if (!this.leftCell.containFormula() && this.rightCell.containFormula()) - return this.rightCell.getFormula().createCycle(cell); - - if (this.leftCell.containFormula() && this.rightCell.containFormula()) - return this.leftCell.getFormula().createCycle(cell) && this.rightCell.getFormula().createCycle(cell); - - return (cell.getId().equals(this.rightCell.getId()) || cell.getId().equals(this.leftCell.getId())); - } - - public List getUtilisedCells() { - List cells = new ArrayList<>(); - cells.add(this.leftCell); - cells.add(this.rightCell); - - return cells; - } + + private static final long serialVersionUID = 1L; + protected Cell leftCell; + protected Cell rightCell; + + public BinaryOperation(Cell leftCell, Cell rightCell) { + this.leftCell = leftCell; + this.rightCell = rightCell; + } + + abstract public double eval(); + + abstract public String getOperator(); + + public String getDevelopedFormula() { + return "(" + this.leftCell.getDevelopedFormula() + this.getOperator() + this.rightCell.getDevelopedFormula() + ")"; + } + + public String toString() { + return "(" + this.leftCell.getId() + this.getOperator() + this.rightCell.getId() + ")"; + } + + public boolean createCycle(Cell cell) { + if (this.leftCell.containFormula() && !this.rightCell.containFormula()) + return this.leftCell.getFormula().createCycle(cell); + + if (!this.leftCell.containFormula() && this.rightCell.containFormula()) + return this.rightCell.getFormula().createCycle(cell); + + if (this.leftCell.containFormula() && this.rightCell.containFormula()) + return this.leftCell.getFormula().createCycle(cell) && this.rightCell.getFormula().createCycle(cell); + + return (cell.getId().equals(this.rightCell.getId()) || cell.getId().equals(this.leftCell.getId())); + } + + public List getUtilisedCells() { + List cells = new ArrayList<>(); + cells.add(this.leftCell); + cells.add(this.rightCell); + + return cells; + } } diff --git a/src/kernel/operation/Division.java b/src/kernel/operation/Division.java index 51b4734..9db6dfa 100644 --- a/src/kernel/operation/Division.java +++ b/src/kernel/operation/Division.java @@ -3,18 +3,16 @@ package kernel.operation; import kernel.Cell; public class Division extends BinaryOperation { - - private static final long serialVersionUID = 1L; - - public Division(Cell leftCell, Cell rightCell) { - super(leftCell, rightCell); - } - - public String getOperator() { - return "/"; - } - - public double eval() { - return this.leftCell.getValue() / this.rightCell.getValue(); - } + + public Division(Cell leftCell, Cell rightCell) { + super(leftCell, rightCell); + } + + public String getOperator() { + return "/"; + } + + public double eval() { + return this.leftCell.getValue() / this.rightCell.getValue(); + } } diff --git a/src/kernel/operation/Multiplication.java b/src/kernel/operation/Multiplication.java index a626697..fc718c4 100644 --- a/src/kernel/operation/Multiplication.java +++ b/src/kernel/operation/Multiplication.java @@ -3,18 +3,16 @@ package kernel.operation; import kernel.Cell; public class Multiplication extends BinaryOperation { - - private static final long serialVersionUID = 1L; - - public Multiplication(Cell leftCell, Cell rightCell) { - super(leftCell, rightCell); - } - - public String getOperator() { - return "*"; - } - - public double eval() { - return this.leftCell.getValue() * this.rightCell.getValue(); - } + + public Multiplication(Cell leftCell, Cell rightCell) { + super(leftCell, rightCell); + } + + public String getOperator() { + return "*"; + } + + public double eval() { + return this.leftCell.getValue() * this.rightCell.getValue(); + } } diff --git a/src/kernel/operation/Subtraction.java b/src/kernel/operation/Subtraction.java index 02d68ec..1ab7598 100644 --- a/src/kernel/operation/Subtraction.java +++ b/src/kernel/operation/Subtraction.java @@ -3,19 +3,16 @@ package kernel.operation; import kernel.Cell; public class Subtraction extends BinaryOperation { - - private static final long serialVersionUID = 1L; - - public Subtraction(Cell leftCell, Cell rightCell) { - super(leftCell, rightCell); - } - - - public String getOperator() { - return "-"; - } - - public double eval() { - return this.leftCell.getValue() - this.rightCell.getValue(); - } + + public Subtraction(Cell leftCell, Cell rightCell) { + super(leftCell, rightCell); + } + + public String getOperator() { + return "-"; + } + + public double eval() { + return this.leftCell.getValue() - this.rightCell.getValue(); + } } diff --git a/src/kernel/test/BinaryOperationTest.java b/src/kernel/test/BinaryOperationTest.java index b45bc10..b6a97bf 100644 --- a/src/kernel/test/BinaryOperationTest.java +++ b/src/kernel/test/BinaryOperationTest.java @@ -4,7 +4,7 @@ import kernel.Cell; import kernel.Grid; import kernel.LanguageEnum; import kernel.exception.CreateCycleException; -import kernel.exception.InvalidIntervalLineColumnEception; +import kernel.exception.InvalidIntervalException; import kernel.function.Sum; import kernel.operation.Addition; import kernel.operation.Division; @@ -19,108 +19,108 @@ import java.util.List; import static org.junit.Assert.assertEquals; public class BinaryOperationTest { - - private List cells; - - @Before - public void initData() throws CreateCycleException, InvalidIntervalLineColumnEception { - Grid.language = LanguageEnum.EN; - this.cells = new ArrayList<>(); - - Cell a1 = new Cell("A", 1, 10.); - Cell a2 = new Cell("A", 2, 0.); - Cell a3 = new Cell("A", 3, 20.); - - List sumList = new ArrayList<>(); - sumList.add(a1); - sumList.add(a2); - sumList.add(a3); - - Cell b1 = new Cell("B", 1, new Sum(sumList)); - - this.cells.add(a1); - this.cells.add(a2); - this.cells.add(a3); - this.cells.add(b1); - } - - @Test - public void testAddition() { - Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null); - Cell a2 = this.cells.stream().filter(c -> c.getId().equals("A2")).findFirst().orElse(null); - - Addition addition = new Addition(a1, a2); - - assertEquals(10, addition.eval(), 0); - assertEquals("A1+A2", addition.toString()); - assertEquals("A1+A2", addition.getDevelopedFormula()); - - Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null); - - addition = new Addition(a1, b1); - - assertEquals(40, addition.eval(), 0); - assertEquals("A1+B1", addition.toString()); - assertEquals("A1+SUM(A1,A2,A3)", addition.getDevelopedFormula()); - } - - @Test - public void testMultiplication() { - Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null); - Cell a3 = this.cells.stream().filter(c -> c.getId().equals("A3")).findFirst().orElse(null); - - Multiplication multiplication = new Multiplication(a1, a3); - - assertEquals(200, multiplication.eval(), 0); - assertEquals("A1*A3", multiplication.toString()); - assertEquals("A1*A3", multiplication.getDevelopedFormula()); - - Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null); - - multiplication = new Multiplication(a1, b1); - - assertEquals(300, multiplication.eval(), 0); - assertEquals("A1*B1", multiplication.toString()); - assertEquals("A1*SUM(A1,A2,A3)", multiplication.getDevelopedFormula()); - } - - @Test - public void testSubtraction() { - Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null); - Cell a3 = this.cells.stream().filter(c -> c.getId().equals("A3")).findFirst().orElse(null); - - Subtraction subtraction = new Subtraction(a3, a1); - - assertEquals(10, subtraction.eval(), 0); - assertEquals("A3-A1", subtraction.toString()); - assertEquals("A3-A1", subtraction.getDevelopedFormula()); - - Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null); - - subtraction = new Subtraction(a1, b1); - - assertEquals(-20, subtraction.eval(), 0); - assertEquals("A1-B1", subtraction.toString()); - assertEquals("A1-SUM(A1,A2,A3)", subtraction.getDevelopedFormula()); - } - - @Test - public void testDivision() { - Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null); - Cell a3 = this.cells.stream().filter(c -> c.getId().equals("A3")).findFirst().orElse(null); - - Division division = new Division(a3, a1); - - assertEquals(2, division.eval(), 0); - assertEquals("A3/A1", division.toString()); - assertEquals("A3/A1", division.getDevelopedFormula()); - - Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null); - - division = new Division(b1, a1); - - assertEquals(3, division.eval(), 0); - assertEquals("B1/A1", division.toString()); - assertEquals("SUM(A1,A2,A3)/A1", division.getDevelopedFormula()); - } + + private List cells; + + @Before + public void initData() throws CreateCycleException, InvalidIntervalException { + Grid.language = LanguageEnum.EN; + this.cells = new ArrayList<>(); + + Cell a1 = new Cell("A", 1, 10.); + Cell a2 = new Cell("A", 2, 0.); + Cell a3 = new Cell("A", 3, 20.); + + List sumList = new ArrayList<>(); + sumList.add(a1); + sumList.add(a2); + sumList.add(a3); + + Cell b1 = new Cell("B", 1, new Sum(sumList)); + + this.cells.add(a1); + this.cells.add(a2); + this.cells.add(a3); + this.cells.add(b1); + } + + @Test + public void testAddition() { + Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null); + Cell a2 = this.cells.stream().filter(c -> c.getId().equals("A2")).findFirst().orElse(null); + + Addition addition = new Addition(a1, a2); + + assertEquals(10, addition.eval(), 0); + assertEquals("(A1+A2)", addition.toString()); + assertEquals("(A1+A2)", addition.getDevelopedFormula()); + + Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null); + + addition = new Addition(a1, b1); + + assertEquals(40, addition.eval(), 0); + assertEquals("(A1+B1)", addition.toString()); + assertEquals("(A1+SUM(A1,A2,A3))", addition.getDevelopedFormula()); + } + + @Test + public void testMultiplication() { + Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null); + Cell a3 = this.cells.stream().filter(c -> c.getId().equals("A3")).findFirst().orElse(null); + + Multiplication multiplication = new Multiplication(a1, a3); + + assertEquals(200, multiplication.eval(), 0); + assertEquals("(A1*A3)", multiplication.toString()); + assertEquals("(A1*A3)", multiplication.getDevelopedFormula()); + + Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null); + + multiplication = new Multiplication(a1, b1); + + assertEquals(300, multiplication.eval(), 0); + assertEquals("(A1*B1)", multiplication.toString()); + assertEquals("(A1*SUM(A1,A2,A3))", multiplication.getDevelopedFormula()); + } + + @Test + public void testSubtraction() { + Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null); + Cell a3 = this.cells.stream().filter(c -> c.getId().equals("A3")).findFirst().orElse(null); + + Subtraction subtraction = new Subtraction(a3, a1); + + assertEquals(10, subtraction.eval(), 0); + assertEquals("(A3-A1)", subtraction.toString()); + assertEquals("(A3-A1)", subtraction.getDevelopedFormula()); + + Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null); + + subtraction = new Subtraction(a1, b1); + + assertEquals(-20, subtraction.eval(), 0); + assertEquals("(A1-B1)", subtraction.toString()); + assertEquals("(A1-SUM(A1,A2,A3))", subtraction.getDevelopedFormula()); + } + + @Test + public void testDivision() { + Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null); + Cell a3 = this.cells.stream().filter(c -> c.getId().equals("A3")).findFirst().orElse(null); + + Division division = new Division(a3, a1); + + assertEquals(2, division.eval(), 0); + assertEquals("(A3/A1)", division.toString()); + assertEquals("(A3/A1)", division.getDevelopedFormula()); + + Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null); + + division = new Division(b1, a1); + + assertEquals(3, division.eval(), 0); + assertEquals("(B1/A1)", division.toString()); + assertEquals("(SUM(A1,A2,A3)/A1)", division.getDevelopedFormula()); + } } diff --git a/src/kernel/test/CellTest.java b/src/kernel/test/CellTest.java index d2d32b8..ffffc20 100644 --- a/src/kernel/test/CellTest.java +++ b/src/kernel/test/CellTest.java @@ -7,7 +7,7 @@ import java.util.List; import kernel.Cell; import kernel.exception.CreateCycleException; -import kernel.exception.InvalidIntervalLineColumnEception; +import kernel.exception.InvalidIntervalException; import kernel.function.Average; import kernel.function.Sum; import kernel.operation.Addition; @@ -16,7 +16,7 @@ import kernel.operation.Addition; public class CellTest { @org.junit.Test - public void CreateCellValueTest() throws InvalidIntervalLineColumnEception { + public void CreateCellValueTest() throws InvalidIntervalException { Cell A1= new Cell("A",1,25.); assertEquals(A1.getValue(),25.,0); assertEquals(A1.containFormula(),false); @@ -26,7 +26,7 @@ public class CellTest { @org.junit.Test - public void CreateCellBinaryOperationTest() throws CreateCycleException, InvalidIntervalLineColumnEception { + public void CreateCellBinaryOperationTest() throws CreateCycleException, InvalidIntervalException { Cell A1= new Cell("A",1,25.); Cell A2= new Cell("A",2,35.); @@ -47,7 +47,7 @@ public class CellTest { @org.junit.Test - public void CreateCellFunctionTest() throws CreateCycleException, InvalidIntervalLineColumnEception { + public void CreateCellFunctionTest() throws CreateCycleException, InvalidIntervalException { Cell A1= new Cell("A",1,25.); Cell A2= new Cell("A",2,35.); Cell A3= new Cell("A",3,new Addition(A1,A2)); @@ -76,7 +76,7 @@ public class CellTest { @org.junit.Test - public void ModifyCellValueTest() throws CreateCycleException, InvalidIntervalLineColumnEception { + public void ModifyCellValueTest() throws CreateCycleException, InvalidIntervalException { Cell A1= new Cell("A",1,25.); Cell A2= new Cell("A",2,35.); Cell A3= new Cell("A",3,new Addition(A1,A2)); @@ -90,7 +90,7 @@ public class CellTest { } @org.junit.Test - public void ModifyCellFormulaTest() throws CreateCycleException, InvalidIntervalLineColumnEception { + public void ModifyCellFormulaTest() throws CreateCycleException, InvalidIntervalException { Cell A1= new Cell("A",1,25.); Cell A2= new Cell("A",2,35.); Cell A3= new Cell("A",3,new Addition(A1,A2)); @@ -137,7 +137,7 @@ public class CellTest { } @org.junit.Test(expected=CreateCycleException.class) - public void DirectCycleBynaryOperation() throws CreateCycleException, InvalidIntervalLineColumnEception { + public void DirectCycleBynaryOperation() throws CreateCycleException, InvalidIntervalException { Cell A1= new Cell("A",1,25.); Cell A2= new Cell("A",2,35.); try{ @@ -150,7 +150,7 @@ public class CellTest { @org.junit.Test(expected=CreateCycleException.class) - public void DirectCycleFunction() throws CreateCycleException, InvalidIntervalLineColumnEception { + public void DirectCycleFunction() throws CreateCycleException, InvalidIntervalException { Cell A2= new Cell("A",2,25.); Cell B2= new Cell("B",2,35.); Cell A3= new Cell("A",3,0.5); @@ -168,7 +168,7 @@ public class CellTest { } @org.junit.Test(expected=CreateCycleException.class) - public void IndirectCycle() throws CreateCycleException, InvalidIntervalLineColumnEception { + public void IndirectCycle() throws CreateCycleException, InvalidIntervalException { Cell A1= new Cell("A",1,25.); Cell A2= new Cell("A",2,5.); Cell B4= new Cell("B",4,new Addition(A1,A2)); diff --git a/src/kernel/test/FunctionTest.java b/src/kernel/test/FunctionTest.java index b6bc7c3..9ec7a62 100644 --- a/src/kernel/test/FunctionTest.java +++ b/src/kernel/test/FunctionTest.java @@ -3,7 +3,7 @@ package kernel.test; import kernel.Cell; import kernel.Grid; import kernel.LanguageEnum; -import kernel.exception.InvalidIntervalLineColumnEception; +import kernel.exception.InvalidIntervalException; import kernel.function.Sum; import org.junit.Before; import org.junit.Test; @@ -11,27 +11,26 @@ import org.junit.Test; import java.util.ArrayList; import java.util.List; - public class FunctionTest { - - private List cells; - - @Before - public void initData() throws InvalidIntervalLineColumnEception { - Grid.language = LanguageEnum.EN; - this.cells = new ArrayList<>(); - - Cell a1 = new Cell("A", 1, 10.); - Cell a2 = new Cell("A", 2, 0.); - Cell a3 = new Cell("A", 3, 20.); - - this.cells.add(a1); - this.cells.add(a2); - this.cells.add(a3); - } - - @Test - public void testSum() { - Sum sum = new Sum(this.cells); - } + + private List cells; + + @Before + public void initData() throws InvalidIntervalException { + Grid.language = LanguageEnum.EN; + this.cells = new ArrayList<>(); + + Cell a1 = new Cell("A", 1, 10.); + Cell a2 = new Cell("A", 2, 0.); + Cell a3 = new Cell("A", 3, 20.); + + this.cells.add(a1); + this.cells.add(a2); + this.cells.add(a3); + } + + @Test + public void testSum() { + Sum sum = new Sum(this.cells); + } } diff --git a/src/kernel/test/GridTest.java b/src/kernel/test/GridTest.java index df33901..95f0f7f 100644 --- a/src/kernel/test/GridTest.java +++ b/src/kernel/test/GridTest.java @@ -5,7 +5,7 @@ import kernel.Grid; import kernel.LanguageEnum; import kernel.exception.CellNotFoundException; import kernel.exception.CreateCycleException; -import kernel.exception.InvalidIntervalLineColumnEception; +import kernel.exception.InvalidIntervalException; import kernel.function.Average; import kernel.function.Sum; import kernel.operation.Addition; @@ -16,190 +16,212 @@ import org.junit.Test; import java.util.ArrayList; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.Assert.*; public class GridTest { - - private Grid grid; - - @Before - public void initData() { - Grid.language = LanguageEnum.EN; - this.grid = new Grid(); - } - - @Test - public void testCreateCellWithValue() throws InvalidIntervalLineColumnEception { - assertEquals(0, this.grid.getCells().size()); - - this.grid.createCell("A", 1, 10.); - - assertEquals(1, this.grid.getCells().size()); - } - - @Test - public void testCreateCellWithFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalLineColumnEception { - this.createCellsWithValue(); - assertEquals(3, this.grid.getCells().size()); - - List cellsFormula = new ArrayList<>(); - cellsFormula.add(this.grid.getCell("A", 1)); - cellsFormula.add(this.grid.getCell("A", 2)); - - this.grid.createCell("B", 1, new Sum(cellsFormula)); - - assertEquals(4, this.grid.getCells().size()); - } - - @Test - public void testGetValue() throws CellNotFoundException, CreateCycleException, InvalidIntervalLineColumnEception { - this.createCellsWithFormula(); - - assertEquals(10, this.grid.getValue("A", 1), 0); - assertEquals(0, this.grid.getValue("A", 2), 0); - assertEquals(2, this.grid.getValue("A", 3), 0); - assertEquals(10, this.grid.getValue("B", 1), 0); - assertEquals(12, this.grid.getValue("B", 2), 0); - assertEquals(24, this.grid.getValue("B", 3), 0); - } - - @Test - public void testGetFormulaAsString() throws CellNotFoundException, CreateCycleException, InvalidIntervalLineColumnEception { - Grid.language = LanguageEnum.EN; - this.createCellsWithFormula(); - - assertEquals("A1", this.grid.getFormulaAsString("A", 1)); - assertEquals("A2", this.grid.getFormulaAsString("A", 2)); - assertEquals("A3", this.grid.getFormulaAsString("A", 3)); - assertEquals("A1+A2", this.grid.getFormulaAsString("B", 1)); - assertEquals("SUM(A1,A2,A3)", this.grid.getFormulaAsString("B", 2)); - assertEquals("A3*B2", this.grid.getFormulaAsString("B", 3)); - } - - @Test - public void testGetDevelopedFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalLineColumnEception { - this.createCellsWithFormula(); - - assertEquals("A3*SUM(A1,A2,A3)", this.grid.getDevelopedFormula("B", 3)); - } - - @Test - public void testSetValue() throws CellNotFoundException, CreateCycleException, InvalidIntervalLineColumnEception { - this.createCellsWithFormula(); - - assertEquals(10, this.grid.getValue("A", 1), 0); - assertEquals(12, this.grid.getValue("B", 2), 0); - assertEquals(24, this.grid.getValue("B", 3), 0); - - this.grid.setValue("A", 1, 15.); - - assertEquals(15, this.grid.getValue("A", 1), 0); - assertEquals(17, this.grid.getValue("B", 2), 0); - assertEquals(34, this.grid.getValue("B", 3), 0); - assertEquals("A3*SUM(A1,A2,A3)", this.grid.getDevelopedFormula("B", 3)); - - this.grid.setValue("B", 2, 20.); - - assertEquals(40, this.grid.getValue("B", 3), 0); - assertEquals("A3*B2", this.grid.getDevelopedFormula("B", 3)); - } - - @Test - public void testSetFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalLineColumnEception { - this.createCellsWithFormula(); - - assertEquals(12, this.grid.getValue("B", 2), 0); - assertEquals(24, this.grid.getValue("B", 3), 0); - assertEquals("SUM(A1,A2,A3)", this.grid.getDevelopedFormula("B", 2)); - assertEquals("A3*SUM(A1,A2,A3)", this.grid.getDevelopedFormula("B", 3)); - - List sumList = new ArrayList<>(); - sumList.add(this.grid.getCell("A", 1)); - sumList.add(this.grid.getCell("A", 3)); - sumList.add(this.grid.getCell("B", 1)); - - this.grid.setFormula("B", 2, new Sum(sumList)); - - // B2 - assertEquals(22, this.grid.getValue("B", 2), 0); - assertEquals("SUM(A1,A3,B1)", this.grid.getFormulaAsString("B", 2)); - assertEquals("SUM(A1,A3,A1+A2)", this.grid.getDevelopedFormula("B", 2)); - // B3 - assertEquals(44, this.grid.getValue("B", 3), 0); - assertEquals("A3*B2", this.grid.getFormulaAsString("B", 3)); - assertEquals("A3*SUM(A1,A3,A1+A2)", this.grid.getDevelopedFormula("B", 3)); - - this.grid.createCell("C", 1, 10.); - this.grid.createCell("C", 2, 20.); - - List averageList = new ArrayList<>(); - averageList.add(this.grid.getCell("C", 1)); - averageList.add(this.grid.getCell("C", 2)); - - this.grid.setFormula("A", 1, new Average(averageList)); - - // A1 - assertEquals(15, this.grid.getValue("A", 1), 0); - assertEquals("AVERAGE(C1,C2)", this.grid.getDevelopedFormula("A", 1)); - // B1 - assertEquals(15, this.grid.getValue("B", 1), 0); - assertEquals("A1+A2", this.grid.getFormulaAsString("B", 1)); - assertEquals("AVERAGE(C1,C2)+A2", this.grid.getDevelopedFormula("B", 1)); - // B2 - assertEquals(32, this.grid.getValue("B", 2), 0); - assertEquals("SUM(A1,A3,B1)", this.grid.getFormulaAsString("B", 2)); - assertEquals("SUM(AVERAGE(C1,C2),A3,AVERAGE(C1,C2)+A2)", this.grid.getDevelopedFormula("B", 2)); - } - - @Test - public void testGetCell() throws CellNotFoundException, InvalidIntervalLineColumnEception { - this.createCellsWithValue(); - - Cell cell = this.grid.getCell("A", 1); - - assertEquals("A1", cell.getId()); - assertEquals(10, cell.getValue(), 0); - assertFalse(cell.containFormula()); - } - - @Test(expected = CellNotFoundException.class) - public void testGetCellNotFound() throws CellNotFoundException { - Cell cell = this.grid.getCell("A", 1); - } - - @Test - public void testGetCells() throws InvalidIntervalLineColumnEception { - assertEquals(0, this.grid.getCells().size()); - - this.createCellsWithValue(); - - assertEquals(3, this.grid.getCells().size()); - } - - private void createCellsWithValue() throws InvalidIntervalLineColumnEception { - this.grid.createCell("A", 1, 10.); - this.grid.createCell("A", 2, 0.); - this.grid.createCell("A", 3, 2.); - } - - private void createCellsWithFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalLineColumnEception { - this.createCellsWithValue(); - - Cell a1 = this.grid.getCell("A", 1); - Cell a2 = this.grid.getCell("A", 2); - Cell a3 = this.grid.getCell("A", 3); - - List sumList = new ArrayList<>(); - sumList.add(a1); - sumList.add(a2); - sumList.add(a3); - - // A1+A2 - this.grid.createCell("B", 1, new Addition(a1, a2)); - // SUM(A1+A2+A3) - this.grid.createCell("B", 2, new Sum(sumList)); - // A3*B2 - this.grid.createCell("B", 3, new Multiplication(a3, this.grid.getCell("B", 2))); - } + + private Grid grid; + + @Before + public void initData() { + Grid.language = LanguageEnum.EN; + this.grid = new Grid(); + } + + @Test + public void testCreateCellWithValue() throws InvalidIntervalException { + assertEquals(0, this.grid.getCells().size()); + + this.grid.createCell("A", 1, 10.); + + assertEquals(1, this.grid.getCells().size()); + } + + @Test + public void testCreateCellWithFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalException { + this.createCellsWithValue(); + assertEquals(3, this.grid.getCells().size()); + + List cellsFormula = new ArrayList<>(); + cellsFormula.add(this.grid.getCell("A", 1)); + cellsFormula.add(this.grid.getCell("A", 2)); + + this.grid.createCell("B", 1, new Sum(cellsFormula)); + + assertEquals(4, this.grid.getCells().size()); + } + + @Test(expected = InvalidIntervalException.class) + public void testCreateCellWithInvalidInterval() throws InvalidIntervalException { + this.grid.createCell("AZ", 120, 0.); + } + + @Test + public void testGetValue() throws CellNotFoundException, CreateCycleException, InvalidIntervalException { + this.createCellsWithFormula(); + + assertEquals(10, this.grid.getValue("A", 1), 0); + assertEquals(0, this.grid.getValue("A", 2), 0); + assertEquals(2, this.grid.getValue("A", 3), 0); + assertEquals(10, this.grid.getValue("B", 1), 0); + assertEquals(12, this.grid.getValue("B", 2), 0); + assertEquals(24, this.grid.getValue("B", 3), 0); + } + + @Test + public void testGetFormulaAsString() throws CellNotFoundException, CreateCycleException, InvalidIntervalException { + Grid.language = LanguageEnum.EN; + this.createCellsWithFormula(); + + assertEquals("A1", this.grid.getFormulaAsString("A", 1)); + assertEquals("A2", this.grid.getFormulaAsString("A", 2)); + assertEquals("A3", this.grid.getFormulaAsString("A", 3)); + assertEquals("(A1+A2)", this.grid.getFormulaAsString("B", 1)); + assertEquals("SUM(A1,A2,A3)", this.grid.getFormulaAsString("B", 2)); + assertEquals("(A3*B2)", this.grid.getFormulaAsString("B", 3)); + } + + @Test + public void testGetDevelopedFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalException { + this.createCellsWithFormula(); + + assertEquals("(A3*SUM(A1,A2,A3))", this.grid.getDevelopedFormula("B", 3)); + } + + @Test + public void testSetValue() throws CellNotFoundException, CreateCycleException, InvalidIntervalException { + this.createCellsWithFormula(); + + assertEquals(10, this.grid.getValue("A", 1), 0); + assertEquals(12, this.grid.getValue("B", 2), 0); + assertEquals(24, this.grid.getValue("B", 3), 0); + + this.grid.setValue("A", 1, 15.); + + assertEquals(15, this.grid.getValue("A", 1), 0); + assertEquals(17, this.grid.getValue("B", 2), 0); + assertEquals(34, this.grid.getValue("B", 3), 0); + assertEquals("(A3*SUM(A1,A2,A3))", this.grid.getDevelopedFormula("B", 3)); + + this.grid.setValue("B", 2, 20.); + + assertEquals(40, this.grid.getValue("B", 3), 0); + assertEquals("(A3*B2)", this.grid.getDevelopedFormula("B", 3)); + } + + @Test + public void testSetFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalException { + this.createCellsWithFormula(); + + assertEquals(12, this.grid.getValue("B", 2), 0); + assertEquals(24, this.grid.getValue("B", 3), 0); + assertEquals("SUM(A1,A2,A3)", this.grid.getDevelopedFormula("B", 2)); + assertEquals("(A3*SUM(A1,A2,A3))", this.grid.getDevelopedFormula("B", 3)); + + List sumList = new ArrayList<>(); + sumList.add(this.grid.getCell("A", 1)); + sumList.add(this.grid.getCell("A", 3)); + sumList.add(this.grid.getCell("B", 1)); + + this.grid.setFormula("B", 2, new Sum(sumList)); + + // B2 + assertEquals(22, this.grid.getValue("B", 2), 0); + assertEquals("SUM(A1,A3,B1)", this.grid.getFormulaAsString("B", 2)); + assertEquals("SUM(A1,A3,(A1+A2))", this.grid.getDevelopedFormula("B", 2)); + // B3 + assertEquals(44, this.grid.getValue("B", 3), 0); + assertEquals("(A3*B2)", this.grid.getFormulaAsString("B", 3)); + assertEquals("(A3*SUM(A1,A3,(A1+A2)))", this.grid.getDevelopedFormula("B", 3)); + + this.grid.createCell("C", 1, 10.); + this.grid.createCell("C", 2, 20.); + + List averageList = new ArrayList<>(); + averageList.add(this.grid.getCell("C", 1)); + averageList.add(this.grid.getCell("C", 2)); + + this.grid.setFormula("A", 1, new Average(averageList)); + + // A1 + assertEquals(15, this.grid.getValue("A", 1), 0); + assertEquals("AVERAGE(C1,C2)", this.grid.getDevelopedFormula("A", 1)); + // B1 + assertEquals(15, this.grid.getValue("B", 1), 0); + assertEquals("(A1+A2)", this.grid.getFormulaAsString("B", 1)); + assertEquals("(AVERAGE(C1,C2)+A2)", this.grid.getDevelopedFormula("B", 1)); + // B2 + assertEquals(32, this.grid.getValue("B", 2), 0); + assertEquals("SUM(A1,A3,B1)", this.grid.getFormulaAsString("B", 2)); + assertEquals("SUM(AVERAGE(C1,C2),A3,(AVERAGE(C1,C2)+A2))", this.grid.getDevelopedFormula("B", 2)); + } + + @Test + public void testGetCell() throws CellNotFoundException, CreateCycleException, InvalidIntervalException { + this.createCellsWithFormula(); + + Cell cell = this.grid.getCell("A", 1); + + assertEquals("A1", cell.getId()); + assertEquals(10, cell.getValue(), 0); + assertEquals("A1", cell.toString()); + assertEquals("A1", cell.getDevelopedFormula()); + assertFalse(cell.containFormula()); + + cell = this.grid.getCell("B", 2); + + assertEquals("B2", cell.getId()); + assertEquals(12, cell.getValue(), 0); + assertEquals("SUM(A1,A2,A3)", cell.toString()); + assertEquals("SUM(A1,A2,A3)", cell.getDevelopedFormula()); + assertTrue(cell.containFormula()); + + cell = this.grid.getCell("B", 3); + + assertEquals("B3", cell.getId()); + assertEquals(24, cell.getValue(), 0); + assertEquals("(A3*B2)", cell.toString()); + assertEquals("(A3*SUM(A1,A2,A3))", cell.getDevelopedFormula()); + assertTrue(cell.containFormula()); + } + + @Test(expected = CellNotFoundException.class) + public void testGetCellNotFound() throws CellNotFoundException { + Cell cell = this.grid.getCell("A", 1); + } + + @Test + public void testGetCells() throws InvalidIntervalException { + assertEquals(0, this.grid.getCells().size()); + + this.createCellsWithValue(); + + assertEquals(3, this.grid.getCells().size()); + } + + private void createCellsWithValue() throws InvalidIntervalException { + this.grid.createCell("A", 1, 10.); + this.grid.createCell("A", 2, 0.); + this.grid.createCell("A", 3, 2.); + } + + private void createCellsWithFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalException { + this.createCellsWithValue(); + + Cell a1 = this.grid.getCell("A", 1); + Cell a2 = this.grid.getCell("A", 2); + Cell a3 = this.grid.getCell("A", 3); + + List sumList = new ArrayList<>(); + sumList.add(a1); + sumList.add(a2); + sumList.add(a3); + + // A1+A2 + this.grid.createCell("B", 1, new Addition(a1, a2)); + // SUM(A1+A2+A3) + this.grid.createCell("B", 2, new Sum(sumList)); + // A3*B2 + this.grid.createCell("B", 3, new Multiplication(a3, this.grid.getCell("B", 2))); + } } -- libgit2 0.21.2