add test with catch2 lib
diff --git a/CMakeLists.txt b/CMakeLists.txt index 14f9b8d..c065656 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -5,13 +5,30 @@ execute_process(COMMAND bash "-c" "(cd ${CMAKE_CURRENT_SOURCE_DIR} && ./fetch-includes.sh)") endif() -add_executable(triangulate src/triangulate.cpp) +#delaunator add_library(delaunator src/delaunator.cpp) -target_include_directories (triangulate PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/rapidjson/include") -target_include_directories (triangulate PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/prettyprint") target_include_directories (delaunator PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/rapidjson/include") target_include_directories (delaunator PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/prettyprint") + +#delaunator +add_library(json-helpers src/json-helpers.cpp) +target_include_directories (json-helpers PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/rapidjson/include") + +#delaunator-test +add_executable(delaunator-test src/delaunator-test.cpp) +target_link_libraries(delaunator-test delaunator) +target_include_directories (delaunator-test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/catch/single_include/catch2") +target_link_libraries(delaunator-test json-helpers) +target_link_libraries(delaunator-test delaunator) + + +#triangulate +add_executable(triangulate src/triangulate.cpp) +target_include_directories (triangulate PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/rapidjson/include") +target_include_directories (triangulate PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/prettyprint") target_link_libraries(triangulate delaunator) +target_link_libraries(triangulate json-helpers) + set(CPACK_PROJECT_NAME ${PROJECT_NAME}) set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
diff --git a/fetch-includes.sh b/fetch-includes.sh index 5fd564c..8aab5bd 100755 --- a/fetch-includes.sh +++ b/fetch-includes.sh
@@ -5,3 +5,4 @@ git clone --branch v1.1.0 --depth=1 git@github.com:Tencent/rapidjson.git ./includes/rapidjson git clone --branch master --depth=1 git@github.com:louisdx/cxx-prettyprint.git ./includes/prettyprint +git clone --branch master --depth=1 git@github.com:catchorg/Catch2.git ./includes/catch
diff --git a/src/delaunator-test.cpp b/src/delaunator-test.cpp new file mode 100644 index 0000000..fde929e --- /dev/null +++ b/src/delaunator-test.cpp
@@ -0,0 +1,28 @@ +#define CATCH_CONFIG_MAIN +#include "delaunator.h" +#include "catch.hpp" +#include "json-helpers.h" +#include <string> +#include <vector> + +using namespace std; + +TEST_CASE("triangles match JS version ouput", "[Delaunator]") { + string points_str = json_helpers::read_file("./test-files/playgrounds-1356-epsg-3857.geojson"); + string triangles_str = json_helpers::read_file("./test-files/playgrounds-1356-triangles.json"); + const vector<double> coords = json_helpers::get_geo_json_points(points_str); + const vector<double> triangles = json_helpers::get_array_points(triangles_str); + Delaunator delaunator(move(coords)); + + SECTION("length of triangles is the same") { + REQUIRE(delaunator.triangles.size() == triangles.size()); + } + + SECTION("values are the same") { + for(size_t i = 0; i < triangles.size(); i++) + { + REQUIRE(delaunator.triangles[i] == triangles[i]); + } + } +} +
diff --git a/src/json-helpers.cpp b/src/json-helpers.cpp new file mode 100644 index 0000000..8844f61 --- /dev/null +++ b/src/json-helpers.cpp
@@ -0,0 +1,56 @@ + +#include "json-helpers.h" +#include <fstream> +#include <exception> +#include "rapidjson/document.h" + +using namespace std; + +string json_helpers::read_file(const char* filename) { + ifstream input_file(filename); + if(input_file.good()) { + string json_str( + (istreambuf_iterator<char>(input_file)), + istreambuf_iterator<char>() + ); + return json_str; + } else { + printf("Error reading file %s", filename); + throw runtime_error("Error reading file"); + } +} + +vector<double> json_helpers::get_geo_json_points(const string& json) { + rapidjson::Document document; + if(document.Parse(json.c_str()).HasParseError()) { + throw runtime_error("Cannot parse JSON"); + } + const rapidjson::Value& features = document["features"]; + vector<double> coords; + // vector<double> y_vector; + for(rapidjson::SizeType i = 0; i < features.Size(); i++) { + const rapidjson::Value& coordinates = features[i]["geometry"]["coordinates"]; + const double x = coordinates[0].GetDouble(); + const double y = coordinates[1].GetDouble(); + coords.push_back(x); + coords.push_back(y); + } + return coords; +} + +vector<double> json_helpers::get_array_points(const string& json) { + vector<double> points; + rapidjson::Document document; + if(document.Parse(json.c_str()).HasParseError()) { + throw runtime_error("Cannot parse JSON"); + } + if(!document.IsArray()) { + throw runtime_error("It's not JSON Array"); + } + points.reserve(static_cast<int>(document.Size())); + for(rapidjson::SizeType i = 0; i < document.Size(); i++) { + points.push_back(document[i].GetDouble()); + } + return points; + +}
diff --git a/src/json-helpers.h b/src/json-helpers.h new file mode 100644 index 0000000..e48f3c9 --- /dev/null +++ b/src/json-helpers.h
@@ -0,0 +1,10 @@ +#pragma once + +#include <string> +#include <vector> + +namespace json_helpers { + std::string read_file(const char* filename); + std::vector<double> get_geo_json_points(const std::string& json); + std::vector<double> get_array_points(const std::string& json); +}
diff --git a/src/triangulate.cpp b/src/triangulate.cpp index fa6d575..abe1fde 100644 --- a/src/triangulate.cpp +++ b/src/triangulate.cpp
@@ -1,10 +1,9 @@ #include "rapidjson/document.h" #include "rapidjson/prettywriter.h" #include "delaunator.h" +#include "json-helpers.h" #include <cstdio> #include <fstream> -#include <string> -#include <exception> #include <vector> #include <initializer_list> // #include "prettyprint.hpp" @@ -12,38 +11,6 @@ using namespace std; namespace { - string read_file(const char* filename) { - ifstream input_file(filename); - if(input_file.good()) { - string json_str( - (istreambuf_iterator<char>(input_file)), - istreambuf_iterator<char>() - ); - return json_str; - } else { - printf("Error reading file %s", filename); - throw exception(); - } - } - - vector<double> get_geo_json_points(const string& json) { - rapidjson::Document document; - if(document.Parse(json.c_str()).HasParseError()) { - fprintf(stderr, "Cannot parse JSON"); - throw exception(); - } - const rapidjson::Value& features = document["features"]; - vector<double> coords; - // vector<double> y_vector; - for(rapidjson::SizeType i = 0; i < features.Size(); i++) { - const rapidjson::Value& coordinates = features[i]["geometry"]["coordinates"]; - const double x = coordinates[0].GetDouble(); - const double y = coordinates[1].GetDouble(); - coords.push_back(x); - coords.push_back(y); - } - return coords; - } const string serialize_to_json(Delaunator &delaunator) { rapidjson::StringBuffer sb; @@ -101,8 +68,8 @@ int main(int, char* argv[]) { const char* filename = argv[1]; const char* output = argv[2]; - string json = read_file(filename); - const vector<double> coords = get_geo_json_points(json); + string json = json_helpers::read_file(filename); + const vector<double> coords = json_helpers::get_geo_json_points(json); Delaunator delaunator(move(coords)); const char* out_json = serialize_to_json(delaunator).c_str();