diff --git a/grid.data b/grid.data index a77ad6f..a19fca4 100644 Binary files a/grid.data and b/grid.data differ diff --git a/src/ihm/TablooProto.java b/src/ihm/TablooProto.java index cb9f273..58ce381 100644 --- a/src/ihm/TablooProto.java +++ b/src/ihm/TablooProto.java @@ -36,7 +36,7 @@ public class TablooProto extends JPanel { private static Grid grid; // Fourni: ne rien changer. - public TablooProto(Grid grid) throws ClassNotFoundException, IOException { + public TablooProto(Grid grid) { super(new GridLayout(1, 0)); // modele de donnees @@ -61,7 +61,6 @@ public class TablooProto extends JPanel { TableColumn tm = table.getColumnModel().getColumn(0); tm.setPreferredWidth(tm.getPreferredWidth() * 2 / 3); tm.setCellRenderer(new PremiereColonneSpecificRenderer(Color.LIGHT_GRAY)); - } // Inner class pour changer l'aspect de la premiere colonne consacree a la numerotation des lignes diff --git a/src/kernel/Cell.java b/src/kernel/Cell.java index 61037dc..b01f5c7 100644 --- a/src/kernel/Cell.java +++ b/src/kernel/Cell.java @@ -1,7 +1,6 @@ package kernel; import kernel.exception.CreateCycleException; -import kernel.exception.InvalidIntervalException; import java.io.Serializable; import java.util.ArrayList; @@ -19,9 +18,6 @@ public class Cell implements Serializable { private List usedIn = new ArrayList<>(); public Cell(String column, int line, double value) { - - - this.column = column; this.line = line; this.setValue(value); @@ -29,8 +25,6 @@ public class Cell implements Serializable { public Cell(String column, int line, Formula formula) throws CreateCycleException { - - this.column = column; this.line = line; this.setFormula(formula); @@ -100,5 +94,5 @@ public class Cell implements Serializable { } } - + } diff --git a/src/kernel/Grid.java b/src/kernel/Grid.java index 70caa1d..81e23f9 100644 --- a/src/kernel/Grid.java +++ b/src/kernel/Grid.java @@ -19,7 +19,6 @@ public class Grid implements Serializable { private Map cells = new HashMap<>(); public static LanguageEnum language = LanguageEnum.FR; - public void createCell(String column, int line, double value) throws InvalidIntervalException { column = column.toUpperCase(); diff --git a/src/kernel/test/CellTest.java b/src/kernel/test/CellTest.java index ffffc20..ada024a 100644 --- a/src/kernel/test/CellTest.java +++ b/src/kernel/test/CellTest.java @@ -1,200 +1,175 @@ package kernel.test; -import static org.junit.Assert.*; - -import java.util.ArrayList; -import java.util.List; - import kernel.Cell; +import kernel.Grid; +import kernel.LanguageEnum; import kernel.exception.CreateCycleException; import kernel.exception.InvalidIntervalException; import kernel.function.Average; import kernel.function.Sum; import kernel.operation.Addition; +import org.junit.Before; +import org.junit.Test; +import java.util.ArrayList; +import java.util.List; -public class CellTest { +import static org.junit.Assert.*; - @org.junit.Test - public void CreateCellValueTest() throws InvalidIntervalException { - 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"); - } +public class CellTest { - @org.junit.Test - public void CreateCellBinaryOperationTest() 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)); + @Before + public void initData() { + Grid.language = LanguageEnum.EN; + } + + @Test + public void CreateCellValueTest() { + Cell A1 = new Cell("A", 1, 25.); + assertEquals(A1.getValue(), 25., 0); + assertFalse(A1.containFormula()); + assertEquals(A1.getUsedIn().size(), 0); + assertEquals(A1.getId(), "A1"); + } + + @Test + public void CreateCellBinaryOperationTest() throws CreateCycleException { + 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.getValue(), 60., 0); + assertTrue(A3.containFormula()); + assertEquals(A3.getUsedIn().size(), 0); + assertEquals(A3.getId(), "A3"); - assertEquals(A3.toString(),"A1+A2"); - assertEquals(A1.getUsedIn().size(),1); - assertEquals(A2.getUsedIn().size(),1); - - + assertEquals(A3.toString(), "(A1+A2)"); + assertEquals(A1.getUsedIn().size(), 1); + assertEquals(A2.getUsedIn().size(), 1); } - - @org.junit.Test - 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)); - Cell A5= new Cell("A",5,45.); + @Test + public void CreateCellFunctionTest() throws CreateCycleException { + 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); + 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); + assertTrue(A4.containFormula()); + 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); - - + assertEquals(A4.toString(), "SUM(A1,A2,A3,A5)"); + assertEquals(A4.getDevelopedFormula(), "SUM(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() 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)); - assertEquals(A1.getValue(),25.,0); - assertEquals(A2.getValue(),35.,0); - assertEquals(A3.getValue(),60.,0); + @Test + public void ModifyCellValueTest() throws CreateCycleException { + 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); - + assertEquals(A1.getValue(), 45., 0); + assertEquals(A3.getValue(), 80., 0); } - - @org.junit.Test - 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)); - Cell A5= new Cell("A",5,45.); + + @Test + public void ModifyCellFormulaTest() throws CreateCycleException { + 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)); + 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.getValue(), 165., 0); + assertTrue(A4.containFormula()); + assertEquals(A4.getUsedIn().size(), 0); - assertEquals(A1.getUsedIn().size(),2); - assertEquals(A2.getUsedIn().size(),2); - assertEquals(A3.getUsedIn().size(),1); - assertEquals(A5.getUsedIn().size(),1); + assertEquals(A1.getUsedIn().size(), 2); + assertEquals(A2.getUsedIn().size(), 2); + assertEquals(A3.getUsedIn().size(), 1); + assertEquals(A5.getUsedIn().size(), 1); - assertEquals(A1.containFormula(),false); - A1.setFormula(new Addition(A2,A5)); - assertEquals(A1.containFormula(),true); + assertFalse(A1.containFormula()); + A1.setFormula(new Addition(A2, A5)); + assertTrue(A1.containFormula()); - assertEquals(A1.getValue(),80.,0); - assertEquals(A3.getValue(),115.,0); - assertEquals(A4.getValue(),275.,0); - - + assertEquals(A1.getValue(), 80., 0); + assertEquals(A3.getValue(), 115., 0); + assertEquals(A4.getValue(), 275., 0); + assertEquals(A1.toString(), "(A2+A5)"); + assertEquals(A1.getDevelopedFormula(), "(A2+A5)"); + assertEquals(A4.toString(), "SUM(A1,A2,A3,A5)"); + assertEquals(A4.getDevelopedFormula(), "SUM((A2+A5),A2,((A2+A5)+A2),A5)"); - assertEquals(A1.toString(),"A2+A5"); - assertEquals(A1.getDevelopedFormula(),"(A2+A5)"); - assertEquals(A4.toString(),"SOMME(A1,A2,A3,A5)"); - assertEquals(A4.getDevelopedFormula(),"SOMME((A2+A5),A2,((A2+A5)+A2),A5)"); - - - assertEquals(A1.getUsedIn().size(),2); - assertEquals(A2.getUsedIn().size(),3); - assertEquals(A3.getUsedIn().size(),1); - assertEquals(A5.getUsedIn().size(),2); - - + assertEquals(A1.getUsedIn().size(), 2); + assertEquals(A2.getUsedIn().size(), 3); + assertEquals(A3.getUsedIn().size(), 1); + assertEquals(A5.getUsedIn().size(), 2); } - @org.junit.Test(expected=CreateCycleException.class) - public void DirectCycleBynaryOperation() throws CreateCycleException, InvalidIntervalException { - Cell A1= new Cell("A",1,25.); - Cell A2= new Cell("A",2,35.); - try{ - A1.setFormula(new Addition(A1,A2)); - } - catch(CreateCycleException ex){ - throw ex; - } + @Test(expected = CreateCycleException.class) + public void DirectCycleBynaryOperation() throws CreateCycleException { + Cell A1 = new Cell("A", 1, 25.); + Cell A2 = new Cell("A", 2, 35.); + A1.setFormula(new Addition(A1, A2)); } - @org.junit.Test(expected=CreateCycleException.class) - 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); + @Test(expected = CreateCycleException.class) + public void DirectCycleFunction() throws CreateCycleException { + Cell A2 = new Cell("A", 2, 25.); + Cell B2 = new Cell("B", 2, 35.); + Cell A3 = new Cell("A", 3, 0.5); List sumList = new ArrayList<>(); - sumList.add(A2); - sumList.add(B2); - sumList.add(A3); - - try{ - B2.setFormula(new Sum(sumList)); - } - catch(CreateCycleException ex){ - throw ex; - } + sumList.add(A2); + sumList.add(B2); + sumList.add(A3); + + B2.setFormula(new Sum(sumList)); } - @org.junit.Test(expected=CreateCycleException.class) - 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)); - Cell A4= new Cell("A",4,0.); - Cell A3= new Cell("A",3,0.5); - Cell B2= new Cell("B",2,12.); + @Test(expected = CreateCycleException.class) + public void IndirectCycle() throws CreateCycleException { + Cell A1 = new Cell("A", 1, 25.); + Cell A2 = new Cell("A", 2, 5.); + Cell B4 = new Cell("B", 4, new Addition(A1, A2)); + Cell A4 = new Cell("A", 4, 0.); + Cell A3 = new Cell("A", 3, 0.5); + Cell B2 = new Cell("B", 2, 12.); List sumList = new ArrayList<>(); - sumList.add(A2); - sumList.add(B2); - sumList.add(A3); - Cell B6= new Cell("B",6,new Sum(sumList)); - + sumList.add(A2); + sumList.add(B2); + sumList.add(A3); + Cell B6 = new Cell("B", 6, new Sum(sumList)); + List averageList = new ArrayList<>(); averageList.add(B6); averageList.add(B4); averageList.add(A4); - Cell C6= new Cell("C",6,new Average(averageList)); - - try{ - B2.setFormula(new Addition(A1,C6)); - } - catch(CreateCycleException ex){ - throw ex; - } + Cell C6 = new Cell("C", 6, new Average(averageList)); + + B2.setFormula(new Addition(A1, C6)); } - - } diff --git a/src/kernel/test/FunctionTest.java b/src/kernel/test/FunctionTest.java index 9ec7a62..b9f4223 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.InvalidIntervalException; +import kernel.function.Average; import kernel.function.Sum; import org.junit.Before; import org.junit.Test; @@ -11,12 +11,15 @@ import org.junit.Test; import java.util.ArrayList; import java.util.List; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + public class FunctionTest { private List cells; @Before - public void initData() throws InvalidIntervalException { + public void initData() { Grid.language = LanguageEnum.EN; this.cells = new ArrayList<>(); @@ -32,5 +35,18 @@ public class FunctionTest { @Test public void testSum() { Sum sum = new Sum(this.cells); + + assertNotNull(sum); + assertEquals(30, sum.eval(), 0); + assertEquals("SUM(A1,A2,A3)", sum.toString()); + } + + @Test + public void testAverage() { + Average average = new Average(this.cells); + + assertNotNull(average); + assertEquals(10, average.eval(), 0); + assertEquals("AVERAGE(A1,A2,A3)", average.toString()); } } diff --git a/src/kernel/test/GridTest.java b/src/kernel/test/GridTest.java index 95f0f7f..c8c0739 100644 --- a/src/kernel/test/GridTest.java +++ b/src/kernel/test/GridTest.java @@ -3,6 +3,7 @@ package kernel.test; import kernel.Cell; import kernel.Grid; import kernel.LanguageEnum; +import kernel.exception.CannotDeleteCellException; import kernel.exception.CellNotFoundException; import kernel.exception.CreateCycleException; import kernel.exception.InvalidIntervalException; @@ -13,6 +14,7 @@ import kernel.operation.Multiplication; import org.junit.Before; import org.junit.Test; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -56,6 +58,13 @@ public class GridTest { this.grid.createCell("AZ", 120, 0.); } + @Test(expected = CellNotFoundException.class) + public void testGetUnknownCell() throws CellNotFoundException, InvalidIntervalException { + this.createCellsWithValue(); + + this.grid.getCell("B", 5); + } + @Test public void testGetValue() throws CellNotFoundException, CreateCycleException, InvalidIntervalException { this.createCellsWithFormula(); @@ -199,6 +208,52 @@ public class GridTest { assertEquals(3, this.grid.getCells().size()); } + @Test + public void testSave() throws InvalidIntervalException, IOException, CellNotFoundException, ClassNotFoundException, + CreateCycleException { + this.createCellsWithFormula(); + + this.grid.save(); + + Grid g = Grid.load(); + + Cell cell = g.getCell("A", 1); + + assertEquals("A1", cell.getId()); + assertEquals(10, cell.getValue(), 0); + + cell = g.getCell("B", 1); + + assertEquals("B1", cell.getId()); + assertTrue(cell.containFormula()); + assertEquals(10, cell.getValue(), 0); + } + + @Test + public void testDeleteCell() throws CannotDeleteCellException, InvalidIntervalException { + this.createCellsWithValue(); + + Cell cell = this.grid.getCell("A2"); + + assertNotNull(cell); + assertEquals(0, cell.getValue(), 0); + assertFalse(cell.containFormula()); + + this.grid.deleteCell("A", 2); + + cell = this.grid.getCell("A2"); + + assertNull(cell); + } + + @Test(expected = CannotDeleteCellException.class) + public void testDeleteCellUsedInAnother() throws CannotDeleteCellException, InvalidIntervalException, + CellNotFoundException, CreateCycleException { + this.createCellsWithFormula(); + + this.grid.deleteCell("A", 1); + } + private void createCellsWithValue() throws InvalidIntervalException { this.grid.createCell("A", 1, 10.); this.grid.createCell("A", 2, 0.); -- libgit2 0.21.2