Search in sources :

Example 56 with DependsOnMethod

use of org.apache.sis.test.DependsOnMethod in project sis by apache.

the class DoubleDoubleTest method testSqrt.

/**
 * Tests {@link DoubleDouble#sqrt()} first with the square root of 2, then with random values.
 * In the {@code sqrt(2)} case:
 *
 * <ul>
 *   <li>The error using {@code double} arithmetic is approximatively 1E-16.</li>
 *   <li>The error using double-double arithmetic is expected to be slightly less that 1E-32.</li>
 * </ul>
 */
@Test
@DependsOnMethod({ "testMultiply", "testDivide" })
public void testSqrt() {
    final BigDecimal SQRT2 = new BigDecimal("1.414213562373095048801688724209698");
    final DoubleDouble dd = new DoubleDouble(2, 0);
    dd.sqrt();
    assertNormalizedAndEquals(sqrt(2), dd);
    assertEquals(0, SQRT2.subtract(toBigDecimal(dd)).doubleValue(), 1E-32);
    /*
         * If we have been able to compute √2, now test with random values.
         * Since the range of values is approximatively [-1000 … 1000], use
         * a tolerance value 1000 time the one that we used for √2.
         */
    for (int i = 0; i < NUMBER_OF_REPETITIONS; i++) {
        nextRandom(dd);
        if (dd.value < 0) {
            dd.negate();
        }
        final double value = dd.value;
        final double error = dd.error;
        dd.square();
        dd.sqrt();
        dd.subtract(value, error);
        assertEquals(0, dd.doubleValue(), 1E-29);
    }
    dd.clear();
    dd.sqrt();
    assertTrue(dd.isZero());
}
Also used : BigDecimal(java.math.BigDecimal) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 57 with DependsOnMethod

use of org.apache.sis.test.DependsOnMethod in project sis by apache.

the class DoubleDoubleTest method testErrorForWellKnownValue.

/**
 * Tests {@link DoubleDouble#errorForWellKnownValue(double)}.
 */
@Test
@DependsOnMethod("testArraysConsistency")
public void testErrorForWellKnownValue() {
    for (final String text : PREDEFINED_VALUES) {
        final double value = Double.valueOf(text);
        final BigDecimal accurate = new BigDecimal(text);
        final BigDecimal approximation = new BigDecimal(value);
        final double expected = accurate.subtract(approximation).doubleValue();
        assertEquals(text, expected, DoubleDouble.errorForWellKnownValue(value), STRICT);
        assertEquals(text, -expected, DoubleDouble.errorForWellKnownValue(-value), STRICT);
        assertFalse("There is no point to define an entry for values having no error.", expected == 0);
    }
}
Also used : BigDecimal(java.math.BigDecimal) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 58 with DependsOnMethod

use of org.apache.sis.test.DependsOnMethod in project sis by apache.

the class DecimalFunctionsTest method testDeltaForDoubleToDecimal.

/**
 * Tests {@link DecimalFunctions#deltaForDoubleToDecimal(double)}.
 * This method uses {@link BigDecimal} as the reference implementation.
 */
