From 635b0979917f214cf6001798db4709c4dda8957f Mon Sep 17 00:00:00 2001 From: arnaudlabourel <arnaud.labourel@univ-amu.fr> Date: Mon, 20 Sep 2021 14:54:29 +0200 Subject: [PATCH] Ajout test --- src/main/java/formula/Constant.java | 62 +++++++++++++++++++++ src/main/java/viewer/FunctionList.java | 6 +- src/main/java/viewer/MainAppController.java | 1 + src/test/java/ConstantTest.java | 42 ++++++++++++++ 4 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 src/main/java/formula/Constant.java create mode 100644 src/test/java/ConstantTest.java diff --git a/src/main/java/formula/Constant.java b/src/main/java/formula/Constant.java new file mode 100644 index 0000000..12359a7 --- /dev/null +++ b/src/main/java/formula/Constant.java @@ -0,0 +1,62 @@ +package formula; + +import java.util.HashMap; + +public class Constant implements Formula { + + private double value; + + public Constant(double value) { + // TODO : change the code. + } + + /** + * Compute the value of the formula + * + * @param xValue the value of the variable x + * @return the value of the function when the variable x has value {@code xValue} + */ + @Override + public double eval(double xValue) { + // TODO : change the code. + return 0; + } + + /** + * Compute the derivative of the formula. + * + * @return the derivative of the formula + */ + @Override + public Formula derivative() { + // TODO : change the code. + return this; + } + + /** + * Return a {@code String} representation of the formula. + * + * @return the formula as a {@code String} + */ + @Override + public String toString() { + // TODO : change the code. + return "toto"; + } + + /** + * Indicates whether some other object is "equal to" this one. + * + * @param obj the reference object with which to compare. + * @return {@code true} if this object is the same as the obj + * argument; {@code false} otherwise. + * @see #hashCode() + * @see HashMap + */ + @Override + public boolean equals(Object obj) { + if(obj == null) return false; + if(!(obj instanceof Constant constant)) return false; + return this.value == constant.value; + } +} diff --git a/src/main/java/viewer/FunctionList.java b/src/main/java/viewer/FunctionList.java index 85ccac4..68d0c34 100644 --- a/src/main/java/viewer/FunctionList.java +++ b/src/main/java/viewer/FunctionList.java @@ -1,5 +1,6 @@ package viewer; +import formula.Constant; import javafx.scene.chart.XYChart; import java.util.ArrayList; @@ -18,9 +19,8 @@ class FunctionList { this.lowerBound = functionChart.getLowerBound(); this.upperBound = functionChart.getUpperBound(); - // TODO: add functions - - + PlottableFunction function = new PlottableFunction(new Constant(1), "f"); + addFunctionAndItsDerivative(function); } void toggleFunction(PlottableFunction function) { diff --git a/src/main/java/viewer/MainAppController.java b/src/main/java/viewer/MainAppController.java index fa24019..329cd94 100644 --- a/src/main/java/viewer/MainAppController.java +++ b/src/main/java/viewer/MainAppController.java @@ -45,6 +45,7 @@ public class MainAppController implements Initializable { Button button = new Button(function.toString()); addButton(button); button.setOnAction(event -> toggleFunction(function)); + toggleFunction(function); } private void toggleFunction(PlottableFunction function){ diff --git a/src/test/java/ConstantTest.java b/src/test/java/ConstantTest.java new file mode 100644 index 0000000..71eed1b --- /dev/null +++ b/src/test/java/ConstantTest.java @@ -0,0 +1,42 @@ +import formula.Constant; +import formula.Formula; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.within; + +public class ConstantTest { + @Test + public void testEval(){ + Constant constantTen = new Constant(10); + assertThat(constantTen.eval(0.)).isCloseTo(10., within(.001)); + Constant constantOne = new Constant(1); + assertThat(constantOne.eval(10.)).isCloseTo(1., within(.001)); + } + + @Test + public void testToString(){ + Constant constantTen = new Constant(10); + assertThat(constantTen.toString()).isEqualTo("10.0"); + Constant constantOne = new Constant(1); + assertThat(constantOne.toString()).isEqualTo("1.0"); + } + + @Test + public void testEquals() { + Constant constantTen = new Constant(10); + Constant constantTen2 = new Constant(10); + Constant constantOne = new Constant(0); + assertThat(constantTen).isEqualTo(constantTen); + assertThat(constantTen).isEqualTo(constantTen2); + assertThat(constantTen).isNotEqualTo(constantOne); + } + + @Test + public void testDerivative() { + Constant constantTen = new Constant(10); + Formula derivedConstant = constantTen.derivative(); + assertThat(derivedConstant.eval(10.)).isCloseTo(0., within(.001)); + Constant constantZero = new Constant(0); + assertThat(derivedConstant).isEqualTo(constantZero); + } +} -- GitLab