Skip to content

Commit

Permalink
eckit::maths::ConvexHull::Exception specialised exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaciel committed Jan 24, 2024
1 parent ecdea36 commit 19701fe
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/eckit/maths/ConvexHull.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ class ConvexHull {
const std::string& command;
};

struct DimensionError : Exception {
using Exception::Exception;
};

struct InputError : Exception {
using Exception::Exception;
};

struct OptionError : Exception {
using Exception::Exception;
};

struct PrecisionError : Exception {
using Exception::Exception;
};

struct TopologyError : Exception {
using Exception::Exception;
};

// -- Constructors

ConvexHull(const ConvexHull&) = delete;
Expand Down
27 changes: 26 additions & 1 deletion src/eckit/maths/Qhull.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "eckit/maths/Qhull.h"

#include <set>
#include <sstream>

#include "eckit/log/Log.h"
Expand Down Expand Up @@ -46,7 +47,31 @@ Qhull::Qhull(size_t N, const coord_t& coord, const std::string& command) {
ASSERT(qh_->qhullStatus() == 0);
}
catch (const orgQhull::QhullError& e) {
throw ConvexHull::Exception(err.str(), e.errorCode(), command);
static const std::set<int> DIMENSION_ERROR{6050};

static const std::set<int> INPUT_ERROR{6010, 6013, 6019, 6020, 6021, 6022, 6023, 6033, 6067, 6070,
6072, 6073, 6074, 6075, 6077, 6078, 6150, 6151, 6152, 6153,
6203, 6204, 6214, 6220, 6221, 6222, 6223, 6229, 6411, 6412};

static const std::set<int> OPTION_ERROR{6006, 6029, 6035, 6036, 6037, 6041, 6044, 6045, 6046,
6047, 6048, 6049, 6051, 6053, 6054, 6055, 6056, 6057,
6058, 6059, 6215, 6362, 6363, 6364, 6365, 6375};

static const std::set<int> PRECISION_ERROR{6012, 6109, 6110, 6111, 6112, 6113, 6114, 6115, 6116,
6117, 6118, 6136, 6154, 6239, 6240, 6297, 6298, 6347,
6348, 6354, 6379, 6380, 6417, 6418, 6422};

static const std::set<int> TOPOLOGY_ERROR{6001, 6107, 6155, 6168, 6170, 6208, 6227, 6260,
6271, 6356, 6361, 6381, 6391, 6420, 6425};

auto is = [](int err, const auto& set) { return set.find(err) != set.end(); };

is(e.errorCode(), DIMENSION_ERROR) ? throw ConvexHull::DimensionError(err.str(), e.errorCode(), command)
: is(e.errorCode(), INPUT_ERROR) ? throw ConvexHull::InputError(err.str(), e.errorCode(), command)
: is(e.errorCode(), OPTION_ERROR) ? throw ConvexHull::OptionError(err.str(), e.errorCode(), command)
: is(e.errorCode(), PRECISION_ERROR) ? throw ConvexHull::PrecisionError(err.str(), e.errorCode(), command)
: is(e.errorCode(), TOPOLOGY_ERROR) ? throw ConvexHull::TopologyError(err.str(), e.errorCode(), command)
: throw ConvexHull::Exception(err.str(), e.errorCode(), command);
}
}

Expand Down
40 changes: 38 additions & 2 deletions tests/maths/test_convex_hull.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,51 @@ CASE("Qhull errors/exceptions") {
{6421, "QH6421 qhull internal error (qh_maxsimplex)", "Qt", 2, {1, 1, 1, 1, 1, 1}},
};

auto trigger = [](const auto& test) { maths::Qhull(test.N, test.coord, test.command); };

for (const auto& test : tests) {
try {
maths::Qhull(test.N, test.coord, test.command);
trigger(test);
EXPECT(false);
}
catch (const maths::ConvexHull::Exception& e) {
catch (const ConvexHull::Exception& e) {
EXPECT_EQUAL(test.errorCode, e.errorCode);
EXPECT_EQUAL(test.what, std::string(e.what(), test.what.length()));
}
catch (...) {
EXPECT(false);
}
}

EXPECT_THROWS_AS(trigger(tests[0]), ConvexHull::DimensionError);
EXPECT_THROWS_AS(trigger(tests[1]), ConvexHull::PrecisionError);
EXPECT_THROWS_AS(trigger(tests[2]), ConvexHull::InputError);
EXPECT_THROWS_AS(trigger(tests[3]), ConvexHull::InputError);

// tests[4] does not throw a specialised error type
try {
trigger(tests[4]);
EXPECT(false);
}
catch (const ConvexHull::DimensionError&) {
EXPECT(false);
}
catch (const ConvexHull::InputError&) {
EXPECT(false);
}
catch (const ConvexHull::OptionError&) {
EXPECT(false);
}
catch (const ConvexHull::PrecisionError&) {
EXPECT(false);
}
catch (const ConvexHull::TopologyError&) {
EXPECT(false);
}
catch (const ConvexHull::Exception&) {
}
catch (...) {
EXPECT(false);
}
}
}
Expand Down

0 comments on commit 19701fe

Please sign in to comment.