@Test
@DependsOnMethod("testPow10")
public void testDeltaForDoubleToDecimal() {
    assertEquals(0, deltaForDoubleToDecimal(0), STRICT);
    assertEquals(0, deltaForDoubleToDecimal(1), STRICT);
    assertEquals(0, deltaForDoubleToDecimal(NaN), STRICT);
    assertEquals(0, deltaForDoubleToDecimal(POSITIVE_INFINITY), STRICT);
    assertEquals(0, deltaForDoubleToDecimal(NEGATIVE_INFINITY), STRICT);
    assertEquals(-2.2204460492503132E-17, deltaForDoubleToDecimal(0.9), STRICT);
    assertEquals(-5.5511151231257827E-18, deltaForDoubleToDecimal(0.1), STRICT);
    assertEquals(-2.0816681711721684E-19, deltaForDoubleToDecimal(0.01), STRICT);
    assertEquals(-2.0816681711721686E-20, deltaForDoubleToDecimal(0.001), STRICT);
    assertEquals(-4.7921736023859296E-21, deltaForDoubleToDecimal(0.0001), STRICT);
    assertEquals(-8.1803053914031310E-22, deltaForDoubleToDecimal(0.00001), STRICT);
    assertEquals(4.5251888174113741E-23, deltaForDoubleToDecimal(0.000001), STRICT);
    // Link to metres
    assertEquals(-1.3471890270011499E-17, deltaForDoubleToDecimal(0.201168), STRICT);
    // Feet to metres
    assertEquals(-1.5365486660812166E-17, deltaForDoubleToDecimal(0.3048), STRICT);
    // Yard to metres
    assertEquals(9.4146912488213275E-18, deltaForDoubleToDecimal(0.9144), STRICT);
    // Fathom to metres
    assertEquals(1.8829382497642655E-17, deltaForDoubleToDecimal(1.8288), STRICT);
    // Inch to centimetres
    assertEquals(-3.5527136788005009E-17, deltaForDoubleToDecimal(2.54), STRICT);
    // Chain to metres
    assertEquals(-1.3471890270011499E-15, deltaForDoubleToDecimal(20.1168), STRICT);
    /*
         * Tests random value that do not use the full 'double' accuracy.
         * This is a simpler case than the next one after this one, because the
         * final adjustment at the end of deltaForDoubleToDecimal is not needed.
         */
    final Random random = TestUtilities.createRandomNumberGenerator();
    for (int fractionDigits = 0; fractionDigits <= 9; fractionDigits++) {
        for (int i = 0; i < 10; i++) {
            final BigDecimal value = BigDecimal.valueOf(random.nextInt(1000000000)).movePointLeft(fractionDigits);
            // Inexact approximation of value.
            final double ieee = value.doubleValue();
            final BigDecimal delta = value.subtract(new BigDecimal(ieee));
            assertEquals(delta.doubleValue(), deltaForDoubleToDecimal(ieee), STRICT);
        }
    }
    /*
         * Tests random values that do use the full 'double' accuracy. First, tests a few values which
         * were known to fail in an earlier version of deltaForDoubleToDecimal, then uses random values.
         * The expected values were computed with BigDecimal. The tolerance thresholds were determined
         * empirically. Comments on the right side give the tolerance thresholds in ULP of the delta.
         * The later are sometime hight, but it does not really matter. What matter is the tolerance
         * relative to the given value, not to the returned delta.
         */
    assertEquals(-1.9216378778219224E-23, deltaForDoubleToDecimal(3.3446045755169960E-7), STRICT);
    // 4 ULP
    assertEquals(-4.1861088853329420E-24, deltaForDoubleToDecimal(3.5496578465465944E-7), 3E-39);
    // 3 ULP
    assertEquals(-4.1997787803848041E-17, deltaForDoubleToDecimal(0.7714013208272988), 2E-32);
    // 4 ULP
    assertEquals(4.0373325589462183E-18, deltaForDoubleToDecimal(0.37197394704138476), 4E-33);
    // 9 ULP
    assertEquals(-2.3295945035351907E-18, deltaForDoubleToDecimal(0.25380700796141886), 4E-33);
    // 43 ULP
    assertEquals(-4.1729149110324215E-18, deltaForDoubleToDecimal(0.6546245266605436), 4E-32);
    // 5666840 ULP
    assertEquals(4.8633955884724856E-23, deltaForDoubleToDecimal(0.8234936921177336), 4E-32);
    // 36267774 ULP
    assertEquals(-2.1507730707526207E-25, deltaForDoubleToDecimal(0.19920566694813302), 2E-33);
    for (int i = 0; i < 500; i++) {
        double ieee = StrictMath.scalb(random.nextDouble(), 20 - random.nextInt(48));
        if (random.nextBoolean()) {
            ieee = -ieee;
        }
        final String text = String.valueOf(ieee);
        final BigDecimal value = new BigDecimal(text);
        final double delta = value.subtract(new BigDecimal(ieee)).doubleValue();
        final double actual = deltaForDoubleToDecimal(ieee);
        if (!Double.isNaN(actual)) {
            assertEquals(text, delta, actual, StrictMath.ulp(ieee) * 1E-12);
        }
    }
}
Also used : Random(java.util.Random) BigDecimal(java.math.BigDecimal) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 59 with DependsOnMethod

use of org.apache.sis.test.DependsOnMethod in project sis by apache.

the class DecimalFunctionsTest method testFloatToDouble.

/**
 * Tests {@link DecimalFunctions#floatToDouble(float)}.
 */
