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> |
Lajos Molnar | f296e2b | 2014-06-18 18:08:52 -0700 | [diff] [blame] | 27 | #include <media/stagefright/foundation/AStringUtils.h> |
Lajos Molnar | c416bec | 2014-10-01 21:30:20 -0700 | [diff] [blame] | 28 | #include <media/stagefright/foundation/AUtils.h> |
| 29 | #include <media/stagefright/Utils.h> |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | class UtilsTest : public ::testing::Test { |
| 34 | }; |
| 35 | |
Lajos Molnar | f296e2b | 2014-06-18 18:08:52 -0700 | [diff] [blame] | 36 | TEST_F(UtilsTest, TestStringUtils) { |
| 37 | ASSERT_EQ(AStringUtils::Compare("Audio", "AudioExt", 5, false), 0); |
| 38 | ASSERT_EQ(AStringUtils::Compare("Audio", "audiOExt", 5, true), 0); |
| 39 | ASSERT_NE(AStringUtils::Compare("Audio", "audioExt", 5, false), 0); |
| 40 | ASSERT_NE(AStringUtils::Compare("Audio", "AudiOExt", 5, false), 0); |
| 41 | |
| 42 | ASSERT_LT(AStringUtils::Compare("Audio", "AudioExt", 7, false), 0); |
| 43 | ASSERT_LT(AStringUtils::Compare("Audio", "audiOExt", 7, true), 0); |
| 44 | |
| 45 | ASSERT_GT(AStringUtils::Compare("AudioExt", "Audio", 7, false), 0); |
| 46 | ASSERT_GT(AStringUtils::Compare("audiOext", "Audio", 7, true), 0); |
| 47 | |
| 48 | ASSERT_LT(AStringUtils::Compare("Audio", "Video", 5, false), 0); |
| 49 | ASSERT_LT(AStringUtils::Compare("Audio1", "Audio2", 6, false), 0); |
| 50 | ASSERT_LT(AStringUtils::Compare("audio", "VIDEO", 5, true), 0); |
| 51 | ASSERT_LT(AStringUtils::Compare("audio1", "AUDIO2", 6, true), 0); |
| 52 | |
| 53 | ASSERT_GT(AStringUtils::Compare("Video", "Audio", 5, false), 0); |
| 54 | ASSERT_GT(AStringUtils::Compare("Audio2", "Audio1", 6, false), 0); |
| 55 | ASSERT_GT(AStringUtils::Compare("VIDEO", "audio", 5, true), 0); |
| 56 | ASSERT_GT(AStringUtils::Compare("AUDIO2", "audio1", 6, true), 0); |
| 57 | |
| 58 | ASSERT_TRUE(AStringUtils::MatchesGlob("AudioA", 5, "AudioB", 5, false)); |
| 59 | ASSERT_FALSE(AStringUtils::MatchesGlob("AudioA", 6, "AudioA", 5, false)); |
| 60 | ASSERT_FALSE(AStringUtils::MatchesGlob("AudioA", 5, "AudioA", 6, false)); |
| 61 | ASSERT_FALSE(AStringUtils::MatchesGlob("AudioA", 5, "audiOB", 5, false)); |
| 62 | ASSERT_TRUE(AStringUtils::MatchesGlob("AudioA", 5, "audiOB", 5, true)); |
| 63 | ASSERT_FALSE(AStringUtils::MatchesGlob("AudioA", 6, "AudioA", 5, true)); |
| 64 | ASSERT_FALSE(AStringUtils::MatchesGlob("AudioA", 5, "AudioA", 6, true)); |
| 65 | |
| 66 | ASSERT_TRUE(AStringUtils::MatchesGlob("*1", 1, "String8", 6, true)); |
| 67 | ASSERT_TRUE(AStringUtils::MatchesGlob("*1", 1, "String8", 6, false)); |
| 68 | ASSERT_TRUE(AStringUtils::MatchesGlob("*1", 1, "String8", 0, true)); |
| 69 | ASSERT_TRUE(AStringUtils::MatchesGlob("*1", 1, "String8", 0, false)); |
| 70 | |
| 71 | ASSERT_TRUE(AStringUtils::MatchesGlob("*ring1", 5, "String8", 6, false)); |
| 72 | ASSERT_TRUE(AStringUtils::MatchesGlob("*ring2", 5, "STRING8", 6, true)); |
| 73 | ASSERT_FALSE(AStringUtils::MatchesGlob("*ring4", 5, "StRing8", 6, false)); |
| 74 | ASSERT_FALSE(AStringUtils::MatchesGlob("*ring5", 5, "StrinG8", 6, false)); |
| 75 | ASSERT_FALSE(AStringUtils::MatchesGlob("*ring8", 5, "String8", 7, false)); |
| 76 | ASSERT_FALSE(AStringUtils::MatchesGlob("*ring8", 5, "String8", 7, true)); |
| 77 | |
| 78 | ASSERT_TRUE(AStringUtils::MatchesGlob("Str*1", 4, "String8", 6, false)); |
| 79 | ASSERT_TRUE(AStringUtils::MatchesGlob("Str*2", 4, "STRING8", 6, true)); |
| 80 | ASSERT_FALSE(AStringUtils::MatchesGlob("Str*3", 4, "string8", 6, false)); |
| 81 | ASSERT_FALSE(AStringUtils::MatchesGlob("Str*4", 4, "StRing8", 6, false)); |
| 82 | ASSERT_FALSE(AStringUtils::MatchesGlob("Str*5", 4, "AString8", 7, false)); |
| 83 | ASSERT_FALSE(AStringUtils::MatchesGlob("Str*6", 4, "AString8", 7, true)); |
| 84 | |
| 85 | ASSERT_TRUE(AStringUtils::MatchesGlob("Str*ng1", 6, "String8", 6, false)); |
| 86 | ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng2", 6, "string8", 6, false)); |
| 87 | ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng3", 6, "StRing8", 6, false)); |
| 88 | ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng4", 6, "StriNg8", 6, false)); |
| 89 | ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng5", 6, "StrinG8", 6, false)); |
| 90 | ASSERT_TRUE(AStringUtils::MatchesGlob("Str*ng6", 6, "STRING8", 6, true)); |
| 91 | ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng8", 6, "AString8", 7, false)); |
| 92 | ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng1", 6, "String16", 7, false)); |
| 93 | ASSERT_TRUE(AStringUtils::MatchesGlob("Str*ing9", 7, "String8", 6, false)); |
| 94 | ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ringA", 8, "String8", 6, false)); |
| 95 | ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng8", 6, "AString8", 7, true)); |
| 96 | ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ng1", 6, "String16", 7, true)); |
| 97 | ASSERT_TRUE(AStringUtils::MatchesGlob("Str*ing9", 7, "STRING8", 6, true)); |
| 98 | ASSERT_FALSE(AStringUtils::MatchesGlob("Str*ringA", 8, "String8", 6, true)); |
| 99 | |
| 100 | ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str1", 8, "bestrestroom", 9, false)); |
| 101 | ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str1", 8, "bestrestrestroom", 13, false)); |
| 102 | ASSERT_FALSE(AStringUtils::MatchesGlob("*str*stro", 8, "bestrestrestroom", 14, false)); |
| 103 | ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str*1", 9, "bestrestrestroom", 14, false)); |
| 104 | ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str1", 8, "beSTReSTRoom", 9, true)); |
| 105 | ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str1", 8, "beSTRestreSTRoom", 13, true)); |
| 106 | ASSERT_FALSE(AStringUtils::MatchesGlob("*str*stro", 8, "bestreSTReSTRoom", 14, true)); |
| 107 | ASSERT_TRUE(AStringUtils::MatchesGlob("*str*str*1", 9, "bestreSTReSTRoom", 14, true)); |
| 108 | } |
| 109 | |
| 110 | TEST_F(UtilsTest, TestDebug) { |
| 111 | #define LVL(x) (ADebug::Level)(x) |
Lajos Molnar | 9903589 | 2015-06-26 15:40:37 -0700 | [diff] [blame^] | 112 | ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", "", LVL(5)), LVL(5)); |
| 113 | ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", " \t \n ", LVL(2)), LVL(2)); |
| 114 | ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", "3", LVL(5)), LVL(3)); |
| 115 | ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", "3:*deo", LVL(5)), LVL(3)); |
| 116 | ASSERT_EQ(ADebug::GetLevelFromSettingsString( |
Lajos Molnar | f296e2b | 2014-06-18 18:08:52 -0700 | [diff] [blame] | 117 | "video", "\t\n 3 \t\n:\t\n video \t\n", LVL(5)), LVL(3)); |
Lajos Molnar | 9903589 | 2015-06-26 15:40:37 -0700 | [diff] [blame^] | 118 | ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", "3:*deo,2:vid*", LVL(5)), LVL(2)); |
| 119 | ASSERT_EQ(ADebug::GetLevelFromSettingsString( |
Lajos Molnar | f296e2b | 2014-06-18 18:08:52 -0700 | [diff] [blame] | 120 | "avideo", "\t\n 3 \t\n:\t\n avideo \t\n,\t\n 2 \t\n:\t\n video \t\n", LVL(5)), LVL(3)); |
Lajos Molnar | 9903589 | 2015-06-26 15:40:37 -0700 | [diff] [blame^] | 121 | ASSERT_EQ(ADebug::GetLevelFromSettingsString( |
Lajos Molnar | f296e2b | 2014-06-18 18:08:52 -0700 | [diff] [blame] | 122 | "audio.omx", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(2)); |
Lajos Molnar | 9903589 | 2015-06-26 15:40:37 -0700 | [diff] [blame^] | 123 | ASSERT_EQ(ADebug::GetLevelFromSettingsString( |
Lajos Molnar | f296e2b | 2014-06-18 18:08:52 -0700 | [diff] [blame] | 124 | "video.omx", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(3)); |
Lajos Molnar | 9903589 | 2015-06-26 15:40:37 -0700 | [diff] [blame^] | 125 | ASSERT_EQ(ADebug::GetLevelFromSettingsString("video", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(3)); |
| 126 | ASSERT_EQ(ADebug::GetLevelFromSettingsString("omx", "4:*omx,3:*d*o*,2:audio*", LVL(5)), LVL(4)); |
Lajos Molnar | f296e2b | 2014-06-18 18:08:52 -0700 | [diff] [blame] | 127 | #undef LVL |
| 128 | } |
| 129 | |
Lajos Molnar | c416bec | 2014-10-01 21:30:20 -0700 | [diff] [blame] | 130 | TEST_F(UtilsTest, TestFourCC) { |
| 131 | ASSERT_EQ(FOURCC('s', 't', 'm' , 'u'), 'stmu'); |
| 132 | } |
| 133 | |
| 134 | TEST_F(UtilsTest, TestMathTemplates) { |
| 135 | ASSERT_EQ(divRound(-10, -4), 3); |
| 136 | ASSERT_EQ(divRound(-11, -4), 3); |
| 137 | ASSERT_EQ(divRound(-12, -4), 3); |
| 138 | ASSERT_EQ(divRound(-13, -4), 3); |
| 139 | ASSERT_EQ(divRound(-14, -4), 4); |
| 140 | |
| 141 | ASSERT_EQ(divRound(10, -4), -3); |
| 142 | ASSERT_EQ(divRound(11, -4), -3); |
| 143 | ASSERT_EQ(divRound(12, -4), -3); |
| 144 | ASSERT_EQ(divRound(13, -4), -3); |
| 145 | ASSERT_EQ(divRound(14, -4), -4); |
| 146 | |
| 147 | ASSERT_EQ(divRound(-10, 4), -3); |
| 148 | ASSERT_EQ(divRound(-11, 4), -3); |
| 149 | ASSERT_EQ(divRound(-12, 4), -3); |
| 150 | ASSERT_EQ(divRound(-13, 4), -3); |
| 151 | ASSERT_EQ(divRound(-14, 4), -4); |
| 152 | |
| 153 | ASSERT_EQ(divRound(10, 4), 3); |
| 154 | ASSERT_EQ(divRound(11, 4), 3); |
| 155 | ASSERT_EQ(divRound(12, 4), 3); |
| 156 | ASSERT_EQ(divRound(13, 4), 3); |
| 157 | ASSERT_EQ(divRound(14, 4), 4); |
| 158 | |
| 159 | ASSERT_EQ(divUp(-11, -4), 3); |
| 160 | ASSERT_EQ(divUp(-12, -4), 3); |
| 161 | ASSERT_EQ(divUp(-13, -4), 4); |
| 162 | |
| 163 | ASSERT_EQ(divUp(11, -4), -2); |
| 164 | ASSERT_EQ(divUp(12, -4), -3); |
| 165 | ASSERT_EQ(divUp(13, -4), -3); |
| 166 | |
| 167 | ASSERT_EQ(divUp(-11, 4), -2); |
| 168 | ASSERT_EQ(divUp(-12, 4), -3); |
| 169 | ASSERT_EQ(divUp(-13, 4), -3); |
| 170 | |
| 171 | ASSERT_EQ(divUp(11, 4), 3); |
| 172 | ASSERT_EQ(divUp(12, 4), 3); |
| 173 | ASSERT_EQ(divUp(13, 4), 4); |
| 174 | |
Lajos Molnar | a0940a5 | 2014-09-24 13:57:35 -0700 | [diff] [blame] | 175 | ASSERT_EQ(align(11, 4), 12); |
| 176 | ASSERT_EQ(align(12, 4), 12); |
| 177 | ASSERT_EQ(align(13, 4), 16); |
| 178 | ASSERT_EQ(align(11, 8), 16); |
| 179 | ASSERT_EQ(align(11, 2), 12); |
| 180 | ASSERT_EQ(align(11, 1), 11); |
| 181 | |
Lajos Molnar | c416bec | 2014-10-01 21:30:20 -0700 | [diff] [blame] | 182 | ASSERT_EQ(abs(5L), 5L); |
| 183 | ASSERT_EQ(abs(-25), 25); |
| 184 | |
| 185 | ASSERT_EQ(min(5.6f, 6.0f), 5.6f); |
| 186 | ASSERT_EQ(min(6.0f, 5.6f), 5.6f); |
| 187 | ASSERT_EQ(min(-4.3, 8.6), -4.3); |
| 188 | ASSERT_EQ(min(8.6, -4.3), -4.3); |
| 189 | |
| 190 | ASSERT_EQ(max(5.6f, 6.0f), 6.0f); |
| 191 | ASSERT_EQ(max(6.0f, 5.6f), 6.0f); |
| 192 | ASSERT_EQ(max(-4.3, 8.6), 8.6); |
| 193 | ASSERT_EQ(max(8.6, -4.3), 8.6); |
| 194 | |
Lajos Molnar | bfed843 | 2015-04-01 19:32:25 -0700 | [diff] [blame] | 195 | ASSERT_FALSE(isInRange(-43, 86u, -44)); |
| 196 | ASSERT_TRUE(isInRange(-43, 87u, -43)); |
| 197 | ASSERT_TRUE(isInRange(-43, 88u, -1)); |
| 198 | ASSERT_TRUE(isInRange(-43, 89u, 0)); |
| 199 | ASSERT_TRUE(isInRange(-43, 90u, 46)); |
| 200 | ASSERT_FALSE(isInRange(-43, 91u, 48)); |
| 201 | ASSERT_FALSE(isInRange(-43, 92u, 50)); |
| 202 | |
| 203 | ASSERT_FALSE(isInRange(43, 86u, 42)); |
| 204 | ASSERT_TRUE(isInRange(43, 87u, 43)); |
| 205 | ASSERT_TRUE(isInRange(43, 88u, 44)); |
| 206 | ASSERT_TRUE(isInRange(43, 89u, 131)); |
| 207 | ASSERT_FALSE(isInRange(43, 90u, 133)); |
| 208 | ASSERT_FALSE(isInRange(43, 91u, 135)); |
| 209 | |
| 210 | ASSERT_FALSE(isInRange(43u, 86u, 42u)); |
| 211 | ASSERT_TRUE(isInRange(43u, 85u, 43u)); |
| 212 | ASSERT_TRUE(isInRange(43u, 84u, 44u)); |
| 213 | ASSERT_TRUE(isInRange(43u, 83u, 125u)); |
| 214 | ASSERT_FALSE(isInRange(43u, 82u, 125u)); |
| 215 | ASSERT_FALSE(isInRange(43u, 81u, 125u)); |
| 216 | |
| 217 | ASSERT_FALSE(isInRange(-43, ~0u, 43)); |
| 218 | ASSERT_FALSE(isInRange(-43, ~0u, 44)); |
| 219 | ASSERT_FALSE(isInRange(-43, ~0u, ~0)); |
| 220 | ASSERT_FALSE(isInRange(-43, ~0u, 41)); |
| 221 | ASSERT_FALSE(isInRange(-43, ~0u, 40)); |
| 222 | |
| 223 | ASSERT_FALSE(isInRange(43u, ~0u, 43u)); |
| 224 | ASSERT_FALSE(isInRange(43u, ~0u, 41u)); |
| 225 | ASSERT_FALSE(isInRange(43u, ~0u, 40u)); |
| 226 | ASSERT_FALSE(isInRange(43u, ~0u, ~0u)); |
| 227 | |
| 228 | ASSERT_FALSE(isInRange(-43, 86u, -44, 0u)); |
| 229 | ASSERT_FALSE(isInRange(-43, 86u, -44, 1u)); |
| 230 | ASSERT_FALSE(isInRange(-43, 86u, -44, 2u)); |
| 231 | ASSERT_FALSE(isInRange(-43, 86u, -44, ~0u)); |
| 232 | ASSERT_TRUE(isInRange(-43, 87u, -43, 0u)); |
| 233 | ASSERT_TRUE(isInRange(-43, 87u, -43, 1u)); |
| 234 | ASSERT_TRUE(isInRange(-43, 87u, -43, 86u)); |
| 235 | ASSERT_TRUE(isInRange(-43, 87u, -43, 87u)); |
| 236 | ASSERT_FALSE(isInRange(-43, 87u, -43, 88u)); |
| 237 | ASSERT_FALSE(isInRange(-43, 87u, -43, ~0u)); |
| 238 | ASSERT_TRUE(isInRange(-43, 88u, -1, 0u)); |
| 239 | ASSERT_TRUE(isInRange(-43, 88u, -1, 45u)); |
| 240 | ASSERT_TRUE(isInRange(-43, 88u, -1, 46u)); |
| 241 | ASSERT_FALSE(isInRange(-43, 88u, -1, 47u)); |
| 242 | ASSERT_FALSE(isInRange(-43, 88u, -1, ~3u)); |
| 243 | ASSERT_TRUE(isInRange(-43, 90u, 46, 0u)); |
| 244 | ASSERT_TRUE(isInRange(-43, 90u, 46, 1u)); |
| 245 | ASSERT_FALSE(isInRange(-43, 90u, 46, 2u)); |
| 246 | ASSERT_FALSE(isInRange(-43, 91u, 48, 0u)); |
| 247 | ASSERT_FALSE(isInRange(-43, 91u, 48, 2u)); |
| 248 | ASSERT_FALSE(isInRange(-43, 91u, 48, ~6u)); |
| 249 | ASSERT_FALSE(isInRange(-43, 92u, 50, 0u)); |
| 250 | ASSERT_FALSE(isInRange(-43, 92u, 50, 1u)); |
| 251 | |
| 252 | ASSERT_FALSE(isInRange(43u, 86u, 42u, 0u)); |
| 253 | ASSERT_FALSE(isInRange(43u, 86u, 42u, 1u)); |
| 254 | ASSERT_FALSE(isInRange(43u, 86u, 42u, 2u)); |
| 255 | ASSERT_FALSE(isInRange(43u, 86u, 42u, ~0u)); |
| 256 | ASSERT_TRUE(isInRange(43u, 87u, 43u, 0u)); |
| 257 | ASSERT_TRUE(isInRange(43u, 87u, 43u, 1u)); |
| 258 | ASSERT_TRUE(isInRange(43u, 87u, 43u, 86u)); |
| 259 | ASSERT_TRUE(isInRange(43u, 87u, 43u, 87u)); |
| 260 | ASSERT_FALSE(isInRange(43u, 87u, 43u, 88u)); |
| 261 | ASSERT_FALSE(isInRange(43u, 87u, 43u, ~0u)); |
| 262 | ASSERT_TRUE(isInRange(43u, 88u, 60u, 0u)); |
| 263 | ASSERT_TRUE(isInRange(43u, 88u, 60u, 70u)); |
| 264 | ASSERT_TRUE(isInRange(43u, 88u, 60u, 71u)); |
| 265 | ASSERT_FALSE(isInRange(43u, 88u, 60u, 72u)); |
| 266 | ASSERT_FALSE(isInRange(43u, 88u, 60u, ~3u)); |
| 267 | ASSERT_TRUE(isInRange(43u, 90u, 132u, 0u)); |
| 268 | ASSERT_TRUE(isInRange(43u, 90u, 132u, 1u)); |
| 269 | ASSERT_FALSE(isInRange(43u, 90u, 132u, 2u)); |
| 270 | ASSERT_FALSE(isInRange(43u, 91u, 134u, 0u)); |
| 271 | ASSERT_FALSE(isInRange(43u, 91u, 134u, 2u)); |
| 272 | ASSERT_FALSE(isInRange(43u, 91u, 134u, ~6u)); |
| 273 | ASSERT_FALSE(isInRange(43u, 92u, 136u, 0u)); |
| 274 | ASSERT_FALSE(isInRange(43u, 92u, 136u, 1u)); |
| 275 | |
Lajos Molnar | c416bec | 2014-10-01 21:30:20 -0700 | [diff] [blame] | 276 | ASSERT_EQ(periodicError(124, 100), 24); |
| 277 | ASSERT_EQ(periodicError(288, 100), 12); |
| 278 | ASSERT_EQ(periodicError(-345, 100), 45); |
| 279 | ASSERT_EQ(periodicError(-493, 100), 7); |
| 280 | ASSERT_EQ(periodicError(-550, 100), 50); |
| 281 | ASSERT_EQ(periodicError(-600, 100), 0); |
| 282 | } |
| 283 | |
| 284 | } // namespace android |