Search in sources :

Example 46 with DependsOnMethod

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

the class UnitFormatTest method testParsePosition.

/**
 * Tests parsing a unit from another position than zero and verifies that {@code UnitFormat} detects
 * correctly where the unit symbol ends.
 */
@Test
@DependsOnMethod("testParseSymbol")
public void testParsePosition() {
    final UnitFormat f = new UnitFormat(Locale.UK);
    final ParsePosition pos = new ParsePosition(4);
    assertSame(Units.CENTIMETRE, f.parse("ABC cm DEF", pos));
    assertEquals("ParsePosition.getIndex()", 6, pos.getIndex());
    assertEquals("ParsePosition.getErrorIndex()", -1, pos.getErrorIndex());
    /*
         * Adding "cm DEF" as a unit label should allow UnitFormat to recognize those characters.
         * We associate a random unit to that label, just for testing purpose.
         */
    pos.setIndex(4);
    f.label(Units.HECTARE, "cm DEF");
    assertSame(Units.HECTARE, f.parse("ABC cm DEF", pos));
    assertEquals("ParsePosition.getIndex()", 10, pos.getIndex());
    assertEquals("ParsePosition.getErrorIndex()", -1, pos.getErrorIndex());
}
Also used : ParsePosition(java.text.ParsePosition) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 47 with DependsOnMethod

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

the class LocalesTest method testParse.

/**
 * Tests the {@link Locales#parse(String)} method.
 */
@Test
@DependsOnMethod("testUnique")
public void testParse() {
    assertSame(Locale.ENGLISH, Locales.parse("en"));
    assertSame(Locale.FRENCH, Locales.parse("fr"));
    assertSame(Locale.FRENCH, Locales.parse("fra"));
    assertSame(Locale.CANADA_FRENCH, Locales.parse("fr_CA"));
    assertSame(Locale.CANADA_FRENCH, Locales.parse("fra_CA"));
    assertSame(Locale.CANADA_FRENCH, Locales.parse("fr_CAN"));
    assertSame(Locale.CANADA_FRENCH, Locales.parse("fra_CAN"));
    assertSame(Locale.JAPAN, Locales.parse("ja_JP"));
    assertSame(Locale.US, Locales.parse("en; USA"));
    assertEquals(new Locale("de", "DE"), Locales.parse("de_DE"));
    assertEquals(new Locale("", "GB"), Locales.parse("_GB"));
    assertEquals(new Locale("en", "US", "WINDOWS"), Locales.parse("en_US_WINDOWS"));
    assertEquals(new Locale("de", "", "POSIX"), Locales.parse("de__POSIX"));
}
Also used : Locale(java.util.Locale) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 48 with DependsOnMethod

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

the class CacheTest method stress.

/**
 * Starts many threads writing in the same cache, with a high probability that two threads
 * ask for the same key in some occasions.
 *
 * @throws InterruptedException if the test has been interrupted.
 */
@Test
@Performance
@DependsOnMethod("testThreadBlocking")
public void stress() throws InterruptedException {
    final int count = 5000;
    final Cache<Integer, Integer> cache = new Cache<>();
    final AtomicReference<Throwable> failures = new AtomicReference<>();
    final class WriterThread extends Thread {

        /**
         * Incremented every time a value has been added. This is not the number of time the
         * loop has been executed, since this variable is not incremented when a value already
         * exists for a key.
         */
        int addCount;

        /**
         * Creates a new thread.
         */
        WriterThread(final int i) {
            super(TestUtilities.THREADS, "CacheTest.stress() #" + i);
        }

        /**
         * Puts random values in the map.
         */
        @SuppressWarnings({ "UnnecessaryBoxing", "CallToThreadYield", "NumberEquality" })
        @Override
        public void run() {
            for (int i = 0; i < count; i++) {
                final Integer key = i;
                // We really want new instance.
                final Integer expected = new Integer(i * i);
                final Integer value;
                try {
                    value = cache.getOrCreate(key, () -> expected);
                    assertEquals(expected, value);
                } catch (Throwable e) {
                    if (!failures.compareAndSet(null, e)) {
                        failures.get().addSuppressed(e);
                    }
                    continue;
                }
                if (expected == value) {
                    // Identity comparison (not value comparison).
                    addCount++;
                    // Gives a chance to other threads.
                    yield();
                }
            }
        }
    }
    final WriterThread[] threads = new WriterThread[50];
    for (int i = 0; i < threads.length; i++) threads[i] = new WriterThread(i);
    for (int i = 0; i < threads.length; i++) threads[i].start();
    for (int i = 0; i < threads.length; i++) threads[i].join();
    TestUtilities.rethrownIfNotNull(failures.get());
    /*
         * Verifies the values.
         */
    final Statistics beforeGC = validateStressEntries("Before GC", cache);
    assertTrue("Should not have more entries than what we put in.", cache.size() <= count);
    assertFalse("Some entries should be retained by strong references.", cache.isEmpty());
    /*
         * If verbose test output is enabled, report the number of cache hits.
         * The numbers below are for tuning the test only. The output is somewhat
         * random so we can not check it in a test suite.  However if the test is
         * properly tuned, most values should be non-zero.
         */
    final PrintWriter out = CacheTest.out;
    TestUtilities.printSeparator("CacheTest.stress() - testing concurrent accesses");
    out.print("There is ");
    out.print(threads.length);
    out.print(" threads, each of them" + " fetching or creating ");
    out.print(count);
    out.println(" values.");
    out.println("Number of times a new value has been created, for each thread:");
    for (int i = 0; i < threads.length; ) {
        final String n = String.valueOf(threads[i++].addCount);
        out.print(CharSequences.spaces(6 - n.length()));
        out.print(n);
        if ((i % 10) == 0) {
            out.println();
        }
    }
    out.println();
    out.println("Now observe how the background thread cleans the cache.");
    long time = System.nanoTime();
    for (int i = 0; i < 10; i++) {
        final long t = System.nanoTime();
        out.printf("Cache size: %4d (after %3d ms)%n", cache.size(), round((t - time) / (double) StandardDateFormat.NANOS_PER_MILLISECOND));
        time = t;
        Thread.sleep(250);
        if (i >= 2) {
            System.gc();
        }
    }
    out.println();
    /*
         * Gets the statistics of key values after garbage collection. Most values should
         * be higher, because oldest values (which should have been garbage collected first)
         * have lower values. If verbose output is enabled, then we will print the statistics
         * before to perform the actual check in order to allow the developer to have more
         * information in case of failure.
         *
         * The mean value is often greater, but not always. Since we have fewer remaining values
         * (100 instead of 10000), the remaining low values will have a much greater impact on
         * the mean. Only the check on the minimal value is fully reliable.
         */
    final Statistics afterGC = validateStressEntries("After GC", cache);
    out.println("Statistics on the keys before and after garbage collection.");
    out.println("The minimum value shall always be equals or greater after GC.");
    out.println("The mean value is usually greater too, except by coincidence.");
    final StatisticsFormat format = StatisticsFormat.getInstance();
    format.setBorderWidth(1);
    try {
        format.format(new Statistics[] { beforeGC, afterGC }, out);
    } catch (IOException e) {
        throw new AssertionError(e);
    }
    assertTrue("Minimum key value should be greater after garbage collection.", afterGC.minimum() >= beforeGC.minimum());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) Statistics(org.apache.sis.math.Statistics) StatisticsFormat(org.apache.sis.math.StatisticsFormat) PrintWriter(java.io.PrintWriter) Test(org.junit.Test) Performance(org.apache.sis.test.Performance) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 49 with DependsOnMethod

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

