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());
}
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"));
}
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());
}
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);
}
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);
}
Aggregations