/
Mr.Stalin
/
FOnline-Engine
Обзор
Документация
Войти
/
Mr.Stalin
/
FOnline-Engine
Код
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Source/Tests/Test_SafeArithmetics.cpp
127 строк
6 KB
cvet
Non const locals (#190)
24 июл 2026, 10:46
Не верифицирован
24 июл 2026, 10:46
4883d25
Код
Авторство
О чём код?
// __________ ___ ______ _ // / ____/ __ \____ / (_)___ ___ / ____/___ ____ _(_)___ ___ // / /_ / / / / __ \/ / / __ \/ _ \ / __/ / __ \/ __ `/ / __ \/ _ ` // / __/ / /_/ / / / / / / / / / __/ / /___/ / / / /_/ / / / / / __/ // /_/ \____/_/ /_/_/_/_/ /_/\___/ /_____/_/ /_/\__, /_/_/ /_/\___/ // /____/ // FOnline Engine // https://fonline.ru // https://github.com/cvet/fonline // // MIT License // // Copyright (c) 2006 - 2026, Anton Tsvetinskiy aka cvet <cvet@tut.by> // // 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 "catch_amalgamated.hpp" #include "Common.h" FO_BEGIN_NAMESPACE TEST_CASE("SafeArithmetics") { SECTION("FloatCompare") { CHECK(is_float_equal(1.0f, 1.0f + 1.0e-7f)); CHECK_FALSE(is_float_equal(1.0f, 1.1f)); } SECTION("NumericCast") { CHECK(numeric_cast<int32_t>(uint16_t {123}) == 123); CHECK(clamp_to<uint8_t>(int32_t {-1}) == 0); CHECK(clamp_to<uint8_t>(int32_t {300}) == 255); CHECK(clamp_to<int8_t>(int32_t {127}) == 127); CHECK(clamp_to<int8_t>(int32_t {128}) == std::numeric_limits<int8_t>::max()); CHECK(clamp_to<int8_t>(int32_t {-129}) == std::numeric_limits<int8_t>::min()); CHECK(numeric_cast<int64_t>(uint32_t {4294967295u}) == 4294967295ll); CHECK_THROWS_AS((numeric_cast<int8_t>(int32_t {128})), OverflowException); CHECK_THROWS_AS((numeric_cast<uint8_t>(int32_t {-1})), OverflowException); } SECTION("CheckedDiv") { CHECK(checked_div<int32_t>(10, 2) == 5); CHECK_THROWS_AS((checked_div<int32_t>(10, 0)), DivisionByZeroException); CHECK_THROWS_AS((checked_div<float32_t>(1.0f, 0.0f)), DivisionByZeroException); CHECK_THROWS_AS((checked_div<float32_t>(1.0f, std::numeric_limits<float32_t>::epsilon() * 0.5f)), DivisionByZeroException); CHECK(checked_div<float32_t>(1.0f, std::numeric_limits<float32_t>::epsilon() * 2.0f) > 0.0f); } SECTION("CheckedIntegerOverflow") { CHECK(checked_add<int32_t>(100, 23) == 123); CHECK(checked_sub<int32_t>(100, 23) == 77); CHECK(checked_mul<int32_t>(12, 11) == 132); CHECK_THROWS_AS((checked_add<int32_t>(std::numeric_limits<int32_t>::max(), 1)), OverflowException); CHECK_THROWS_AS((checked_add<int32_t>(std::numeric_limits<int32_t>::min(), -1)), OverflowException); CHECK_THROWS_AS((checked_sub<int32_t>(std::numeric_limits<int32_t>::min(), 1)), OverflowException); CHECK_THROWS_AS((checked_sub<int32_t>(std::numeric_limits<int32_t>::max(), -1)), OverflowException); CHECK_THROWS_AS((checked_mul<int32_t>(std::numeric_limits<int32_t>::max(), 2)), OverflowException); CHECK_THROWS_AS((checked_mul<int32_t>(std::numeric_limits<int32_t>::min(), -1)), OverflowException); CHECK(checked_add<uint32_t>(10u, 20u) == 30u); CHECK(checked_sub<uint32_t>(20u, 10u) == 10u); CHECK(checked_mul<uint32_t>(12u, 11u) == 132u); CHECK_THROWS_AS((checked_add<uint32_t>(std::numeric_limits<uint32_t>::max(), 1u)), OverflowException); CHECK_THROWS_AS((checked_sub<uint32_t>(0u, 1u)), OverflowException); CHECK_THROWS_AS((checked_mul<uint32_t>(std::numeric_limits<uint32_t>::max(), 2u)), OverflowException); } SECTION("Lerp") { CHECK(lerp(10.0f, 20.0f, 0.5f) == 15.0f); CHECK(lerp(10, 20, 0.5f) == 15); CHECK(lerp(20u, 10u, 0.5f) == 15u); CHECK(lerp(5, 7, 0.24f) == 5); CHECK(lerp(5, 7, 0.26f) == 6); CHECK(lerp(5, 7, 0.76f) == 7); CHECK(lerp(20u, 10u, -1.0f) == 20u); CHECK(lerp(20u, 10u, 2.0f) == 10u); } SECTION("IRoundAndConstCast") { CHECK(iround<int32_t>(1.4f) == 1); CHECK(iround<int32_t>(1.6f) == 2); CHECK(iround<int32_t>(-1.6f) == -2); CHECK_THROWS_AS((iround<int32_t>(std::numeric_limits<float32_t>::infinity())), OverflowException); CHECK_THROWS_AS((iround<int32_t>(-std::numeric_limits<float32_t>::infinity())), OverflowException); CHECK_THROWS_AS((iround<int32_t>(std::numeric_limits<float32_t>::quiet_NaN())), OverflowException); CHECK_THROWS_AS((iround<int32_t>(std::numeric_limits<float64_t>::infinity())), OverflowException); CHECK_THROWS_AS((iround<int32_t>(-std::numeric_limits<float64_t>::infinity())), OverflowException); CHECK_THROWS_AS((iround<int32_t>(std::numeric_limits<float64_t>::quiet_NaN())), OverflowException); CHECK_THROWS_AS((iround<int64_t>(1e30f)), OverflowException); CHECK_THROWS_AS((iround<int64_t>(-1e30f)), OverflowException); CHECK_THROWS_AS((iround<int64_t>(1e300)), OverflowException); CHECK_THROWS_AS((iround<int64_t>(-1e300)), OverflowException); CHECK_THROWS_AS((iround<int64_t>(std::numeric_limits<float64_t>::max())), OverflowException); constexpr int32_t casted = const_numeric_cast<int32_t>(uint16_t {42}); STATIC_REQUIRE(casted == 42); } } FO_END_NAMESPACE