Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port midpoint tests from turf.js #4

Merged
merged 1 commit into from
Nov 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 147 additions & 7 deletions test/components/midpoint_test.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import 'package:test/test.dart';
import 'package:turf/distance.dart';
import 'package:turf/helpers.dart';
import 'package:turf/midpoint.dart';

void checkLatLngInRange(Point result) {
_lngRange(num lng) => lng >= -180 && lng <= 180;
_latRange(num lat) => lat >= -90 && lat <= 90;

expect(_lngRange(result.coordinates.lng), true,
reason: 'Longitude of ${result.coordinates.lng} out of range');
expect(_latRange(result.coordinates.lat), true,
reason: 'Latitude of ${result.coordinates.lat} out of range');
}

main() {
test('midpoint', () {
_lngRange(num lng) => lng >= -180 && lng <= 180;
_latRange(num lat) => lat >= -90 && lat <= 90;
var result = midpoint(
test('simple midpoint', () {
Point result = midpoint(
Point(
coordinates: Position.named(
lat: -33.4312226,
Expand All @@ -20,8 +29,139 @@ main() {
),
),
);
print(result.coordinates.join(', '));
expect(_lngRange(result.coordinates.lng), true);
expect(_latRange(result.coordinates.lat), true);
checkLatLngInRange(result);
});

test('midpoint -- horizontal equator', () {
Point pt1 = Point(
coordinates: Position.named(
lng: 0,
lat: 0,
),
);
Point pt2 = Point(
coordinates: Position.named(
lng: 10,
lat: 0,
),
);
Point result = midpoint(pt1, pt2);

checkLatLngInRange(result);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test is actually missing the distance check that the other tests have, which also perhaps could be pulled out into a separate function, like checkLatLngInRange. I'll keep the fix on hold in case there is other feedback that should be addressed in another patch.

});

test('midpoint -- vertical from equator', () {
Point pt1 = Point(
coordinates: Position.named(
lng: 0,
lat: 0,
),
);
Point pt2 = Point(
coordinates: Position.named(
lng: 0,
lat: 10,
),
);

Point result = midpoint(pt1, pt2);

checkLatLngInRange(result);
expect(
distance(pt1, result).toStringAsFixed(6) ==
distance(pt2, result).toStringAsFixed(6),
true);
});

test('midpoint -- vertical to equator', () {
Point pt1 = Point(
coordinates: Position.named(
lng: 0,
lat: 10,
),
);
Point pt2 = Point(
coordinates: Position.named(
lng: 0,
lat: 0,
),
);

Point result = midpoint(pt1, pt2);

checkLatLngInRange(result);
expect(
distance(pt1, result).toStringAsFixed(6) ==
distance(pt2, result).toStringAsFixed(6),
true);
});

test('midpoint -- diagonal back over equator', () {
Point pt1 = Point(
coordinates: Position.named(
lng: -1,
lat: 10,
),
);
Point pt2 = Point(
coordinates: Position.named(
lng: 1,
lat: -1,
),
);

Point result = midpoint(pt1, pt2);

checkLatLngInRange(result);
expect(
distance(pt1, result).toStringAsFixed(6) ==
distance(pt2, result).toStringAsFixed(6),
true);
});

test('midpoint -- diagonal forward over equator', () {
Point pt1 = Point(
coordinates: Position.named(
lng: -5,
lat: -1,
),
);
Point pt2 = Point(
coordinates: Position.named(
lng: 5,
lat: 10,
),
);

Point result = midpoint(pt1, pt2);

checkLatLngInRange(result);
expect(
distance(pt1, result).toStringAsFixed(6) ==
distance(pt2, result).toStringAsFixed(6),
true);
});

test('midpoint -- long distance', () {
Point pt1 = Point(
coordinates: Position.named(
lng: 22.5,
lat: 21.94304553343818,
),
);
Point pt2 = Point(
coordinates: Position.named(
lng: 92.10937499999999,
lat: 46.800059446787316,
),
);

Point result = midpoint(pt1, pt2);

checkLatLngInRange(result);
expect(
distance(pt1, result).toStringAsFixed(6) ==
distance(pt2, result).toStringAsFixed(6),
true);
});
}