Lajos Molnar | c416bec | 2014-10-01 21:30:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "Utils_test" |
| 19 | |
| 20 | #include <gtest/gtest.h> |
| 21 | #include <utils/String8.h> |
| 22 | #include <utils/Errors.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <media/stagefright/foundation/ADebug.h> |
| 27 | #include <media/stagefright/foundation/AUtils.h> |
| 28 | #include <media/stagefright/Utils.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | class UtilsTest : public ::testing::Test { |
| 33 | }; |
| 34 | |
| 35 | TEST_F(UtilsTest, TestFourCC) { |
| 36 | ASSERT_EQ(FOURCC('s', 't', 'm' , 'u'), 'stmu'); |
| 37 | } |
| 38 | |
| 39 | TEST_F(UtilsTest, TestMathTemplates) { |
| 40 | ASSERT_EQ(divRound(-10, -4), 3); |
| 41 | ASSERT_EQ(divRound(-11, -4), 3); |
| 42 | ASSERT_EQ(divRound(-12, -4), 3); |
| 43 | ASSERT_EQ(divRound(-13, -4), 3); |
| 44 | ASSERT_EQ(divRound(-14, -4), 4); |
| 45 | |
| 46 | ASSERT_EQ(divRound(10, -4), -3); |
| 47 | ASSERT_EQ(divRound(11, -4), -3); |
| 48 | ASSERT_EQ(divRound(12, -4), -3); |
| 49 | ASSERT_EQ(divRound(13, -4), -3); |
| 50 | ASSERT_EQ(divRound(14, -4), -4); |
| 51 | |
| 52 | ASSERT_EQ(divRound(-10, 4), -3); |
| 53 | ASSERT_EQ(divRound(-11, 4), -3); |
| 54 | ASSERT_EQ(divRound(-12, 4), -3); |
| 55 | ASSERT_EQ(divRound(-13, 4), -3); |
| 56 | ASSERT_EQ(divRound(-14, 4), -4); |
| 57 | |
| 58 | ASSERT_EQ(divRound(10, 4), 3); |
| 59 | ASSERT_EQ(divRound(11, 4), 3); |
| 60 | ASSERT_EQ(divRound(12, 4), 3); |
| 61 | ASSERT_EQ(divRound(13, 4), 3); |
| 62 | ASSERT_EQ(divRound(14, 4), 4); |
| 63 | |
| 64 | ASSERT_EQ(divUp(-11, -4), 3); |
| 65 | ASSERT_EQ(divUp(-12, -4), 3); |
| 66 | ASSERT_EQ(divUp(-13, -4), 4); |
| 67 | |
| 68 | ASSERT_EQ(divUp(11, -4), -2); |
| 69 | ASSERT_EQ(divUp(12, -4), -3); |
| 70 | ASSERT_EQ(divUp(13, -4), -3); |
| 71 | |
| 72 | ASSERT_EQ(divUp(-11, 4), -2); |
| 73 | ASSERT_EQ(divUp(-12, 4), -3); |
| 74 | ASSERT_EQ(divUp(-13, 4), -3); |
| 75 | |
| 76 | ASSERT_EQ(divUp(11, 4), 3); |
| 77 | ASSERT_EQ(divUp(12, 4), 3); |
| 78 | ASSERT_EQ(divUp(13, 4), 4); |
| 79 | |
| 80 | ASSERT_EQ(abs(5L), 5L); |
| 81 | ASSERT_EQ(abs(-25), 25); |
| 82 | |
| 83 | ASSERT_EQ(min(5.6f, 6.0f), 5.6f); |
| 84 | ASSERT_EQ(min(6.0f, 5.6f), 5.6f); |
| 85 | ASSERT_EQ(min(-4.3, 8.6), -4.3); |
| 86 | ASSERT_EQ(min(8.6, -4.3), -4.3); |
| 87 | |
| 88 | ASSERT_EQ(max(5.6f, 6.0f), 6.0f); |
| 89 | ASSERT_EQ(max(6.0f, 5.6f), 6.0f); |
| 90 | ASSERT_EQ(max(-4.3, 8.6), 8.6); |
| 91 | ASSERT_EQ(max(8.6, -4.3), 8.6); |
| 92 | |
Lajos Molnar | bfed843 | 2015-04-01 19:32:25 -0700 | [diff] [blame^] | 93 | ASSERT_FALSE(isInRange(-43, 86u, -44)); |
| 94 | ASSERT_TRUE(isInRange(-43, 87u, -43)); |
| 95 | ASSERT_TRUE(isInRange(-43, 88u, -1)); |
| 96 | ASSERT_TRUE(isInRange(-43, 89u, 0)); |
| 97 | ASSERT_TRUE(isInRange(-43, 90u, 46)); |
| 98 | ASSERT_FALSE(isInRange(-43, 91u, 48)); |
| 99 | ASSERT_FALSE(isInRange(-43, 92u, 50)); |
| 100 | |
| 101 | ASSERT_FALSE(isInRange(43, 86u, 42)); |
| 102 | ASSERT_TRUE(isInRange(43, 87u, 43)); |
| 103 | ASSERT_TRUE(isInRange(43, 88u, 44)); |
| 104 | ASSERT_TRUE(isInRange(43, 89u, 131)); |
| 105 | ASSERT_FALSE(isInRange(43, 90u, 133)); |
| 106 | ASSERT_FALSE(isInRange(43, 91u, 135)); |
| 107 | |
| 108 | ASSERT_FALSE(isInRange(43u, 86u, 42u)); |
| 109 | ASSERT_TRUE(isInRange(43u, 85u, 43u)); |
| 110 | ASSERT_TRUE(isInRange(43u, 84u, 44u)); |
| 111 | ASSERT_TRUE(isInRange(43u, 83u, 125u)); |
| 112 | ASSERT_FALSE(isInRange(43u, 82u, 125u)); |
| 113 | ASSERT_FALSE(isInRange(43u, 81u, 125u)); |
| 114 | |
| 115 | ASSERT_FALSE(isInRange(-43, ~0u, 43)); |
| 116 | ASSERT_FALSE(isInRange(-43, ~0u, 44)); |
| 117 | ASSERT_FALSE(isInRange(-43, ~0u, ~0)); |
| 118 | ASSERT_FALSE(isInRange(-43, ~0u, 41)); |
| 119 | ASSERT_FALSE(isInRange(-43, ~0u, 40)); |
| 120 | |
| 121 | ASSERT_FALSE(isInRange(43u, ~0u, 43u)); |
| 122 | ASSERT_FALSE(isInRange(43u, ~0u, 41u)); |
| 123 | ASSERT_FALSE(isInRange(43u, ~0u, 40u)); |
| 124 | ASSERT_FALSE(isInRange(43u, ~0u, ~0u)); |
| 125 | |
| 126 | ASSERT_FALSE(isInRange(-43, 86u, -44, 0u)); |
| 127 | ASSERT_FALSE(isInRange(-43, 86u, -44, 1u)); |
| 128 | ASSERT_FALSE(isInRange(-43, 86u, -44, 2u)); |
| 129 | ASSERT_FALSE(isInRange(-43, 86u, -44, ~0u)); |
| 130 | ASSERT_TRUE(isInRange(-43, 87u, -43, 0u)); |
| 131 | ASSERT_TRUE(isInRange(-43, 87u, -43, 1u)); |
| 132 | ASSERT_TRUE(isInRange(-43, 87u, -43, 86u)); |
| 133 | ASSERT_TRUE(isInRange(-43, 87u, -43, 87u)); |
| 134 | ASSERT_FALSE(isInRange(-43, 87u, -43, 88u)); |
| 135 | ASSERT_FALSE(isInRange(-43, 87u, -43, ~0u)); |
| 136 | ASSERT_TRUE(isInRange(-43, 88u, -1, 0u)); |
| 137 | ASSERT_TRUE(isInRange(-43, 88u, -1, 45u)); |
| 138 | ASSERT_TRUE(isInRange(-43, 88u, -1, 46u)); |
| 139 | ASSERT_FALSE(isInRange(-43, 88u, -1, 47u)); |
| 140 | ASSERT_FALSE(isInRange(-43, 88u, -1, ~3u)); |
| 141 | ASSERT_TRUE(isInRange(-43, 90u, 46, 0u)); |
| 142 | ASSERT_TRUE(isInRange(-43, 90u, 46, 1u)); |
| 143 | ASSERT_FALSE(isInRange(-43, 90u, 46, 2u)); |
| 144 | ASSERT_FALSE(isInRange(-43, 91u, 48, 0u)); |
| 145 | ASSERT_FALSE(isInRange(-43, 91u, 48, 2u)); |
| 146 | ASSERT_FALSE(isInRange(-43, 91u, 48, ~6u)); |
| 147 | ASSERT_FALSE(isInRange(-43, 92u, 50, 0u)); |
| 148 | ASSERT_FALSE(isInRange(-43, 92u, 50, 1u)); |
| 149 | |
| 150 | ASSERT_FALSE(isInRange(43u, 86u, 42u, 0u)); |
| 151 | ASSERT_FALSE(isInRange(43u, 86u, 42u, 1u)); |
| 152 | ASSERT_FALSE(isInRange(43u, 86u, 42u, 2u)); |
| 153 | ASSERT_FALSE(isInRange(43u, 86u, 42u, ~0u)); |
| 154 | ASSERT_TRUE(isInRange(43u, 87u, 43u, 0u)); |
| 155 | ASSERT_TRUE(isInRange(43u, 87u, 43u, 1u)); |
| 156 | ASSERT_TRUE(isInRange(43u, 87u, 43u, 86u)); |
| 157 | ASSERT_TRUE(isInRange(43u, 87u, 43u, 87u)); |
| 158 | ASSERT_FALSE(isInRange(43u, 87u, 43u, 88u)); |
| 159 | ASSERT_FALSE(isInRange(43u, 87u, 43u, ~0u)); |
| 160 | ASSERT_TRUE(isInRange(43u, 88u, 60u, 0u)); |
| 161 | ASSERT_TRUE(isInRange(43u, 88u, 60u, 70u)); |
| 162 | ASSERT_TRUE(isInRange(43u, 88u, 60u, 71u)); |
| 163 | ASSERT_FALSE(isInRange(43u, 88u, 60u, 72u)); |
| 164 | ASSERT_FALSE(isInRange(43u, 88u, 60u, ~3u)); |
| 165 | ASSERT_TRUE(isInRange(43u, 90u, 132u, 0u)); |
| 166 | ASSERT_TRUE(isInRange(43u, 90u, 132u, 1u)); |
| 167 | ASSERT_FALSE(isInRange(43u, 90u, 132u, 2u)); |
| 168 | ASSERT_FALSE(isInRange(43u, 91u, 134u, 0u)); |
| 169 | ASSERT_FALSE(isInRange(43u, 91u, 134u, 2u)); |
| 170 | ASSERT_FALSE(isInRange(43u, 91u, 134u, ~6u)); |
| 171 | ASSERT_FALSE(isInRange(43u, 92u, 136u, 0u)); |
| 172 | ASSERT_FALSE(isInRange(43u, 92u, 136u, 1u)); |
| 173 | |
Lajos Molnar | c416bec | 2014-10-01 21:30:20 -0700 | [diff] [blame] | 174 | ASSERT_EQ(periodicError(124, 100), 24); |
| 175 | ASSERT_EQ(periodicError(288, 100), 12); |
| 176 | ASSERT_EQ(periodicError(-345, 100), 45); |
| 177 | ASSERT_EQ(periodicError(-493, 100), 7); |
| 178 | ASSERT_EQ(periodicError(-550, 100), 50); |
| 179 | ASSERT_EQ(periodicError(-600, 100), 0); |
| 180 | } |
| 181 | |
| 182 | } // namespace android |