Improved test coverage.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1571737 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2014-02-25 16:42:07 +00:00
parent d3470c37ab
commit eb66fe4a1f
14 changed files with 1174 additions and 8 deletions

View File

@ -53,13 +53,13 @@ public class Euclidean1D implements Serializable, Space {
/** {@inheritDoc}
* <p>
* As the 1-dimension Euclidean space does not have proper sub-spaces,
* this method always throws a {@link MathUnsupportedOperationException}
* this method always throws a {@link NoSubSpaceException}
* </p>
* @return nothing
* @throws MathUnsupportedOperationException in all cases
* @throws NoSubSpaceException in all cases
*/
public Space getSubSpace() throws MathUnsupportedOperationException {
throw new MathUnsupportedOperationException(LocalizedFormats.NOT_SUPPORTED_IN_DIMENSION_N, 1);
public Space getSubSpace() throws NoSubSpaceException {
throw new NoSubSpaceException();
}
// CHECKSTYLE: stop HideUtilityClassConstructor
@ -80,4 +80,22 @@ public class Euclidean1D implements Serializable, Space {
return LazyHolder.INSTANCE;
}
/** Specialized exception for inexistent sub-space.
* <p>
* This exception is thrown when attempting to get the sub-space of a one-dimensional space
* </p>
*/
public static class NoSubSpaceException extends MathUnsupportedOperationException {
/** Serializable UID. */
private static final long serialVersionUID = 20140225L;
/** Simple constructor.
*/
public NoSubSpaceException() {
super(LocalizedFormats.NOT_SUPPORTED_IN_DIMENSION_N, 1);
}
}
}

View File

@ -59,13 +59,13 @@ public class Sphere1D implements Serializable, Space {
/** {@inheritDoc}
* <p>
* As the 1-dimension sphere does not have proper sub-spaces,
* this method always throws a {@link MathUnsupportedOperationException}
* this method always throws a {@link NoSubSpaceException}
* </p>
* @return nothing
* @throws MathUnsupportedOperationException in all cases
* @throws NoSubSpaceException in all cases
*/
public Space getSubSpace() throws MathUnsupportedOperationException {
throw new MathUnsupportedOperationException(LocalizedFormats.NOT_SUPPORTED_IN_DIMENSION_N, 1);
public Space getSubSpace() throws NoSubSpaceException {
throw new NoSubSpaceException();
}
// CHECKSTYLE: stop HideUtilityClassConstructor
@ -86,4 +86,22 @@ public class Sphere1D implements Serializable, Space {
return LazyHolder.INSTANCE;
}
/** Specialized exception for inexistent sub-space.
* <p>
* This exception is thrown when attempting to get the sub-space of a one-dimensional space
* </p>
*/
public static class NoSubSpaceException extends MathUnsupportedOperationException {
/** Serializable UID. */
private static final long serialVersionUID = 20140225L;
/** Simple constructor.
*/
public NoSubSpaceException() {
super(LocalizedFormats.NOT_SUPPORTED_IN_DIMENSION_N, 1);
}
}
}

View File

@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.geometry.euclidean.oned;
import org.apache.commons.math3.TestUtils;
import org.apache.commons.math3.geometry.Space;
import org.junit.Assert;
import org.junit.Test;
public class Euclidean1DTest {
@Test
public void testDimension() {
Assert.assertEquals(1, Euclidean1D.getInstance().getDimension());
}
@Test(expected=Euclidean1D.NoSubSpaceException.class)
public void testSubSpace() {
Euclidean1D.getInstance().getSubSpace();
}
@Test
public void testSerialization() {
Space e1 = Euclidean1D.getInstance();
Space deserialized = (Space) TestUtils.serializeAndRecover(e1);
Assert.assertTrue(e1 == deserialized);
}
}

View File

@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.geometry.euclidean.oned;
import java.util.Locale;
public class FrenchVector1DFormatTest extends Vector1DFormatAbstractTest {
@Override
protected char getDecimalCharacter() {
return ',';
}
@Override
protected Locale getLocale() {
return Locale.FRENCH;
}
}

View File

@ -0,0 +1,273 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.geometry.euclidean.oned;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Locale;
import org.apache.commons.math3.exception.MathParseException;
import org.junit.Assert;
import org.junit.Test;
public abstract class Vector1DFormatAbstractTest {
Vector1DFormat vector1DFormat = null;
Vector1DFormat vector1DFormatSquare = null;
protected abstract Locale getLocale();
protected abstract char getDecimalCharacter();
protected Vector1DFormatAbstractTest() {
vector1DFormat = Vector1DFormat.getInstance(getLocale());
final NumberFormat nf = NumberFormat.getInstance(getLocale());
nf.setMaximumFractionDigits(2);
vector1DFormatSquare = new Vector1DFormat("[", "]", nf);
}
@Test
public void testSimpleNoDecimals() {
Vector1D c = new Vector1D(1);
String expected = "{1}";
String actual = vector1DFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testSimpleWithDecimals() {
Vector1D c = new Vector1D(1.23);
String expected =
"{1" + getDecimalCharacter() +
"23}";
String actual = vector1DFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testSimpleWithDecimalsTrunc() {
Vector1D c = new Vector1D(1.232323232323);
String expected =
"{1" + getDecimalCharacter() +
"2323232323}";
String actual = vector1DFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testNegativeX() {
Vector1D c = new Vector1D(-1.232323232323);
String expected =
"{-1" + getDecimalCharacter() +
"2323232323}";
String actual = vector1DFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testNonDefaultSetting() {
Vector1D c = new Vector1D(1);
String expected = "[1]";
String actual = vector1DFormatSquare.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testDefaultFormatVector1D() {
Locale defaultLocal = Locale.getDefault();
Locale.setDefault(getLocale());
Vector1D c = new Vector1D(232.22222222222);
String expected =
"{232" + getDecimalCharacter() +
"2222222222}";
String actual = (new Vector1DFormat()).format(c);
Assert.assertEquals(expected, actual);
Locale.setDefault(defaultLocal);
}
@Test
public void testNan() {
Vector1D c = Vector1D.NaN;
String expected = "{(NaN)}";
String actual = vector1DFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testPositiveInfinity() {
Vector1D c = Vector1D.POSITIVE_INFINITY;
String expected = "{(Infinity)}";
String actual = vector1DFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void tesNegativeInfinity() {
Vector1D c = Vector1D.NEGATIVE_INFINITY;
String expected = "{(-Infinity)}";
String actual = vector1DFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseSimpleNoDecimals() throws MathParseException {
String source = "{1}";
Vector1D expected = new Vector1D(1);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseIgnoredWhitespace() {
Vector1D expected = new Vector1D(1);
ParsePosition pos1 = new ParsePosition(0);
String source1 = "{1}";
Assert.assertEquals(expected, vector1DFormat.parse(source1, pos1));
Assert.assertEquals(source1.length(), pos1.getIndex());
ParsePosition pos2 = new ParsePosition(0);
String source2 = " { 1 } ";
Assert.assertEquals(expected, vector1DFormat.parse(source2, pos2));
Assert.assertEquals(source2.length() - 1, pos2.getIndex());
}
@Test
public void testParseSimpleWithDecimals() throws MathParseException {
String source =
"{1" + getDecimalCharacter() +
"23}";
Vector1D expected = new Vector1D(1.23);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseSimpleWithDecimalsTrunc() throws MathParseException {
String source =
"{1" + getDecimalCharacter() +
"2323}";
Vector1D expected = new Vector1D(1.2323);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseNegativeX() throws MathParseException {
String source =
"{-1" + getDecimalCharacter() +
"2323}";
Vector1D expected = new Vector1D(-1.2323);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseNegativeY() throws MathParseException {
String source =
"{1" + getDecimalCharacter() +
"2323}";
Vector1D expected = new Vector1D(1.2323);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseNegativeZ() throws MathParseException {
String source =
"{1" + getDecimalCharacter() +
"2323}";
Vector1D expected = new Vector1D(1.2323);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseNegativeAll() throws MathParseException {
String source =
"{-1" + getDecimalCharacter() +
"2323}";
Vector1D expected = new Vector1D(-1.2323);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseZeroX() throws MathParseException {
String source =
"{0" + getDecimalCharacter() +
"0}";
Vector1D expected = new Vector1D(0.0);
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseNonDefaultSetting() throws MathParseException {
String source =
"[1" + getDecimalCharacter() +
"2323]";
Vector1D expected = new Vector1D(1.2323);
Vector1D actual = vector1DFormatSquare.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseNan() throws MathParseException {
String source = "{(NaN)}";
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(Vector1D.NaN, actual);
}
@Test
public void testParsePositiveInfinity() throws MathParseException {
String source = "{(Infinity)}";
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(Vector1D.POSITIVE_INFINITY, actual);
}
@Test
public void testParseNegativeInfinity() throws MathParseException {
String source = "{(-Infinity)}";
Vector1D actual = vector1DFormat.parse(source);
Assert.assertEquals(Vector1D.NEGATIVE_INFINITY, actual);
}
@Test
public void testConstructorSingleFormat() {
NumberFormat nf = NumberFormat.getInstance();
Vector1DFormat cf = new Vector1DFormat(nf);
Assert.assertNotNull(cf);
Assert.assertEquals(nf, cf.getFormat());
}
@Test
public void testForgottenPrefix() {
ParsePosition pos = new ParsePosition(0);
Assert.assertNull(new Vector1DFormat().parse("1}", pos));
Assert.assertEquals(0, pos.getErrorIndex());
}
@Test
public void testForgottenSuffix() {
ParsePosition pos = new ParsePosition(0);
Assert.assertNull(new Vector1DFormat().parse("{1 ", pos));
Assert.assertEquals(2, pos.getErrorIndex());
}
}

View File

@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.geometry.euclidean.oned;
import java.util.Locale;
public class Vector1DFormatTest extends Vector1DFormatAbstractTest {
@Override
protected char getDecimalCharacter() {
return '.';
}
@Override
protected Locale getLocale() {
return Locale.US;
}
}

View File

@ -0,0 +1,219 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.geometry.euclidean.oned;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;
import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.MathArithmeticException;
import org.apache.commons.math3.geometry.Space;
import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.util.Precision;
import org.junit.Assert;
import org.junit.Test;
public class Vector1DTest {
@Test
public void testConstructors() throws DimensionMismatchException {
checkVector(new Vector1D(3, new Vector1D(FastMath.PI / 3)),
FastMath.PI);
checkVector(new Vector1D(2, Vector1D.ONE, -3, new Vector1D(2)),
-4);
checkVector(new Vector1D(2, Vector1D.ONE,
5, new Vector1D(2),
-3, new Vector1D(3)),
3);
checkVector(new Vector1D(2, Vector1D.ONE,
5, new Vector1D(2),
5, new Vector1D(-2),
-3, new Vector1D(-3)),
11);
}
@Test
public void testSpace() {
Space space = new Vector1D(1).getSpace();
Assert.assertEquals(1, space.getDimension());
}
@Test
public void testZero() {
Assert.assertEquals(0, new Vector1D(1).getZero().getNorm(), 1.0e-15);
}
@Test
public void testEquals() {
Vector1D u1 = new Vector1D(1);
Vector1D u2 = new Vector1D(1);
Assert.assertTrue(u1.equals(u1));
Assert.assertTrue(u1.equals(u2));
Assert.assertFalse(u1.equals(new Vector1D(1 + 10 * Precision.EPSILON)));
Assert.assertTrue(new Vector1D(Double.NaN).equals(new Vector1D(Double.NaN)));
}
@Test
public void testHash() {
Assert.assertEquals(new Vector1D(Double.NaN).hashCode(), new Vector1D(Double.NaN).hashCode());
Vector1D u = new Vector1D(1);
Vector1D v = new Vector1D(1 + 10 * Precision.EPSILON);
Assert.assertTrue(u.hashCode() != v.hashCode());
}
@Test
public void testInfinite() {
Assert.assertTrue(new Vector1D(Double.NEGATIVE_INFINITY).isInfinite());
Assert.assertTrue(new Vector1D(Double.POSITIVE_INFINITY).isInfinite());
Assert.assertFalse(new Vector1D(1).isInfinite());
Assert.assertFalse(new Vector1D(Double.NaN).isInfinite());
}
@Test
public void testNaN() {
Assert.assertTrue(new Vector1D(Double.NaN).isNaN());
Assert.assertFalse(new Vector1D(1).isNaN());
Assert.assertFalse(new Vector1D(Double.NEGATIVE_INFINITY).isNaN());
}
@Test
public void testToString() {
Assert.assertEquals("{3}", new Vector1D(3).toString());
NumberFormat format = new DecimalFormat("0.000", new DecimalFormatSymbols(Locale.US));
Assert.assertEquals("{3.000}", new Vector1D(3).toString(format));
}
@Test
public void testCoordinates() {
Vector1D v = new Vector1D(1);
Assert.assertTrue(FastMath.abs(v.getX() - 1) < 1.0e-12);
}
@Test
public void testNorm1() {
Assert.assertEquals(0.0, Vector1D.ZERO.getNorm1(), 0);
Assert.assertEquals(6.0, new Vector1D(6).getNorm1(), 0);
}
@Test
public void testNorm() {
Assert.assertEquals(0.0, Vector1D.ZERO.getNorm(), 0);
Assert.assertEquals(3.0, new Vector1D(-3).getNorm(), 1.0e-12);
}
@Test
public void testNormSq() {
Assert.assertEquals(0.0, new Vector1D(0).getNormSq(), 0);
Assert.assertEquals(9.0, new Vector1D(-3).getNormSq(), 1.0e-12);
}
@Test
public void testNormInf() {
Assert.assertEquals(0.0, Vector1D.ZERO.getNormInf(), 0);
Assert.assertEquals(3.0, new Vector1D(-3).getNormInf(), 0);
}
@Test
public void testDistance1() {
Vector1D v1 = new Vector1D(1);
Vector1D v2 = new Vector1D(-4);
Assert.assertEquals(0.0, new Vector1D(-1).distance1(new Vector1D(-1)), 0);
Assert.assertEquals(5.0, v1.distance1(v2), 1.0e-12);
Assert.assertEquals(v1.subtract(v2).getNorm1(), v1.distance1(v2), 1.0e-12);
}
@Test
public void testDistance() {
Vector1D v1 = new Vector1D(1);
Vector1D v2 = new Vector1D(-4);
Assert.assertEquals(0.0, Vector1D.distance(new Vector1D(-1), new Vector1D(-1)), 0);
Assert.assertEquals(5.0, Vector1D.distance(v1, v2), 1.0e-12);
Assert.assertEquals(v1.subtract(v2).getNorm(), Vector1D.distance(v1, v2), 1.0e-12);
}
@Test
public void testDistanceSq() {
Vector1D v1 = new Vector1D(1);
Vector1D v2 = new Vector1D(-4);
Assert.assertEquals(0.0, Vector1D.distanceSq(new Vector1D(-1), new Vector1D(-1)), 0);
Assert.assertEquals(25.0, Vector1D.distanceSq(v1, v2), 1.0e-12);
Assert.assertEquals(Vector1D.distance(v1, v2) * Vector1D.distance(v1, v2),
Vector1D.distanceSq(v1, v2), 1.0e-12);
}
@Test
public void testDistanceInf() {
Vector1D v1 = new Vector1D(1);
Vector1D v2 = new Vector1D(-4);
Assert.assertEquals(0.0, Vector1D.distanceInf(new Vector1D(-1), new Vector1D(-1)), 0);
Assert.assertEquals(5.0, Vector1D.distanceInf(v1, v2), 1.0e-12);
Assert.assertEquals(v1.subtract(v2).getNormInf(), Vector1D.distanceInf(v1, v2), 1.0e-12);
}
@Test
public void testSubtract() {
Vector1D v1 = new Vector1D(1);
Vector1D v2 = new Vector1D(-3);
v1 = v1.subtract(v2);
checkVector(v1, 4);
checkVector(v2.subtract(v1), -7);
checkVector(v2.subtract(3, v1), -15);
}
@Test
public void testAdd() {
Vector1D v1 = new Vector1D(1);
Vector1D v2 = new Vector1D(-3);
v1 = v1.add(v2);
checkVector(v1, -2);
checkVector(v2.add(v1), -5);
checkVector(v2.add(3, v1), -9);
}
@Test
public void testScalarProduct() {
Vector1D v = new Vector1D(1);
v = v.scalarMultiply(3);
checkVector(v, 3);
checkVector(v.scalarMultiply(0.5), 1.5);
}
@Test
public void testNormalize() throws MathArithmeticException {
Assert.assertEquals(1.0, new Vector1D(5).normalize().getNorm(), 1.0e-12);
try {
Vector1D.ZERO.normalize();
Assert.fail("an exception should have been thrown");
} catch (MathArithmeticException ae) {
// expected behavior
}
}
@Test
public void testNegate() {
checkVector(new Vector1D(0.1).negate(), -0.1);
}
private void checkVector(Vector1D v, double x) {
Assert.assertEquals(x, v.getX(), 1.0e-12);
}
}

View File

@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.geometry.euclidean.threed;
import org.apache.commons.math3.TestUtils;
import org.apache.commons.math3.geometry.Space;
import org.apache.commons.math3.geometry.euclidean.twod.Euclidean2D;
import org.junit.Assert;
import org.junit.Test;
public class Euclidean3DTest {
@Test
public void testDimension() {
Assert.assertEquals(3, Euclidean3D.getInstance().getDimension());
}
@Test
public void testSubSpace() {
Assert.assertTrue(Euclidean2D.getInstance() == Euclidean3D.getInstance().getSubSpace());
}
@Test
public void testSerialization() {
Space e3 = Euclidean3D.getInstance();
Space deserialized = (Space) TestUtils.serializeAndRecover(e3);
Assert.assertTrue(e3 == deserialized);
}
}

View File

@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.geometry.euclidean.twod;
import org.apache.commons.math3.TestUtils;
import org.apache.commons.math3.geometry.Space;
import org.apache.commons.math3.geometry.euclidean.oned.Euclidean1D;
import org.apache.commons.math3.geometry.euclidean.twod.Euclidean2D;
import org.junit.Assert;
import org.junit.Test;
public class Euclidean2DTest {
@Test
public void testDimension() {
Assert.assertEquals(2, Euclidean2D.getInstance().getDimension());
}
@Test
public void testSubSpace() {
Assert.assertTrue(Euclidean1D.getInstance() == Euclidean2D.getInstance().getSubSpace());
}
@Test
public void testSerialization() {
Space e2 = Euclidean2D.getInstance();
Space deserialized = (Space) TestUtils.serializeAndRecover(e2);
Assert.assertTrue(e2 == deserialized);
}
}

View File

@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.geometry.euclidean.twod;
import java.util.Locale;
public class FrenchVector2DFormatTest extends Vector2DFormatAbstractTest {
@Override
protected char getDecimalCharacter() {
return ',';
}
@Override
protected Locale getLocale() {
return Locale.FRENCH;
}
}

View File

@ -75,6 +75,19 @@ public class PolygonsSetTest {
checkVertices(set.getVertices(), vertices);
}
@Test
public void testBox() {
PolygonsSet box = new PolygonsSet(0, 2, -1, 1, 1.0e-10);
Assert.assertEquals(4.0, box.getSize(), 1.0e-10);
Assert.assertEquals(8.0, box.getBoundarySize(), 1.0e-10);
}
@Test
public void testInfinite() {
PolygonsSet box = new PolygonsSet(new BSPTree<Euclidean2D>(Boolean.TRUE), 1.0e-10);
Assert.assertTrue(Double.isInfinite(box.getSize()));
}
@Test
public void testStair() {
Vector2D[][] vertices = new Vector2D[][] {

View File

@ -0,0 +1,314 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.geometry.euclidean.twod;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Locale;
import org.apache.commons.math3.exception.MathParseException;
import org.junit.Assert;
import org.junit.Test;
public abstract class Vector2DFormatAbstractTest {
Vector2DFormat vector2DFormat = null;
Vector2DFormat vector2DFormatSquare = null;
protected abstract Locale getLocale();
protected abstract char getDecimalCharacter();
protected Vector2DFormatAbstractTest() {
vector2DFormat = Vector2DFormat.getInstance(getLocale());
final NumberFormat nf = NumberFormat.getInstance(getLocale());
nf.setMaximumFractionDigits(2);
vector2DFormatSquare = new Vector2DFormat("[", "]", " : ", nf);
}
@Test
public void testSimpleNoDecimals() {
Vector2D c = new Vector2D(1, 1);
String expected = "{1; 1}";
String actual = vector2DFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testSimpleWithDecimals() {
Vector2D c = new Vector2D(1.23, 1.43);
String expected =
"{1" + getDecimalCharacter() +
"23; 1" + getDecimalCharacter() +
"43}";
String actual = vector2DFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testSimpleWithDecimalsTrunc() {
Vector2D c = new Vector2D(1.232323232323, 1.434343434343);
String expected =
"{1" + getDecimalCharacter() +
"2323232323; 1" + getDecimalCharacter() +
"4343434343}";
String actual = vector2DFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testNegativeX() {
Vector2D c = new Vector2D(-1.232323232323, 1.43);
String expected =
"{-1" + getDecimalCharacter() +
"2323232323; 1" + getDecimalCharacter() +
"43}";
String actual = vector2DFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testNegativeY() {
Vector2D c = new Vector2D(1.23, -1.434343434343);
String expected =
"{1" + getDecimalCharacter() +
"23; -1" + getDecimalCharacter() +
"4343434343}";
String actual = vector2DFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testNegativeZ() {
Vector2D c = new Vector2D(1.23, 1.43);
String expected =
"{1" + getDecimalCharacter() +
"23; 1" + getDecimalCharacter() +
"43}";
String actual = vector2DFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testNonDefaultSetting() {
Vector2D c = new Vector2D(1, 1);
String expected = "[1 : 1]";
String actual = vector2DFormatSquare.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testDefaultFormatVector2D() {
Locale defaultLocal = Locale.getDefault();
Locale.setDefault(getLocale());
Vector2D c = new Vector2D(232.22222222222, -342.3333333333);
String expected =
"{232" + getDecimalCharacter() +
"2222222222; -342" + getDecimalCharacter() +
"3333333333}";
String actual = (new Vector2DFormat()).format(c);
Assert.assertEquals(expected, actual);
Locale.setDefault(defaultLocal);
}
@Test
public void testNan() {
Vector2D c = Vector2D.NaN;
String expected = "{(NaN); (NaN)}";
String actual = vector2DFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testPositiveInfinity() {
Vector2D c = Vector2D.POSITIVE_INFINITY;
String expected = "{(Infinity); (Infinity)}";
String actual = vector2DFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void tesNegativeInfinity() {
Vector2D c = Vector2D.NEGATIVE_INFINITY;
String expected = "{(-Infinity); (-Infinity)}";
String actual = vector2DFormat.format(c);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseSimpleNoDecimals() throws MathParseException {
String source = "{1; 1}";
Vector2D expected = new Vector2D(1, 1);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseIgnoredWhitespace() {
Vector2D expected = new Vector2D(1, 1);
ParsePosition pos1 = new ParsePosition(0);
String source1 = "{1;1}";
Assert.assertEquals(expected, vector2DFormat.parse(source1, pos1));
Assert.assertEquals(source1.length(), pos1.getIndex());
ParsePosition pos2 = new ParsePosition(0);
String source2 = " { 1 ; 1 } ";
Assert.assertEquals(expected, vector2DFormat.parse(source2, pos2));
Assert.assertEquals(source2.length() - 1, pos2.getIndex());
}
@Test
public void testParseSimpleWithDecimals() throws MathParseException {
String source =
"{1" + getDecimalCharacter() +
"23; 1" + getDecimalCharacter() +
"43}";
Vector2D expected = new Vector2D(1.23, 1.43);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseSimpleWithDecimalsTrunc() throws MathParseException {
String source =
"{1" + getDecimalCharacter() +
"2323; 1" + getDecimalCharacter() +
"4343}";
Vector2D expected = new Vector2D(1.2323, 1.4343);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseNegativeX() throws MathParseException {
String source =
"{-1" + getDecimalCharacter() +
"2323; 1" + getDecimalCharacter() +
"4343}";
Vector2D expected = new Vector2D(-1.2323, 1.4343);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseNegativeY() throws MathParseException {
String source =
"{1" + getDecimalCharacter() +
"2323; -1" + getDecimalCharacter() +
"4343}";
Vector2D expected = new Vector2D(1.2323, -1.4343);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseNegativeZ() throws MathParseException {
String source =
"{1" + getDecimalCharacter() +
"2323; 1" + getDecimalCharacter() +
"4343}";
Vector2D expected = new Vector2D(1.2323, 1.4343);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseNegativeAll() throws MathParseException {
String source =
"{-1" + getDecimalCharacter() +
"2323; -1" + getDecimalCharacter() +
"4343}";
Vector2D expected = new Vector2D(-1.2323, -1.4343);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseZeroX() throws MathParseException {
String source =
"{0" + getDecimalCharacter() +
"0; -1" + getDecimalCharacter() +
"4343}";
Vector2D expected = new Vector2D(0.0, -1.4343);
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseNonDefaultSetting() throws MathParseException {
String source =
"[1" + getDecimalCharacter() +
"2323 : 1" + getDecimalCharacter() +
"4343]";
Vector2D expected = new Vector2D(1.2323, 1.4343);
Vector2D actual = vector2DFormatSquare.parse(source);
Assert.assertEquals(expected, actual);
}
@Test
public void testParseNan() throws MathParseException {
String source = "{(NaN); (NaN)}";
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(Vector2D.NaN, actual);
}
@Test
public void testParsePositiveInfinity() throws MathParseException {
String source = "{(Infinity); (Infinity)}";
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(Vector2D.POSITIVE_INFINITY, actual);
}
@Test
public void testParseNegativeInfinity() throws MathParseException {
String source = "{(-Infinity); (-Infinity)}";
Vector2D actual = vector2DFormat.parse(source);
Assert.assertEquals(Vector2D.NEGATIVE_INFINITY, actual);
}
@Test
public void testConstructorSingleFormat() {
NumberFormat nf = NumberFormat.getInstance();
Vector2DFormat cf = new Vector2DFormat(nf);
Assert.assertNotNull(cf);
Assert.assertEquals(nf, cf.getFormat());
}
@Test
public void testForgottenPrefix() {
ParsePosition pos = new ParsePosition(0);
Assert.assertNull(new Vector2DFormat().parse("1; 1}", pos));
Assert.assertEquals(0, pos.getErrorIndex());
}
@Test
public void testForgottenSeparator() {
ParsePosition pos = new ParsePosition(0);
Assert.assertNull(new Vector2DFormat().parse("{1 1}", pos));
Assert.assertEquals(3, pos.getErrorIndex());
}
@Test
public void testForgottenSuffix() {
ParsePosition pos = new ParsePosition(0);
Assert.assertNull(new Vector2DFormat().parse("{1; 1 ", pos));
Assert.assertEquals(5, pos.getErrorIndex());
}
}

View File

@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.geometry.euclidean.twod;
import java.util.Locale;
public class Vector2DFormatTest extends Vector2DFormatAbstractTest {
@Override
protected char getDecimalCharacter() {
return '.';
}
@Override
protected Locale getLocale() {
return Locale.US;
}
}

View File

@ -0,0 +1,43 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.geometry.spherical.oned;
import org.apache.commons.math3.TestUtils;
import org.apache.commons.math3.geometry.Space;
import org.junit.Assert;
import org.junit.Test;
public class Sphere1Test {
@Test
public void testDimension() {
Assert.assertEquals(1, Sphere1D.getInstance().getDimension());
}
@Test(expected=Sphere1D.NoSubSpaceException.class)
public void testSubSpace() {
Sphere1D.getInstance().getSubSpace();
}
@Test
public void testSerialization() {
Space s1 = Sphere1D.getInstance();
Space deserialized = (Space) TestUtils.serializeAndRecover(s1);
Assert.assertTrue(s1 == deserialized);
}
}