@Test
@DependsOnMethod("testPow10")
public void testFloatToDouble() {
    assertEquals(NaN, floatToDouble(Float.NaN), STRICT);
    assertEquals(POSITIVE_INFINITY, floatToDouble(Float.POSITIVE_INFINITY), STRICT);
    assertEquals(NEGATIVE_INFINITY, floatToDouble(Float.NEGATIVE_INFINITY), STRICT);
    assertEquals(0.0, floatToDouble(0.0f), STRICT);
    assertEquals(-0.0, floatToDouble(-0.0f), STRICT);
    assertEquals(10, floatToDouble(10f), STRICT);
    assertEquals(-10, floatToDouble(-10f), STRICT);
    assertEquals(0.1, floatToDouble(0.1f), STRICT);
    assertEquals(0.01, floatToDouble(0.01f), STRICT);
    assertEquals(-0.01, floatToDouble(-0.01f), STRICT);
    assertEquals(0.001, floatToDouble(0.001f), STRICT);
    assertEquals(0.0001, floatToDouble(0.0001f), STRICT);
    assertEquals(3.7E-8, floatToDouble(3.7E-8f), STRICT);
    assertEquals(3.7E-9, floatToDouble(3.7E-9f), STRICT);
    final Random random = TestUtilities.createRandomNumberGenerator();
    for (int i = 0; i < 100; i++) {
        float value = StrictMath.scalb(random.nextFloat(), random.nextInt(20) - 10);
        if (random.nextBoolean()) {
            value = -value;
        }
        assertEquals(String.valueOf(value), String.valueOf(floatToDouble(value)));
    }
}
Also used : Random(java.util.Random) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 60 with DependsOnMethod

use of org.apache.sis.test.DependsOnMethod in project sis by apache.

the class MetadataStandardTest method testHashCode.

/**
 * Tests {@link MetadataStandard#hashCode(Object)} using {@link HashSet} as the reference
 * implementation for computing hash code values. The hash code is defined as the sum of
 * hash code values of all non-empty properties, plus the hash code of the interface.
 */
@Test
@DependsOnMethod("testValueMap")
public void testHashCode() {
    standard = MetadataStandard.ISO_19115;
    final DefaultCitation instance = HardCodedCitations.EPSG;
    final Map<String, Object> map = standard.asValueMap(instance, null, KeyNamePolicy.JAVABEANS_PROPERTY, ValueExistencePolicy.NON_EMPTY);
    // Actually 'testValueMap()' job, but verified for safety.
    assertFalse(map.isEmpty());
    assertEquals("hashCode()", new HashSet<>(map.values()).hashCode() + Citation.class.hashCode(), standard.hashCode(instance));
}
Also used : DefaultCitation(org.apache.sis.metadata.iso.citation.DefaultCitation) SimpleIdentifiedObject(org.apache.sis.internal.simple.SimpleIdentifiedObject) IdentifiedObject(org.opengis.referencing.IdentifiedObject) SimpleInternationalString(org.apache.sis.util.iso.SimpleInternationalString) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Aggregations

DependsOnMethod (org.apache.sis.test.DependsOnMethod)298 Test (org.junit.Test)296 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)27 ProjectedCRS (org.opengis.referencing.crs.ProjectedCRS)23 DefaultCitation (org.apache.sis.metadata.iso.citation.DefaultCitation)21 CoordinateOperation (org.opengis.referencing.operation.CoordinateOperation)21 Rectangle (java.awt.Rectangle)19 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)19 SimpleInternationalString (org.apache.sis.util.iso.SimpleInternationalString)18 GeographicCRS (org.opengis.referencing.crs.GeographicCRS)15 Random (java.util.Random)11 Matrix (org.opengis.referencing.operation.Matrix)11 HashMap (java.util.HashMap)10 IdentifiedObject (org.opengis.referencing.IdentifiedObject)8 ReferenceIdentifier (org.opengis.referencing.ReferenceIdentifier)8 MathTransform (org.opengis.referencing.operation.MathTransform)8 DefaultFeatureType (org.apache.sis.feature.DefaultFeatureType)7 DefaultFeatureTypeTest (org.apache.sis.feature.DefaultFeatureTypeTest)7 DirectPosition2D (org.apache.sis.geometry.DirectPosition2D)7 GeneralParameterDescriptor (org.opengis.parameter.GeneralParameterDescriptor)7