From 4dcb9fc66649abb63c3669c19862c3c3a7e10eec Mon Sep 17 00:00:00 2001 From: Asheem Mamoowala Date: Fri, 29 Jun 2018 18:15:46 -0700 Subject: Add FuzzTest for TileCover using @mapnik/geometry-test-data repo --- test/util/tile_cover.test.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/test/util/tile_cover.test.cpp b/test/util/tile_cover.test.cpp index 72d77450a4..9071a11536 100644 --- a/test/util/tile_cover.test.cpp +++ b/test/util/tile_cover.test.cpp @@ -1,10 +1,13 @@ #include #include #include +#include #include #include /* srand, rand */ #include /* time */ +#include +#include #include using namespace mbgl; @@ -370,3 +373,81 @@ TEST(TileCover, DISABLED_FuzzLine) { }; } } + +mapbox::geometry::geometry parseGeometry(const std::string& json) { + using namespace mapbox::geojson; + auto geojson = parse(json); + return geojson.match( + [](const geometry& geom) { + return geom; + }, + [](const feature& feature) { + return feature.geometry; + }, + [](const feature_collection& featureCollection) { + if (featureCollection.size() < 1) { + throw std::runtime_error("No features in feature collection"); + } + geometry_collection geometries; + + for (auto feature : featureCollection) { + geometries.push_back(feature.geometry); + } + + return geometries; + }); +} + + +#define DATA_INPUT_POLYJSON +#ifndef DATA_INPUT_POLYJSON +#define DATA_INPUT_FOLDER "test/geometry-test-data/input/" +#else +#define DATA_INPUT_FOLDER "test/geometry-test-data/input-polyjson/" +#endif + +//Following tests require cloning @mapnik/geometry-test-data into test. +class TileCoverFuzzTest : public ::testing::TestWithParam {}; +INSTANTIATE_TEST_CASE_P(TileCover, TileCoverFuzzTest, ::testing::ValuesIn([] { + std::vector names; + const std::string ending = ".json"; + static const std::string test_data_directory = DATA_INPUT_FOLDER; + DIR *dir = opendir(test_data_directory.c_str()); + if (dir != nullptr) { + for (dirent *dp = nullptr; (dp = readdir(dir)) != nullptr;) { + const std::string name = dp->d_name; + if (name.length() >= ending.length() && name.compare(name.length() - ending.length(), ending.length(), ending) == 0) { + names.push_back(name); + } + } + closedir(dir); + } + return names; +}())); + +TEST_P(TileCoverFuzzTest, GeometryTestData) { + static const std::string test_data_directory = DATA_INPUT_FOLDER; + auto readFile = [](const std::string& fileName) -> std::string { + std::ifstream stream(fileName.c_str()); + if (!stream.good()) { + throw std::runtime_error("Cannot read file: " + fileName); + } + + std::stringstream buffer; + buffer << stream.rdbuf(); + stream.close(); + + return buffer.str(); + }; + + std::string json = readFile(test_data_directory + GetParam()); +#ifdef DATA_INPUT_POLYJSON + // The input-polyjson folder in geometry-test-data contains only point arrays + // without the GeoJSON structure. + json = R"({"type": "Polygon", "coordinates": )" + json + R"(})"; +#endif + auto geometry = parseGeometry(json); + + util::TileCover tc(geometry, 10, false); + while (tc.next()){}; +} -- cgit v1.2.1