#include #include using namespace mapbox::geometry; static void testPoint() { point p1; assert(int(p1.x) == 0); assert(int(p1.y) == 0); point p2(2, 3); point p3(4, 6); assert((p2 + p3) == point(6, 9)); assert((p2 + 1u) == point(3, 4)); assert((p3 - p2) == point(2, 3)); assert((p3 - 1u) == point(3, 5)); assert((p3 * p2) == point(8, 18)); assert((p2 * 2u) == point(4, 6)); assert((p3 / p2) == point(2, 2)); assert((p3 / 2u) == point(2, 3)); { point p(2, 3); assert((p += p3) == point(6, 9)); } { point p(2, 3); assert((p += 1u) == point(3, 4)); } { point p(4, 6); assert((p -= p2) == point(2, 3)); } { point p(4, 6); assert((p -= 1u) == point(3, 5)); } { point p(4, 6); assert((p *= p2) == point(8, 18)); } { point p(2, 3); assert((p *= 2u) == point(4, 6)); } { point p(4, 6); assert((p /= p2) == point(2, 2)); } { point p(4, 6); assert((p /= 2u) == point(2, 3)); } } static void testMultiPoint() { multi_point mp1; assert(mp1.size() == 0); multi_point mp2(10); assert(mp2.size() == 10); assert(mp1 == mp1); assert(!(mp1 != mp1)); assert(mp1 != mp2); } static void testLineString() { line_string ls1; assert(ls1.size() == 0); line_string ls2(10); assert(ls2.size() == 10); assert(ls1 == ls1); assert(!(ls1 != ls1)); assert(ls1 != ls2); } static void testMultiLineString() { multi_line_string mls1; assert(mls1.size() == 0); multi_line_string mls2(10); assert(mls2.size() == 10); assert(mls1 == mls1); assert(!(mls1 != mls1)); assert(mls1 != mls2); } static void testPolygon() { polygon pg1; assert(pg1.size() == 0); polygon pg2({{{0, 1}}}); assert(pg2.size() == 1); assert(pg2[0].size() == 1); assert(pg2[0][0] == point(0, 1)); assert(pg1 == pg1); assert(!(pg1 != pg1)); assert(pg1 != pg2); } static void testMultiPolygon() { multi_polygon mpg1; assert(mpg1.size() == 0); multi_polygon mpg2(10); assert(mpg2.size() == 10); assert(mpg1 == mpg1); assert(!(mpg1 != mpg1)); assert(mpg1 != mpg2); } static void testGeometry() { geometry pg { point() }; assert(pg.is>()); geometry lsg { line_string() }; assert(lsg.is>()); geometry pgg { polygon() }; assert(pgg.is>()); geometry mpg { multi_point() }; assert(mpg.is>()); geometry mlsg { multi_line_string() }; assert(mlsg.is>()); geometry mpgg { multi_polygon() }; assert(mpgg.is>()); geometry gcg { geometry_collection() }; assert(gcg.is>()); assert(pg == pg); assert(!(pg != pg)); assert(pg != lsg); } static void testGeometryCollection() { geometry_collection gc1; assert(gc1.size() == 0); assert(gc1 == gc1); assert(!(gc1 != gc1)); } static void testFeature() { feature pf { point() }; assert(pf.geometry.is>()); assert(pf.properties.size() == 0); auto &p = pf.properties; p["bool"] = true; p["string"] = std::string("foo"); p["double"] = 2.5; p["uint"] = uint64_t(10); p["int"] = int64_t(-10); p["null"] = null_value; assert(p["bool"].is()); assert(p["bool"] == true); assert(p["string"].is()); assert(p["string"] == std::string("foo")); assert(p["double"].is()); assert(p["double"] == 2.5); assert(p["uint"].is()); assert(p["uint"] == uint64_t(10)); assert(p["int"].is()); assert(p["int"] == int64_t(-10)); assert(p["null"].is()); assert(p["null"] == null_value); p["null"] = null_value_t{}; assert(p["null"].is()); assert(p["null"] == null_value); assert(p == p); assert(!(p != p)); assert(pf == pf); assert(!(pf != pf)); assert(p.size() == 6); feature id1 { point() }; id1.id = { uint64_t(1) }; feature id2 { point() }; id1.id = { uint64_t(2) }; assert(id1 == id1); assert(id1 != id2); } static void testFeatureCollection() { feature_collection fc1; assert(fc1.size() == 0); assert(fc1 == fc1); assert(!(fc1 != fc1)); } struct point_counter { std::size_t count = 0; template void operator()(Point const&) { count++; }; }; static void testForEachPoint() { auto count_points = [] (auto const& g) { point_counter counter; for_each_point(g, counter); return counter.count; }; assert(count_points(point()) == 1); assert(count_points(line_string({{0, 1}, {2, 3}})) == 2); assert(count_points(geometry(polygon({{{0, 1}, {2, 3}}}))) == 2); auto point_negator = [] (point& p) { p *= -1.0; }; point p(1, 2); for_each_point(p, point_negator); assert(p == point(-1, -2)); line_string ls({{0, 1}, {2, 3}}); for_each_point(ls, point_negator); assert(ls == line_string({{0, -1}, {-2, -3}})); geometry g(polygon({{{0, 1}, {2, 3}}})); for_each_point(g, point_negator); assert(g == geometry(polygon({{{0, -1}, {-2, -3}}}))); // Custom geometry type using my_geometry = mapbox::util::variant>; assert(count_points(my_geometry(point())) == 1); // Custom point type struct my_point { int16_t x; int16_t y; }; assert(count_points(std::vector({my_point{0, 1}})) == 1); assert(count_points(mapbox::util::variant(my_point{0, 1})) == 1); } static void testEnvelope() { assert(envelope(point(0, 0)) == box({0, 0}, {0, 0})); assert(envelope(line_string({{0, 1}, {2, 3}})) == box({0, 1}, {2, 3})); assert(envelope(polygon({{{0, 1}, {2, 3}}})) == box({0, 1}, {2, 3})); assert(envelope(multi_point({{0, 0}})) == box({0, 0}, {0, 0})); assert(envelope(multi_line_string({{{0, 1}, {2, 3}}})) == box({0, 1}, {2, 3})); assert(envelope(multi_polygon({{{{0, 1}, {2, 3}}}})) == box({0, 1}, {2, 3})); assert(envelope(geometry(point(0, 0))) == box({0, 0}, {0, 0})); assert(envelope(geometry_collection({point(0, 0)})) == box({0, 0}, {0, 0})); } int main() { testPoint(); testMultiPoint(); testLineString(); testMultiLineString(); testPolygon(); testMultiPolygon(); testGeometry(); testGeometryCollection(); testFeature(); testFeatureCollection(); testForEachPoint(); testEnvelope(); return 0; }