Yuri Wiitala | 51398aa | 2019-07-17 18:48:30 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "util/integer_division.h" |
| 6 | |
| 7 | #include <chrono> |
| 8 | |
| 9 | #include "gtest/gtest.h" |
| 10 | |
| 11 | namespace openscreen { |
| 12 | namespace { |
| 13 | |
| 14 | constexpr int kDenominators[2] = {3, 4}; |
| 15 | |
| 16 | // Common test routine that tests one of the integer division functions using a |
| 17 | // fixed denominator and stepping, one-by-one, over a range of numerators around |
| 18 | // zero. |
| 19 | template <typename Input, typename Output> |
| 20 | void TestRangeAboutZero(int denom, |
| 21 | int range_of_numerators, |
| 22 | int first_expected_result, |
| 23 | Output (*function_to_test)(Input, Input)) { |
| 24 | int expected_result = first_expected_result; |
| 25 | int count_until_next_change = denom; |
| 26 | for (int num = -range_of_numerators; num <= range_of_numerators; ++num) { |
| 27 | EXPECT_EQ(expected_result, function_to_test(Input(num), Input(denom))) |
| 28 | << "num=" << num << ", denom=" << denom; |
| 29 | EXPECT_EQ(expected_result, function_to_test(Input(-num), Input(-denom))) |
| 30 | << "num=" << (-num) << ", denom=" << (-denom); |
| 31 | |
| 32 | --count_until_next_change; |
| 33 | if (count_until_next_change == 0) { // Next result will be one higher. |
| 34 | ++expected_result; |
| 35 | count_until_next_change = denom; |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | TEST(IntegerDivision, DividesAndRoundsUpInts) { |
| 41 | auto* const function_to_test = &DivideRoundingUp<int>; |
| 42 | for (int denom : kDenominators) { |
| 43 | TestRangeAboutZero(denom, denom == 3 ? 11 : 15, -3, function_to_test); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | TEST(IntegerDivision, DividesAndRoundsUpChronoDurations) { |
| 48 | auto* const function_to_test = &DivideRoundingUp<std::chrono::milliseconds>; |
| 49 | for (int denom : kDenominators) { |
| 50 | TestRangeAboutZero(denom, denom == 3 ? 11 : 15, -3, function_to_test); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Assumption: DivideRoundingUp() is working (tested by the above two tests). |
| 55 | TEST(IntegerDivision, DividesPositivesAndRoundsUp) { |
| 56 | for (int num = 0; num <= 6; ++num) { |
| 57 | for (int denom = 1; denom <= 6; ++denom) { |
| 58 | EXPECT_EQ(DivideRoundingUp(num, denom), |
| 59 | DividePositivesRoundingUp(num, denom)); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | TEST(IntegerDivision, DividesAndRoundsNearestInts) { |
| 65 | auto* const function_to_test = &DivideRoundingNearest<int>; |
| 66 | for (int denom : kDenominators) { |
| 67 | TestRangeAboutZero(denom, denom == 3 ? 10 : 14, -3, function_to_test); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | TEST(IntegerDivision, DividesAndRoundsNearestChronoDurations) { |
| 72 | auto* const function_to_test = |
| 73 | &DivideRoundingNearest<std::chrono::milliseconds>; |
| 74 | for (int denom : kDenominators) { |
| 75 | TestRangeAboutZero(denom, denom == 3 ? 10 : 14, -3, function_to_test); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Assumption: DivideRoundingNearest() is working (tested by the above two |
| 80 | // tests). |
| 81 | TEST(IntegerDivision, DividesPositivesAndRoundsNearest) { |
| 82 | for (int num = 0; num <= 6; ++num) { |
| 83 | for (int denom = 1; denom <= 6; ++denom) { |
| 84 | EXPECT_EQ(DivideRoundingNearest(num, denom), |
| 85 | DividePositivesRoundingNearest(num, denom)); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | } // namespace |
| 91 | } // namespace openscreen |