philipel | 5ab4c6d | 2016-03-08 03:36:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | #include "webrtc/base/mod_ops.h" |
| 13 | |
| 14 | namespace webrtc { |
| 15 | class TestModOps : public ::testing::Test { |
| 16 | protected: |
| 17 | // Can't use std::numeric_limits<unsigned long>::max() since |
| 18 | // MSVC doesn't support constexpr. |
| 19 | static const unsigned long ulmax = ~0ul; // NOLINT |
| 20 | }; |
| 21 | |
| 22 | TEST_F(TestModOps, Add) { |
| 23 | const int D = 100; |
| 24 | EXPECT_EQ(1u, Add<D>(0, 1)); |
| 25 | EXPECT_EQ(0u, Add<D>(0, D)); |
| 26 | for (int i = 0; i < D; ++i) |
| 27 | EXPECT_EQ(0u, Add<D>(i, D - i)); |
| 28 | |
| 29 | int t = 37; |
| 30 | uint8_t a = t; |
| 31 | for (int i = 0; i < 256; ++i) { |
| 32 | EXPECT_EQ(a, static_cast<uint8_t>(t)); |
| 33 | t = Add<256>(t, 1); |
| 34 | ++a; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | TEST_F(TestModOps, AddLarge) { |
| 39 | // NOLINTNEXTLINE |
| 40 | const unsigned long D = ulmax - 10ul; // NOLINT |
| 41 | unsigned long l = D - 1ul; // NOLINT |
| 42 | EXPECT_EQ(D - 2ul, Add<D>(l, l)); |
| 43 | EXPECT_EQ(9ul, Add<D>(l, ulmax)); |
| 44 | EXPECT_EQ(10ul, Add<D>(0ul, ulmax)); |
| 45 | } |
| 46 | |
| 47 | TEST_F(TestModOps, Subtract) { |
| 48 | const int D = 100; |
| 49 | EXPECT_EQ(99u, Subtract<D>(0, 1)); |
| 50 | EXPECT_EQ(0u, Subtract<D>(0, D)); |
| 51 | for (int i = 0; i < D; ++i) |
| 52 | EXPECT_EQ(0u, Subtract<D>(i, D + i)); |
| 53 | |
| 54 | int t = 37; |
| 55 | uint8_t a = t; |
| 56 | for (int i = 0; i < 256; ++i) { |
| 57 | EXPECT_EQ(a, static_cast<uint8_t>(t)); |
| 58 | t = Subtract<256>(t, 1); |
| 59 | --a; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | TEST_F(TestModOps, SubtractLarge) { |
| 64 | // NOLINTNEXTLINE |
| 65 | const unsigned long D = ulmax - 10ul; // NOLINT |
| 66 | unsigned long l = D - 1ul; // NOLINT |
| 67 | EXPECT_EQ(0ul, Subtract<D>(l, l)); |
| 68 | EXPECT_EQ(D - 11ul, Subtract<D>(l, ulmax)); |
| 69 | EXPECT_EQ(D - 10ul, Subtract<D>(0ul, ulmax)); |
| 70 | } |
| 71 | |
| 72 | TEST_F(TestModOps, ForwardDiff) { |
| 73 | EXPECT_EQ(0u, ForwardDiff(4711u, 4711u)); |
| 74 | |
| 75 | uint8_t x = 0; |
| 76 | uint8_t y = 255; |
| 77 | for (int i = 0; i < 256; ++i) { |
| 78 | EXPECT_EQ(255u, ForwardDiff(x, y)); |
| 79 | ++x; |
| 80 | ++y; |
| 81 | } |
| 82 | |
| 83 | int yi = 255; |
| 84 | for (int i = 0; i < 256; ++i) { |
| 85 | EXPECT_EQ(255u, ForwardDiff<uint8_t>(x, yi)); |
| 86 | ++x; |
| 87 | ++yi; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | TEST_F(TestModOps, ReverseDiff) { |
| 92 | EXPECT_EQ(0u, ReverseDiff(4711u, 4711u)); |
| 93 | |
| 94 | uint8_t x = 0; |
| 95 | uint8_t y = 255; |
| 96 | for (int i = 0; i < 256; ++i) { |
| 97 | EXPECT_EQ(1u, ReverseDiff(x, y)); |
| 98 | ++x; |
| 99 | ++y; |
| 100 | } |
| 101 | |
| 102 | int yi = 255; |
| 103 | for (int i = 0; i < 256; ++i) { |
| 104 | EXPECT_EQ(1u, ReverseDiff<uint8_t>(x, yi)); |
| 105 | ++x; |
| 106 | ++yi; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | TEST_F(TestModOps, AheadOrAt) { |
| 111 | uint8_t x = 0; |
| 112 | uint8_t y = 0; |
| 113 | EXPECT_TRUE(AheadOrAt(x, y)); |
| 114 | ++x; |
| 115 | EXPECT_TRUE(AheadOrAt(x, y)); |
| 116 | EXPECT_FALSE(AheadOrAt(y, x)); |
| 117 | for (int i = 0; i < 256; ++i) { |
| 118 | EXPECT_TRUE(AheadOrAt(x, y)); |
| 119 | ++x; |
| 120 | ++y; |
| 121 | } |
| 122 | |
| 123 | x = 128; |
| 124 | y = 0; |
| 125 | EXPECT_TRUE(AheadOrAt(x, y)); |
| 126 | EXPECT_FALSE(AheadOrAt(y, x)); |
| 127 | |
| 128 | x = 129; |
| 129 | EXPECT_FALSE(AheadOrAt(x, y)); |
| 130 | EXPECT_TRUE(AheadOrAt(y, x)); |
| 131 | EXPECT_TRUE(AheadOrAt<uint16_t>(x, y)); |
| 132 | EXPECT_FALSE(AheadOrAt<uint16_t>(y, x)); |
| 133 | } |
| 134 | |
| 135 | TEST_F(TestModOps, AheadOf) { |
| 136 | uint8_t x = 0; |
| 137 | uint8_t y = 0; |
| 138 | EXPECT_FALSE(AheadOf(x, y)); |
| 139 | ++x; |
| 140 | EXPECT_TRUE(AheadOf(x, y)); |
| 141 | EXPECT_FALSE(AheadOf(y, x)); |
| 142 | for (int i = 0; i < 256; ++i) { |
| 143 | EXPECT_TRUE(AheadOf(x, y)); |
| 144 | ++x; |
| 145 | ++y; |
| 146 | } |
| 147 | |
| 148 | x = 128; |
| 149 | y = 0; |
| 150 | for (int i = 0; i < 128; ++i) { |
| 151 | EXPECT_TRUE(AheadOf(x, y)); |
| 152 | EXPECT_FALSE(AheadOf(y, x)); |
| 153 | x++; |
| 154 | y++; |
| 155 | } |
| 156 | |
| 157 | for (int i = 0; i < 128; ++i) { |
| 158 | EXPECT_FALSE(AheadOf(x, y)); |
| 159 | EXPECT_TRUE(AheadOf(y, x)); |
| 160 | x++; |
| 161 | y++; |
| 162 | } |
| 163 | |
| 164 | x = 129; |
| 165 | y = 0; |
| 166 | EXPECT_FALSE(AheadOf(x, y)); |
| 167 | EXPECT_TRUE(AheadOf(y, x)); |
| 168 | EXPECT_TRUE(AheadOf<uint16_t>(x, y)); |
| 169 | EXPECT_FALSE(AheadOf<uint16_t>(y, x)); |
| 170 | } |
| 171 | |
| 172 | } // namespace webrtc |