Skip to content

Commit

Permalink
use custom file format for polygon tests (#913)
Browse files Browse the repository at this point in the history
* use custom format

* try fix windows

* emscripten stuff

* address comments

* remove test filter

* fix format
  • Loading branch information
pca006132 authored Sep 5, 2024
1 parent e9c065b commit 5b2a95e
Show file tree
Hide file tree
Showing 15 changed files with 132,652 additions and 133,987 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/manifold.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ jobs:
shell: bash
run: |
cd build/bin/Release
./manifold_test.exe --gtest_filter=-Polygon.Zebra*
./manifold_test.exe
cd ../../
- name: test cmake consumer
run: |
Expand Down
14 changes: 7 additions & 7 deletions src/polygon/src/polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ void CheckGeometry(const std::vector<ivec3> &triangles,
geometryErr, "triangulation is not entirely CCW!");
}

void Dump(const PolygonsIdx &polys) {
void Dump(const PolygonsIdx &polys, double precision) {
std::cout << "Polygon 0 " << precision << " " << polys.size() << std::endl;
for (auto poly : polys) {
std::cout << "polys.push_back({" << std::setprecision(9) << std::endl;
std::cout << poly.size() << std::endl;
for (auto v : poly) {
std::cout << " {" << v.pos.x << ", " << v.pos.y << "}, //"
<< std::endl;
std::cout << v.pos.x << " " << v.pos.y << std::endl;
}
std::cout << "});" << std::endl;
}
std::cout << "# ... " << std::endl;
for (auto poly : polys) {
std::cout << "show(array([" << std::endl;
for (auto v : poly) {
Expand All @@ -144,12 +144,12 @@ void PrintFailure(const std::exception &e, const PolygonsIdx &polys,
std::cout << "-----------------------------------" << std::endl;
std::cout << "Triangulation failed! Precision = " << precision << std::endl;
std::cout << e.what() << std::endl;
if (triangles.size() > 1000) {
if (triangles.size() > 1000 && !PolygonParams().verbose) {
std::cout << "Output truncated due to producing " << triangles.size()
<< " triangles." << std::endl;
return;
}
Dump(polys);
Dump(polys, precision);
std::cout << "produced this triangulation:" << std::endl;
for (size_t j = 0; j < triangles.size(); ++j) {
std::cout << triangles[j][0] << ", " << triangles[j][1] << ", "
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ target_precompile_headers(${PROJECT_NAME} INTERFACE test.h)

if(EMSCRIPTEN)
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS
"-s ASSERTIONS=1 -s DEMANGLE_SUPPORT=1 --bind")
"-s ASSERTIONS=1 -s DEMANGLE_SUPPORT=1 --bind --preload-file ${CMAKE_CURRENT_SOURCE_DIR}/polygons@/polygons")
endif()

if(MSVC)
Expand Down
45 changes: 32 additions & 13 deletions test/polygon_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include <atomic>
#include <fstream>
#include <future>

#include "fuzztest/fuzztest.h"
Expand Down Expand Up @@ -103,21 +104,39 @@ void TestPoly(Polygons polys, int _unused, float precision = -1.0) {
TestCases.push_back({polys, precision});
}

// ----------------------------------------------------------------------------
// UGLY HACK FOR SEED REUSE
// ----------------------------------------------------------------------------

#undef TEST
#define TEST(_unused1, _unused2)

std::vector<TestCase> SeedProvider() {
std::string file = __FILE__;
std::string dir = file.substr(0, file.rfind('/'));
auto f = std::ifstream(dir + "/polygons/" + "polygon_corpus.txt");
// for each test:
// test name, expectedNumTri, precision, num polygons
// for each polygon:
// num points
// for each vertex:
// x coord, y coord
//
// note that we should not have commas in the file

std::string name;
double precision, x, y;
int expectedNumTri, numPolys, numPoints;

std::vector<TestCase> TestCases;
auto TestPoly = [&TestCases](Polygons polys, int _unused,
float precision = -1.0) {
while (1) {
f >> name;
if (f.eof()) break;
f >> expectedNumTri >> precision >> numPolys;
Polygons polys;
for (int i = 0; i < numPolys; i++) {
polys.emplace_back();
f >> numPoints;
for (int j = 0; j < numPoints; j++) {
f >> x >> y;
polys.back().emplace_back(x, y);
}
}
TestCases.push_back({polys, precision});
};

#include "polygon_corpus.cpp"

}
f.close();
return TestCases;
}
69 changes: 64 additions & 5 deletions test/polygon_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#include "polygon.h"

#include <algorithm>
#include <fstream>
#include <limits>
#include <random>

#include "test.h"

Expand Down Expand Up @@ -71,9 +71,68 @@ void TestPoly(const Polygons &polys, int expectedNumTri,

PolygonParams().verbose = false;
}

class PolygonTestFixture : public testing::Test {
public:
Polygons polys;
double precision;
int expectedNumTri;
explicit PolygonTestFixture(Polygons polys, double precision,
int expectedNumTri)
: polys(polys), precision(precision), expectedNumTri(expectedNumTri) {}
void TestBody() { TestPoly(polys, expectedNumTri, precision); }
};

void RegisterPolygonTestsFile(const std::string &filename) {
auto f = std::ifstream(filename);
EXPECT_TRUE(f.is_open());

// for each test:
// test name, expectedNumTri, precision, num polygons
// for each polygon:
// num points
// for each vertex:
// x coord, y coord
//
// note that we should not have commas in the file

std::string name;
double precision, x, y;
int expectedNumTri, numPolys, numPoints;

while (1) {
f >> name;
if (f.eof()) break;
f >> expectedNumTri >> precision >> numPolys;
Polygons polys;
for (int i = 0; i < numPolys; i++) {
polys.emplace_back();
f >> numPoints;
for (int j = 0; j < numPoints; j++) {
f >> x >> y;
polys.back().emplace_back(x, y);
}
}
testing::RegisterTest(
"Polygon", name.c_str(), nullptr, nullptr, __FILE__, __LINE__,
[=, polys = std::move(polys)]() -> PolygonTestFixture * {
return new PolygonTestFixture(polys, precision, expectedNumTri);
});
}
f.close();
}
} // namespace

#include "polygons/polygon_corpus.cpp"
#include "polygons/sponge.cpp"
#include "polygons/zebra.cpp"
#include "polygons/zebra3.cpp"
void RegisterPolygonTests() {
std::string files[] = {"polygon_corpus.txt", "sponge.txt", "zebra.txt",
"zebra3.txt"};

#ifdef __EMSCRIPTEN__
for (auto f : files) RegisterPolygonTestsFile("/polygons/" + f);
#else
std::string file = __FILE__;
auto end = std::min(file.rfind('\\'), file.rfind('/'));
std::string dir = file.substr(0, end);
for (auto f : files) RegisterPolygonTestsFile(dir + "/polygons/" + f);
#endif
}
Loading

0 comments on commit 5b2a95e

Please sign in to comment.