File: integer_division_unittest.cc

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (91 lines) | stat: -rw-r--r-- 3,058 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "util/integer_division.h"

#include <chrono>

#include "gtest/gtest.h"

namespace openscreen {
namespace {

constexpr int kDenominators[2] = {3, 4};

// Common test routine that tests one of the integer division functions using a
// fixed denominator and stepping, one-by-one, over a range of numerators around
// zero.
template <typename Input, typename Output>
void TestRangeAboutZero(int denom,
                        int range_of_numerators,
                        int first_expected_result,
                        Output (*function_to_test)(Input, Input)) {
  int expected_result = first_expected_result;
  int count_until_next_change = denom;
  for (int num = -range_of_numerators; num <= range_of_numerators; ++num) {
    EXPECT_EQ(expected_result, function_to_test(Input(num), Input(denom)))
        << "num=" << num << ", denom=" << denom;
    EXPECT_EQ(expected_result, function_to_test(Input(-num), Input(-denom)))
        << "num=" << (-num) << ", denom=" << (-denom);

    --count_until_next_change;
    if (count_until_next_change == 0) {  // Next result will be one higher.
      ++expected_result;
      count_until_next_change = denom;
    }
  }
}

TEST(IntegerDivision, DividesAndRoundsUpInts) {
  auto* const function_to_test = &DivideRoundingUp<int>;
  for (int denom : kDenominators) {
    TestRangeAboutZero(denom, denom == 3 ? 11 : 15, -3, function_to_test);
  }
}

TEST(IntegerDivision, DividesAndRoundsUpChronoDurations) {
  auto* const function_to_test = &DivideRoundingUp<std::chrono::milliseconds>;
  for (int denom : kDenominators) {
    TestRangeAboutZero(denom, denom == 3 ? 11 : 15, -3, function_to_test);
  }
}

// Assumption: DivideRoundingUp() is working (tested by the above two tests).
TEST(IntegerDivision, DividesPositivesAndRoundsUp) {
  for (int num = 0; num <= 6; ++num) {
    for (int denom = 1; denom <= 6; ++denom) {
      EXPECT_EQ(DivideRoundingUp(num, denom),
                DividePositivesRoundingUp(num, denom));
    }
  }
}

TEST(IntegerDivision, DividesAndRoundsNearestInts) {
  auto* const function_to_test = &DivideRoundingNearest<int>;
  for (int denom : kDenominators) {
    TestRangeAboutZero(denom, denom == 3 ? 10 : 14, -3, function_to_test);
  }
}

TEST(IntegerDivision, DividesAndRoundsNearestChronoDurations) {
  auto* const function_to_test =
      &DivideRoundingNearest<std::chrono::milliseconds>;
  for (int denom : kDenominators) {
    TestRangeAboutZero(denom, denom == 3 ? 10 : 14, -3, function_to_test);
  }
}

// Assumption: DivideRoundingNearest() is working (tested by the above two
// tests).
TEST(IntegerDivision, DividesPositivesAndRoundsNearest) {
  for (int num = 0; num <= 6; ++num) {
    for (int denom = 1; denom <= 6; ++denom) {
      EXPECT_EQ(DivideRoundingNearest(num, denom),
                DividePositivesRoundingNearest(num, denom));
    }
  }
}

}  // namespace
}  // namespace openscreen