Files
openlayers/tests/Geometry/test_Point.html

174 lines
5.8 KiB
HTML

<html>
<head>
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript"><!--
var point;
function test_01_Point_constructor (t) {
t.plan( 8 );
//empty
point = new OpenLayers.Geometry.Point();
t.ok( point instanceof OpenLayers.Geometry.Point, "new OpenLayers.Geometry.Point returns point object" );
t.eq( point.CLASS_NAME, "OpenLayers.Geometry.Point", "point.CLASS_NAME is set correctly");
//valid
var x = 10;
var y = 20;
point = new OpenLayers.Geometry.Point(x, y);
t.ok( point instanceof OpenLayers.Geometry.Point, "new OpenLayers.Geometry.Point returns point object" );
t.eq( point.CLASS_NAME, "OpenLayers.Geometry.Point", "point.CLASS_NAME is set correctly");
t.eq( point.x, x, "point.x is set correctly");
t.eq( point.y, y, "point.y is set correctly");
t.eq( point.lon, null, "point.lon is not set");
t.eq( point.lat, null, "point.lat is not set");
}
function test_03_Point_calculateBounds (t) {
t.plan(4);
var x = 10;
var y = 20;
point = new OpenLayers.Geometry.Point(x, y);
point.calculateBounds();
t.eq( point.bounds.left, x, "bounds.left is 10" );
t.eq( point.bounds.right, x, "bounds.right is 10" );
t.eq( point.bounds.top, y, "bounds.top is 20" );
t.eq( point.bounds.bottom, y, "bounds.bottom is 20" );
}
function test_04_Point_distanceTo(t) {
t.plan(2);
var x1 = 10;
var y1 = 20;
point1 = new OpenLayers.Geometry.Point(x1, y1);
var x2 = 100;
var y2 = 200;
point2 = new OpenLayers.Geometry.Point(x2, y2);
var dist = point1.distanceTo(point2)
t.eq( dist, 201.24611797498107267682563018581, "distances calculating correctly");
t.eq( dist, Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)), "distance calculation correct");
}
function test_05_Point_toString(t) {
t.plan(1);
var x = 10;
var y = 20;
point = new OpenLayers.Geometry.Point(x, y);
t.eq(point.toString(), "POINT(" + x + " " + y + ")",
"toString() returns WKT" );
}
function test_06_Point_move(t) {
t.plan(3);
var x = 10;
var y = 20;
point = new OpenLayers.Geometry.Point(x, y);
var dx = 10 * Math.random();
var dy = 10 * Math.random();
point.bounds = "foo";
point.move(dx, dy);
t.eq(point.x, x + dx, "move() correctly modifies x");
t.eq(point.y, y + dy, "move() correctly modifies y");
t.ok(point.bounds == null, "bounds is cleared after a move()");
}
function test_Point_rotate(t) {
t.plan(5);
var tolerance = 1e-10;
var x = 10;
var y = 20;
var point = new OpenLayers.Geometry.Point(x, y);
var origin = new OpenLayers.Geometry.Point(5, 10);
// rotate a full revolution
point.bounds = "foo";
point.rotate(2 * Math.PI, origin);
t.ok(((point.x - x) / x) < tolerance,
"rotate by 2 * Math.PI returns to the same y");
t.ok(((point.y - y) / y) < tolerance,
"rotate by 2 * Math.PI returns to the same y");
t.ok(point.bounds == null, "bounds is cleared after a rotate()");
// rotate an 1/8 turn
point.rotate(Math.PI / 4, origin);
t.ok(((point.x - 1.4644660940672636) / 1.4644660940672636) < tolerance,
"rotate 1/8 turn correctly");
t.ok(((point.y - 20.606601717798213) / 20.606601717798213) < tolerance,
"rotate 1/8 turn correctly");
}
function test_Point_resize(t) {
t.plan(3);
var tolerance = 1e-10;
var x = 100 * Math.random();
var y = 100 * Math.random();
var point = new OpenLayers.Geometry.Point(x, y);
point.bounds = "foo";
var i = 100 * Math.random();
var j = 100 * Math.random();
var origin = new OpenLayers.Geometry.Point(i, j);
var scale = 10 * Math.random();
var oldDistance = origin.distanceTo(point);
point.resize(scale, origin);
var newDistance = origin.distanceTo(point);
t.ok((origin.x == i) && (origin.y == j),
"resize leaves the origin untouched");
t.ok((((newDistance / oldDistance) - scale) / scale) < tolerance,
"resize moves points the correct distance from the origin");
t.ok(point.bounds == null, "bounds is correctly cleared after a resize()");
}
function test_Point_equals(t) {
t.plan(3);
var x = Math.random() * 100;
var y = Math.random() * 100;
var geometry = new OpenLayers.Geometry.Point(x, y);
var equal = new OpenLayers.Geometry.Point(x, y);
var offX = new OpenLayers.Geometry.Point(x + 1, y);
var offY = new OpenLayers.Geometry.Point(x, y + 1);
t.ok(geometry.equals(equal),
"equals() returns true for a geometry with equivalent coordinates");
t.ok(!geometry.equals(offX),
"equals() returns false for a geometry with offset x");
t.ok(!geometry.equals(offY),
"equals() returns false for a geometry with offset y");
}
function test_Point_clone(t) {
t.plan(2);
var x = Math.random() * 100;
var y = Math.random() * 100;
var geometry = new OpenLayers.Geometry.Point(x, y);
var clone = geometry.clone();
t.ok(clone instanceof OpenLayers.Geometry.Point,
"clone() creates an OpenLayers.Geometry.Point");
t.ok(geometry.equals(clone), "clone has equivalent coordinates");
}
// -->
</script>
</head>
<body>
</body>
</html>