Skip to content

Commit

Permalink
Fix PackedCoordinateSequence.Float constructor (#379)
Browse files Browse the repository at this point in the history
Reformat unit tests.

Signed-off-by: Martin Davis <[email protected]>
  • Loading branch information
dr-jts authored Feb 21, 2019
1 parent 8eea6fd commit 16ddbed
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,13 @@ public Double(Coordinate[] coordinates, int dimension, int measures) {

coords = new double[coordinates.length * this.dimension];
for (int i = 0; i < coordinates.length; i++) {
coords[i * this.dimension] = coordinates[i].x;
if (this.dimension >= 2)
coords[i * this.dimension + 1] = coordinates[i].y;
if (this.dimension >= 3)
coords[i * this.dimension + 2] = coordinates[i].getOrdinate(2); // Z or M
if (this.dimension >= 4)
coords[i * this.dimension + 3] = coordinates[i].getOrdinate(3); // M
int offset = i * dimension;
coords[offset] = coordinates[i].x;
coords[offset + 1] = coordinates[i].y;
if (dimension >= 3)
coords[offset + 2] = coordinates[i].getOrdinate(2); // Z or M
if (dimension >= 4)
coords[offset + 3] = coordinates[i].getOrdinate(3); // M
}
}
/**
Expand Down Expand Up @@ -463,13 +463,15 @@ public Float(Coordinate[] coordinates, int dimension, int measures) {
if (coordinates == null)
coordinates = new Coordinate[0];

coords = new float[coordinates.length * this.dimension];
coords = new float[coordinates.length * dimension];
for (int i = 0; i < coordinates.length; i++) {
coords[i * this.dimension] = (float) coordinates[i].x;
if (this.dimension >= 2)
coords[i * this.dimension + 1] = (float) coordinates[i].y;
if (this.dimension >= 3)
coords[i * this.dimension + 2] = (float) coordinates[i].getZ();
int offset = i * dimension;
coords[offset] = (float) coordinates[i].x;
coords[offset + 1] = (float) coordinates[i].y;
if (dimension >= 3)
coords[offset + 2] = (float) coordinates[i].getOrdinate(2); // Z or M
if (dimension >= 4)
coords[offset + 3] = (float) coordinates[i].getOrdinate(3); // M
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2016 Vivid Solutions.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
*
* http://www.eclipse.org/org/documents/edl-v10.php.
*/

package org.locationtech.jts.geom.impl;

import org.locationtech.jts.geom.CoordinateSequenceFactory;

import junit.textui.TestRunner;

/**
* Test {@link PackedCoordinateSequence.Double}
* using the {@link CoordinateSequenceTestBase}
* @version 1.7
*/
public class PackedCoordinateSequenceDoubleTest
extends CoordinateSequenceTestBase
{
public static void main(String args[]) {
TestRunner.run(PackedCoordinateSequenceDoubleTest.class);
}

public PackedCoordinateSequenceDoubleTest(String name)
{
super(name);
}

@Override
CoordinateSequenceFactory getCSFactory() {
return PackedCoordinateSequenceFactory.DOUBLE_FACTORY;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2016 Vivid Solutions.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
*
* http://www.eclipse.org/org/documents/edl-v10.php.
*/

package org.locationtech.jts.geom.impl;

import org.locationtech.jts.geom.CoordinateSequenceFactory;

import junit.textui.TestRunner;

/**
* Test {@link PackedCoordinateSequence.Float}
* using the {@link CoordinateSequenceTestBase}
*
* @version 1.7
*/
public class PackedCoordinateSequenceFloatTest
extends CoordinateSequenceTestBase
{
public static void main(String args[]) {
TestRunner.run(PackedCoordinateSequenceFloatTest.class);
}

public PackedCoordinateSequenceFloatTest(String name)
{
super(name);
}

@Override
CoordinateSequenceFactory getCSFactory() {
return PackedCoordinateSequenceFactory.FLOAT_FACTORY;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,92 +42,136 @@ CoordinateSequenceFactory getCSFactory() {
return new PackedCoordinateSequenceFactory();
}

public void testDimensionAndMeasure()
public void testDouble() {
checkAll( PackedCoordinateSequenceFactory.DOUBLE_FACTORY );
}

public void testFloat() {
checkAll( PackedCoordinateSequenceFactory.FLOAT_FACTORY) ;
}

public void checkAll(CoordinateSequenceFactory factory)
{
checkDim2(factory);
checkDim3(factory);
checkDim3_M1(factory);
checkDim4_M1(factory);
checkDimInvalid(factory);
}

public void checkDim2(CoordinateSequenceFactory factory)
{
CoordinateSequenceFactory factory = getCSFactory();
CoordinateSequence seq = factory.create(5, 2);
CoordinateSequence copy;
Coordinate coord;
Coordinate[] array;

initProgression(seq);
assertEquals("xz", 2, seq.getDimension());
assertTrue("Z", !seq.hasZ());
assertTrue("M", !seq.hasM());
coord = seq.getCoordinate(4);

assertEquals("Dimension should be 2", 2, seq.getDimension());
assertTrue("Z should not be present", !seq.hasZ());
assertTrue("M should not be present", !seq.hasM());

Coordinate coord = seq.getCoordinate(4);
assertTrue( coord instanceof CoordinateXY);
assertEquals( 4.0, coord.getX());
assertEquals( 4.0, coord.getY());
array = seq.toCoordinateArray();

Coordinate[] array = seq.toCoordinateArray();
assertEquals(coord, array[4]);
assertTrue(coord != array[4]);
assertTrue(isEqual(seq,array));
copy = factory.create(array);
assertTrue(isEqual(copy,array));
copy = factory.create(seq);

CoordinateSequence copy = factory.create(array);
assertTrue(isEqual(copy,array));

seq = factory.create(5, 3);
CoordinateSequence copy2 = factory.create(seq);
assertTrue(isEqual(copy2,array));
}

public void checkDim3(CoordinateSequenceFactory factory)
{
CoordinateSequence seq = factory.create(5, 3);
initProgression(seq);
assertEquals("xyz", 3, seq.getDimension());
assertTrue("Z", seq.hasZ());
assertTrue("M", !seq.hasM());
coord = seq.getCoordinate(4);

assertEquals("Dimension should be 3", 3, seq.getDimension());
assertTrue("Z should be present", seq.hasZ());
assertTrue("M should not be present", !seq.hasM());

Coordinate coord = seq.getCoordinate(4);
assertTrue( coord.getClass() == Coordinate.class);
assertEquals( 4.0, coord.getX());
assertEquals( 4.0, coord.getY());
assertEquals( 4.0, coord.getZ());
array = seq.toCoordinateArray();

Coordinate[] array = seq.toCoordinateArray();
assertEquals(coord, array[4]);
assertTrue(coord != array[4]);
assertTrue(isEqual(seq,array));
copy = factory.create(array);
assertTrue(isEqual(copy,array));
copy = factory.create(seq);
assertTrue(isEqual(copy,array));
assertTrue(isEqual(seq, array));

seq = factory.create(5, 3, 1);
initProgression(seq);
assertEquals("xym", 3, seq.getDimension());
assertTrue("Z", !seq.hasZ());
assertTrue("M", seq.hasM());
coord = seq.getCoordinate(4);
CoordinateSequence copy = factory.create(array);
assertTrue(isEqual(copy, array));

CoordinateSequence copy2 = factory.create(seq);
assertTrue(isEqual(copy2, array));
}

public void checkDim3_M1(CoordinateSequenceFactory factory)
{
CoordinateSequence seq = factory.create(5, 3, 1);
initProgression(seq);

assertEquals("Dimension should be 3", 3, seq.getDimension());
assertTrue("Z should not be present", !seq.hasZ());
assertTrue("M should be present", seq.hasM());

Coordinate coord = seq.getCoordinate(4);
assertTrue( coord instanceof CoordinateXYM);
assertEquals( 4.0, coord.getX());
assertEquals( 4.0, coord.getY());
assertEquals( 4.0, coord.getM());
array = seq.toCoordinateArray();

Coordinate[] array = seq.toCoordinateArray();
assertEquals(coord, array[4]);
assertTrue(coord != array[4]);
assertTrue(isEqual(seq,array));
copy = factory.create(array);
assertTrue(isEqual(copy,array));
copy = factory.create(seq);

CoordinateSequence copy = factory.create(array);
assertTrue(isEqual(copy,array));

seq = factory.create(5, 4, 1);
CoordinateSequence copy2 = factory.create(seq);
assertTrue(isEqual(copy2,array));
}

public void checkDim4_M1(CoordinateSequenceFactory factory)
{
CoordinateSequence seq = factory.create(5, 4, 1);
initProgression(seq);
assertEquals("xyzm", 4, seq.getDimension());
assertTrue("Z", seq.hasZ());
assertTrue("M", seq.hasM());
coord = seq.getCoordinate(4);

assertEquals("Dimension should be 4", 4, seq.getDimension());
assertTrue("Z should be present", seq.hasZ());
assertTrue("M should be present", seq.hasM());

Coordinate coord = seq.getCoordinate(4);
assertTrue( coord instanceof CoordinateXYZM);
assertEquals( 4.0, coord.getX());
assertEquals( 4.0, coord.getY());
assertEquals( 4.0, coord.getZ());
assertEquals( 4.0, coord.getM());
array = seq.toCoordinateArray();

Coordinate[] array = seq.toCoordinateArray();
assertEquals(coord, array[4]);
assertTrue(coord != array[4]);
assertTrue(isEqual(seq,array));
copy = factory.create(array);
assertTrue(isEqual(copy,array));
copy = factory.create(seq);

CoordinateSequence copy = factory.create(array);
assertTrue(isEqual(copy,array));

CoordinateSequence copy2 = factory.create(seq);
assertTrue(isEqual(copy2, array));
}

public void checkDimInvalid(CoordinateSequenceFactory factory)
{
try {
seq = factory.create(5, 2, 1);
fail("xm not supported");
CoordinateSequence seq = factory.create(5, 2, 1);
fail("Dimension=2/Measure=1 (XM) not supported");
} catch (IllegalArgumentException expected) {
}
}
Expand Down

0 comments on commit 16ddbed

Please sign in to comment.