Skip to content

Commit

Permalink
auto-registering tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgreywater committed Oct 19, 2017
1 parent 0cd09ff commit c2d48d7
Show file tree
Hide file tree
Showing 37 changed files with 234 additions and 357 deletions.
43 changes: 18 additions & 25 deletions test/bench.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
#include "comparison/earcut.hpp"
#include "comparison/libtess2.hpp"

#include "fixtures/geometries.hpp"

#include <iostream>
#include <iomanip>
#include <vector>
#include <chrono>
#include <unordered_set>

template <template <typename, typename> class Tesselator, typename Coord, typename Polygon>
double bench(const Polygon &polygon) {
template<typename Proc>
double bench(Proc&& procedure) {
std::vector<int64_t> runs;
int64_t total = 0;
uint32_t warmup = 0;

Tesselator<Coord, Polygon> tesselator { polygon };

while (total < 2000000000ll || runs.size() < 100) {
const auto started = std::chrono::high_resolution_clock::now();
tesselator.run();
procedure();
const auto finished = std::chrono::high_resolution_clock::now();

// Don't count the first couple of iterations.
Expand All @@ -34,12 +30,11 @@ double bench(const Polygon &polygon) {
return double(runs.size()) / (double(total) / 1e9);
}

template <typename Coord, typename Polygon>
void report(const char *name, const Polygon &polygon, const int cols[]) {
std::cerr << "| " << std::left << std::setw(cols[0]) << name << " | ";
auto earcut = bench<EarcutTesselator, Coord>(polygon);
void report(mapbox::fixtures::FixtureTester* fixture, const int cols[]) {
std::cerr << "| " << std::left << std::setw(cols[0]) << fixture->name << " | ";
auto earcut = bench([&]{ fixture->earcut(); });
std::cerr << std::right << std::setw(cols[1] - 6) << std::fixed << std::setprecision(0) << earcut << " ops/s | ";
auto libtess2 = bench<Libtess2Tesselator, Coord>(polygon);
auto libtess2 = bench([&]{ fixture->libtess(); });
std::cerr << std::setw(cols[2] - 6) << std::setprecision(0) << libtess2 << " ops/s |" << std::endl;
}

Expand Down Expand Up @@ -67,18 +62,16 @@ int main() {

separator(cols);

report<int>("bad_hole", mapbox::fixtures::bad_hole, cols);
report<int>("building", mapbox::fixtures::building, cols);
report<int>("degenerate", mapbox::fixtures::degenerate, cols);
report<double>("dude", mapbox::fixtures::dude, cols);
report<int>("empty_square", mapbox::fixtures::empty_square, cols);
report<int>("water_huge", mapbox::fixtures::water_huge, cols);
report<int>("water_huge2", mapbox::fixtures::water_huge2, cols);
report<int>("water", mapbox::fixtures::water, cols);
report<int>("water2", mapbox::fixtures::water2, cols);
report<int>("water3", mapbox::fixtures::water3, cols);
report<int>("water3b", mapbox::fixtures::water3b, cols);
report<int>("water4", mapbox::fixtures::water4, cols);
auto& fixtures = mapbox::fixtures::FixtureTester::collection();
std::unordered_set<std::string> bench_whitelist = {
"bad_hole", "building", "degenerate", "dude", "empty_square", "water_huge",
"water_huge2", "water", "water2", "water3", "water3b", "water4"
};
for (auto fixture : fixtures) {
if (bench_whitelist.find(fixture->name) != bench_whitelist.end()) {
report(fixture, cols);
}
}

separator(cols);

Expand Down
4 changes: 2 additions & 2 deletions test/comparison/earcut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class EarcutTesselator {
indices_ = mapbox::earcut(polygon);
}

std::vector<uint32_t> indices() const {
std::vector<uint32_t> const& indices() const {
return indices_;
}

Vertices vertices() const {
Vertices const& vertices() const {
return vertices_;
}

Expand Down
84 changes: 34 additions & 50 deletions test/convert_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,7 @@

var fs = require('fs');
var path = require('path');

var hpp = '// This file is auto-generated, manual changes will be lost if the code is regenerated.\n\n';
hpp += '#pragma once\n\n';
hpp += '#include <vector>\n';
hpp += '#include <map>\n';
hpp += '#include <string>\n';
hpp += '#include <utility>\n\n';

hpp += '#ifdef _MSC_VER // msvc has trouble optimizing large inline std::vectors, so we disable the optimization\n';
hpp += '# define MAPBOX_MSVC_DISABLE_OPTIMIZATION() __pragma(optimize("", off))\n';
hpp += '# define MAPBOX_MSVC_ENABLE_OPTIMIZATION() __pragma(optimize("", on))\n';
hpp += '#else\n';
hpp += '# define MAPBOX_MSVC_DISABLE_OPTIMIZATION()\n';
hpp += '# define MAPBOX_MSVC_ENABLE_OPTIMIZATION()\n';
hpp += '#endif\n\n';

hpp += 'namespace mapbox {\n';
hpp += 'namespace fixtures {\n\n';

hpp += 'template <typename T> using Polygon = std::vector<std::vector<T>>;\n';
hpp += 'template <typename T> using Triangles = std::vector<T>;\n\n';

hpp += 'using ShortPoint = std::pair<short, short>;\n';
hpp += 'using ShortTriangles = Triangles<ShortPoint>;\n';
hpp += 'using ShortPolygon = Polygon<ShortPoint>;\n\n';

hpp += 'using IntegerPoint = std::pair<int, int>;\n';
hpp += 'using IntegerTriangles = Triangles<IntegerPoint>;\n';
hpp += 'using IntegerPolygon = Polygon<IntegerPoint>;\n\n';

hpp += 'using DoublePoint = std::pair<double, double>;\n';
hpp += 'using DoubleTriangles = Triangles<DoublePoint>;\n';
hpp += 'using DoublePolygon = Polygon<DoublePoint>;\n\n\n';

hpp += 'extern const ShortPolygon park;\n';
var earcut = require('../../earcut/src/earcut.js');

var integerPolygons = '';
var doublePolygons = '';
Expand All @@ -47,14 +13,22 @@ fs.readdirSync(base).filter(function (name) {
return path.extname(name) === '.json';
}).forEach(function (name) {
var json = JSON.parse(fs.readFileSync(path.join(base, name), 'utf-8'));
var data = earcut.flatten(json),
indices = earcut(data.vertices, data.holes, data.dimensions),
deviation = earcut.deviation(data.vertices, data.holes, data.dimensions, indices);

var id = path.basename(name, path.extname(name)).replace(/[^a-z0-9]+/g, '_');

var integer = true;
var short_integer = true;

function processPoint(p) {
if (integer && (p[0] % 1 !== 0 || p[1] % 1 !== 0)) {
integer = false;
short_integer = false;
}
if (short_integer && (p[0] < -32767 || p[0] > 32767 || p[1] < -32767 || p[1] > 32767)) {
short_integer = false;
}
return p.join(',');
}
Expand All @@ -64,28 +38,38 @@ fs.readdirSync(base).filter(function (name) {
geometry += ' {{' + (json[i].map(processPoint).join('},{')) + '}},\n';
}

var className = integer ? 'IntegerPolygon' : 'DoublePolygon';
var className = "Fixture<double>"
if (short_integer) {
className = "Fixture<short>"
} else if (integer) {
className = "Fixture<int>"
}

var expectedTriangles = indices.length / 3;
var expectedDeviation = deviation;
expectedDeviation += 1e-14;
var libtessDeviationMap = {
"water": 0.00002,
"water_huge": 0.0002,
"water_huge2": 0.00015,
"bad_hole": 0.0022,
"issue16": 0.0255,
"self_touching": 0.002,
"simplified_us_border": 0.001,
"issue45": 0.094,
"empty_square": Infinity
};
var expectedLibtessDeviation = libtessDeviationMap[id];
if (!expectedLibtessDeviation) expectedLibtessDeviation = 0.000001;
var cpp = '// This file is auto-generated, manual changes will be lost if the code is regenerated.\n\n';
cpp += '#include "geometries.hpp"\n\n';
cpp += 'MAPBOX_MSVC_DISABLE_OPTIMIZATION()\n';
cpp += 'namespace mapbox {\n';
cpp += 'namespace fixtures {\n\n';
cpp += 'const ' + className + ' ' + id + ' = {\n';
cpp += 'static const ' + className + ' ' + id + '("' + id + '", ' + expectedTriangles + ', ' + expectedDeviation + ', ' + expectedLibtessDeviation +', {\n';
cpp += geometry;
cpp += '};\n\n';
cpp += '});\n\n';
cpp += '}\n';
cpp += '}\n';
cpp += 'MAPBOX_MSVC_ENABLE_OPTIMIZATION()\n';

hpp += 'extern const ' + className + ' ' + id + ';\n';


fs.writeFileSync('test/fixtures/' + id + '.cpp', cpp);
});

hpp += '\n';
hpp += '}\n';
hpp += '}\n';


fs.writeFileSync('test/fixtures/geometries.hpp', hpp);
6 changes: 2 additions & 4 deletions test/fixtures/bad_diagonals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

#include "geometries.hpp"

MAPBOX_MSVC_DISABLE_OPTIMIZATION()
namespace mapbox {
namespace fixtures {

const IntegerPolygon bad_diagonals = {
static const Fixture<short> bad_diagonals("bad_diagonals", 7, 1e-14, 0.000001, {
{{440,4152},{440,4208},{296,4192},{368,4192},{400,4200},{400,4176},{368,4192},{296,4192},{264,4200},{288,4160},{296,4192}},
};
});

}
}
MAPBOX_MSVC_ENABLE_OPTIMIZATION()
6 changes: 2 additions & 4 deletions test/fixtures/bad_hole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

#include "geometries.hpp"

MAPBOX_MSVC_DISABLE_OPTIMIZATION()
namespace mapbox {
namespace fixtures {

const IntegerPolygon bad_hole = {
static const Fixture<short> bad_hole("bad_hole", 42, 0.018674136321205143, 0.0022, {
{{810,2828},{818,2828},{832,2818},{844,2806},{855,2808},{866,2816},{867,2824},{876,2827},{883,2834},{875,2834},{867,2840},{878,2838},{889,2844},{880,2847},{870,2847},{860,2864},{852,2879},{847,2867},{810,2828},{810,2828}},
{{818,2834},{823,2833},{831,2828},{839,2829},{839,2837},{851,2845},{847,2835},{846,2827},{847,2827},{837,2827},{840,2815},{835,2823},{818,2834},{818,2834}},
{{857,2846},{864,2850},{866,2839},{857,2846},{857,2846}},
{{848,2863},{848,2866},{854,2852},{846,2854},{847,2862},{838,2851},{838,2859},{848,2863},{848,2863}},
};
});

}
}
MAPBOX_MSVC_ENABLE_OPTIMIZATION()
6 changes: 2 additions & 4 deletions test/fixtures/building.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

#include "geometries.hpp"

MAPBOX_MSVC_DISABLE_OPTIMIZATION()
namespace mapbox {
namespace fixtures {

const IntegerPolygon building = {
static const Fixture<short> building("building", 13, 1e-14, 0.000001, {
{{661,112},{661,96},{666,96},{666,87},{743,87},{771,87},{771,114},{750,114},{750,113},{742,113},{742,106},{710,106},{710,113},{666,113},{666,112}},
};
});

}
}
MAPBOX_MSVC_ENABLE_OPTIMIZATION()
6 changes: 2 additions & 4 deletions test/fixtures/degenerate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

#include "geometries.hpp"

MAPBOX_MSVC_DISABLE_OPTIMIZATION()
namespace mapbox {
namespace fixtures {

const IntegerPolygon degenerate = {
static const Fixture<short> degenerate("degenerate", 0, 1e-14, 0.000001, {
{{100,100},{100,100},{200,100},{200,200},{200,100},{0,100}},
};
});

}
}
MAPBOX_MSVC_ENABLE_OPTIMIZATION()
6 changes: 2 additions & 4 deletions test/fixtures/dude.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

#include "geometries.hpp"

MAPBOX_MSVC_DISABLE_OPTIMIZATION()
namespace mapbox {
namespace fixtures {

const DoublePolygon dude = {
static const Fixture<double> dude("dude", 106, 1.122056470349405e-14, 0.000001, {
{{280.35714,648.79075},{286.78571,662.8979},{263.28607,661.17871},{262.31092,671.41548},{250.53571,677.00504},{250.53571,683.43361},{256.42857,685.21933},{297.14286,669.50504},{289.28571,649.50504},{285,631.6479},{285,608.79075},{292.85714,585.21932},{306.42857,563.79075},{323.57143,548.79075},{339.28571,545.21932},{357.85714,547.36218},{375,550.21932},{391.42857,568.07647},{404.28571,588.79075},{413.57143,612.36218},{417.14286,628.07647},{438.57143,619.1479},{438.03572,618.96932},{437.5,609.50504},{426.96429,609.86218},{424.64286,615.57647},{419.82143,615.04075},{420.35714,605.04075},{428.39286,598.43361},{437.85714,599.68361},{443.57143,613.79075},{450.71429,610.21933},{431.42857,575.21932},{405.71429,550.21932},{372.85714,534.50504},{349.28571,531.6479},{346.42857,521.6479},{346.42857,511.6479},{350.71429,496.6479},{367.85714,476.6479},{377.14286,460.93361},{385.71429,445.21932},{388.57143,404.50504},{360,352.36218},{337.14286,325.93361},{330.71429,334.50504},{347.14286,354.50504},{337.85714,370.21932},{333.57143,359.50504},{319.28571,353.07647},{312.85714,366.6479},{350.71429,387.36218},{368.57143,408.07647},{375.71429,431.6479},{372.14286,454.50504},{366.42857,462.36218},{352.85714,462.36218},{336.42857,456.6479},{332.85714,438.79075},{338.57143,423.79075},{338.57143,411.6479},{327.85714,405.93361},{320.71429,407.36218},{315.71429,423.07647},{314.28571,440.21932},{325,447.71932},{324.82143,460.93361},{317.85714,470.57647},{304.28571,483.79075},{287.14286,491.29075},{263.03571,498.61218},{251.60714,503.07647},{251.25,533.61218},{260.71429,533.61218},{272.85714,528.43361},{286.07143,518.61218},{297.32143,508.25504},{297.85714,507.36218},{298.39286,506.46932},{307.14286,496.6479},{312.67857,491.6479},{317.32143,503.07647},{322.5,514.1479},{325.53571,521.11218},{327.14286,525.75504},{326.96429,535.04075},{311.78571,540.04075},{291.07143,552.71932},{274.82143,568.43361},{259.10714,592.8979},{254.28571,604.50504},{251.07143,621.11218},{250.53571,649.1479},{268.1955,654.36208}},
{{325,437},{320,423},{329,413},{332,423}},
{{320.72342,480},{338.90617,465.96863},{347.99754,480.61584},{329.8148,510.41534},{339.91632,480.11077},{334.86556,478.09046}},
};
});

}
}
MAPBOX_MSVC_ENABLE_OPTIMIZATION()
6 changes: 2 additions & 4 deletions test/fixtures/eberly_3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

#include "geometries.hpp"

MAPBOX_MSVC_DISABLE_OPTIMIZATION()
namespace mapbox {
namespace fixtures {

const IntegerPolygon eberly_3 = {
static const Fixture<short> eberly_3("eberly_3", 73, 1e-14, 0.000001, {
{{2328,2408},{2328,2472},{2344,2472},{2344,2432},{2384,2448},{2384,2536},{2408,2552},{2448,2544},{2456,2560},{2496,2544},{2480,2624},{2456,2664},{2424,2680},{2400,2768},{2376,2768},{2368,2704},{2336,2704},{2264,2784},{2216,2784},{2200,2760},{2168,2760},{2152,2744},{2128,2744},{2128,2784},{2072,2768},{2032,2720},{2000,2720},{2000,2688},{1936,2696},{1920,2736},{1888,2728},{1896,2696},{1928,2688},{1928,2664},{1896,2664},{1896,2640},{1912,2632},{1872,2608},{1888,2576},{2056,2576},{2088,2600},{2184,2608},{2216,2632},{2256,2624},{2248,2600},{2216,2592},{2192,2560},{2120,2576},{2072,2544},{2096,2544},{2080,2520},{2080,2488},{2096,2480},{2080,2448},{2096,2432},{2176,2496},{2200,2488},{2224,2528},{2248,2528},{2240,2488},{2256,2472},{2280,2480},{2264,2416},{2272,2392},{2328,2408}},
{{2320,2608},{2304,2640},{2312,2664},{2360,2632},{2352,2608},{2320,2608}},
{{1912,2632},{1936,2632},{1936,2616},{1912,2608},{1912,2632}},
};
});

}
}
MAPBOX_MSVC_ENABLE_OPTIMIZATION()
6 changes: 2 additions & 4 deletions test/fixtures/eberly_6.cpp

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions test/fixtures/empty_square.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

#include "geometries.hpp"

MAPBOX_MSVC_DISABLE_OPTIMIZATION()
namespace mapbox {
namespace fixtures {

const IntegerPolygon empty_square = {
static const Fixture<short> empty_square("empty_square", 0, 1e-14, Infinity, {
{{0,0},{4000,0},{4000,4000},{0,4000}},
{{0,0},{4000,0},{4000,4000},{0,4000}},
};
});

}
}
MAPBOX_MSVC_ENABLE_OPTIMIZATION()
Loading

0 comments on commit c2d48d7

Please sign in to comment.