// Copyright 2024 The Pigweed Authors // // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy of // the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations under // the License. #include "pw_numeric/integer_division.h" #include #include #include "pw_unit_test/framework.h" namespace { TEST(IntegerDivisionRoundNearest, Sweep) { for (int dividend = -100; dividend <= 100; ++dividend) { for (int divisor = -100; divisor <= 100; ++divisor) { if (divisor == 0) { continue; } EXPECT_EQ(pw::IntegerDivisionRoundNearest(dividend, divisor), std::round(static_cast(dividend) / static_cast(divisor))); } } } static_assert(pw::IntegerDivisionRoundNearest(255, 255) == 1); static_assert(pw::IntegerDivisionRoundNearest(254, 255) == 1); static_assert(pw::IntegerDivisionRoundNearest(128, 255) == 1); static_assert(pw::IntegerDivisionRoundNearest(127, 255) == 0); static_assert(pw::IntegerDivisionRoundNearest(1, 255) == 0); static_assert(pw::IntegerDivisionRoundNearest(127, 127) == 1); static_assert(pw::IntegerDivisionRoundNearest(126, 127) == 1); static_assert(pw::IntegerDivisionRoundNearest(64, 127) == 1); static_assert(pw::IntegerDivisionRoundNearest(63, 127) == 0); static_assert(pw::IntegerDivisionRoundNearest(1, 127) == 0); static_assert(pw::IntegerDivisionRoundNearest(-128, -128) == 1); static_assert(pw::IntegerDivisionRoundNearest(-127, -128) == 1); static_assert(pw::IntegerDivisionRoundNearest(-64, -128) == 1); static_assert(pw::IntegerDivisionRoundNearest(-63, -128) == 0); static_assert(pw::IntegerDivisionRoundNearest(-1, -128) == 0); static_assert(pw::IntegerDivisionRoundNearest(-128, 127) == -1); static_assert(pw::IntegerDivisionRoundNearest(-127, 127) == -1); static_assert(pw::IntegerDivisionRoundNearest(-64, 127) == -1); static_assert(pw::IntegerDivisionRoundNearest(-63, 127) == 0); static_assert(pw::IntegerDivisionRoundNearest(-1, 127) == 0); static_assert(pw::IntegerDivisionRoundNearest(127, -128) == -1); static_assert(pw::IntegerDivisionRoundNearest(64, -128) == -1); static_assert(pw::IntegerDivisionRoundNearest(63, -128) == 0); static_assert(pw::IntegerDivisionRoundNearest(1, -128) == 0); } // namespace