1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_numeric/integer_division.h"
16
17 #include <cmath>
18 #include <limits>
19
20 #include "pw_unit_test/framework.h"
21
22 namespace {
23
TEST(IntegerDivisionRoundNearest,Sweep)24 TEST(IntegerDivisionRoundNearest, Sweep) {
25 for (int dividend = -100; dividend <= 100; ++dividend) {
26 for (int divisor = -100; divisor <= 100; ++divisor) {
27 if (divisor == 0) {
28 continue;
29 }
30 EXPECT_EQ(pw::IntegerDivisionRoundNearest(dividend, divisor),
31 std::round(static_cast<double>(dividend) /
32 static_cast<double>(divisor)));
33 }
34 }
35 }
36
37 static_assert(pw::IntegerDivisionRoundNearest<unsigned char>(255, 255) == 1);
38 static_assert(pw::IntegerDivisionRoundNearest<unsigned char>(254, 255) == 1);
39 static_assert(pw::IntegerDivisionRoundNearest<unsigned char>(128, 255) == 1);
40 static_assert(pw::IntegerDivisionRoundNearest<unsigned char>(127, 255) == 0);
41 static_assert(pw::IntegerDivisionRoundNearest<unsigned char>(1, 255) == 0);
42
43 static_assert(pw::IntegerDivisionRoundNearest<signed char>(127, 127) == 1);
44 static_assert(pw::IntegerDivisionRoundNearest<signed char>(126, 127) == 1);
45 static_assert(pw::IntegerDivisionRoundNearest<signed char>(64, 127) == 1);
46 static_assert(pw::IntegerDivisionRoundNearest<signed char>(63, 127) == 0);
47 static_assert(pw::IntegerDivisionRoundNearest<signed char>(1, 127) == 0);
48
49 static_assert(pw::IntegerDivisionRoundNearest<signed char>(-128, -128) == 1);
50 static_assert(pw::IntegerDivisionRoundNearest<signed char>(-127, -128) == 1);
51 static_assert(pw::IntegerDivisionRoundNearest<signed char>(-64, -128) == 1);
52 static_assert(pw::IntegerDivisionRoundNearest<signed char>(-63, -128) == 0);
53 static_assert(pw::IntegerDivisionRoundNearest<signed char>(-1, -128) == 0);
54
55 static_assert(pw::IntegerDivisionRoundNearest<signed char>(-128, 127) == -1);
56 static_assert(pw::IntegerDivisionRoundNearest<signed char>(-127, 127) == -1);
57 static_assert(pw::IntegerDivisionRoundNearest<signed char>(-64, 127) == -1);
58 static_assert(pw::IntegerDivisionRoundNearest<signed char>(-63, 127) == 0);
59 static_assert(pw::IntegerDivisionRoundNearest<signed char>(-1, 127) == 0);
60
61 static_assert(pw::IntegerDivisionRoundNearest<signed char>(127, -128) == -1);
62 static_assert(pw::IntegerDivisionRoundNearest<signed char>(64, -128) == -1);
63 static_assert(pw::IntegerDivisionRoundNearest<signed char>(63, -128) == 0);
64 static_assert(pw::IntegerDivisionRoundNearest<signed char>(1, -128) == 0);
65
66 } // namespace
67