From 35c209df8f13acb73369500c96f3aab28a8d7460 Mon Sep 17 00:00:00 2001 From: Remi Date: Tue, 11 Jun 2019 15:27:05 +0200 Subject: [PATCH] merge --- src/kernel/operation/Addition.java | 1 - src/kernel/test/BinaryOperationTest.java | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/kernel/test/FunctionTest.java | 37 +++++++++++++++++++++++++++++++++++++ src/kernel/test/GridTest.java | 204 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 366 insertions(+), 1 deletion(-) create mode 100644 src/kernel/test/BinaryOperationTest.java create mode 100644 src/kernel/test/FunctionTest.java create mode 100644 src/kernel/test/GridTest.java diff --git a/src/kernel/operation/Addition.java b/src/kernel/operation/Addition.java index 7ce3f37..641a65f 100644 --- a/src/kernel/operation/Addition.java +++ b/src/kernel/operation/Addition.java @@ -17,7 +17,6 @@ public class Addition extends BinaryOperation { } public double eval() { - return this.leftCell.getValue() + this.rightCell.getValue(); } } diff --git a/src/kernel/test/BinaryOperationTest.java b/src/kernel/test/BinaryOperationTest.java new file mode 100644 index 0000000..2223b52 --- /dev/null +++ b/src/kernel/test/BinaryOperationTest.java @@ -0,0 +1,125 @@ +package kernel.test; + +import kernel.Cell; +import kernel.Grid; +import kernel.LanguageEnum; +import kernel.exception.CreateCycleException; +import kernel.function.Sum; +import kernel.operation.Addition; +import kernel.operation.Division; +import kernel.operation.Multiplication; +import kernel.operation.Subtraction; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class BinaryOperationTest { + + private List cells; + + @Before + public void initData() throws CreateCycleException { + 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/FunctionTest.java b/src/kernel/test/FunctionTest.java new file mode 100644 index 0000000..f0338fe --- /dev/null +++ b/src/kernel/test/FunctionTest.java @@ -0,0 +1,37 @@ +package kernel.test; + +import kernel.Cell; +import kernel.Grid; +import kernel.LanguageEnum; +import kernel.function.Sum; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; + +public class FunctionTest { + + private List cells; + + @Before + public void initData() { + 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 new file mode 100644 index 0000000..61158d6 --- /dev/null +++ b/src/kernel/test/GridTest.java @@ -0,0 +1,204 @@ +package kernel.test; + +import kernel.Cell; +import kernel.Grid; +import kernel.LanguageEnum; +import kernel.exception.CellNotFoundException; +import kernel.exception.CreateCycleException; +import kernel.function.Average; +import kernel.function.Sum; +import kernel.operation.Addition; +import kernel.operation.Multiplication; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +public class GridTest { + + private Grid grid; + + @Before + public void initData() { + Grid.language = LanguageEnum.EN; + this.grid = new Grid(); + } + + @Test + public void testCreateCellWithValue() { + 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 { + 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 { + 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 { + 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 { + this.createCellsWithFormula(); + + assertEquals("A3*SUM(A1,A2,A3)", this.grid.getDevelopedFormula("B", 3)); + } + + @Test + public void testSetValue() throws CellNotFoundException, CreateCycleException { + 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 { + 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 { + 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() { + assertEquals(0, this.grid.getCells().size()); + + this.createCellsWithValue(); + + assertEquals(3, this.grid.getCells().size()); + } + + private void createCellsWithValue() { + this.grid.createCell("A", 1, 10.); + this.grid.createCell("A", 2, 0.); + this.grid.createCell("A", 3, 2.); + } + + private void createCellsWithFormula() throws CellNotFoundException, CreateCycleException { + 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