计算因子未知

时间:2016-05-30 13:48:16

标签: java math testing junit apache-commons-math

我目前正在尝试将一些JUnit测试添加到我的路径绘制系统中,以检查计算是否正确...
目前我有:

要测试的类(MapRouteDrawer.java):

protected static double[] getCoords(PolynomialSplineFunction curve, double[] index) {
  final double detailLevel = 1.0;//Tweak this value in order to achieve good rendering results
  final double defaultCoordSize = index[index.length - 1];
  final double[] coords = new double[(int)Math.round(detailLevel * defaultCoordSize) + 1];
  final double stepSize = curve.getKnots()[curve.getKnots().length - 1] / coords.length;
  double curValue = 0;
  for (int i = 0; i < coords.length; i ++) {
    coords[i] = curve.value(curValue);//gets y value of specified x value
    curValue += stepSize;
  }
  return coords;
}

protected static double[] getIndex(Point[] points) {
  final double[] index = new double[points.length];
  if(index.length > 0){
    index[0] = 0;
  }
  for (int i = 1; i < points.length; i++) {
    index[i] = index[i - 1] + Math.sqrt(points[i - 1].distance(points[i]));
  }
  return index;
}

识别TestClass:

private Point[] dummyPoints = new Point[]{new Point(0,0), new Point(100,0), new Point(0,100)};//Some Points for Testing
//This returns a so called index - the squareroot distance between addedn on top of each other
private double[] dummyIndex = MapRouteDrawer.getIndex(dummyPoints);

@Test
public void testCurve(){
  final double[] testYValues = new double[]{20, 40, 90};
  final PolynomialSplineFunction testFunction = new SplineInterpolator().interpolate(dummyIndex, testYValues);//get a Spline-Interpolated curve
  final double[] coords = MapRouteDrawer.getCoords(testFunction, dummyIndex);//Calls the function to test
  final double factor = testFunction.getKnots()[testFunction.getKnots().length - 1] / coords.length;
  assertEquals(testYValues[0] * factor, coords[(int)Math.round(dummyIndex[0])], 1);//Check if the coordinates are equal
  assertEquals(testYValues[1] * factor, coords[(int)Math.round(dummyIndex[1])], 1);
  assertEquals(testYValues[2] * factor, coords[(int)Math.round(dummyIndex[2])], 1);
}

这样可以正常工作,但是如果您熟悉JUnit,您可能会注意到为了使此测试正常工作所需的增量值1 ... 我想要实现的目标是:

@Test
public void testCurve(){
  final double[] testYValues = new double[]{20, 40, 90};
  final PolynomialSplineFunction testFunction = new SplineInterpolator().interpolate(dummyIndex, testYValues);//get a Spline-Interpolated curve
  final double[] coords = MapRouteDrawer.getCoords(testFunction, dummyIndex);//Calls the function to test
  final double factor;//Unknown... should be dynamic, so it does not matter which detail level I chose
  assertEquals(testYValues[0], coords[(int)Math.round(dummyIndex[0])] * factor, 0);//Check if the coordinates are equal
  assertEquals(testYValues[1], coords[(int)Math.round(dummyIndex[1])] * factor, 0);//e.g. coords[(int)Math.round(dummyIndex[0])] * factor == 20 etc.
  assertEquals(testYValues[2], coords[(int)Math.round(dummyIndex[2])] * factor, 0);
}

因此assertEquals()中的第一个值是20,40,90等而不是一个奇怪的21.39587576787686(或类似的)数字,delta是0(如果没有其他方式则为0.01)而不是1和I #39; m目前正在使用

因为我在我的getCoords()方法中使用了一个细节级别,而这个细节级别应该能够在不改变测试的情况下进行调整,所以需要制作&#34;因子&#34;在我的测试中与coords-size相关。

我如何计算因子以使测试成功?
非常感谢任何帮助

0 个答案:

没有答案