the class PlaneTest method testFitGrid.

/**
 * Verifies that {@link Plane#fit(int, int, Vector)} produces the same result than
 * {@link Plane#fit(double[], double[], double[])}.
 *
 * @since 0.8
 */
@Test
@DependsOnMethod("testFitManyPoints")
public void testFitGrid() {
    final int nx = 20;
    final int ny = 30;
    final Random rd = new Random(1224444984079270867L);
    final double[] z = random(rd, nx * ny, 12, -6);
    final double[] x = new double[z.length];
    final double[] y = new double[z.length];
    final Plane reference = new Plane(-46, 17, 35);
    for (int i = 0; i < z.length; i++) {
        x[i] = i % nx;
        y[i] = i / nx;
        z[i] += reference.z(x[i], y[i]);
    }
    /*
         * Opportunistically verify the result of fit(double[], double[], double[]), but it is
         * not the purpose of this test (it should have been verified by testFitManyPoints()).
         * For a random seed fixed to 7241997054993322309, the coefficients are:
         *
         *   - reference:     z(x,y) = -46.0⋅x + 17.0⋅y + 35.0
         *   - fitted:        z(x,y) = -45.96034442769177⋅x + 16.981792790278828⋅y + 34.901440258861314
         */
    final Plane fitted = assertFitEquals(7, x, y, z);
    assertEquals("sx", reference.slopeX(), fitted.slopeX(), 0.05);
    assertEquals("sy", reference.slopeY(), fitted.slopeY(), 0.02);
    assertEquals("z₀", reference.z0(), fitted.z0(), 0.1);
    final double ep = pearson;
    /*
         * Verify that the optimized code path for a grid produces the exact same result than the
         * generic code path.
         */
    final Plane gf = new Plane();
    gf.fit(nx, ny, Vector.create(z, false));
    assertEquals("sx", fitted.slopeX(), gf.slopeX(), STRICT);
    assertEquals("sy", fitted.slopeY(), gf.slopeY(), STRICT);
    assertEquals("z₀", fitted.z0(), gf.z0(), STRICT);
    assertEquals("Pearson", ep, pearson, STRICT);
}
Also used : Random(java.util.Random) Test(org.junit.Test) DependsOnMethod(org.apache.sis.test.DependsOnMethod)

Example 50 with DependsOnMethod

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

the class PlaneTest method testFitManyPoints.

/**
 * Tests {@link Plane#fit(double[], double[], double[])} with 4000 points.
 */
@Test
@DependsOnMethod("testFit3Points")
public void testFitManyPoints() {
    final Random rd = new Random(7241997054993322309L);
    final double[] x = random(rd, 4000, 100, -25);
    final double[] y = random(rd, 4000, 80, -20);
    final double[] z = random(rd, 4000, 10, -5);
    final Plane reference = new Plane(2, 3, -4);
    for (int i = 0; i < z.length; i++) {
        // Compute points with random displacements above or below a known plane.
        z[i] += reference.z(x[i], y[i]);
    }
    /*
         * For a random seed fixed to 7241997054993322309, the coefficients are:
         *
         *   - reference:     z(x,y) = 2.0⋅x + 3.0⋅y + -4.0
         *   - fitted:        z(x,y) = 2.001595888896693⋅x + 3.0021028196088055⋅y + -4.105960575835259
         */
    final Plane fitted = assertFitEquals(6, x, y, z);
    assertEquals("sx", reference.slopeX(), fitted.slopeX(), 0.002);
    assertEquals("sy", reference.slopeY(), fitted.slopeY(), 0.003);
    assertEquals("z₀", reference.z0(), fitted.z0(), 0.2);
}
Also used : Random(java.util.Random) 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