Skip to content

Commit 619b5cd

Browse files
committed
add test with catch2 lib
1 parent 85629f4 commit 619b5cd

File tree

6 files changed

+118
-39
lines changed

6 files changed

+118
-39
lines changed

CMakeLists.txt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,30 @@ if (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/includes")
55
execute_process(COMMAND bash "-c" "(cd ${CMAKE_CURRENT_SOURCE_DIR} && ./fetch-includes.sh)")
66
endif()
77

8-
add_executable(triangulate src/triangulate.cpp)
8+
#delaunator
99
add_library(delaunator src/delaunator.cpp)
10-
target_include_directories (triangulate PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/rapidjson/include")
11-
target_include_directories (triangulate PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/prettyprint")
1210
target_include_directories (delaunator PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/rapidjson/include")
1311
target_include_directories (delaunator PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/prettyprint")
12+
13+
#delaunator
14+
add_library(json-helpers src/json-helpers.cpp)
15+
target_include_directories (json-helpers PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/rapidjson/include")
16+
17+
#delaunator-test
18+
add_executable(delaunator-test src/delaunator-test.cpp)
19+
target_link_libraries(delaunator-test delaunator)
20+
target_include_directories (delaunator-test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/catch/single_include/catch2")
21+
target_link_libraries(delaunator-test json-helpers)
22+
target_link_libraries(delaunator-test delaunator)
23+
24+
25+
#triangulate
26+
add_executable(triangulate src/triangulate.cpp)
27+
target_include_directories (triangulate PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/rapidjson/include")
28+
target_include_directories (triangulate PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/includes/prettyprint")
1429
target_link_libraries(triangulate delaunator)
30+
target_link_libraries(triangulate json-helpers)
31+
1532

1633
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
1734
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})

fetch-includes.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ rm -rf ./includes/*
55

66
git clone --branch v1.1.0 --depth=1 git@github.com:Tencent/rapidjson.git ./includes/rapidjson
77
git clone --branch master --depth=1 git@github.com:louisdx/cxx-prettyprint.git ./includes/prettyprint
8+
git clone --branch master --depth=1 git@github.com:catchorg/Catch2.git ./includes/catch

src/delaunator-test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#define CATCH_CONFIG_MAIN
2+
#include "delaunator.h"
3+
#include "catch.hpp"
4+
#include "json-helpers.h"
5+
#include <string>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
TEST_CASE("triangles match JS version ouput", "[Delaunator]") {
11+
string points_str = json_helpers::read_file("./test-files/playgrounds-1356-epsg-3857.geojson");
12+
string triangles_str = json_helpers::read_file("./test-files/playgrounds-1356-triangles.json");
13+
const vector<double> coords = json_helpers::get_geo_json_points(points_str);
14+
const vector<double> triangles = json_helpers::get_array_points(triangles_str);
15+
Delaunator delaunator(move(coords));
16+
17+
SECTION("length of triangles is the same") {
18+
REQUIRE(delaunator.triangles.size() == triangles.size());
19+
}
20+
21+
SECTION("values are the same") {
22+
for(size_t i = 0; i < triangles.size(); i++)
23+
{
24+
REQUIRE(delaunator.triangles[i] == triangles[i]);
25+
}
26+
}
27+
}
28+

src/json-helpers.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
#include "json-helpers.h"
3+
#include <fstream>
4+
#include <exception>
5+
#include "rapidjson/document.h"
6+
7+
using namespace std;
8+
9+
string json_helpers::read_file(const char* filename) {
10+
ifstream input_file(filename);
11+
if(input_file.good()) {
12+
string json_str(
13+
(istreambuf_iterator<char>(input_file)),
14+
istreambuf_iterator<char>()
15+
);
16+
return json_str;
17+
} else {
18+
printf("Error reading file %s", filename);
19+
throw runtime_error("Error reading file");
20+
}
21+
}
22+
23+
vector<double> json_helpers::get_geo_json_points(const string& json) {
24+
rapidjson::Document document;
25+
if(document.Parse(json.c_str()).HasParseError()) {
26+
throw runtime_error("Cannot parse JSON");
27+
}
28+
const rapidjson::Value& features = document["features"];
29+
vector<double> coords;
30+
// vector<double> y_vector;
31+
for(rapidjson::SizeType i = 0; i < features.Size(); i++) {
32+
const rapidjson::Value& coordinates = features[i]["geometry"]["coordinates"];
33+
const double x = coordinates[0].GetDouble();
34+
const double y = coordinates[1].GetDouble();
35+
coords.push_back(x);
36+
coords.push_back(y);
37+
}
38+
return coords;
39+
}
40+
41+
vector<double> json_helpers::get_array_points(const string& json) {
42+
vector<double> points;
43+
rapidjson::Document document;
44+
if(document.Parse(json.c_str()).HasParseError()) {
45+
throw runtime_error("Cannot parse JSON");
46+
}
47+
if(!document.IsArray()) {
48+
throw runtime_error("It's not JSON Array");
49+
}
50+
points.reserve(static_cast<int>(document.Size()));
51+
for(rapidjson::SizeType i = 0; i < document.Size(); i++) {
52+
points.push_back(document[i].GetDouble());
53+
}
54+
return points;
55+
56+
}

src/json-helpers.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
6+
namespace json_helpers {
7+
std::string read_file(const char* filename);
8+
std::vector<double> get_geo_json_points(const std::string& json);
9+
std::vector<double> get_array_points(const std::string& json);
10+
}

src/triangulate.cpp

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,16 @@
11
#include "rapidjson/document.h"
22
#include "rapidjson/prettywriter.h"
33
#include "delaunator.h"
4+
#include "json-helpers.h"
45
#include <cstdio>
56
#include <fstream>
6-
#include <string>
7-
#include <exception>
87
#include <vector>
98
#include <initializer_list>
109
// #include "prettyprint.hpp"
1110
#include <iostream>
1211
using namespace std;
1312

1413
namespace {
15-
string read_file(const char* filename) {
16-
ifstream input_file(filename);
17-
if(input_file.good()) {
18-
string json_str(
19-
(istreambuf_iterator<char>(input_file)),
20-
istreambuf_iterator<char>()
21-
);
22-
return json_str;
23-
} else {
24-
printf("Error reading file %s", filename);
25-
throw exception();
26-
}
27-
}
28-
29-
vector<double> get_geo_json_points(const string& json) {
30-
rapidjson::Document document;
31-
if(document.Parse(json.c_str()).HasParseError()) {
32-
fprintf(stderr, "Cannot parse JSON");
33-
throw exception();
34-
}
35-
const rapidjson::Value& features = document["features"];
36-
vector<double> coords;
37-
// vector<double> y_vector;
38-
for(rapidjson::SizeType i = 0; i < features.Size(); i++) {
39-
const rapidjson::Value& coordinates = features[i]["geometry"]["coordinates"];
40-
const double x = coordinates[0].GetDouble();
41-
const double y = coordinates[1].GetDouble();
42-
coords.push_back(x);
43-
coords.push_back(y);
44-
}
45-
return coords;
46-
}
4714

4815
const string serialize_to_json(Delaunator &delaunator) {
4916
rapidjson::StringBuffer sb;
@@ -101,8 +68,8 @@ namespace {
10168
int main(int, char* argv[]) {
10269
const char* filename = argv[1];
10370
const char* output = argv[2];
104-
string json = read_file(filename);
105-
const vector<double> coords = get_geo_json_points(json);
71+
string json = json_helpers::read_file(filename);
72+
const vector<double> coords = json_helpers::get_geo_json_points(json);
10673
Delaunator delaunator(move(coords));
10774
const char* out_json = serialize_to_json(delaunator).c_str();
10875

0 commit comments

Comments
 (0)