// The MIT License (MIT) // // Copyright (c) 2018 Mateusz Pusz // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. #include #include #include #include #include #ifdef MP_UNITS_IMPORT_STD import std; #else #include #endif using namespace mp_units; #if MP_UNITS_HOSTED // ============================================================================ // constraint_violation_handler — throw_policy throws std::domain_error. // Everything else (arithmetic transparency, concepts, numeric_limits) is // covered by static_assert tests in test/static/constrained_test.cpp. // ============================================================================ TEST_CASE("throw_policy: on_constraint_violation throws std::domain_error", "[constrained][throw_policy]") { CHECK_THROWS_AS(throw_policy::on_constraint_violation("test"), std::domain_error); } TEST_CASE("constraint_violation_handler: constrained delegates to throw_policy", "[constrained][handler]") { using T = constrained; CHECK_THROWS_AS(constraint_violation_handler::on_violation("test"), std::domain_error); } // ============================================================================ // constrained as quantity_point rep with check_in_range. // Only the throwing (out-of-bounds) paths are tested here; in-bounds paths // are covered by static_assert tests in test/static/bounded_quantity_point_test.cpp. // ============================================================================ namespace { QUANTITY_SPEC(geo_latitude_qs, isq::angular_measure); inline constexpr struct geo_equator final : absolute_point_origin { } geo_equator; } // namespace template<> inline constexpr auto mp_units::quantity_bounds = mp_units::check_in_range{-90 * mp_units::si::unit_symbols::deg, 90 * mp_units::si::unit_symbols::deg}; namespace { using safe_double = constrained; using latitude_qp = quantity_point; } // namespace TEST_CASE("constrained as quantity_point rep: out-of-range values throw", "[constrained][quantity_point][check_in_range]") { using namespace si::unit_symbols; CHECK_THROWS_AS(latitude_qp(91.0 * geo_latitude_qs[deg], geo_equator), std::domain_error); CHECK_THROWS_AS(latitude_qp(-91.0 * geo_latitude_qs[deg], geo_equator), std::domain_error); CHECK_THROWS_AS(latitude_qp(180.0 * geo_latitude_qs[deg], geo_equator), std::domain_error); } TEST_CASE("constrained as quantity_point rep: arithmetic throws when result out of range", "[constrained][quantity_point][check_in_range]") { using namespace si::unit_symbols; auto pt = latitude_qp(80.0 * geo_latitude_qs[deg], geo_equator); CHECK_THROWS_AS(pt += 20.0 * geo_latitude_qs[deg], std::domain_error); // 80 + 20 = 100 > 90 } #endif // MP_UNITS_HOSTED