|
| 1 | +#include <gtest/gtest.h> |
| 2 | +#include <vector> |
| 3 | + |
| 4 | +#include "PolyUtil.hpp" |
| 5 | + |
| 6 | + |
| 7 | +TEST(PolyUtil, containsLocation) { |
| 8 | + // Empty. |
| 9 | + std::vector<LatLng> empty; |
| 10 | + EXPECT_FALSE(PolyUtil::containsLocation(LatLng(0, 0), empty, true)); |
| 11 | + EXPECT_FALSE(PolyUtil::containsLocation(LatLng(0, 0), empty, false)); |
| 12 | + |
| 13 | + |
| 14 | + // One point. |
| 15 | + std::vector<LatLng> one = { {1, 2} }; |
| 16 | + EXPECT_TRUE(PolyUtil::containsLocation(LatLng(1, 2), one, true)); |
| 17 | + EXPECT_TRUE(PolyUtil::containsLocation(LatLng(1, 2), one, false)); |
| 18 | + |
| 19 | + EXPECT_FALSE(PolyUtil::containsLocation(LatLng(0, 0), one, true)); |
| 20 | + EXPECT_FALSE(PolyUtil::containsLocation(LatLng(0, 0), one, false)); |
| 21 | + |
| 22 | + |
| 23 | + // Two points. |
| 24 | + std::vector<LatLng> two = { {1, 2}, {3, 5} }; |
| 25 | + for (const auto & point : { LatLng(1, 2), LatLng(3, 5) }) { |
| 26 | + EXPECT_TRUE(PolyUtil::containsLocation(point, two, true)); |
| 27 | + EXPECT_TRUE(PolyUtil::containsLocation(point, two, false)); |
| 28 | + } |
| 29 | + for (const auto & point : { LatLng(0, 0), LatLng(40, 4) }) { |
| 30 | + EXPECT_FALSE(PolyUtil::containsLocation(point, two, true)); |
| 31 | + EXPECT_FALSE(PolyUtil::containsLocation(point, two, false)); |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + // Some arbitrary triangle. |
| 36 | + std::vector<LatLng> triangle = { {0, 0}, {10, 12}, {20, 5} }; |
| 37 | + for (const auto & point : { LatLng(10, 12), LatLng(10, 11), LatLng(19, 5) }) { |
| 38 | + EXPECT_TRUE(PolyUtil::containsLocation(point, triangle, true)); |
| 39 | + EXPECT_TRUE(PolyUtil::containsLocation(point, triangle, false)); |
| 40 | + } |
| 41 | + for (const auto & point : { LatLng(0, 1), LatLng(11, 12), LatLng(30, 5), LatLng(0, -180), LatLng(0, 90) }) { |
| 42 | + EXPECT_FALSE(PolyUtil::containsLocation(point, triangle, true)); |
| 43 | + EXPECT_FALSE(PolyUtil::containsLocation(point, triangle, false)); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + // Around North Pole. |
| 48 | + std::vector<LatLng> northPole = { {89, 0}, {89, 120}, {89, -120} }; |
| 49 | + for (const auto & point : { LatLng(90, 0), /* LatLng(90, 180), */ LatLng(90, -90) }) { |
| 50 | + EXPECT_TRUE(PolyUtil::containsLocation(point, northPole, true)); |
| 51 | + EXPECT_TRUE(PolyUtil::containsLocation(point, northPole, false)); |
| 52 | + } |
| 53 | + for (const auto & point : { LatLng(-90, 0), LatLng(0, 0) }) { |
| 54 | + EXPECT_FALSE(PolyUtil::containsLocation(point, northPole, true)); |
| 55 | + EXPECT_FALSE(PolyUtil::containsLocation(point, northPole, false)); |
| 56 | + } |
| 57 | + |
| 58 | + // Around South Pole. |
| 59 | + std::vector<LatLng> southPole = { {-89, 0}, {-89, 120}, {-89, -120} }; |
| 60 | + for (const auto & point : { LatLng(90, 0), /* LatLng(90, 180), */ LatLng(90, -90), LatLng(0, 0) }) { |
| 61 | + EXPECT_TRUE(PolyUtil::containsLocation(point, southPole, true)); |
| 62 | + EXPECT_TRUE(PolyUtil::containsLocation(point, southPole, false)); |
| 63 | + } |
| 64 | + for (const auto & point : { LatLng(-90, 0), LatLng(-90, 90) }) { |
| 65 | + EXPECT_FALSE(PolyUtil::containsLocation(point, southPole, true)); |
| 66 | + EXPECT_FALSE(PolyUtil::containsLocation(point, southPole, false)); |
| 67 | + } |
| 68 | + |
| 69 | + // Over/under segment on meridian and equator. |
| 70 | + std::vector<LatLng> poly = { {5, 10}, {10, 10}, {0, 20}, {0, -10} }; |
| 71 | + for (const auto & point : { LatLng(2.5, 10), LatLng(1, 0) }) { |
| 72 | + EXPECT_TRUE(PolyUtil::containsLocation(point, poly, true)); |
| 73 | + EXPECT_TRUE(PolyUtil::containsLocation(point, poly, false)); |
| 74 | + } |
| 75 | + for (const auto & point : { LatLng(15, 10), LatLng(0, -15), LatLng(0, 25), LatLng(-1, 0) }) { |
| 76 | + EXPECT_FALSE(PolyUtil::containsLocation(point, poly, true)); |
| 77 | + EXPECT_FALSE(PolyUtil::containsLocation(point, poly, false)); |
| 78 | + } |
| 79 | +} |
0 commit comments