From ebddbb6f3909665c43f21ebef74ead1de51bcc07 Mon Sep 17 00:00:00 2001 From: nsubiron Date: Thu, 25 Apr 2019 16:21:12 +0200 Subject: [PATCH] Use other levels of asserts through code --- LibCarla/source/carla/geom/Vector2D.h | 2 +- LibCarla/source/carla/geom/Vector3D.h | 2 +- LibCarla/source/carla/road/Map.cpp | 54 ++++++++----------- LibCarla/source/test/common/test_vector3D.cpp | 11 ++-- 4 files changed, 31 insertions(+), 38 deletions(-) diff --git a/LibCarla/source/carla/geom/Vector2D.h b/LibCarla/source/carla/geom/Vector2D.h index 5c4e31597..7b3bc193c 100644 --- a/LibCarla/source/carla/geom/Vector2D.h +++ b/LibCarla/source/carla/geom/Vector2D.h @@ -49,7 +49,7 @@ namespace geom { Vector2D MakeUnitVector() const { const float len = Length(); - DEBUG_ASSERT(len > 2.0f * std::numeric_limits::epsilon()); + DEVELOPMENT_ASSERT(len > 2.0f * std::numeric_limits::epsilon()); const float k = 1.0f / len; return Vector2D(x * k, y * k); } diff --git a/LibCarla/source/carla/geom/Vector3D.h b/LibCarla/source/carla/geom/Vector3D.h index 2a6e3080d..de51763fb 100644 --- a/LibCarla/source/carla/geom/Vector3D.h +++ b/LibCarla/source/carla/geom/Vector3D.h @@ -52,7 +52,7 @@ namespace geom { Vector3D MakeUnitVector() const { const float length = Length(); - DEBUG_ASSERT(length > 2.0f * std::numeric_limits::epsilon()); + DEVELOPMENT_ASSERT(length > 2.0f * std::numeric_limits::epsilon()); const float k = 1.0f / length; return Vector3D(x * k, y * k, z * k); } diff --git a/LibCarla/source/carla/road/Map.cpp b/LibCarla/source/carla/road/Map.cpp index 4944c7c41..f88f90b70 100644 --- a/LibCarla/source/carla/road/Map.cpp +++ b/LibCarla/source/carla/road/Map.cpp @@ -25,16 +25,6 @@ namespace road { /// sections to avoid floating point precision errors. static constexpr double EPSILON = 10.0 * std::numeric_limits::epsilon(); - // =========================================================================== - // -- Error handling --------------------------------------------------------- - // =========================================================================== - - [[ noreturn ]] static void throw_invalid_input(const char *message) { - throw_exception(std::invalid_argument(message)); - } - -#define THROW_INVALID_INPUT_ASSERT(pred) if (!(pred)) { throw_invalid_input("assert failed: " #pred); } - // =========================================================================== // -- Static local methods --------------------------------------------------- // =========================================================================== @@ -114,7 +104,7 @@ namespace road { double tangent = 0.0; for (const auto &lane : container) { auto info = lane.second.template GetInfo(s); - THROW_INVALID_INPUT_ASSERT(info != nullptr); + RELEASE_ASSERT(info != nullptr); const auto current_polynomial = info->GetPolynomial(); auto current_dist = current_polynomial.Evaluate(s); auto current_tang = current_polynomial.Tangent(s); @@ -213,7 +203,7 @@ namespace road { auto &lane = road.GetLaneByDistance(waypoint.s, waypoint.lane_id); const auto lane_section = lane.GetLaneSection(); - THROW_INVALID_INPUT_ASSERT(lane_section != nullptr); + RELEASE_ASSERT(lane_section != nullptr); const auto lane_section_id = lane_section->GetId(); waypoint.section_id = lane_section_id; @@ -243,21 +233,21 @@ namespace road { geom::Transform Map::ComputeTransform(Waypoint waypoint) const { // lane_id can't be 0 - THROW_INVALID_INPUT_ASSERT(waypoint.lane_id != 0); + RELEASE_ASSERT(waypoint.lane_id != 0); const auto &road = _data.GetRoad(waypoint.road_id); // must s be smaller (or eq) than road lenght and bigger (or eq) than 0? - THROW_INVALID_INPUT_ASSERT(waypoint.s <= road.GetLength()); - THROW_INVALID_INPUT_ASSERT(waypoint.s >= 0.0); + RELEASE_ASSERT(waypoint.s <= road.GetLength()); + RELEASE_ASSERT(waypoint.s >= 0.0); const auto &lane_section = road.GetLaneSectionById(waypoint.section_id); const std::map &lanes = lane_section.GetLanes(); // check that lane_id exists on the current s - THROW_INVALID_INPUT_ASSERT(!lanes.empty()); - THROW_INVALID_INPUT_ASSERT(waypoint.lane_id >= lanes.begin()->first); - THROW_INVALID_INPUT_ASSERT(waypoint.lane_id <= lanes.rbegin()->first); + RELEASE_ASSERT(!lanes.empty()); + RELEASE_ASSERT(waypoint.lane_id >= lanes.begin()->first); + RELEASE_ASSERT(waypoint.lane_id <= lanes.rbegin()->first); double lane_width = 0; double lane_tangent = 0; @@ -322,11 +312,11 @@ namespace road { const auto s = waypoint.s; const auto &lane = GetLane(waypoint); - THROW_INVALID_INPUT_ASSERT(lane.GetRoad() != nullptr); - THROW_INVALID_INPUT_ASSERT(s <= lane.GetRoad()->GetLength()); + RELEASE_ASSERT(lane.GetRoad() != nullptr); + RELEASE_ASSERT(s <= lane.GetRoad()->GetLength()); const auto lane_width_info = lane.GetInfo(s); - THROW_INVALID_INPUT_ASSERT(lane_width_info != nullptr); + RELEASE_ASSERT(lane_width_info != nullptr); return lane_width_info->GetPolynomial().Evaluate(s); } @@ -344,8 +334,8 @@ namespace road { const auto s = waypoint.s; const auto ¤t_lane = GetLane(waypoint); - THROW_INVALID_INPUT_ASSERT(current_lane.GetRoad() != nullptr); - THROW_INVALID_INPUT_ASSERT(s <= current_lane.GetRoad()->GetLength()); + RELEASE_ASSERT(current_lane.GetRoad() != nullptr); + RELEASE_ASSERT(s <= current_lane.GetRoad()->GetLength()); const auto inner_lane_id = waypoint.lane_id < 0 ? waypoint.lane_id + 1 : @@ -374,13 +364,13 @@ namespace road { std::vector result; result.reserve(next_lanes.size()); for (auto *next_lane : next_lanes) { - THROW_INVALID_INPUT_ASSERT(next_lane != nullptr); + RELEASE_ASSERT(next_lane != nullptr); const auto lane_id = next_lane->GetId(); - THROW_INVALID_INPUT_ASSERT(lane_id != 0); + RELEASE_ASSERT(lane_id != 0); const auto *section = next_lane->GetLaneSection(); - THROW_INVALID_INPUT_ASSERT(section != nullptr); + RELEASE_ASSERT(section != nullptr); const auto *road = next_lane->GetRoad(); - THROW_INVALID_INPUT_ASSERT(road != nullptr); + RELEASE_ASSERT(road != nullptr); const auto distance = GetDistanceAtStartOfLane(*next_lane); result.emplace_back(Waypoint{road->GetId(), section->GetId(), lane_id, distance}); } @@ -390,7 +380,7 @@ namespace road { std::vector Map::GetNext( const Waypoint waypoint, const double distance) const { - THROW_INVALID_INPUT_ASSERT(distance > 0.0); + RELEASE_ASSERT(distance > 0.0); const auto &lane = GetLane(waypoint); const bool forward = (waypoint.lane_id <= 0); const double signed_distance = forward ? distance : -distance; @@ -404,7 +394,7 @@ namespace road { Waypoint result = waypoint; result.s += signed_distance; result.s += forward ? -EPSILON : EPSILON; - THROW_INVALID_INPUT_ASSERT(result.s > 0.0); + RELEASE_ASSERT(result.s > 0.0); return { result }; } @@ -421,7 +411,7 @@ namespace road { } boost::optional Map::GetRight(Waypoint waypoint) const { - THROW_INVALID_INPUT_ASSERT(waypoint.lane_id != 0); + RELEASE_ASSERT(waypoint.lane_id != 0); if (waypoint.lane_id > 0) { ++waypoint.lane_id; } else { @@ -431,7 +421,7 @@ namespace road { } boost::optional Map::GetLeft(Waypoint waypoint) const { - THROW_INVALID_INPUT_ASSERT(waypoint.lane_id != 0); + RELEASE_ASSERT(waypoint.lane_id != 0); if (std::abs(waypoint.lane_id) == 1) { waypoint.lane_id *= -1; } else if (waypoint.lane_id > 0) { @@ -478,5 +468,3 @@ namespace road { } // namespace road } // namespace carla - -#undef THROW_INVALID_INPUT_ASSERT diff --git a/LibCarla/source/test/common/test_vector3D.cpp b/LibCarla/source/test/common/test_vector3D.cpp index 1c7796334..02c5af083 100644 --- a/LibCarla/source/test/common/test_vector3D.cpp +++ b/LibCarla/source/test/common/test_vector3D.cpp @@ -18,8 +18,13 @@ TEST(vector3D, make_unit_vec) { ASSERT_EQ(Vector3D(0,10,0).MakeUnitVector(), Vector3D(0,1,0)); ASSERT_EQ(Vector3D(0,0,512).MakeUnitVector(), Vector3D(0,0,1)); ASSERT_NE(Vector3D(0,1,512).MakeUnitVector(), Vector3D(0,0,1)); -#ifndef NDEBUG +#ifdef LIBCARLA_NO_EXCEPTIONS ASSERT_DEATH_IF_SUPPORTED( - Vector3D().MakeUnitVector(), "length > 2.0f \\* std::numeric_limits::epsilon()"); -#endif // NDEBUG + Vector3D().MakeUnitVector(), + "length > 2.0f \\* std::numeric_limits::epsilon()"); +#else + ASSERT_THROW( + Vector3D().MakeUnitVector(), + std::runtime_error); +#endif // LIBCARLA_NO_EXCEPTIONS }