001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2002, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.coverage;
018:
019: // J2SE dependencies
020: import java.util.Random;
021:
022: // JUnit dependencies
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: // OpenGIS dependencies
028: import org.opengis.referencing.operation.MathTransform1D;
029: import org.opengis.referencing.operation.TransformException;
030:
031: /**
032: * Tests the {@link Category} implementation.
033: *
034: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/test/java/org/geotools/coverage/CategoryTest.java $
035: * @version $Id: CategoryTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
036: * @author Martin Desruisseaux
037: */
038: public class CategoryTest extends TestCase {
039: /**
040: * Run the suite from the command line.
041: */
042: public static void main(String[] args) {
043: org.geotools.util.logging.Logging.GEOTOOLS
044: .forceMonolineConsoleOutput();
045: junit.textui.TestRunner.run(suite());
046: }
047:
048: /**
049: * Returns the test suite.
050: */
051: public static Test suite() {
052: return new TestSuite(CategoryTest.class);
053: }
054:
055: /**
056: * Random number generator for this test.
057: */
058: private static final Random random = new Random(
059: 9119969932919929834L);
060:
061: /**
062: * Constructs a test case with the given name.
063: */
064: public CategoryTest(final String name) {
065: super (name);
066: }
067:
068: /**
069: * Check if a {@link Comparable} is a number identical to the supplied integer value.
070: */
071: private static void assertEquals(String message, Comparable number,
072: int expected) {
073: assertTrue("Integer.class", number instanceof Integer);
074: assertEquals(message, expected, ((Number) number).intValue());
075: }
076:
077: /**
078: * Check if a {@link Comparable} is a number identical to the supplied float value.
079: */
080: private static void assertEquals(String message, Comparable number,
081: double expected, double EPS) {
082: assertTrue("Double.class", number instanceof Double);
083: final double actual = ((Number) number).doubleValue();
084: if (Double.isNaN(expected)) {
085: assertEquals(message, toHexString(expected),
086: toHexString(actual));
087: } else {
088: assertEquals(message, expected, actual, EPS);
089: }
090: }
091:
092: /**
093: * Returns the specified value as an hexadecimal string. Usefull
094: * for comparing NaN values.
095: */
096: private static String toHexString(final double value) {
097: return Integer.toHexString(Float
098: .floatToRawIntBits((float) value));
099: }
100:
101: /**
102: * Make sure that qualitative category produce the expected result.
103: */
104: public void testQualitativeCategory() throws TransformException {
105: for (int pass = 0; pass < 100; pass++) {
106: final int sample = random.nextInt(64);
107: final Category category1 = new Category("Auto", null,
108: sample);
109: final Category category2 = new Category(
110: category1.getName(), category1.getColors(),
111: category1.getRange(), category1
112: .getSampleToGeophysics());
113:
114: assertEquals("<init>", category1, category2);
115: assertEquals("lower", category1.geophysics(false)
116: .getRange().getMinValue(), sample);
117: assertEquals("upper", category1.geophysics(false)
118: .getRange().getMaxValue(), sample);
119:
120: assertNull("geophysics(false)", category1.geophysics(false)
121: .getSampleToGeophysics());
122: assertNull("geophysics(true)", category1.geophysics(true)
123: .getSampleToGeophysics());
124: for (int i = 0; i < 200; i++) {
125: final double x = 100 * random.nextDouble();
126: final double y1 = category1.transform.transform(x);
127: final double y2 = category2.transform.transform(x);
128: assertTrue("toGeophysics(1)", Double.isNaN(y1));
129: assertTrue("toGeophysics(2)", Double.isNaN(y2));
130: assertEquals("NaN", Double.doubleToRawLongBits(y1),
131: Double.doubleToRawLongBits(y2));
132: assertEquals("toSample(1)", sample,
133: category1.inverse.transform.transform(y1), 0);
134: assertEquals("toSample(2)", sample,
135: category2.inverse.transform.transform(y2), 0);
136: }
137: }
138: }
139:
140: /**
141: * Make sure that linear category produce the expected result.
142: * This test check also if the default {@link MathTransform1D}
143: * for a linear relation is right.
144: */
145: public void testLinearCategory() throws TransformException {
146: final double EPS = 1E-9;
147: for (int pass = 0; pass < 100; pass++) {
148: final int lower = random.nextInt(64);
149: final int upper = random.nextInt(128) + lower + 1;
150: final double scale = 10 * random.nextDouble() + 0.1; // Must be positive for this test.
151: final double offset = 10 * random.nextDouble() - 5.0;
152: final Category category = new Category("Auto", null, lower,
153: upper, scale, offset);
154:
155: assertEquals("lower", category.geophysics(false).getRange()
156: .getMinValue(), lower);
157: assertEquals("upper", category.geophysics(false).getRange()
158: .getMaxValue(), upper);
159: assertEquals("minimum", category.geophysics(true)
160: .getRange().getMinValue(), lower * scale + offset,
161: EPS);
162: assertEquals("maximum", category.geophysics(true)
163: .getRange().getMaxValue(), upper * scale + offset,
164: EPS);
165:
166: for (int i = 0; i < 200; i++) {
167: final double x = 100 * random.nextDouble();
168: final double y = x * scale + offset;
169: assertEquals("toGeophysics", y, category.transform
170: .transform(x), EPS);
171: assertEquals("toSample", x, category.inverse.transform
172: .transform(y), EPS);
173: }
174: }
175: }
176: }
|